OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 #include "chrome/installer/util/experiment_storage.h" | |
6 | |
7 #include <windows.h> | |
8 | |
9 #include <stdint.h> | |
10 | |
11 #include <limits> | |
12 #include <string> | |
13 | |
14 #include "base/base64.h" | |
15 #include "base/bind.h" | |
16 #include "base/bind_helpers.h" | |
17 #include "base/logging.h" | |
18 #include "base/memory/ptr_util.h" | |
19 #include "base/strings/utf_string_conversions.h" | |
20 #include "base/task_scheduler/post_task.h" | |
21 #include "base/task_scheduler/task_traits.h" | |
22 #include "base/time/time.h" | |
23 #include "base/win/registry.h" | |
24 #include "base/win/win_util.h" | |
25 #include "chrome/install_static/install_details.h" | |
26 #include "chrome/install_static/install_modes.h" | |
27 #include "chrome/install_static/install_util.h" | |
28 #include "chrome/installer/util/experiment.h" | |
29 #include "chrome/installer/util/experiment_labels.h" | |
30 #include "chrome/installer/util/experiment_metrics.h" | |
31 #include "chrome/installer/util/google_update_settings.h" | |
32 #include "chrome/installer/util/shell_util.h" | |
33 | |
34 namespace installer { | |
35 | |
36 namespace { | |
37 | |
38 constexpr base::char16 kExperimentLabelName[] = L"CrExp60"; | |
39 constexpr wchar_t kRegKeyRetention[] = L"Retention"; | |
40 constexpr wchar_t kRegValueActionDelay[] = L"ActionDelay"; | |
41 constexpr wchar_t kRegValueFirstDisplayTime[] = L"FirstDisplayTime"; | |
42 constexpr wchar_t kRegValueGroup[] = L"Group"; | |
43 constexpr wchar_t kRegValueInactiveDays[] = L"InactiveDays"; | |
44 constexpr wchar_t kRegValueLatestDisplayTime[] = L"LatestDisplayTime"; | |
45 constexpr wchar_t kRegValueRetentionStudy[] = L"RetentionStudy"; | |
46 constexpr wchar_t kRegValueState[] = L"State"; | |
47 constexpr wchar_t kRegValueToastCount[] = L"ToastCount"; | |
48 constexpr wchar_t kRegValueToastLocation[] = L"ToastLocation"; | |
49 constexpr wchar_t kRegValueUserSessionUptime[] = L"UserSessionUptime"; | |
50 | |
51 constexpr int kSessionLengthBucketLowestBit = 0; | |
52 constexpr int kActionDelayBucketLowestBit = | |
53 ExperimentMetrics::kSessionLengthBucketBits + kSessionLengthBucketLowestBit; | |
54 constexpr int kLastUsedBucketLowestBit = | |
55 ExperimentMetrics::kActionDelayBucketBits + kActionDelayBucketLowestBit; | |
56 constexpr int kToastHourLowestBit = | |
57 ExperimentMetrics::kLastUsedBucketBits + kLastUsedBucketLowestBit; | |
58 constexpr int kFirstToastOffsetLowestBit = | |
59 ExperimentMetrics::kToastHourBits + kToastHourLowestBit; | |
60 constexpr int kToastCountLowestBit = | |
61 ExperimentMetrics::kFirstToastOffsetBits + kFirstToastOffsetLowestBit; | |
62 constexpr int kToastLocationLowestBit = | |
63 ExperimentMetrics::kToastCountBits + kToastCountLowestBit; | |
64 constexpr int kStateLowestBit = | |
65 ExperimentMetrics::kToastLocationBits + kToastLocationLowestBit; | |
66 constexpr int kGroupLowestBit = ExperimentMetrics::kStateBits + kStateLowestBit; | |
67 | |
68 // Helper functions ------------------------------------------------------------ | |
69 | |
70 // Returns the name of the global mutex used to protect the storage location. | |
71 base::string16 GetMutexName() { | |
72 base::string16 name(L"Global\\"); | |
73 name.append(install_static::kCompanyPathName); | |
74 name.append(ShellUtil::GetBrowserModelId(!install_static::IsSystemInstall())); | |
75 name.append(L"ExperimentStorageMutex"); | |
76 return name; | |
77 } | |
78 | |
79 // Populates |path| with the path to the registry key in which the current | |
80 // user's experiment state is stored. Returns false if the path cannot be | |
81 // determined. | |
82 bool GetExperimentStateKeyPath(bool system_level, base::string16* path) { | |
83 const install_static::InstallDetails& install_details = | |
84 install_static::InstallDetails::Get(); | |
85 | |
86 if (!system_level) { | |
87 *path = install_details.GetClientStateKeyPath().append(kRegKeyRetention); | |
88 return true; | |
89 } | |
90 | |
91 base::string16 user_sid; | |
92 if (base::win::GetUserSidString(&user_sid)) { | |
93 *path = install_details.GetClientStateMediumKeyPath() | |
94 .append(L"\\") | |
95 .append(kRegKeyRetention) | |
96 .append(L"\\") | |
97 .append(user_sid); | |
98 return true; | |
99 } | |
100 | |
101 NOTREACHED(); | |
102 return false; | |
103 } | |
104 | |
105 bool OpenParticipationKey(bool write_access, base::win::RegKey* key) { | |
106 const install_static::InstallDetails& details = | |
107 install_static::InstallDetails::Get(); | |
108 LONG result = key->Open( | |
109 details.system_level() ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, | |
110 details.GetClientStateKeyPath().c_str(), | |
111 KEY_WOW64_32KEY | (write_access ? KEY_SET_VALUE : KEY_QUERY_VALUE)); | |
112 return result == ERROR_SUCCESS; | |
113 } | |
114 | |
115 // Reads |value_name| into |result|. Returns false if the value is not found or | |
116 // is out of range. | |
117 template <class T> | |
118 bool ReadBoundedDWORD(base::win::RegKey* key, | |
119 const wchar_t* value_name, | |
120 DWORD min_value, | |
121 DWORD max_value, | |
122 T* result) { | |
123 DWORD dword_value; | |
124 if (key->ReadValueDW(value_name, &dword_value) != ERROR_SUCCESS) | |
125 return false; | |
126 if (dword_value < min_value || dword_value > max_value) | |
127 return false; | |
128 *result = static_cast<T>(dword_value); | |
129 return true; | |
130 } | |
131 | |
132 // Reads the internal representation of a Time or TimeDelta from |value_name| | |
133 // into |result|. Returns false if the value is not found or is out of range. | |
134 template <class T> | |
135 bool ReadTime(base::win::RegKey* key, const wchar_t* value_name, T* result) { | |
136 int64_t qword_value; | |
137 if (key->ReadInt64(value_name, &qword_value) != ERROR_SUCCESS) | |
138 return false; | |
139 if (qword_value > std::numeric_limits<int64_t>::max()) | |
140 return false; | |
141 *result = T::FromInternalValue(static_cast<int64_t>(qword_value)); | |
142 return true; | |
143 } | |
144 | |
145 void WriteTime(base::win::RegKey* key, | |
146 const wchar_t* value_name, | |
147 int64_t internal_time_value) { | |
148 key->WriteValue(value_name, &internal_time_value, sizeof(internal_time_value), | |
149 REG_QWORD); | |
150 } | |
151 | |
152 } // namespace | |
153 | |
154 // ExperimentStorage::Lock ----------------------------------------------------- | |
155 | |
156 ExperimentStorage::Lock::~Lock() { | |
157 BOOL result = ::ReleaseMutex(storage_->mutex_.Get()); | |
158 DCHECK(result); | |
159 } | |
160 | |
161 bool ExperimentStorage::Lock::ReadParticipation(Participation* participation) { | |
162 base::win::RegKey key; | |
163 // A failure to open the key likely indicates that this isn't running from a | |
164 // real install of Chrome. | |
165 if (!OpenParticipationKey(false /* !write_access */, &key)) | |
166 return false; | |
167 | |
168 DWORD value = 0; | |
169 LONG result = key.ReadValueDW(kRegValueRetentionStudy, &value); | |
170 if (result != ERROR_SUCCESS) { | |
171 // This likely means that the value is not present. | |
172 *participation = Participation::kNotEvaluated; | |
173 } else if (value == 0) { | |
174 *participation = Participation::kNotParticipating; | |
175 } else { | |
176 *participation = Participation::kIsParticipating; | |
177 } | |
178 return true; | |
179 } | |
180 | |
181 bool ExperimentStorage::Lock::WriteParticipation(Participation participation) { | |
182 base::win::RegKey key; | |
183 // A failure to open the key likely indicates that this isn't running from a | |
184 // real install of Chrome. | |
185 if (!OpenParticipationKey(true /* write_access */, &key)) | |
186 return false; | |
187 | |
188 if (participation == Participation::kNotEvaluated) | |
189 return key.DeleteValue(kRegValueRetentionStudy) == ERROR_SUCCESS; | |
190 const DWORD value = participation == Participation::kIsParticipating ? 1 : 0; | |
191 return key.WriteValue(kRegValueRetentionStudy, value) == ERROR_SUCCESS; | |
192 } | |
193 | |
194 bool ExperimentStorage::Lock::LoadExperiment(Experiment* experiment) { | |
195 // This function loads both the experiment metrics and state from the | |
196 // registry. | |
197 // - If no metrics are found: |experiment| is cleared, and true is returned. | |
198 // (Per-user experiment data in the registry is ignored for all users.) | |
199 // - If metrics indicate an initial state (prior to a user being elected into | |
200 // an experiment group): |experiment| is populated with the metrics and true | |
201 // is returned. (Per-user experiment data in the registry is ignored for all | |
202 // users.) | |
203 // - If metrics indicate an intermediate or terminal state and per-user | |
204 // experiment data is in the same state: |experiment| is populated with all | |
205 // data from the registry and true is returned. | |
206 // Otherwise, the metrics correspond to a different user on the machine, so | |
207 // false is returned. | |
208 | |
209 *experiment = Experiment(); | |
210 | |
211 ExperimentMetrics metrics; | |
212 if (!storage_->LoadMetricsUnsafe(&metrics)) | |
213 return false; // Error reading metrics -- do nothing. | |
214 | |
215 if (metrics.InInitialState()) { | |
216 // There should be no per-user experiment data present (ignore it if there | |
217 // happens to be somehow). | |
218 if (metrics.state != ExperimentMetrics::kUninitialized) { | |
grt (UTC plus 2)
2017/06/09 08:38:06
i think this should be removed in favor of making
nikunjb
2017/06/09 09:01:45
Done.
| |
219 experiment->InitializeFromMetrics(metrics); | |
220 } | |
221 return true; | |
222 } | |
223 | |
224 Experiment temp_experiment; | |
225 if (!storage_->LoadStateUnsafe(&temp_experiment)) | |
226 return false; | |
227 | |
228 // Verify that the state matches the metrics. Ignore the state if this is not | |
229 // the case, as the metrics are the source of truth. | |
230 if (temp_experiment.state_ != metrics.state) | |
231 return false; | |
232 | |
233 *experiment = temp_experiment; | |
234 return true; | |
235 } | |
236 | |
237 bool ExperimentStorage::Lock::StoreExperiment(const Experiment& experiment) { | |
238 bool ret = storage_->StoreMetricsUnsafe(experiment.metrics()); | |
239 return storage_->StoreStateUnsafe(experiment) && ret; | |
240 } | |
241 | |
242 bool ExperimentStorage::Lock::LoadMetrics(ExperimentMetrics* metrics) { | |
243 DCHECK_EQ(ExperimentMetrics::kUninitialized, metrics->state); | |
244 return storage_->LoadMetricsUnsafe(metrics); | |
245 } | |
246 | |
247 bool ExperimentStorage::Lock::StoreMetrics(const ExperimentMetrics& metrics) { | |
248 DCHECK_NE(ExperimentMetrics::kUninitialized, metrics.state); | |
249 return storage_->StoreMetricsUnsafe(metrics); | |
250 } | |
251 | |
252 ExperimentStorage::Lock::Lock(ExperimentStorage* storage) : storage_(storage) { | |
253 DCHECK(storage); | |
254 DWORD result = ::WaitForSingleObject(storage_->mutex_.Get(), INFINITE); | |
255 PLOG_IF(FATAL, result == WAIT_FAILED) | |
256 << "Failed to lock ExperimentStorage mutex"; | |
257 } | |
258 | |
259 // ExperimentStorage ----------------------------------------------------------- | |
260 | |
261 ExperimentStorage::ExperimentStorage() | |
262 : mutex_(::CreateMutex(nullptr, FALSE, GetMutexName().c_str())) {} | |
263 | |
264 ExperimentStorage::~ExperimentStorage() {} | |
265 | |
266 std::unique_ptr<ExperimentStorage::Lock> ExperimentStorage::AcquireLock() { | |
267 return base::WrapUnique(new Lock(this)); | |
268 } | |
269 | |
270 // static | |
271 int ExperimentStorage::ReadUint64Bits(uint64_t source, int len, int low_bit) { | |
272 DCHECK(len > 0 && len <= 32 && low_bit + len <= 64); | |
273 uint64_t bit_mask = (uint64_t(1) << len) - 1; | |
274 return static_cast<int>((source >> low_bit) & bit_mask); | |
275 } | |
276 | |
277 // static | |
278 void ExperimentStorage::SetUint64Bits(int value, | |
279 int len, | |
280 int low_bit, | |
281 uint64_t* source) { | |
282 DCHECK(len > 0 && len <= 32); | |
283 uint64_t bit_mask = (uint64_t(1) << len) - 1; | |
284 *source |= ((uint64_t(value) & bit_mask) << low_bit); | |
285 } | |
286 | |
287 bool ExperimentStorage::DecodeMetrics(base::StringPiece16 encoded_metrics, | |
288 ExperimentMetrics* metrics) { | |
289 std::string metrics_data; | |
290 | |
291 if (!base::Base64Decode(base::UTF16ToASCII(encoded_metrics), &metrics_data)) | |
292 return false; | |
293 | |
294 if (metrics_data.size() != 6) | |
295 return false; | |
296 | |
297 uint64_t metrics_value = 0; | |
298 for (size_t i = 0; i < metrics_data.size(); ++i) { | |
299 SetUint64Bits(metrics_data[i], 8, 8 * i, &metrics_value); | |
300 } | |
301 | |
302 ExperimentMetrics result; | |
303 result.session_length_bucket = | |
304 ReadUint64Bits(metrics_value, ExperimentMetrics::kSessionLengthBucketBits, | |
305 kSessionLengthBucketLowestBit); | |
306 result.action_delay_bucket = | |
307 ReadUint64Bits(metrics_value, ExperimentMetrics::kActionDelayBucketBits, | |
308 kActionDelayBucketLowestBit); | |
309 result.last_used_bucket = | |
310 ReadUint64Bits(metrics_value, ExperimentMetrics::kLastUsedBucketBits, | |
311 kLastUsedBucketLowestBit); | |
312 result.toast_hour = ReadUint64Bits( | |
313 metrics_value, ExperimentMetrics::kToastHourBits, kToastHourLowestBit); | |
314 result.first_toast_offset = | |
315 ReadUint64Bits(metrics_value, ExperimentMetrics::kFirstToastOffsetBits, | |
316 kFirstToastOffsetLowestBit); | |
317 result.toast_count = ReadUint64Bits( | |
318 metrics_value, ExperimentMetrics::kToastCountBits, kToastCountLowestBit); | |
319 result.toast_location = static_cast<ExperimentMetrics::ToastLocation>( | |
320 ReadUint64Bits(metrics_value, ExperimentMetrics::kToastLocationBits, | |
321 kToastLocationLowestBit)); | |
322 | |
323 static_assert(ExperimentMetrics::State::NUM_STATES <= (1 << 4), | |
324 "Too many states for ExperimentMetrics encoding."); | |
325 result.state = static_cast<ExperimentMetrics::State>(ReadUint64Bits( | |
326 metrics_value, ExperimentMetrics::kStateBits, kStateLowestBit)); | |
327 result.group = ReadUint64Bits(metrics_value, ExperimentMetrics::kGroupBits, | |
328 kGroupLowestBit); | |
329 | |
330 int lowest_unused_bit = ExperimentMetrics::kGroupBits + kGroupLowestBit; | |
331 if (ReadUint64Bits(metrics_value, 64 - lowest_unused_bit, | |
332 lowest_unused_bit) != 0) | |
333 return false; | |
334 | |
335 *metrics = result; | |
336 return true; | |
337 } | |
338 | |
339 // static | |
340 base::string16 ExperimentStorage::EncodeMetrics( | |
341 const ExperimentMetrics& metrics) { | |
342 uint64_t metrics_value = 0; | |
343 SetUint64Bits(metrics.session_length_bucket, | |
344 ExperimentMetrics::kSessionLengthBucketBits, | |
345 kSessionLengthBucketLowestBit, &metrics_value); | |
346 SetUint64Bits(metrics.action_delay_bucket, | |
347 ExperimentMetrics::kActionDelayBucketBits, | |
348 kActionDelayBucketLowestBit, &metrics_value); | |
349 SetUint64Bits(metrics.last_used_bucket, | |
350 ExperimentMetrics::kLastUsedBucketBits, | |
351 kLastUsedBucketLowestBit, &metrics_value); | |
352 SetUint64Bits(metrics.toast_hour, ExperimentMetrics::kToastHourBits, | |
353 kToastHourLowestBit, &metrics_value); | |
354 SetUint64Bits(metrics.first_toast_offset, | |
355 ExperimentMetrics::kFirstToastOffsetBits, | |
356 kFirstToastOffsetLowestBit, &metrics_value); | |
357 SetUint64Bits(metrics.toast_count, ExperimentMetrics::kToastCountBits, | |
358 kToastCountLowestBit, &metrics_value); | |
359 SetUint64Bits(metrics.toast_location, ExperimentMetrics::kToastLocationBits, | |
360 kToastLocationLowestBit, &metrics_value); | |
361 static_assert(ExperimentMetrics::State::NUM_STATES <= (1 << 4), | |
362 "Too many states for ExperimentMetrics encoding."); | |
363 SetUint64Bits(metrics.state, ExperimentMetrics::kStateBits, kStateLowestBit, | |
364 &metrics_value); | |
365 SetUint64Bits(metrics.group, ExperimentMetrics::kGroupBits, kGroupLowestBit, | |
366 &metrics_value); | |
367 | |
368 std::string metrics_data(6, '\0'); | |
369 for (size_t i = 0; i < metrics_data.size(); ++i) { | |
370 metrics_data[i] = | |
371 static_cast<char>(ReadUint64Bits(metrics_value, 8, 8 * i)); | |
372 } | |
373 std::string encoded_metrics; | |
374 base::Base64Encode(metrics_data, &encoded_metrics); | |
375 return base::ASCIIToUTF16(encoded_metrics); | |
376 } | |
377 | |
378 bool ExperimentStorage::LoadMetricsUnsafe(ExperimentMetrics* metrics) { | |
379 base::string16 value; | |
380 | |
381 if (!GoogleUpdateSettings::ReadExperimentLabels( | |
382 install_static::IsSystemInstall(), &value)) { | |
383 return false; | |
384 } | |
385 | |
386 ExperimentLabels experiment_labels(value); | |
387 base::StringPiece16 encoded_metrics = | |
388 experiment_labels.GetValueForLabel(kExperimentLabelName); | |
389 if (encoded_metrics.empty()) { | |
390 *metrics = ExperimentMetrics(); | |
391 return true; | |
392 } | |
393 | |
394 return DecodeMetrics(encoded_metrics, metrics); | |
395 } | |
396 | |
397 bool ExperimentStorage::StoreMetricsUnsafe(const ExperimentMetrics& metrics) { | |
398 base::string16 value; | |
399 if (!GoogleUpdateSettings::ReadExperimentLabels( | |
400 install_static::IsSystemInstall(), &value)) { | |
401 return false; | |
402 } | |
403 ExperimentLabels experiment_labels(value); | |
404 | |
405 experiment_labels.SetValueForLabel(kExperimentLabelName, | |
406 EncodeMetrics(metrics), | |
407 base::TimeDelta::FromDays(182)); | |
408 | |
409 return GoogleUpdateSettings::SetExperimentLabels( | |
410 install_static::IsSystemInstall(), experiment_labels.value()); | |
411 } | |
412 | |
413 bool ExperimentStorage::LoadStateUnsafe(Experiment* experiment) { | |
414 const bool system_level = install_static::IsSystemInstall(); | |
415 | |
416 base::string16 path; | |
417 if (!GetExperimentStateKeyPath(system_level, &path)) | |
418 return false; | |
419 | |
420 const HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; | |
421 base::win::RegKey key; | |
422 if (key.Open(root, path.c_str(), KEY_QUERY_VALUE | KEY_WOW64_32KEY) != | |
423 ERROR_SUCCESS) { | |
424 return false; | |
425 } | |
426 | |
427 return ReadBoundedDWORD(&key, kRegValueState, 0, | |
428 ExperimentMetrics::NUM_STATES, &experiment->state_) && | |
429 ReadBoundedDWORD(&key, kRegValueGroup, 0, | |
430 ExperimentMetrics::kNumGroups - 1, | |
431 &experiment->group_) && | |
432 ReadBoundedDWORD(&key, kRegValueToastLocation, 0, 1, | |
433 &experiment->toast_location_) && | |
434 ReadBoundedDWORD(&key, kRegValueInactiveDays, 0, INT_MAX, | |
435 &experiment->inactive_days_) && | |
436 ReadBoundedDWORD(&key, kRegValueToastCount, 0, | |
437 ExperimentMetrics::kMaxToastCount, | |
438 &experiment->toast_count_) && | |
439 ReadTime(&key, kRegValueFirstDisplayTime, | |
440 &experiment->first_display_time_) && | |
441 ReadTime(&key, kRegValueLatestDisplayTime, | |
442 &experiment->latest_display_time_) && | |
443 ReadTime(&key, kRegValueUserSessionUptime, | |
444 &experiment->user_session_uptime_) && | |
445 ReadTime(&key, kRegValueActionDelay, &experiment->action_delay_); | |
446 } | |
447 | |
448 bool ExperimentStorage::StoreStateUnsafe(const Experiment& experiment) { | |
449 const bool system_level = install_static::IsSystemInstall(); | |
450 | |
451 base::string16 path; | |
452 if (!GetExperimentStateKeyPath(system_level, &path)) | |
453 return false; | |
454 | |
455 const HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; | |
456 base::win::RegKey key; | |
457 if (key.Create(root, path.c_str(), KEY_SET_VALUE | KEY_WOW64_32KEY) != | |
458 ERROR_SUCCESS) { | |
459 return false; | |
460 } | |
461 | |
462 key.WriteValue(kRegValueState, experiment.state_); | |
463 key.WriteValue(kRegValueGroup, experiment.group_); | |
464 key.WriteValue(kRegValueToastLocation, experiment.toast_location_); | |
465 key.WriteValue(kRegValueInactiveDays, experiment.inactive_days_); | |
466 key.WriteValue(kRegValueToastCount, experiment.toast_count_); | |
467 WriteTime(&key, kRegValueFirstDisplayTime, | |
468 experiment.first_display_time_.ToInternalValue()); | |
469 WriteTime(&key, kRegValueLatestDisplayTime, | |
470 experiment.latest_display_time_.ToInternalValue()); | |
471 WriteTime(&key, kRegValueUserSessionUptime, | |
472 experiment.user_session_uptime_.ToInternalValue()); | |
473 WriteTime(&key, kRegValueActionDelay, | |
474 experiment.action_delay_.ToInternalValue()); | |
475 return true; | |
476 } | |
477 | |
478 } // namespace installer | |
OLD | NEW |