• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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"
19 #include "url/gurl.h"
20 
21 #if defined(OS_WIN)
22 #include "chrome/browser/metrics/variations/variations_registry_syncer_win.h"
23 #endif
24 
25 class PrefService;
26 class PrefRegistrySimple;
27 
28 namespace chrome_variations {
29 
30 class VariationsSeed;
31 
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 {
37  public:
38   virtual ~VariationsService();
39 
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();
44 
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();
49 
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);
54 
55 #if defined(OS_WIN)
56   // Starts syncing Google Update Variation IDs with the registry.
57   void StartGoogleUpdateRegistrySync();
58 #endif
59 
60   // Exposed for testing.
61   void SetCreateTrialsFromSeedCalledForTesting(bool called);
62 
63   // Exposed for testing.
64   static std::string GetDefaultVariationsServerURLForTesting();
65 
66   // Register Variations related prefs in Local State.
67   static void RegisterPrefs(PrefRegistrySimple* registry);
68 
69   // Factory method for creating a VariationsService.
70   static VariationsService* Create(PrefService* local_state);
71 
72  protected:
73   // Starts the fetching process once, where |OnURLFetchComplete| is called with
74   // the response.
75   virtual void DoActualFetch();
76 
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);
81 
82  private:
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);
91 
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);
95 
96   // Checks if prerequisites for fetching the Variations seed are met, and if
97   // so, performs the actual fetch using |DoActualFetch|.
98   void FetchVariationsSeed();
99 
100   // net::URLFetcherDelegate implementation:
101   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
102 
103   // ResourceRequestAllowedNotifier::Observer implementation:
104   virtual void OnResourceRequestsAllowed() OVERRIDE;
105 
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);
111 
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);
116 
117   // Record the time of the most recent successful fetch.
118   void RecordLastFetchTime();
119 
120   // The pref service used to store persist the variations seed.
121   PrefService* local_state_;
122 
123   // Contains the scheduler instance that handles timing for requests to the
124   // server. Initially NULL and instantiated when the initial fetch is
125   // requested.
126   scoped_ptr<VariationsRequestScheduler> request_scheduler_;
127 
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_;
131 
132   // The URL to use for querying the Variations server.
133   GURL variations_server_url_;
134 
135   // Cached serial number from the most recently fetched Variations seed.
136   std::string variations_serial_number_;
137 
138   // Tracks whether |CreateTrialsFromSeed| has been called, to ensure that
139   // it gets called prior to |StartRepeatedVariationsSeedFetch|.
140   bool create_trials_from_seed_called_;
141 
142   // Tracks whether the initial request to the variations server had completed.
143   bool initial_request_completed_;
144 
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_;
148 
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_;
152 
153 #if defined(OS_WIN)
154   // Helper that handles synchronizing Variations with the Registry.
155   VariationsRegistrySyncer registry_syncer_;
156 #endif
157 
158   DISALLOW_COPY_AND_ASSIGN(VariationsService);
159 };
160 
161 }  // namespace chrome_variations
162 
163 #endif  // CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_
164