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.
5 #ifndef CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_
6 #define CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_
10 #include "base/compiler_specific.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/metrics/field_trial.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/metrics/variations/variations_request_scheduler.h"
16 #include "chrome/browser/web_resource/resource_request_allowed_notifier.h"
17 #include "chrome/common/chrome_version_info.h"
18 #include "net/url_request/url_fetcher_delegate.h"
22 #include "chrome/browser/metrics/variations/variations_registry_syncer_win.h"
26 class PrefRegistrySimple
;
28 namespace chrome_variations
{
32 // Used to setup field trials based on stored variations seed data, and fetch
33 // new seed data from the variations server.
34 class VariationsService
35 : public net::URLFetcherDelegate
,
36 public ResourceRequestAllowedNotifier::Observer
{
38 virtual ~VariationsService();
40 // Creates field trials based on Variations Seed loaded from local prefs. If
41 // there is a problem loading the seed data, all trials specified by the seed
42 // may not be created.
43 bool CreateTrialsFromSeed();
45 // Calls FetchVariationsSeed once and repeats this periodically. See
46 // implementation for details on the period. Must be called after
47 // |CreateTrialsFromSeed|.
48 void StartRepeatedVariationsSeedFetch();
50 // Returns the variations server URL, which can vary if a command-line flag is
51 // set and/or the variations restrict pref is set in |local_prefs|. Declared
52 // static for test purposes.
53 static GURL
GetVariationsServerURL(PrefService
* local_prefs
);
56 // Starts syncing Google Update Variation IDs with the registry.
57 void StartGoogleUpdateRegistrySync();
60 // Exposed for testing.
61 void SetCreateTrialsFromSeedCalledForTesting(bool called
);
63 // Exposed for testing.
64 static std::string
GetDefaultVariationsServerURLForTesting();
66 // Register Variations related prefs in Local State.
67 static void RegisterPrefs(PrefRegistrySimple
* registry
);
69 // Factory method for creating a VariationsService.
70 static VariationsService
* Create(PrefService
* local_state
);
73 // Starts the fetching process once, where |OnURLFetchComplete| is called with
75 virtual void DoActualFetch();
77 // This constructor exists for injecting a mock notifier. It is meant for
78 // testing only. This instance will take ownership of |notifier|.
79 VariationsService(ResourceRequestAllowedNotifier
* notifier
,
80 PrefService
* local_state
);
83 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest
, DoNotFetchIfOffline
);
84 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest
, DoNotFetchIfOnlineToOnline
);
85 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest
, FetchOnReconnect
);
86 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest
, LoadSeed
);
87 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest
, StoreSeed
);
88 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest
, SeedStoredWhenOKStatus
);
89 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest
, SeedNotStoredWhenNonOKStatus
);
90 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest
, SeedDateUpdatedOn304Status
);
92 // Creates the VariationsService with the given |local_state| prefs service.
93 // Use the |Create| factory method to create a VariationsService.
94 explicit VariationsService(PrefService
* local_state
);
96 // Checks if prerequisites for fetching the Variations seed are met, and if
97 // so, performs the actual fetch using |DoActualFetch|.
98 void FetchVariationsSeed();
100 // net::URLFetcherDelegate implementation:
101 virtual void OnURLFetchComplete(const net::URLFetcher
* source
) OVERRIDE
;
103 // ResourceRequestAllowedNotifier::Observer implementation:
104 virtual void OnResourceRequestsAllowed() OVERRIDE
;
106 // Store the given seed data to the given local prefs. Note that |seed_data|
107 // is assumed to be the raw serialized protobuf data stored in a string. It
108 // will be Base64Encoded for storage. If the string is invalid or the encoding
109 // fails, the existing prefs are left as is and the function returns false.
110 bool StoreSeedData(const std::string
& seed_data
, const base::Time
& seed_date
);
112 // Loads the variations seed data from local state into |seed|. If there is a
113 // problem with loading, the pref value is cleared and false is returned. If
114 // successful, |seed| will contain the loaded data and true is returned.
115 bool LoadVariationsSeedFromPref(VariationsSeed
* seed
);
117 // Record the time of the most recent successful fetch.
118 void RecordLastFetchTime();
120 // The pref service used to store persist the variations seed.
121 PrefService
* local_state_
;
123 // Contains the scheduler instance that handles timing for requests to the
124 // server. Initially NULL and instantiated when the initial fetch is
126 scoped_ptr
<VariationsRequestScheduler
> request_scheduler_
;
128 // Contains the current seed request. Will only have a value while a request
129 // is pending, and will be reset by |OnURLFetchComplete|.
130 scoped_ptr
<net::URLFetcher
> pending_seed_request_
;
132 // The URL to use for querying the Variations server.
133 GURL variations_server_url_
;
135 // Cached serial number from the most recently fetched Variations seed.
136 std::string variations_serial_number_
;
138 // Tracks whether |CreateTrialsFromSeed| has been called, to ensure that
139 // it gets called prior to |StartRepeatedVariationsSeedFetch|.
140 bool create_trials_from_seed_called_
;
142 // Tracks whether the initial request to the variations server had completed.
143 bool initial_request_completed_
;
145 // Helper class used to tell this service if it's allowed to make network
146 // resource requests.
147 scoped_ptr
<ResourceRequestAllowedNotifier
> resource_request_allowed_notifier_
;
149 // The start time of the last seed request. This is used to measure the
150 // latency of seed requests. Initially zero.
151 base::TimeTicks last_request_started_time_
;
154 // Helper that handles synchronizing Variations with the Registry.
155 VariationsRegistrySyncer registry_syncer_
;
158 DISALLOW_COPY_AND_ASSIGN(VariationsService
);
161 } // namespace chrome_variations
163 #endif // CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_