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