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