Chromium Code Reviews| 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_VARIATIONS_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_ |
| 6 #define CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_ | 6 #define CHROME_BROWSER_METRICS_VARIATIONS_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/browser/resource_request_allowed_notifier.h" | |
| 18 #include "chrome/common/chrome_version_info.h" | 19 #include "chrome/common/chrome_version_info.h" |
| 19 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
| 20 #include "net/base/network_change_notifier.h" | 21 #include "net/base/network_change_notifier.h" |
|
Alexei Svitkine (slow)
2012/09/12 15:15:46
Remove this #include now that you're not using it
SteveT
2012/09/14 20:25:09
Done.
| |
| 21 #include "net/url_request/url_fetcher_delegate.h" | 22 #include "net/url_request/url_fetcher_delegate.h" |
| 22 | 23 |
| 23 class PrefService; | 24 class PrefService; |
| 24 | 25 |
| 25 namespace chrome_variations { | 26 namespace chrome_variations { |
| 26 | 27 |
| 27 // Used to setup field trials based on stored variations seed data, and fetch | 28 // Used to setup field trials based on stored variations seed data, and fetch |
| 28 // new seed data from the variations server. | 29 // new seed data from the variations server. |
| 29 class VariationsService | 30 class VariationsService |
| 30 : public net::URLFetcherDelegate, | 31 : public net::URLFetcherDelegate, |
| 31 public net::NetworkChangeNotifier::ConnectionTypeObserver{ | 32 public ResourceRequestAllowedNotifier::Observer { |
| 32 public: | 33 public: |
| 33 VariationsService(); | 34 VariationsService(); |
| 34 virtual ~VariationsService(); | 35 virtual ~VariationsService(); |
| 35 | 36 |
| 36 // 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 |
| 37 // 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 |
| 38 // may not be created. | 39 // may not be created. |
| 39 bool CreateTrialsFromSeed(PrefService* local_prefs); | 40 bool CreateTrialsFromSeed(PrefService* local_prefs); |
| 40 | 41 |
| 41 // Calls FetchVariationsSeed once and repeats this periodically. See | 42 // Calls FetchVariationsSeed once and repeats this periodically. See |
| 42 // implementation for details on the period. Must be called after | 43 // implementation for details on the period. Must be called after |
| 43 // |CreateTrialsFromSeed|. | 44 // |CreateTrialsFromSeed|. |
| 44 void StartRepeatedVariationsSeedFetch(); | 45 void StartRepeatedVariationsSeedFetch(); |
| 45 | 46 |
| 46 // Starts the fetching process once, where |OnURLFetchComplete| is called with | 47 // Starts the fetching process once, where |OnURLFetchComplete| is called with |
| 47 // the response. If the network is down at the time, sets a flag to retry when | 48 // the response. If the network is down at the time, sets a flag to retry when |
| 48 // the network is back online. This is virtual so it can be overriden for | 49 // the network is back online. This is virtual so it can be overriden for |
| 49 // testing. | 50 // testing. |
| 50 virtual void FetchVariationsSeed(); | 51 virtual void FetchVariationsSeed(); |
| 51 | 52 |
| 52 // Exposed for testing. | 53 // Exposed for testing. |
| 53 void SetWasOfflineDuringLastRequestAttemptForTesting(bool offline); | 54 void SetWasOfflineDuringLastRequestAttemptForTesting(bool offline); |
| 54 | 55 |
| 56 // ResourceRequestAllowedNotifier::Observer implementation: | |
| 57 virtual void OnResourceRequestsAllowed() OVERRIDE; | |
|
Ilya Sherman
2012/09/11 22:51:27
nit: Can this have private visibility?
SteveT
2012/09/14 20:25:09
Yup! Done!
| |
| 58 | |
| 55 // Register Variations related prefs in Local State. | 59 // Register Variations related prefs in Local State. |
| 56 static void RegisterPrefs(PrefService* prefs); | 60 static void RegisterPrefs(PrefService* prefs); |
| 57 | 61 |
| 58 private: | 62 private: |
| 59 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyChannel); | 63 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyChannel); |
| 60 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyLocale); | 64 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyLocale); |
| 61 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyPlatform); | 65 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyPlatform); |
| 62 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersion); | 66 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersion); |
| 63 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersionWildcards); | 67 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersionWildcards); |
| 64 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyStartDate); | 68 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyStartDate); |
| 65 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, DoNotFetchIfOffline); | 69 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, DoNotFetchIfOffline); |
| 66 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, DoNotFetchIfOnlineToOnline); | 70 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, DoNotFetchIfOnlineToOnline); |
| 67 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, FetchOnReconnect); | 71 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, FetchOnReconnect); |
| 68 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, IsStudyExpired); | 72 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, IsStudyExpired); |
| 69 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, LoadSeed); | 73 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, LoadSeed); |
| 70 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, StoreSeed); | 74 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, StoreSeed); |
| 71 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, ValidateStudy); | 75 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, ValidateStudy); |
| 72 | 76 |
| 73 // net::URLFetcherDelegate implementation: | 77 // net::URLFetcherDelegate implementation: |
| 74 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 78 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 75 | 79 |
| 76 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation. | |
| 77 virtual void OnConnectionTypeChanged( | |
| 78 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; | |
| 79 | |
| 80 // Store the given seed data to the given local prefs. Note that |seed_data| | 80 // Store the given seed data to the given local prefs. Note that |seed_data| |
| 81 // is assumed to be the raw serialized protobuf data stored in a string. It | 81 // is assumed to be the raw serialized protobuf data stored in a string. It |
| 82 // will be Base64Encoded for storage. If the string is invalid or the encoding | 82 // will be Base64Encoded for storage. If the string is invalid or the encoding |
| 83 // fails, the |local_prefs| is left as is and the function returns false. | 83 // fails, the |local_prefs| is left as is and the function returns false. |
| 84 bool StoreSeedData(const std::string& seed_data, | 84 bool StoreSeedData(const std::string& seed_data, |
| 85 const base::Time& seed_date, | 85 const base::Time& seed_date, |
| 86 PrefService* local_prefs); | 86 PrefService* local_prefs); |
| 87 | 87 |
| 88 // Returns whether |study| should be disabled according to its restriction | 88 // Returns whether |study| should be disabled according to its restriction |
| 89 // parameters. Uses |version_info| for min / max version checks and | 89 // parameters. Uses |version_info| for min / max version checks and |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 // The URL to use for querying the Variations server. | 139 // The URL to use for querying the Variations server. |
| 140 GURL variations_server_url_; | 140 GURL variations_server_url_; |
| 141 | 141 |
| 142 // Cached serial number from the most recently fetched Variations seed. | 142 // Cached serial number from the most recently fetched Variations seed. |
| 143 std::string variations_serial_number_; | 143 std::string variations_serial_number_; |
| 144 | 144 |
| 145 // Tracks whether |CreateTrialsFromSeed| has been called, to ensure that | 145 // Tracks whether |CreateTrialsFromSeed| has been called, to ensure that |
| 146 // it gets called prior to |StartRepeatedVariationsSeedFetch|. | 146 // it gets called prior to |StartRepeatedVariationsSeedFetch|. |
| 147 bool create_trials_from_seed_called_; | 147 bool create_trials_from_seed_called_; |
| 148 | 148 |
| 149 // Tracks whether or not the last seed request attempt failed due to being | |
| 150 // offline. | |
| 151 bool was_offline_during_last_request_attempt_; | |
| 152 | |
| 153 // The timer used to repeatedly ping the server. Keep this as an instance | 149 // The timer used to repeatedly ping the server. Keep this as an instance |
| 154 // member so if VariationsService goes out of scope, the timer is | 150 // member so if VariationsService goes out of scope, the timer is |
| 155 // automatically canceled. | 151 // automatically canceled. |
| 156 base::RepeatingTimer<VariationsService> timer_; | 152 base::RepeatingTimer<VariationsService> timer_; |
| 153 | |
| 154 // Helper class used to tell this service if it's allowed to make network | |
| 155 // resource requests. | |
| 156 ResourceRequestAllowedNotifier resource_request_allowed_notifier_; | |
| 157 }; | 157 }; |
| 158 | 158 |
| 159 } // namespace chrome_variations | 159 } // namespace chrome_variations |
| 160 | 160 |
| 161 #endif // CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_ | 161 #endif // CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_ |
| OLD | NEW |