1 /** 2 * Copyright (c) 2020, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <climits> 20 #include <functional> 21 #include <map> 22 #include <mutex> 23 #include <string> 24 #include <string_view> 25 26 #include <android-base/thread_annotations.h> 27 #include <netdutils/DumpWriter.h> 28 29 namespace android::net { 30 31 // TODO: Add some way to update the stored experiment flags periodically. 32 // TODO: Refactor this class and make things easier. (e.g. remove string map.) 33 class Experiments { 34 public: 35 using GetExperimentFlagIntFunction = std::function<int(const std::string&, int)>; 36 static Experiments* getInstance(); 37 int getFlag(std::string_view key, int defaultValue) const EXCLUDES(mMutex); 38 void update(); 39 void dump(netdutils::DumpWriter& dw) const EXCLUDES(mMutex); 40 41 Experiments(Experiments const&) = delete; 42 void operator=(Experiments const&) = delete; 43 44 private: 45 explicit Experiments(GetExperimentFlagIntFunction getExperimentFlagIntFunction); 46 Experiments() = delete; 47 void updateInternal() EXCLUDES(mMutex); 48 mutable std::mutex mMutex; 49 std::map<std::string_view, int> mFlagsMapInt GUARDED_BY(mMutex); 50 static constexpr const char* const kExperimentFlagKeyList[] = { 51 "doh_early_data", 52 "doh_idle_timeout_ms", 53 "doh_probe_timeout_ms", 54 "doh_query_timeout_ms", 55 "doh_session_resumption", 56 "dot_async_handshake", 57 "dot_connect_timeout_ms", 58 "dot_maxtries", 59 "dot_query_timeout_ms", 60 "dot_quick_fallback", 61 "dot_revalidation_threshold", 62 "dot_validation_latency_factor", 63 "dot_validation_latency_offset_ms", 64 "dot_xport_unusable_threshold", 65 "fail_fast_on_uid_network_blocking", 66 "keep_listening_udp", 67 "max_cache_entries", 68 "max_queries_global", 69 "mdns_resolution", 70 "parallel_lookup_sleep_time", 71 "retransmission_time_interval", 72 "retry_count", 73 "sort_nameservers", 74 }; 75 // This value is used in updateInternal as the default value if any flags can't be found. 76 static constexpr int kFlagIntDefault = INT_MIN; 77 // For testing. 78 friend class ExperimentsTest; 79 const GetExperimentFlagIntFunction mGetExperimentFlagIntFunction; 80 }; 81 82 } // namespace android::net 83