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 |
56 private: | 52 private: |
57 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyChannel); | 53 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyChannel); |
58 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyLocale); | 54 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyLocale); |
59 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyPlatform); | 55 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyPlatform); |
60 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersion); | 56 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersion); |
61 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersionWildcards); | 57 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersionWildcards); |
62 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyStartDate); | 58 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyStartDate); |
63 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, IsStudyExpired); | 59 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, IsStudyExpired); |
64 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, LoadSeed); | 60 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, LoadSeed); |
65 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, StoreSeed); | 61 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, StoreSeed); |
66 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, ValidateStudy); | 62 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, ValidateStudy); |
67 | 63 |
64 // Overridden from content::NotificationObserver: | |
65 virtual void Observe(int type, | |
66 const content::NotificationSource& source, | |
67 const content::NotificationDetails& details) OVERRIDE; | |
68 | |
69 // Starts the fetching process once, where |OnURLFetchComplete| is called with | |
70 // the response. | |
71 void FetchVariationsSeed(); | |
Alexei Svitkine (slow)
2012/07/23 20:26:05
Nit: Move this below OnURLFetchComplete() so that
Alexei Svitkine (slow)
2012/07/23 20:27:53
Oops, this comment was from earlier that I hadn't
| |
72 | |
73 // net::URLFetcherDelegate implementation: | |
Alexei Svitkine (slow)
2012/07/23 20:26:05
Nit: Change this comment to the same style as the
Alexei Svitkine (slow)
2012/07/23 20:27:53
Oops, this comment got duplicated since I had it s
| |
74 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
75 | |
68 // Store the given seed data to the given local prefs. Note that |seed_data| | 76 // 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 | 77 // 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 | 78 // 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. | 79 // fails, the |local_prefs| is left as is and the function returns false. |
72 bool StoreSeedData(const std::string& seed_data, | 80 bool StoreSeedData(const std::string& seed_data, |
73 const base::Time& seed_date, | 81 const base::Time& seed_date, |
74 PrefService* local_prefs); | 82 PrefService* local_prefs); |
75 | 83 |
76 // Returns whether |study| should be disabled according to its restriction | 84 // Returns whether |study| should be disabled according to its restriction |
77 // parameters. Uses |version_info| for min / max version checks and | 85 // 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|. | 132 // is pending, and will be reset by |OnURLFetchComplete|. |
125 scoped_ptr<net::URLFetcher> pending_seed_request_; | 133 scoped_ptr<net::URLFetcher> pending_seed_request_; |
126 | 134 |
127 // The URL to use for querying the variations server. | 135 // The URL to use for querying the variations server. |
128 GURL variations_server_url_; | 136 GURL variations_server_url_; |
129 | 137 |
130 // 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 |
131 // member so if VariationsService goes out of scope, the timer is | 139 // member so if VariationsService goes out of scope, the timer is |
132 // automatically cancelled. | 140 // automatically cancelled. |
133 base::RepeatingTimer<VariationsService> timer_; | 141 base::RepeatingTimer<VariationsService> timer_; |
142 | |
143 // The registrar used to manage our Notification registrations. | |
144 content::NotificationRegistrar registrar_; | |
134 }; | 145 }; |
135 | 146 |
136 } // namespace chrome_variations | 147 } // namespace chrome_variations |
137 | 148 |
138 #endif // CHROME_BROWSER_METRICS_VARIATIONS_SERVICE_H_ | 149 #endif // CHROME_BROWSER_METRICS_VARIATIONS_SERVICE_H_ |
OLD | NEW |