Chromium Code Reviews| 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/installer/gcapi/gcapi_omaha_experiment.h" | 5 #include "chrome/installer/gcapi/gcapi_omaha_experiment.h" |
| 6 | 6 |
| 7 #include "base/strings/string16.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/lazy_instance.h" | |
| 8 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 9 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 10 #include "chrome/installer/gcapi/gcapi.h" | 11 #include "chrome/installer/gcapi/gcapi.h" |
| 12 #include "chrome/installer/util/google_update_constants.h" | |
| 11 #include "chrome/installer/util/google_update_experiment_util.h" | 13 #include "chrome/installer/util/google_update_experiment_util.h" |
| 12 #include "chrome/installer/util/google_update_settings.h" | 14 #include "chrome/installer/util/google_update_settings.h" |
| 13 | 15 |
| 14 using base::Time; | 16 using base::Time; |
| 15 using base::TimeDelta; | 17 using base::TimeDelta; |
| 16 | 18 |
| 17 namespace { | 19 namespace { |
| 18 | 20 |
| 19 // Returns the number of weeks since 2/3/2003. | 21 // Returns the number of weeks since 2/3/2003. |
| 20 int GetCurrentRlzWeek() { | 22 int GetCurrentRlzWeek() { |
| 21 Time::Exploded february_third_2003_exploded = {2003, 2, 1, 3, 0, 0, 0, 0}; | 23 Time::Exploded february_third_2003_exploded = {2003, 2, 1, 3, 0, 0, 0, 0}; |
| 22 Time f = Time::FromUTCExploded(february_third_2003_exploded); | 24 Time f = Time::FromUTCExploded(february_third_2003_exploded); |
| 23 TimeDelta delta = Time::Now() - f; | 25 TimeDelta delta = Time::Now() - f; |
| 24 return delta.InDays() / 7; | 26 return delta.InDays() / 7; |
| 25 } | 27 } |
| 26 | 28 |
| 27 bool SetLabel(const wchar_t* brand_code, const wchar_t* label, int shell_mode) { | 29 bool SetExperimentLabel(const wchar_t* brand_code, |
| 30 const string16& label, | |
| 31 int shell_mode) { | |
| 28 if (!brand_code) { | 32 if (!brand_code) { |
| 29 return false; | 33 return false; |
| 30 } | 34 } |
| 31 | 35 |
| 32 int week_number = GetCurrentRlzWeek(); | 36 const bool system_level = shell_mode == GCAPI_INVOKED_UAC_ELEVATION; |
| 33 if (week_number < 0 || week_number > 999) | |
| 34 week_number = 999; | |
| 35 | 37 |
| 36 string16 experiment_labels; | 38 string16 experiment_labels; |
| 37 base::SStringPrintf(&experiment_labels, | 39 if (!GoogleUpdateSettings::ReadExperimentLabels(system_level, |
| 38 L"%ls=%ls_%d|%ls", | 40 &experiment_labels)) { |
| 39 label, | 41 return false; |
| 40 brand_code, | 42 } |
| 41 week_number, | |
| 42 installer::BuildExperimentDateString().c_str()); | |
| 43 | 43 |
| 44 return GoogleUpdateSettings::SetExperimentLabels( | 44 // First erase this label from the existing experiment labels if it exists. |
|
Alexei Svitkine (slow)
2013/09/03 15:46:35
I think this logic is now sufficiently complicated
gab
2013/11/05 22:20:38
Done.
| |
| 45 shell_mode == GCAPI_INVOKED_UAC_ELEVATION, | 45 size_t existing_label_begin = experiment_labels.find(label + L"="); |
| 46 experiment_labels); | 46 while (existing_label_begin != string16::npos && |
| 47 existing_label_begin != 0 && | |
| 48 experiment_labels.at( | |
| 49 existing_label_begin - 1) != *google_update::kExperimentLabelSep) { | |
| 50 // Make sure |existing_label_begin|, if found, is either at the beginning of | |
| 51 // |experiment_labels| or right after the separator. | |
| 52 existing_label_begin = experiment_labels.find(label + L"=", | |
| 53 existing_label_begin + 1); | |
| 54 } | |
| 55 if (existing_label_begin != string16::npos) { | |
| 56 size_t existing_label_end = | |
| 57 experiment_labels.find(google_update::kExperimentLabelSep, | |
| 58 existing_label_begin); | |
| 59 // Note: if |existing_label_end == string16::npos| this will work anyways | |
| 60 // as npos is defined as the biggest possible unsigned integer and erase | |
| 61 // will thus, by definition, erase to the end of the string. | |
| 62 experiment_labels.erase(existing_label_begin, | |
| 63 existing_label_end - existing_label_begin + 1); | |
| 64 } | |
| 65 | |
| 66 // Then append the GCAPI experiment label to the |experiment_labels|. | |
| 67 if (!experiment_labels.empty()) | |
| 68 experiment_labels.append(google_update::kExperimentLabelSep); | |
| 69 experiment_labels.append( | |
| 70 gcapi_internals::GetGCAPIExperimentLabel(brand_code, label)); | |
| 71 | |
| 72 return GoogleUpdateSettings::SetExperimentLabels(system_level, | |
| 73 experiment_labels); | |
| 47 } | 74 } |
| 48 | 75 |
| 49 } // namespace | 76 } // namespace |
| 50 | 77 |
| 78 namespace gcapi_internals { | |
| 79 | |
| 80 const wchar_t kReactivationLabel[] = L"reacbrand"; | |
| 81 const wchar_t kRelaunchLabel[] = L"relaunchbrand"; | |
| 82 | |
| 83 // Keeps a fixed state for time dependent constants for the life of this | |
| 84 // GCAPI instance; this makes tests realiable when crossing time boundaries on | |
|
Alexei Svitkine (slow)
2013/09/03 15:46:35
Nit: reliable
gab
2013/11/05 22:20:38
"real" "liable" is nice, no?! :)
| |
| 85 // the system clock. | |
| 86 struct GCAPILabelState { | |
| 87 GCAPILabelState() | |
| 88 : week_number(GetCurrentRlzWeek()), | |
| 89 experiment_date_string(installer::BuildExperimentDateString()) { | |
| 90 if (week_number < 0 || week_number > 999) | |
| 91 week_number = 999; | |
| 92 } | |
| 93 | |
| 94 int week_number; | |
| 95 string16 experiment_date_string; | |
| 96 | |
| 97 private: | |
| 98 DISALLOW_COPY_AND_ASSIGN(GCAPILabelState); | |
| 99 }; | |
| 100 | |
| 101 base::LazyInstance<GCAPILabelState> g_gcapi_label_state = | |
| 102 LAZY_INSTANCE_INITIALIZER; | |
| 103 | |
| 104 string16 GetGCAPIExperimentLabel(const wchar_t* brand_code, | |
| 105 const string16& label) { | |
| 106 const GCAPILabelState& gcapi_label_state = g_gcapi_label_state.Get(); | |
|
Alexei Svitkine (slow)
2013/09/03 15:46:35
Instead of having this GCAPILabelState state in la
gab
2013/11/05 22:20:38
Done.
| |
| 107 | |
| 108 string16 gcapi_experiment_label; | |
| 109 base::SStringPrintf(&gcapi_experiment_label, | |
| 110 L"%ls=%ls_%d|%ls", | |
| 111 label.c_str(), | |
| 112 brand_code, | |
| 113 gcapi_label_state.week_number, | |
| 114 gcapi_label_state.experiment_date_string.c_str()); | |
| 115 return gcapi_experiment_label; | |
| 116 } | |
| 117 | |
| 118 } // namespace gcapi_internals | |
| 119 | |
| 51 bool SetReactivationExperimentLabels(const wchar_t* brand_code, | 120 bool SetReactivationExperimentLabels(const wchar_t* brand_code, |
| 52 int shell_mode) { | 121 int shell_mode) { |
| 53 return SetLabel(brand_code, L"reacbrand", shell_mode); | 122 return SetExperimentLabel(brand_code, gcapi_internals::kReactivationLabel, |
| 123 shell_mode); | |
| 54 } | 124 } |
| 55 | 125 |
| 56 bool SetRelaunchExperimentLabels(const wchar_t* brand_code, int shell_mode) { | 126 bool SetRelaunchExperimentLabels(const wchar_t* brand_code, int shell_mode) { |
| 57 return SetLabel(brand_code, L"relaunchbrand", shell_mode); | 127 return SetExperimentLabel(brand_code, gcapi_internals::kRelaunchLabel, |
| 128 shell_mode); | |
| 58 } | 129 } |
| OLD | NEW |