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/strings/string_split.h" |
| 9 #include "base/strings/string_util.h" |
8 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
9 #include "base/time/time.h" | 11 #include "base/time/time.h" |
10 #include "chrome/installer/gcapi/gcapi.h" | 12 #include "chrome/installer/gcapi/gcapi.h" |
| 13 #include "chrome/installer/util/google_update_constants.h" |
11 #include "chrome/installer/util/google_update_experiment_util.h" | 14 #include "chrome/installer/util/google_update_experiment_util.h" |
12 #include "chrome/installer/util/google_update_settings.h" | 15 #include "chrome/installer/util/google_update_settings.h" |
13 | 16 |
14 using base::Time; | |
15 using base::TimeDelta; | |
16 | |
17 namespace { | 17 namespace { |
18 | 18 |
19 // Returns the number of weeks since 2/3/2003. | 19 // Returns the number of weeks since 2/3/2003. |
20 int GetCurrentRlzWeek() { | 20 int GetCurrentRlzWeek(const base::Time& current_time) { |
21 Time::Exploded february_third_2003_exploded = {2003, 2, 1, 3, 0, 0, 0, 0}; | 21 base::Time::Exploded february_third_2003_exploded = |
22 Time f = Time::FromUTCExploded(february_third_2003_exploded); | 22 {2003, 2, 1, 3, 0, 0, 0, 0}; |
23 TimeDelta delta = Time::Now() - f; | 23 base::Time f = base::Time::FromUTCExploded(february_third_2003_exploded); |
| 24 base::TimeDelta delta = current_time - f; |
24 return delta.InDays() / 7; | 25 return delta.InDays() / 7; |
25 } | 26 } |
26 | 27 |
27 bool SetLabel(const wchar_t* brand_code, const wchar_t* label, int shell_mode) { | 28 bool SetExperimentLabel(const wchar_t* brand_code, |
| 29 const string16& label, |
| 30 int shell_mode) { |
28 if (!brand_code) { | 31 if (!brand_code) { |
29 return false; | 32 return false; |
30 } | 33 } |
31 | 34 |
32 int week_number = GetCurrentRlzWeek(); | 35 const bool system_level = shell_mode == GCAPI_INVOKED_UAC_ELEVATION; |
33 if (week_number < 0 || week_number > 999) | |
34 week_number = 999; | |
35 | 36 |
36 string16 experiment_labels; | 37 string16 original_labels; |
37 base::SStringPrintf(&experiment_labels, | 38 if (!GoogleUpdateSettings::ReadExperimentLabels(system_level, |
38 L"%ls=%ls_%d|%ls", | 39 &original_labels)) { |
39 label, | 40 return false; |
40 brand_code, | 41 } |
41 week_number, | |
42 installer::BuildExperimentDateString().c_str()); | |
43 | 42 |
44 return GoogleUpdateSettings::SetExperimentLabels( | 43 // Split the original labels by the label separator. |
45 shell_mode == GCAPI_INVOKED_UAC_ELEVATION, | 44 std::vector<string16> entries; |
46 experiment_labels); | 45 base::SplitStringUsingSubstr( |
| 46 original_labels, google_update::kExperimentLabelSep, &entries); |
| 47 |
| 48 // Keep all labels, but the one we want to add/replace. |
| 49 string16 new_labels; |
| 50 for (std::vector<string16>::const_iterator it = entries.begin(); |
| 51 it != entries.end(); ++it) { |
| 52 if (!it->empty() && !StartsWith(*it, label + L"=", true)) { |
| 53 new_labels += *it; |
| 54 new_labels += google_update::kExperimentLabelSep; |
| 55 } |
| 56 } |
| 57 |
| 58 new_labels.append( |
| 59 gcapi_internals::GetGCAPIExperimentLabel(brand_code, label)); |
| 60 |
| 61 return GoogleUpdateSettings::SetExperimentLabels(system_level, |
| 62 new_labels); |
47 } | 63 } |
48 | 64 |
49 } // namespace | 65 } // namespace |
50 | 66 |
| 67 namespace gcapi_internals { |
| 68 |
| 69 const wchar_t kReactivationLabel[] = L"reacbrand"; |
| 70 const wchar_t kRelaunchLabel[] = L"relaunchbrand"; |
| 71 |
| 72 string16 GetGCAPIExperimentLabel(const wchar_t* brand_code, |
| 73 const string16& label) { |
| 74 // Keeps a fixed time state for this GCAPI instance; this makes tests reliable |
| 75 // when crossing time boundaries on the system clock and doesn't otherwise |
| 76 // affect results of this short lived binary. |
| 77 static time_t instance_time_value = 0; |
| 78 if (instance_time_value == 0) |
| 79 instance_time_value = base::Time::Now().ToTimeT(); |
| 80 |
| 81 base::Time instance_time = base::Time::FromTimeT(instance_time_value); |
| 82 |
| 83 string16 gcapi_experiment_label; |
| 84 base::SStringPrintf(&gcapi_experiment_label, |
| 85 L"%ls=%ls_%d|%ls", |
| 86 label.c_str(), |
| 87 brand_code, |
| 88 GetCurrentRlzWeek(instance_time), |
| 89 installer::BuildExperimentDateString( |
| 90 instance_time).c_str()); |
| 91 return gcapi_experiment_label; |
| 92 } |
| 93 |
| 94 } // namespace gcapi_internals |
| 95 |
51 bool SetReactivationExperimentLabels(const wchar_t* brand_code, | 96 bool SetReactivationExperimentLabels(const wchar_t* brand_code, |
52 int shell_mode) { | 97 int shell_mode) { |
53 return SetLabel(brand_code, L"reacbrand", shell_mode); | 98 return SetExperimentLabel(brand_code, gcapi_internals::kReactivationLabel, |
| 99 shell_mode); |
54 } | 100 } |
55 | 101 |
56 bool SetRelaunchExperimentLabels(const wchar_t* brand_code, int shell_mode) { | 102 bool SetRelaunchExperimentLabels(const wchar_t* brand_code, int shell_mode) { |
57 return SetLabel(brand_code, L"relaunchbrand", shell_mode); | 103 return SetExperimentLabel(brand_code, gcapi_internals::kRelaunchLabel, |
| 104 shell_mode); |
58 } | 105 } |
OLD | NEW |