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. | 46 // implementation for details on the period. |
44 void StartRepeatedVariationsSeedFetch(); | 47 void StartRepeatedVariationsSeedFetch(); |
45 | 48 |
46 // Starts the fetching process once, where |OnURLFetchComplete| is called with | |
47 // the response. | |
48 void FetchVariationsSeed(); | |
49 | |
50 // net::URLFetcherDelegate implementation: | |
51 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
52 | |
53 // Register Variations related prefs in Local State. | 49 // Register Variations related prefs in Local State. |
54 static void RegisterPrefs(PrefService* prefs); | 50 static void RegisterPrefs(PrefService* prefs); |
55 | 51 |
| 52 protected: |
| 53 // Starts the fetching process once, where |OnURLFetchComplete| is called with |
| 54 // the response. This is protected so we can override this for testing |
| 55 // purposes. |
| 56 virtual void FetchVariationsSeed(); |
| 57 |
| 58 // Overridden from content::NotificationObserver: |
| 59 virtual void Observe(int type, |
| 60 const content::NotificationSource& source, |
| 61 const content::NotificationDetails& details) OVERRIDE; |
| 62 |
56 private: | 63 private: |
57 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyChannel); | 64 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyChannel); |
58 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyLocale); | 65 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyLocale); |
59 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyPlatform); | 66 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyPlatform); |
60 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersion); | 67 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersion); |
61 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersionWildcards); | 68 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersionWildcards); |
62 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyStartDate); | 69 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyStartDate); |
63 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, IsStudyExpired); | 70 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, IsStudyExpired); |
64 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, LoadSeed); | 71 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, LoadSeed); |
65 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, StoreSeed); | 72 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, StoreSeed); |
66 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, ValidateStudy); | 73 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, ValidateStudy); |
67 | 74 |
| 75 // Overridden from net::URLFetcherDelegate: |
| 76 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 77 |
68 // Store the given seed data to the given local prefs. Note that |seed_data| | 78 // Store the given seed data to the given local prefs. Note that |seed_data| |
69 // is assumed to be the raw serialized protobuf data stored in a string. It | 79 // is assumed to be the raw serialized protobuf data stored in a string. It |
70 // will be Base64Encoded for storage. If the string is invalid or the encoding | 80 // will be Base64Encoded for storage. If the string is invalid or the encoding |
71 // fails, the |local_prefs| is left as is and the function returns false. | 81 // fails, the |local_prefs| is left as is and the function returns false. |
72 bool StoreSeedData(const std::string& seed_data, | 82 bool StoreSeedData(const std::string& seed_data, |
73 const base::Time& seed_date, | 83 const base::Time& seed_date, |
74 PrefService* local_prefs); | 84 PrefService* local_prefs); |
75 | 85 |
76 // Returns whether |study| should be disabled according to its restriction | 86 // Returns whether |study| should be disabled according to its restriction |
77 // parameters. Uses |version_info| for min / max version checks and | 87 // parameters. Uses |version_info| for min / max version checks and |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 // is pending, and will be reset by |OnURLFetchComplete|. | 134 // is pending, and will be reset by |OnURLFetchComplete|. |
125 scoped_ptr<net::URLFetcher> pending_seed_request_; | 135 scoped_ptr<net::URLFetcher> pending_seed_request_; |
126 | 136 |
127 // The URL to use for querying the variations server. | 137 // The URL to use for querying the variations server. |
128 GURL variations_server_url_; | 138 GURL variations_server_url_; |
129 | 139 |
130 // The timer used to repeatedly ping the server. Keep this as an instance | 140 // The timer used to repeatedly ping the server. Keep this as an instance |
131 // member so if VariationsService goes out of scope, the timer is | 141 // member so if VariationsService goes out of scope, the timer is |
132 // automatically cancelled. | 142 // automatically cancelled. |
133 base::RepeatingTimer<VariationsService> timer_; | 143 base::RepeatingTimer<VariationsService> timer_; |
| 144 |
| 145 // The registrar used to manage our Notification registrations. |
| 146 content::NotificationRegistrar registrar_; |
134 }; | 147 }; |
135 | 148 |
136 } // namespace chrome_variations | 149 } // namespace chrome_variations |
137 | 150 |
138 #endif // CHROME_BROWSER_METRICS_VARIATIONS_SERVICE_H_ | 151 #endif // CHROME_BROWSER_METRICS_VARIATIONS_SERVICE_H_ |
OLD | NEW |