OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_METRICS_VARIATIONS_SERVICE_H_ | |
6 #define CHROME_BROWSER_METRICS_VARIATIONS_SERVICE_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/compiler_specific.h" | |
11 #include "base/gtest_prod_util.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/metrics/field_trial.h" | |
14 #include "base/time.h" | |
15 #include "base/timer.h" | |
16 #include "chrome/browser/metrics/proto/study.pb.h" | |
17 #include "chrome/browser/metrics/proto/trials_seed.pb.h" | |
18 #include "chrome/common/chrome_version_info.h" | |
19 #include "googleurl/src/gurl.h" | |
20 #include "net/url_request/url_fetcher_delegate.h" | |
21 | |
22 class PrefService; | |
23 | |
24 namespace net { | |
25 class URLFetcher; | |
26 } // namespace net | |
27 | |
28 namespace chrome_variations { | |
29 | |
30 // Used to setup field trials based on stored variations seed data, and fetch | |
31 // new seed data from the variations server. | |
32 class VariationsService : public net::URLFetcherDelegate { | |
33 public: | |
34 VariationsService(); | |
35 virtual ~VariationsService(); | |
36 | |
37 // 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 | |
39 // may not be created. | |
40 bool CreateTrialsFromSeed(PrefService* local_prefs); | |
41 | |
42 // Calls FetchVariationsSeed once and repeats this periodically. See | |
43 // implementation for details on the period. Must be called after | |
44 // |CreateTrialsFromSeed|. | |
45 void StartRepeatedVariationsSeedFetch(); | |
46 | |
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. | |
55 static void RegisterPrefs(PrefService* prefs); | |
56 | |
57 private: | |
58 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyChannel); | |
59 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyLocale); | |
60 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyPlatform); | |
61 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersion); | |
62 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersionWildcards); | |
63 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyStartDate); | |
64 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, IsStudyExpired); | |
65 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, LoadSeed); | |
66 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, StoreSeed); | |
67 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, ValidateStudy); | |
68 | |
69 // 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 | |
71 // 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. | |
73 bool StoreSeedData(const std::string& seed_data, | |
74 const base::Time& seed_date, | |
75 PrefService* local_prefs); | |
76 | |
77 // Returns whether |study| should be disabled according to its restriction | |
78 // parameters. Uses |version_info| for min / max version checks and | |
79 // |reference_date| for the start date check. | |
80 static bool ShouldAddStudy(const Study& study, | |
81 const chrome::VersionInfo& version_info, | |
82 const base::Time& reference_date); | |
83 | |
84 // Checks whether a study is applicable for the given |channel| per |filter|. | |
85 static bool CheckStudyChannel(const Study_Filter& filter, | |
86 chrome::VersionInfo::Channel channel); | |
87 | |
88 // Checks whether a study is applicable for the given |locale| per |filter|. | |
89 static bool CheckStudyLocale(const chrome_variations::Study_Filter& filter, | |
90 const std::string& locale); | |
91 | |
92 // Checks whether a study is applicable for the given |platform| per |filter|. | |
93 static bool CheckStudyPlatform(const Study_Filter& filter, | |
94 chrome_variations::Study_Platform platform); | |
95 | |
96 // Checks whether a study is applicable for the given version per |filter|. | |
97 static bool CheckStudyVersion(const Study_Filter& filter, | |
98 const std::string& version_string); | |
99 | |
100 // Checks whether a study is applicable for the given date/time per |filter|. | |
101 static bool CheckStudyStartDate(const Study_Filter& filter, | |
102 const base::Time& date_time); | |
103 | |
104 // Checks whether |study| is expired using the given date/time. | |
105 static bool IsStudyExpired(const Study& study, | |
106 const base::Time& date_time); | |
107 | |
108 // Validates the sanity of |study| and computes the total probability. | |
109 static bool ValidateStudyAndComputeTotalProbability( | |
110 const Study& study, | |
111 base::FieldTrial::Probability* total_probability); | |
112 | |
113 // Loads the Variations seed data from the given local prefs into |seed|. If | |
114 // there is a problem with loading, the pref value is cleared and false is | |
115 // returned. If successful, |seed| will contain the loaded data and true is | |
116 // returned. | |
117 bool LoadTrialsSeedFromPref(PrefService* local_prefs, TrialsSeed* seed); | |
118 | |
119 // Creates and registers a field trial from the |study| data. Disables the | |
120 // trial if IsStudyExpired(study, reference_date) is true. | |
121 void CreateTrialFromStudy(const Study& study, | |
122 const base::Time& reference_date); | |
123 | |
124 // Contains the current seed request. Will only have a value while a request | |
125 // is pending, and will be reset by |OnURLFetchComplete|. | |
126 scoped_ptr<net::URLFetcher> pending_seed_request_; | |
127 | |
128 // The URL to use for querying the variations server. | |
129 GURL variations_server_url_; | |
130 | |
131 // Cached serial number from the most recently fetched variations seed. | |
132 std::string variations_serial_number_; | |
133 | |
134 // Tracks whether |CreateTrialsFromSeed| has been called, to ensure that | |
135 // it gets called prior to |StartRepeatedVariationsSeedFetch|. | |
136 bool create_trials_from_seed_called_; | |
137 | |
138 // 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 | |
140 // automatically canceled. | |
141 base::RepeatingTimer<VariationsService> timer_; | |
142 }; | |
143 | |
144 } // namespace chrome_variations | |
145 | |
146 #endif // CHROME_BROWSER_METRICS_VARIATIONS_SERVICE_H_ | |
OLD | NEW |