Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1184)

Side by Side Diff: chrome/browser/metrics/variations/variations_service.h

Issue 10917120: Activate the VariationsService for ChromeOS and ensure that it does not ping the server until the E… (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: notification situation solution Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/metrics/variations/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/url_request/url_fetcher_delegate.h" 21 #include "net/url_request/url_fetcher_delegate.h"
22 22
23 class PrefService; 23 class PrefService;
24 24
25 namespace chromeos {
26 class WizardController;
27 }
28
25 namespace chrome_variations { 29 namespace chrome_variations {
26 30
27 // Used to setup field trials based on stored variations seed data, and fetch 31 // Used to setup field trials based on stored variations seed data, and fetch
28 // new seed data from the variations server. 32 // new seed data from the variations server.
29 class VariationsService 33 class VariationsService
30 : public net::URLFetcherDelegate, 34 : public net::URLFetcherDelegate,
31 public net::NetworkChangeNotifier::ConnectionTypeObserver{ 35 public ResourceRequestAllowedNotifier::Observer {
32 public: 36 public:
33 VariationsService(); 37 VariationsService();
34 virtual ~VariationsService(); 38 virtual ~VariationsService();
35 39
36 // 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
37 // 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
38 // may not be created. 42 // may not be created.
39 bool CreateTrialsFromSeed(PrefService* local_prefs); 43 bool CreateTrialsFromSeed(PrefService* local_prefs);
40 44
41 // Calls FetchVariationsSeed once and repeats this periodically. See 45 // Calls FetchVariationsSeed once and repeats this periodically. See
42 // implementation for details on the period. Must be called after 46 // implementation for details on the period. Must be called after
43 // |CreateTrialsFromSeed|. 47 // |CreateTrialsFromSeed|.
44 void StartRepeatedVariationsSeedFetch(); 48 void StartRepeatedVariationsSeedFetch();
45 49
50 // Checks if prerequisites for fetching the Variations seed are met, and if
51 // so, performs the actual fetch using |DoActualFetch|.
52 void FetchVariationsSeed();
53
54 // Register Variations related prefs in Local State.
55 static void RegisterPrefs(PrefService* prefs);
56
57 protected:
46 // Starts the fetching process once, where |OnURLFetchComplete| is called with 58 // 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 59 // 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 60 // the network is back online. This is virtual so it can be overriden for
49 // testing. 61 // testing.
50 virtual void FetchVariationsSeed(); 62 virtual void DoActualFetch();
Alexei Svitkine (slow) 2012/09/19 19:44:28 Why the name change?
SteveT 2012/09/20 19:40:18 I separated all the URLFetchRequest stuff into thi
51 63
52 // Exposed for testing. 64 // Allows tests to replace |resource_request_allowed_notifier_| with a mock.
53 void SetWasOfflineDuringLastRequestAttemptForTesting(bool offline); 65 void SetResourceRequestAllowedNotifierForTesting(
66 ResourceRequestAllowedNotifier* notifier);
54 67
55 // Register Variations related prefs in Local State. 68 // Tracks whether |CreateTrialsFromSeed| has been called, to ensure that
56 static void RegisterPrefs(PrefService* prefs); 69 // it gets called prior to |StartRepeatedVariationsSeedFetch|.
70 // This field is protected so test classes can modify it.
71 bool create_trials_from_seed_called_;
Alexei Svitkine (slow) 2012/09/19 19:44:28 I suggest instead making *ForTesting() getters and
SteveT 2012/09/20 19:40:18 Done.
57 72
58 private: 73 private:
59 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyChannel); 74 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyChannel);
60 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyLocale); 75 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyLocale);
61 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyPlatform); 76 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyPlatform);
62 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersion); 77 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersion);
63 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersionWildcards); 78 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersionWildcards);
64 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyStartDate); 79 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyStartDate);
65 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, DoNotFetchIfOffline); 80 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, DoNotFetchIfOffline);
66 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, DoNotFetchIfOnlineToOnline); 81 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, DoNotFetchIfOnlineToOnline);
67 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, FetchOnReconnect); 82 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, FetchOnReconnect);
68 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, IsStudyExpired); 83 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, IsStudyExpired);
69 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, LoadSeed); 84 FRIEND_TEST_ALL_PREFIXES(VariationsServiceCreatedTest, LoadSeed);
70 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, StoreSeed); 85 FRIEND_TEST_ALL_PREFIXES(VariationsServiceCreatedTest, StoreSeed);
71 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, ValidateStudy); 86 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, ValidateStudy);
72 87
73 // net::URLFetcherDelegate implementation: 88 // net::URLFetcherDelegate implementation:
74 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 89 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
75 90
76 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation. 91 // ResourceRequestAllowedNotifier::Observer implementation:
77 virtual void OnConnectionTypeChanged( 92 virtual void OnResourceRequestsAllowed() OVERRIDE;
78 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
79 93
80 // Store the given seed data to the given local prefs. Note that |seed_data| 94 // 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 95 // 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 96 // 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. 97 // fails, the |local_prefs| is left as is and the function returns false.
84 bool StoreSeedData(const std::string& seed_data, 98 bool StoreSeedData(const std::string& seed_data,
85 const base::Time& seed_date, 99 const base::Time& seed_date,
86 PrefService* local_prefs); 100 PrefService* local_prefs);
87 101
88 // Returns whether |study| should be disabled according to its restriction 102 // Returns whether |study| should be disabled according to its restriction
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 // Contains the current seed request. Will only have a value while a request 149 // Contains the current seed request. Will only have a value while a request
136 // is pending, and will be reset by |OnURLFetchComplete|. 150 // is pending, and will be reset by |OnURLFetchComplete|.
137 scoped_ptr<net::URLFetcher> pending_seed_request_; 151 scoped_ptr<net::URLFetcher> pending_seed_request_;
138 152
139 // The URL to use for querying the Variations server. 153 // The URL to use for querying the Variations server.
140 GURL variations_server_url_; 154 GURL variations_server_url_;
141 155
142 // Cached serial number from the most recently fetched Variations seed. 156 // Cached serial number from the most recently fetched Variations seed.
143 std::string variations_serial_number_; 157 std::string variations_serial_number_;
144 158
145 // Tracks whether |CreateTrialsFromSeed| has been called, to ensure that
146 // it gets called prior to |StartRepeatedVariationsSeedFetch|.
147 bool create_trials_from_seed_called_;
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 159 // 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 160 // member so if VariationsService goes out of scope, the timer is
155 // automatically canceled. 161 // automatically canceled.
156 base::RepeatingTimer<VariationsService> timer_; 162 base::RepeatingTimer<VariationsService> timer_;
163
164 // Helper class used to tell this service if it's allowed to make network
165 // resource requests.
166 scoped_ptr<ResourceRequestAllowedNotifier> resource_request_allowed_notifier_;
157 }; 167 };
158 168
159 } // namespace chrome_variations 169 } // namespace chrome_variations
160 170
161 #endif // CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_ 171 #endif // CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698