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_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_METRICS_VARIATIONS_SERVICE_H_ |
6 #define CHROME_BROWSER_METRICS_VARIATIONS_SERVICE_H_ | 6 #define CHROME_BROWSER_METRICS_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" |
11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/metrics/field_trial.h" | 13 #include "base/metrics/field_trial.h" |
14 #include "base/threading/non_thread_safe.h" | |
14 #include "base/time.h" | 15 #include "base/time.h" |
15 #include "base/timer.h" | 16 #include "base/timer.h" |
16 #include "chrome/browser/metrics/proto/study.pb.h" | 17 #include "chrome/browser/metrics/proto/study.pb.h" |
17 #include "chrome/browser/metrics/proto/trials_seed.pb.h" | 18 #include "chrome/browser/metrics/proto/trials_seed.pb.h" |
18 #include "chrome/common/chrome_version_info.h" | 19 #include "chrome/common/chrome_version_info.h" |
20 #include "content/public/browser/notification_observer.h" | |
21 #include "content/public/browser/notification_registrar.h" | |
19 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
20 #include "net/url_request/url_fetcher_delegate.h" | 23 #include "net/url_request/url_fetcher_delegate.h" |
21 | 24 |
22 class PrefService; | 25 class PrefService; |
23 | 26 |
24 namespace net { | 27 namespace net { |
25 class URLFetcher; | 28 class URLFetcher; |
26 } // namespace net | 29 } // namespace net |
27 | 30 |
28 namespace chrome_variations { | 31 namespace chrome_variations { |
29 | 32 |
30 // Used to setup field trials based on stored variations seed data, and fetch | 33 // Used to setup field trials based on stored variations seed data, and fetch |
31 // new seed data from the variations server. | 34 // new seed data from the variations server. |
32 class VariationsService : public net::URLFetcherDelegate { | 35 class VariationsService : public net::URLFetcherDelegate, |
36 public content::NotificationObserver, | |
37 public base::NonThreadSafe { | |
33 public: | 38 public: |
34 VariationsService(); | 39 VariationsService(); |
35 virtual ~VariationsService(); | 40 virtual ~VariationsService(); |
36 | 41 |
37 // Creates field trials based on Variations Seed loaded from local prefs. If | 42 // Creates field trials based on Variations Seed loaded from local prefs. If |
38 // there is a problem loading the seed data, all trials specified by the seed | 43 // there is a problem loading the seed data, all trials specified by the seed |
39 // may not be created. | 44 // may not be created. |
40 bool CreateTrialsFromSeed(PrefService* local_prefs); | 45 bool CreateTrialsFromSeed(PrefService* local_prefs); |
41 | 46 |
42 // Calls FetchVariationsSeed once and repeats this periodically. See | 47 // Calls FetchVariationsSeed once and repeats this periodically. See |
43 // implementation for details on the period. Must be called after | 48 // implementation for details on the period. Must be called after |
44 // |CreateTrialsFromSeed|. | 49 // |CreateTrialsFromSeed|. |
45 void StartRepeatedVariationsSeedFetch(); | 50 void StartRepeatedVariationsSeedFetch(); |
46 | 51 |
47 // Starts the fetching process once, where |OnURLFetchComplete| is called with | |
48 // the response. | |
49 void FetchVariationsSeed(); | |
50 | |
51 // net::URLFetcherDelegate implementation: | |
52 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
53 | |
54 // Register Variations related prefs in Local State. | 52 // Register Variations related prefs in Local State. |
55 static void RegisterPrefs(PrefService* prefs); | 53 static void RegisterPrefs(PrefService* prefs); |
56 | 54 |
55 protected: | |
56 // Starts the fetching process once, where |OnURLFetchComplete| is called with | |
57 // the response. This is protected so we can override this for testing | |
58 // purposes. | |
59 virtual void FetchVariationsSeed(); | |
60 | |
61 // Overridden from content::NotificationObserver: | |
62 virtual void Observe(int type, | |
63 const content::NotificationSource& source, | |
64 const content::NotificationDetails& details) OVERRIDE; | |
Ilya Sherman
2012/08/08 21:33:47
nit: Can this be private now that the test code is
SteveT
2012/08/09 02:39:26
Done.
| |
65 | |
57 private: | 66 private: |
58 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyChannel); | 67 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyChannel); |
59 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyLocale); | 68 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyLocale); |
60 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyPlatform); | 69 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyPlatform); |
61 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersion); | 70 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersion); |
62 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersionWildcards); | 71 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersionWildcards); |
63 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyStartDate); | 72 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyStartDate); |
64 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, IsStudyExpired); | 73 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, IsStudyExpired); |
65 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, LoadSeed); | 74 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, LoadSeed); |
66 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, StoreSeed); | 75 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, StoreSeed); |
67 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, ValidateStudy); | 76 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, ValidateStudy); |
68 | 77 |
78 // Overridden from net::URLFetcherDelegate: | |
79 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
80 | |
69 // Store the given seed data to the given local prefs. Note that |seed_data| | 81 // Store the given seed data to the given local prefs. Note that |seed_data| |
70 // is assumed to be the raw serialized protobuf data stored in a string. It | 82 // is assumed to be the raw serialized protobuf data stored in a string. It |
71 // will be Base64Encoded for storage. If the string is invalid or the encoding | 83 // will be Base64Encoded for storage. If the string is invalid or the encoding |
72 // fails, the |local_prefs| is left as is and the function returns false. | 84 // fails, the |local_prefs| is left as is and the function returns false. |
73 bool StoreSeedData(const std::string& seed_data, | 85 bool StoreSeedData(const std::string& seed_data, |
74 const base::Time& seed_date, | 86 const base::Time& seed_date, |
75 PrefService* local_prefs); | 87 PrefService* local_prefs); |
76 | 88 |
77 // Returns whether |study| should be disabled according to its restriction | 89 // Returns whether |study| should be disabled according to its restriction |
78 // parameters. Uses |version_info| for min / max version checks and | 90 // parameters. Uses |version_info| for min / max version checks and |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 | 130 |
119 // Creates and registers a field trial from the |study| data. Disables the | 131 // Creates and registers a field trial from the |study| data. Disables the |
120 // trial if IsStudyExpired(study, reference_date) is true. | 132 // trial if IsStudyExpired(study, reference_date) is true. |
121 void CreateTrialFromStudy(const Study& study, | 133 void CreateTrialFromStudy(const Study& study, |
122 const base::Time& reference_date); | 134 const base::Time& reference_date); |
123 | 135 |
124 // Contains the current seed request. Will only have a value while a request | 136 // Contains the current seed request. Will only have a value while a request |
125 // is pending, and will be reset by |OnURLFetchComplete|. | 137 // is pending, and will be reset by |OnURLFetchComplete|. |
126 scoped_ptr<net::URLFetcher> pending_seed_request_; | 138 scoped_ptr<net::URLFetcher> pending_seed_request_; |
127 | 139 |
128 // The URL to use for querying the variations server. | 140 // The URL to use for querying the Variations server. |
129 GURL variations_server_url_; | 141 GURL variations_server_url_; |
130 | 142 |
131 // Cached serial number from the most recently fetched variations seed. | 143 // Cached serial number from the most recently fetched Variations seed. |
132 std::string variations_serial_number_; | 144 std::string variations_serial_number_; |
133 | 145 |
134 // Tracks whether |CreateTrialsFromSeed| has been called, to ensure that | 146 // Tracks whether |CreateTrialsFromSeed| has been called, to ensure that |
135 // it gets called prior to |StartRepeatedVariationsSeedFetch|. | 147 // it gets called prior to |StartRepeatedVariationsSeedFetch|. |
136 bool create_trials_from_seed_called_; | 148 bool create_trials_from_seed_called_; |
137 | 149 |
138 // The timer used to repeatedly ping the server. Keep this as an instance | 150 // The timer used to repeatedly ping the server. Keep this as an instance |
139 // member so if VariationsService goes out of scope, the timer is | 151 // member so if VariationsService goes out of scope, the timer is |
140 // automatically canceled. | 152 // automatically canceled. |
141 base::RepeatingTimer<VariationsService> timer_; | 153 base::RepeatingTimer<VariationsService> timer_; |
154 | |
155 // The registrar used to manage our Notification registrations. | |
156 content::NotificationRegistrar registrar_; | |
142 }; | 157 }; |
143 | 158 |
144 } // namespace chrome_variations | 159 } // namespace chrome_variations |
145 | 160 |
146 #endif // CHROME_BROWSER_METRICS_VARIATIONS_SERVICE_H_ | 161 #endif // CHROME_BROWSER_METRICS_VARIATIONS_SERVICE_H_ |
OLD | NEW |