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 |
25 }// namespace | 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 } | |
MAD
2012/05/05 13:51:17
DCHECK on default please
And didn't you get:
warn
Alexei Svitkine (slow)
2012/05/05 15:14:37
It's fine with clang on the Mac. I believe clang k
Alexei Svitkine (slow)
2012/05/07 15:49:52
Done.
| |
41 } | |
42 | |
43 } // namespace | |
26 | 44 |
27 // Static | 45 // Static |
28 VariationsService* VariationsService::GetInstance() { | 46 VariationsService* VariationsService::GetInstance() { |
29 return Singleton<VariationsService>::get(); | 47 return Singleton<VariationsService>::get(); |
30 } | 48 } |
31 | 49 |
32 VariationsService::~VariationsService() {} | 50 VariationsService::~VariationsService() {} |
33 | 51 |
34 void VariationsService::LoadVariationsSeed(PrefService* local_prefs) { | 52 void VariationsService::LoadVariationsSeed(PrefService* local_prefs) { |
35 std::string base64_seed_data = local_prefs->GetString(prefs::kVariationsSeed); | 53 std::string base64_seed_data = local_prefs->GetString(prefs::kVariationsSeed); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 std::string base64_seed_data; | 106 std::string base64_seed_data; |
89 if (!base::Base64Encode(seed_data, &base64_seed_data)) { | 107 if (!base::Base64Encode(seed_data, &base64_seed_data)) { |
90 VLOG(1) << "Variations Seed data from server fails Base64Encode, rejecting " | 108 VLOG(1) << "Variations Seed data from server fails Base64Encode, rejecting " |
91 << "the seed."; | 109 << "the seed."; |
92 return; | 110 return; |
93 } | 111 } |
94 local_prefs->SetString(prefs::kVariationsSeed, base64_seed_data); | 112 local_prefs->SetString(prefs::kVariationsSeed, base64_seed_data); |
95 } | 113 } |
96 | 114 |
97 VariationsService::VariationsService() {} | 115 VariationsService::VariationsService() {} |
116 | |
117 // static | |
118 bool VariationsService::ShouldAddStudy(const chrome_variations::Study& study) { | |
119 const chrome::VersionInfo current_version_info; | |
120 if (!current_version_info.is_valid()) | |
121 return false; | |
122 | |
123 if (!CheckStudyChannel(study, chrome::VersionInfo::GetChannel())) | |
124 return false; | |
125 | |
126 if (!CheckStudyVersion(study, current_version_info.Version())) | |
127 return false; | |
128 | |
129 if (!CheckStudyDate(study, base::GetBuildTime())) | |
MAD
2012/05/05 13:51:17
Please add a comment explaining why we use build t
Alexei Svitkine (slow)
2012/05/07 15:49:52
Done.
| |
130 return false; | |
131 | |
132 return true; | |
MAD
2012/05/05 13:51:17
I wonder if we should add DVLOG(1)s when we filter
Alexei Svitkine (slow)
2012/05/07 15:49:52
Done.
| |
133 } | |
134 | |
135 // static | |
136 bool VariationsService::CheckStudyChannel( | |
137 const chrome_variations::Study& study, | |
138 chrome::VersionInfo::Channel channel) { | |
139 for (int i = 0; i < study.channel_size(); i++) { | |
140 if (ConvertStudyChannelToVersionChannel(study.channel(i)) == channel) | |
141 return true; | |
142 } | |
143 return false; | |
144 } | |
145 | |
146 // static | |
147 bool VariationsService::CheckStudyVersion(const chrome_variations::Study& study, | |
148 const std::string& version_string) { | |
149 const Version current_version(version_string); | |
150 if (!current_version.IsValid()) { | |
151 DCHECK(false); | |
MAD
2012/05/05 13:51:17
We try not to DCHECK AND handle the error usually.
Alexei Svitkine (slow)
2012/05/05 15:14:37
I copied this from elsewhere. I think the reasonin
| |
152 return false; | |
153 } | |
154 | |
155 if (study.has_min_version()) { | |
156 const Version min_version(study.min_version()); | |
157 if (!min_version.IsValid()) | |
158 return false; | |
159 if (current_version.CompareTo(min_version) < 0) | |
160 return false; | |
161 } | |
162 | |
163 if (study.has_max_version()) { | |
164 const Version max_version(study.max_version()); | |
165 if (!max_version.IsValid()) | |
166 return false; | |
167 if (current_version.CompareTo(max_version) > 0) | |
168 return false; | |
169 } | |
170 | |
171 return true; | |
172 } | |
173 | |
174 // static | |
175 bool VariationsService::CheckStudyDate(const chrome_variations::Study& study, | |
176 const base::Time& date_time) { | |
177 const base::Time epoch = base::Time::UnixEpoch(); | |
178 | |
179 if (study.has_start_date()) { | |
180 const base::Time start_date = | |
181 epoch + base::TimeDelta::FromMilliseconds(study.start_date()); | |
182 if (date_time < start_date) | |
183 return false; | |
184 } | |
185 | |
186 if (study.has_expiry_date()) { | |
187 const base::Time expiry_date = | |
188 epoch + base::TimeDelta::FromMilliseconds(study.expiry_date()); | |
189 if (date_time >= expiry_date) | |
190 return false; | |
191 } | |
192 | |
193 return true; | |
194 } | |
OLD | NEW |