1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_ 6 #define CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_ 7 8 #include <string> 9 10 #include "base/compiler_specific.h" 11 #include "base/gtest_prod_util.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/metrics/field_trial.h" 15 #include "base/time/time.h" 16 #include "chrome/browser/metrics/variations/variations_request_scheduler.h" 17 #include "chrome/browser/metrics/variations/variations_seed_store.h" 18 #include "chrome/browser/web_resource/resource_request_allowed_notifier.h" 19 #include "chrome/common/chrome_version_info.h" 20 #include "net/url_request/url_fetcher_delegate.h" 21 #include "url/gurl.h" 22 23 #if defined(OS_WIN) 24 #include "chrome/browser/metrics/variations/variations_registry_syncer_win.h" 25 #endif 26 27 class PrefService; 28 class PrefRegistrySimple; 29 30 namespace base { 31 class Version; 32 } 33 34 namespace metrics { 35 class MetricsStateManager; 36 } 37 38 namespace user_prefs { 39 class PrefRegistrySyncable; 40 } 41 42 namespace chrome_variations { 43 44 class VariationsSeed; 45 46 // Used to setup field trials based on stored variations seed data, and fetch 47 // new seed data from the variations server. 48 class VariationsService 49 : public net::URLFetcherDelegate, 50 public ResourceRequestAllowedNotifier::Observer { 51 public: 52 virtual ~VariationsService(); 53 54 // Creates field trials based on Variations Seed loaded from local prefs. If 55 // there is a problem loading the seed data, all trials specified by the seed 56 // may not be created. 57 bool CreateTrialsFromSeed(); 58 59 // Calls FetchVariationsSeed once and repeats this periodically. See 60 // implementation for details on the period. Must be called after 61 // |CreateTrialsFromSeed|. 62 void StartRepeatedVariationsSeedFetch(); 63 64 // Returns the variations server URL, which can vary if a command-line flag is 65 // set and/or the variations restrict pref is set in |local_prefs|. Declared 66 // static for test purposes. 67 static GURL GetVariationsServerURL(PrefService* local_prefs); 68 69 // Called when the application enters foreground. This may trigger a 70 // FetchVariationsSeed call. 71 // TODO(rkaplow): Handle this and the similar event in metrics_service by 72 // observing an 'OnAppEnterForeground' event instead of requiring the frontend 73 // code to notify each service individually. 74 void OnAppEnterForeground(); 75 76 #if defined(OS_WIN) 77 // Starts syncing Google Update Variation IDs with the registry. 78 void StartGoogleUpdateRegistrySync(); 79 #endif 80 81 // Exposed for testing. 82 void SetCreateTrialsFromSeedCalledForTesting(bool called); 83 84 // Exposed for testing. 85 static std::string GetDefaultVariationsServerURLForTesting(); 86 87 // Register Variations related prefs in Local State. 88 static void RegisterPrefs(PrefRegistrySimple* registry); 89 90 // Register Variations related prefs in the Profile prefs. 91 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 92 93 // Factory method for creating a VariationsService. Does not take ownership of 94 // |state_manager|. Caller should ensure that |state_manager| is valid for the 95 // lifetime of this class. 96 static scoped_ptr<VariationsService> Create( 97 PrefService* local_state, 98 metrics::MetricsStateManager* state_manager); 99 100 // Set the PrefService responsible for getting policy-related preferences, 101 // such as the restrict parameter. set_policy_pref_service(PrefService * service)102 void set_policy_pref_service(PrefService* service) { 103 DCHECK(service); 104 policy_pref_service_ = service; 105 } 106 107 protected: 108 // Starts the fetching process once, where |OnURLFetchComplete| is called with 109 // the response. 110 virtual void DoActualFetch(); 111 112 // Stores the seed to prefs. Set as virtual and protected so that it can be 113 // overridden by tests. 114 virtual void StoreSeed(const std::string& seed_data, 115 const std::string& seed_signature, 116 const base::Time& date_fetched); 117 118 // This constructor exists for injecting a mock notifier. It is meant for 119 // testing only. This instance will take ownership of |notifier|. Does not 120 // take ownership of |state_manager|. Caller should ensure that 121 // |state_manager| is valid for the lifetime of this class. 122 VariationsService(ResourceRequestAllowedNotifier* notifier, 123 PrefService* local_state, 124 metrics::MetricsStateManager* state_manager); 125 126 private: 127 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, DoNotFetchIfOffline); 128 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, DoNotFetchIfOnlineToOnline); 129 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, FetchOnReconnect); 130 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, LoadSeed); 131 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, StoreSeed); 132 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, SeedStoredWhenOKStatus); 133 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, SeedNotStoredWhenNonOKStatus); 134 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, SeedDateUpdatedOn304Status); 135 136 // Creates the VariationsService with the given |local_state| prefs service 137 // and |state_manager|. Does not take ownership of |state_manager|. Caller 138 // should ensure that |state_manager| is valid for the lifetime of this class. 139 // Use the |Create| factory method to create a VariationsService. 140 VariationsService(PrefService* local_state, 141 metrics::MetricsStateManager* state_manager); 142 143 // Checks if prerequisites for fetching the Variations seed are met, and if 144 // so, performs the actual fetch using |DoActualFetch|. 145 void FetchVariationsSeed(); 146 147 // net::URLFetcherDelegate implementation: 148 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 149 150 // ResourceRequestAllowedNotifier::Observer implementation: 151 virtual void OnResourceRequestsAllowed() OVERRIDE; 152 153 // Performs a variations seed simulation with the given |seed| and |version| 154 // and logs the simulation results as histograms. 155 void PerformSimulationWithVersion(scoped_ptr<VariationsSeed> seed, 156 const base::Version& version); 157 158 // Record the time of the most recent successful fetch. 159 void RecordLastFetchTime(); 160 161 // The pref service used to store persist the variations seed. 162 PrefService* local_state_; 163 164 // Used for instantiating entropy providers for variations seed simulation. 165 // Weak pointer. 166 metrics::MetricsStateManager* state_manager_; 167 168 // Used to obtain policy-related preferences. Depending on the platform, will 169 // either be Local State or Profile prefs. 170 PrefService* policy_pref_service_; 171 172 VariationsSeedStore seed_store_; 173 174 // Contains the scheduler instance that handles timing for requests to the 175 // server. Initially NULL and instantiated when the initial fetch is 176 // requested. 177 scoped_ptr<VariationsRequestScheduler> request_scheduler_; 178 179 // Contains the current seed request. Will only have a value while a request 180 // is pending, and will be reset by |OnURLFetchComplete|. 181 scoped_ptr<net::URLFetcher> pending_seed_request_; 182 183 // The URL to use for querying the Variations server. 184 GURL variations_server_url_; 185 186 // Tracks whether |CreateTrialsFromSeed| has been called, to ensure that 187 // it gets called prior to |StartRepeatedVariationsSeedFetch|. 188 bool create_trials_from_seed_called_; 189 190 // Tracks whether the initial request to the variations server had completed. 191 bool initial_request_completed_; 192 193 // Helper class used to tell this service if it's allowed to make network 194 // resource requests. 195 scoped_ptr<ResourceRequestAllowedNotifier> resource_request_allowed_notifier_; 196 197 // The start time of the last seed request. This is used to measure the 198 // latency of seed requests. Initially zero. 199 base::TimeTicks last_request_started_time_; 200 201 #if defined(OS_WIN) 202 // Helper that handles synchronizing Variations with the Registry. 203 VariationsRegistrySyncer registry_syncer_; 204 #endif 205 206 base::WeakPtrFactory<VariationsService> weak_ptr_factory_; 207 208 DISALLOW_COPY_AND_ASSIGN(VariationsService); 209 }; 210 211 } // namespace chrome_variations 212 213 #endif // CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_ 214