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 #include "chrome/browser/metrics/variations_service.h" | 5 #include "chrome/browser/metrics/variations_service.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/build_time.h" |
| 9 #include "base/version.h" |
8 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
9 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
10 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/metrics/proto/trials_seed.pb.h" | 13 #include "chrome/browser/metrics/proto/trials_seed.pb.h" |
12 #include "chrome/browser/prefs/pref_service.h" | 14 #include "chrome/browser/prefs/pref_service.h" |
13 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
14 #include "content/public/common/url_fetcher.h" | 16 #include "content/public/common/url_fetcher.h" |
15 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
16 #include "net/base/load_flags.h" | 18 #include "net/base/load_flags.h" |
17 #include "net/url_request/url_request_status.h" | 19 #include "net/url_request/url_request_status.h" |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 // Default server of Variations seed info. | 23 // Default server of Variations seed info. |
22 const char kDefaultVariationsServer[] = | 24 const char kDefaultVariationsServer[] = |
23 "https://clients4.google.com/chrome-variations/seed"; | 25 "https://clients4.google.com/chrome-variations/seed"; |
24 | 26 |
| 27 // Maps chrome_variations::Study_Channel enum values to corresponding |
| 28 // chrome::VersionInfo::Channel enum values. |
| 29 chrome::VersionInfo::Channel ConvertStudyChannelToVersionChannel( |
| 30 chrome_variations::Study_Channel study_channel) { |
| 31 switch (study_channel) { |
| 32 case chrome_variations::Study_Channel_CANARY: |
| 33 return chrome::VersionInfo::CHANNEL_CANARY; |
| 34 case chrome_variations::Study_Channel_DEV: |
| 35 return chrome::VersionInfo::CHANNEL_DEV; |
| 36 case chrome_variations::Study_Channel_BETA: |
| 37 return chrome::VersionInfo::CHANNEL_BETA; |
| 38 case chrome_variations::Study_Channel_STABLE: |
| 39 return chrome::VersionInfo::CHANNEL_STABLE; |
| 40 } |
| 41 // All enum values of |study_channel| were handled above. |
| 42 NOTREACHED(); |
| 43 return chrome::VersionInfo::CHANNEL_UNKNOWN; |
| 44 } |
| 45 |
25 } // namespace | 46 } // namespace |
26 | 47 |
27 // Static | 48 // Static |
28 VariationsService* VariationsService::GetInstance() { | 49 VariationsService* VariationsService::GetInstance() { |
29 return Singleton<VariationsService>::get(); | 50 return Singleton<VariationsService>::get(); |
30 } | 51 } |
31 | 52 |
32 void VariationsService::LoadVariationsSeed(PrefService* local_prefs) { | 53 void VariationsService::LoadVariationsSeed(PrefService* local_prefs) { |
33 std::string base64_seed_data = local_prefs->GetString(prefs::kVariationsSeed); | 54 std::string base64_seed_data = local_prefs->GetString(prefs::kVariationsSeed); |
34 std::string seed_data; | 55 std::string seed_data; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 if (!base::Base64Encode(seed_data, &base64_seed_data)) { | 108 if (!base::Base64Encode(seed_data, &base64_seed_data)) { |
88 VLOG(1) << "Variations Seed data from server fails Base64Encode, rejecting " | 109 VLOG(1) << "Variations Seed data from server fails Base64Encode, rejecting " |
89 << "the seed."; | 110 << "the seed."; |
90 return; | 111 return; |
91 } | 112 } |
92 local_prefs->SetString(prefs::kVariationsSeed, base64_seed_data); | 113 local_prefs->SetString(prefs::kVariationsSeed, base64_seed_data); |
93 } | 114 } |
94 | 115 |
95 VariationsService::VariationsService() {} | 116 VariationsService::VariationsService() {} |
96 VariationsService::~VariationsService() {} | 117 VariationsService::~VariationsService() {} |
| 118 |
| 119 // static |
| 120 bool VariationsService::ShouldAddStudy(const chrome_variations::Study& study) { |
| 121 const chrome::VersionInfo current_version_info; |
| 122 if (!current_version_info.is_valid()) |
| 123 return false; |
| 124 |
| 125 if (!CheckStudyChannel(study, chrome::VersionInfo::GetChannel())) { |
| 126 DVLOG(1) << "Filtered out study " << study.name() << " due to version."; |
| 127 return false; |
| 128 } |
| 129 |
| 130 if (!CheckStudyVersion(study, current_version_info.Version())) { |
| 131 DVLOG(1) << "Filtered out study " << study.name() << " due to version."; |
| 132 return false; |
| 133 } |
| 134 |
| 135 // Use build time and not system time to match what is done in field_trial.cc. |
| 136 if (!CheckStudyDate(study, base::GetBuildTime())) { |
| 137 DVLOG(1) << "Filtered out study " << study.name() << " due to date."; |
| 138 return false; |
| 139 } |
| 140 |
| 141 DVLOG(1) << "Kept study " << study.name() << "."; |
| 142 return true; |
| 143 } |
| 144 |
| 145 // static |
| 146 bool VariationsService::CheckStudyChannel( |
| 147 const chrome_variations::Study& study, |
| 148 chrome::VersionInfo::Channel channel) { |
| 149 for (int i = 0; i < study.channel_size(); ++i) { |
| 150 if (ConvertStudyChannelToVersionChannel(study.channel(i)) == channel) |
| 151 return true; |
| 152 } |
| 153 return false; |
| 154 } |
| 155 |
| 156 // static |
| 157 bool VariationsService::CheckStudyVersion(const chrome_variations::Study& study, |
| 158 const std::string& version_string) { |
| 159 const Version current_version(version_string); |
| 160 if (!current_version.IsValid()) { |
| 161 DCHECK(false); |
| 162 return false; |
| 163 } |
| 164 |
| 165 if (study.has_min_version()) { |
| 166 const Version min_version(study.min_version()); |
| 167 if (!min_version.IsValid()) |
| 168 return false; |
| 169 if (current_version.CompareTo(min_version) < 0) |
| 170 return false; |
| 171 } |
| 172 |
| 173 if (study.has_max_version()) { |
| 174 const Version max_version(study.max_version()); |
| 175 if (!max_version.IsValid()) |
| 176 return false; |
| 177 if (current_version.CompareTo(max_version) > 0) |
| 178 return false; |
| 179 } |
| 180 |
| 181 return true; |
| 182 } |
| 183 |
| 184 // static |
| 185 bool VariationsService::CheckStudyDate(const chrome_variations::Study& study, |
| 186 const base::Time& date_time) { |
| 187 const base::Time epoch = base::Time::UnixEpoch(); |
| 188 |
| 189 if (study.has_start_date()) { |
| 190 const base::Time start_date = |
| 191 epoch + base::TimeDelta::FromMilliseconds(study.start_date()); |
| 192 if (date_time < start_date) |
| 193 return false; |
| 194 } |
| 195 |
| 196 if (study.has_expiry_date()) { |
| 197 const base::Time expiry_date = |
| 198 epoch + base::TimeDelta::FromMilliseconds(study.expiry_date()); |
| 199 if (date_time >= expiry_date) |
| 200 return false; |
| 201 } |
| 202 |
| 203 return true; |
| 204 } |
OLD | NEW |