OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_ |
6 #define CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_ | 6 #define CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "chrome/browser/metrics/variations/variations_registry_syncer_win.h" | 23 #include "chrome/browser/metrics/variations/variations_registry_syncer_win.h" |
24 #endif | 24 #endif |
25 | 25 |
26 class PrefService; | 26 class PrefService; |
27 class PrefRegistrySimple; | 27 class PrefRegistrySimple; |
28 | 28 |
29 namespace user_prefs { | 29 namespace user_prefs { |
30 class PrefRegistrySyncable; | 30 class PrefRegistrySyncable; |
31 } | 31 } |
32 | 32 |
| 33 namespace metrics { |
| 34 class MetricsStateManager; |
| 35 } |
| 36 |
33 namespace chrome_variations { | 37 namespace chrome_variations { |
34 | 38 |
35 class VariationsSeed; | 39 class VariationsSeed; |
36 | 40 |
37 // Used to setup field trials based on stored variations seed data, and fetch | 41 // Used to setup field trials based on stored variations seed data, and fetch |
38 // new seed data from the variations server. | 42 // new seed data from the variations server. |
39 class VariationsService | 43 class VariationsService |
40 : public net::URLFetcherDelegate, | 44 : public net::URLFetcherDelegate, |
41 public ResourceRequestAllowedNotifier::Observer { | 45 public ResourceRequestAllowedNotifier::Observer { |
42 public: | 46 public: |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 | 78 |
75 // Exposed for testing. | 79 // Exposed for testing. |
76 static std::string GetDefaultVariationsServerURLForTesting(); | 80 static std::string GetDefaultVariationsServerURLForTesting(); |
77 | 81 |
78 // Register Variations related prefs in Local State. | 82 // Register Variations related prefs in Local State. |
79 static void RegisterPrefs(PrefRegistrySimple* registry); | 83 static void RegisterPrefs(PrefRegistrySimple* registry); |
80 | 84 |
81 // Register Variations related prefs in the Profile prefs. | 85 // Register Variations related prefs in the Profile prefs. |
82 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 86 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
83 | 87 |
84 // Factory method for creating a VariationsService. | 88 // Factory method for creating a VariationsService. Does not take ownership of |
85 static VariationsService* Create(PrefService* local_state); | 89 // |state_manager|. Caller should ensure that |state_manager| is valid for the |
| 90 // lifetime of this class. |
| 91 static scoped_ptr<VariationsService> Create( |
| 92 PrefService* local_state, |
| 93 metrics::MetricsStateManager* state_manager); |
86 | 94 |
87 // Set the PrefService responsible for getting policy-related preferences, | 95 // Set the PrefService responsible for getting policy-related preferences, |
88 // such as the restrict parameter. | 96 // such as the restrict parameter. |
89 void set_policy_pref_service(PrefService* service) { | 97 void set_policy_pref_service(PrefService* service) { |
90 DCHECK(service); | 98 DCHECK(service); |
91 policy_pref_service_ = service; | 99 policy_pref_service_ = service; |
92 } | 100 } |
93 | 101 |
94 protected: | 102 protected: |
95 // Starts the fetching process once, where |OnURLFetchComplete| is called with | 103 // Starts the fetching process once, where |OnURLFetchComplete| is called with |
96 // the response. | 104 // the response. |
97 virtual void DoActualFetch(); | 105 virtual void DoActualFetch(); |
98 | 106 |
99 // Stores the seed to prefs. Set as virtual and protected so that it can be | 107 // Stores the seed to prefs. Set as virtual and protected so that it can be |
100 // overridden by tests. | 108 // overridden by tests. |
101 virtual void StoreSeed(const std::string& seed_data, | 109 virtual void StoreSeed(const std::string& seed_data, |
102 const std::string& seed_signature, | 110 const std::string& seed_signature, |
103 const base::Time& date_fetched); | 111 const base::Time& date_fetched); |
104 | 112 |
105 // This constructor exists for injecting a mock notifier. It is meant for | 113 // This constructor exists for injecting a mock notifier. It is meant for |
106 // testing only. This instance will take ownership of |notifier|. | 114 // testing only. This instance will take ownership of |notifier|. Does not |
| 115 // take ownership of |state_manager|. Caller should ensure that |
| 116 // |state_manager| is valid for the lifetime of this class. |
107 VariationsService(ResourceRequestAllowedNotifier* notifier, | 117 VariationsService(ResourceRequestAllowedNotifier* notifier, |
108 PrefService* local_state); | 118 PrefService* local_state, |
| 119 metrics::MetricsStateManager* state_manager); |
109 | 120 |
110 private: | 121 private: |
111 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, DoNotFetchIfOffline); | 122 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, DoNotFetchIfOffline); |
112 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, DoNotFetchIfOnlineToOnline); | 123 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, DoNotFetchIfOnlineToOnline); |
113 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, FetchOnReconnect); | 124 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, FetchOnReconnect); |
114 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, LoadSeed); | 125 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, LoadSeed); |
115 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, StoreSeed); | 126 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, StoreSeed); |
116 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, SeedStoredWhenOKStatus); | 127 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, SeedStoredWhenOKStatus); |
117 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, SeedNotStoredWhenNonOKStatus); | 128 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, SeedNotStoredWhenNonOKStatus); |
118 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, SeedDateUpdatedOn304Status); | 129 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, SeedDateUpdatedOn304Status); |
119 | 130 |
120 // Creates the VariationsService with the given |local_state| prefs service. | 131 // Creates the VariationsService with the given |local_state| prefs service |
| 132 // and |state_manager|. Does not take ownership of |state_manager|. Caller |
| 133 // should ensure that |state_manager| is valid for the lifetime of this class. |
121 // Use the |Create| factory method to create a VariationsService. | 134 // Use the |Create| factory method to create a VariationsService. |
122 explicit VariationsService(PrefService* local_state); | 135 VariationsService(PrefService* local_state, |
| 136 metrics::MetricsStateManager* state_manager); |
123 | 137 |
124 // Checks if prerequisites for fetching the Variations seed are met, and if | 138 // Checks if prerequisites for fetching the Variations seed are met, and if |
125 // so, performs the actual fetch using |DoActualFetch|. | 139 // so, performs the actual fetch using |DoActualFetch|. |
126 void FetchVariationsSeed(); | 140 void FetchVariationsSeed(); |
127 | 141 |
128 // net::URLFetcherDelegate implementation: | 142 // net::URLFetcherDelegate implementation: |
129 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 143 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
130 | 144 |
131 // ResourceRequestAllowedNotifier::Observer implementation: | 145 // ResourceRequestAllowedNotifier::Observer implementation: |
132 virtual void OnResourceRequestsAllowed() OVERRIDE; | 146 virtual void OnResourceRequestsAllowed() OVERRIDE; |
133 | 147 |
134 // Record the time of the most recent successful fetch. | 148 // Record the time of the most recent successful fetch. |
135 void RecordLastFetchTime(); | 149 void RecordLastFetchTime(); |
136 | 150 |
137 // The pref service used to store persist the variations seed. | 151 // The pref service used to store persist the variations seed. |
138 PrefService* local_state_; | 152 PrefService* local_state_; |
139 | 153 |
| 154 // Used for instantiating entropy providers for variations seed simulation. |
| 155 // Weak pointer. |
| 156 metrics::MetricsStateManager* state_manager_; |
| 157 |
140 // Used to obtain policy-related preferences. Depending on the platform, will | 158 // Used to obtain policy-related preferences. Depending on the platform, will |
141 // either be Local State or Profile prefs. | 159 // either be Local State or Profile prefs. |
142 PrefService* policy_pref_service_; | 160 PrefService* policy_pref_service_; |
143 | 161 |
144 VariationsSeedStore seed_store_; | 162 VariationsSeedStore seed_store_; |
145 | 163 |
146 // Contains the scheduler instance that handles timing for requests to the | 164 // Contains the scheduler instance that handles timing for requests to the |
147 // server. Initially NULL and instantiated when the initial fetch is | 165 // server. Initially NULL and instantiated when the initial fetch is |
148 // requested. | 166 // requested. |
149 scoped_ptr<VariationsRequestScheduler> request_scheduler_; | 167 scoped_ptr<VariationsRequestScheduler> request_scheduler_; |
(...skipping 24 matching lines...) Expand all Loading... |
174 // Helper that handles synchronizing Variations with the Registry. | 192 // Helper that handles synchronizing Variations with the Registry. |
175 VariationsRegistrySyncer registry_syncer_; | 193 VariationsRegistrySyncer registry_syncer_; |
176 #endif | 194 #endif |
177 | 195 |
178 DISALLOW_COPY_AND_ASSIGN(VariationsService); | 196 DISALLOW_COPY_AND_ASSIGN(VariationsService); |
179 }; | 197 }; |
180 | 198 |
181 } // namespace chrome_variations | 199 } // namespace chrome_variations |
182 | 200 |
183 #endif // CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_ | 201 #endif // CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_ |
OLD | NEW |