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 #pragma once | 7 #pragma once |
8 | 8 |
| 9 #include <string> |
| 10 |
9 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/time.h" |
10 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "chrome/browser/metrics/proto/study.pb.h" |
11 #include "chrome/browser/metrics/proto/trials_seed.pb.h" | 16 #include "chrome/browser/metrics/proto/trials_seed.pb.h" |
| 17 #include "chrome/common/chrome_version_info.h" |
12 #include "content/public/common/url_fetcher_delegate.h" | 18 #include "content/public/common/url_fetcher_delegate.h" |
13 | 19 |
14 template <typename T> struct DefaultSingletonTraits; | 20 template <typename T> struct DefaultSingletonTraits; |
15 class PrefService; | 21 class PrefService; |
16 | 22 |
17 // Used to setup field trials based on stored variations seed data, and fetch | 23 // Used to setup field trials based on stored variations seed data, and fetch |
18 // new seed data from the variations server. | 24 // new seed data from the variations server. |
19 class VariationsService : public content::URLFetcherDelegate { | 25 class VariationsService : public content::URLFetcherDelegate { |
20 public: | 26 public: |
21 // Returns the singleton instance; | 27 // Returns the singleton instance; |
22 static VariationsService* GetInstance(); | 28 static VariationsService* GetInstance(); |
23 | 29 |
24 // Loads the Variations seed data from the given local prefs. If there is a | 30 // Loads the Variations seed data from the given local prefs. If there is a |
25 // problem with loading, the pref value is cleared. | 31 // problem with loading, the pref value is cleared. |
26 void LoadVariationsSeed(PrefService* local_prefs); | 32 void LoadVariationsSeed(PrefService* local_prefs); |
27 | 33 |
28 // Starts the fetching process, where |OnURLFetchComplete| is called with the | 34 // Starts the fetching process, where |OnURLFetchComplete| is called with the |
29 // response. | 35 // response. |
30 void StartFetchingVariationsSeed(); | 36 void StartFetchingVariationsSeed(); |
31 | 37 |
32 // content::URLFetcherDelegate implementation: | 38 // content::URLFetcherDelegate implementation: |
33 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; | 39 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
34 | 40 |
35 // Register Variations related prefs in Local State. | 41 // Register Variations related prefs in Local State. |
36 static void RegisterPrefs(PrefService* prefs); | 42 static void RegisterPrefs(PrefService* prefs); |
37 | 43 |
38 private: | 44 private: |
39 friend struct DefaultSingletonTraits<VariationsService>; | 45 friend struct DefaultSingletonTraits<VariationsService>; |
40 | 46 |
| 47 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyChannel); |
| 48 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersion); |
| 49 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyVersionWildcards); |
| 50 FRIEND_TEST_ALL_PREFIXES(VariationsServiceTest, CheckStudyDate); |
| 51 |
41 VariationsService(); | 52 VariationsService(); |
42 virtual ~VariationsService(); | 53 virtual ~VariationsService(); |
43 | 54 |
44 // Store the given seed data to the given local prefs. Note that |seed_data| | 55 // Store the given seed data to the given local prefs. Note that |seed_data| |
45 // is assumed to be the raw serialized protobuf data stored in a string. It | 56 // is assumed to be the raw serialized protobuf data stored in a string. It |
46 // will be Base64Encoded for storage. If the string is invalid or the encoding | 57 // will be Base64Encoded for storage. If the string is invalid or the encoding |
47 // fails, the |local_prefs| is left as is. | 58 // fails, the |local_prefs| is left as is. |
48 void StoreSeedData(const std::string& seed_data, PrefService* local_prefs); | 59 void StoreSeedData(const std::string& seed_data, PrefService* local_prefs); |
49 | 60 |
| 61 // Returns whether |study| should be added to the local field trials list |
| 62 // according to its restriction parameters. |
| 63 static bool ShouldAddStudy(const chrome_variations::Study& study); |
| 64 |
| 65 // Checks whether |study| is applicable for the given |channel|. |
| 66 static bool CheckStudyChannel(const chrome_variations::Study& study, |
| 67 chrome::VersionInfo::Channel channel); |
| 68 |
| 69 // Checks whether |study| is applicable for the given version string. |
| 70 static bool CheckStudyVersion(const chrome_variations::Study& study, |
| 71 const std::string& version_string); |
| 72 |
| 73 // Checks whether |study| is applicable for the given date/time. |
| 74 static bool CheckStudyDate(const chrome_variations::Study& study, |
| 75 const base::Time& date_time); |
| 76 |
50 // Contains the current seed request. Will only have a value while a request | 77 // Contains the current seed request. Will only have a value while a request |
51 // is pending, and will be reset by |OnURLFetchComplete|. | 78 // is pending, and will be reset by |OnURLFetchComplete|. |
52 scoped_ptr<content::URLFetcher> pending_seed_request_; | 79 scoped_ptr<content::URLFetcher> pending_seed_request_; |
53 | 80 |
54 // The variations seed data being used for this session. | 81 // The variations seed data being used for this session. |
55 // TODO(jwd): This should be removed. When the seed data is loaded, it will be | 82 // TODO(jwd): This should be removed. When the seed data is loaded, it will be |
56 // used immediately so it won't need to be stored. | 83 // used immediately so it won't need to be stored. |
57 chrome_variations::TrialsSeed variations_seed_; | 84 chrome_variations::TrialsSeed variations_seed_; |
58 }; | 85 }; |
59 | 86 |
60 #endif // CHROME_BROWSER_METRICS_VARIATIONS_SERVICE_H_ | 87 #endif // CHROME_BROWSER_METRICS_VARIATIONS_SERVICE_H_ |
OLD | NEW |