summaryrefslogtreecommitdiffstats
path: root/chrome/installer/gcapi/gcapi_omaha_experiment.cc
blob: 539bd6f13cf697f49f3f6574e92c352705cfa718 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/installer/gcapi/gcapi_omaha_experiment.h"

#include "base/string16.h"
#include "base/stringprintf.h"
#include "base/string_number_conversions.h"
#include "base/time.h"
#include "base/win/registry.h"
#include "chrome/installer/util/browser_distribution.h"
#include "chrome/installer/util/google_update_constants.h"

using base::Time;
using base::TimeDelta;
using base::win::RegKey;

namespace {

const wchar_t kExperimentLabels[] = L"experiment_labels";

const wchar_t* kExperimentAppGuids[] = {
    L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}",
    L"{8A69D345-D564-463C-AFF1-A69D9E530F96}",
};

const wchar_t* kDays[] =
    { L"Sun", L"Mon", L"Tue", L"Wed", L"Thu", L"Fri", L"Sat" };

const wchar_t* kMonths[] = {L"Jan", L"Feb", L"Mar", L"Apr", L"May", L"Jun",
                            L"Jul", L"Aug", L"Sep", L"Oct", L"Nov", L"Dec"};

// Constructs a date string of the following format for the current time plus
// one year:
// "DAY, DD0 MON YYYY HH0:MI0:SE0 TZ"
//  DAY = 3 character day of week,
//  DD0 = 2 digit day of month,
//  MON = 3 character month of year,
//  YYYY = 4 digit year,
//  HH0 = 2 digit hour,
//  MI0 = 2 digit minute,
//  SE0 = 2 digit second,
//  TZ = 3 character timezone)
string16 BuildOmahaExperimentDateString() {
  Time then_time = Time::Now() + TimeDelta::FromDays(365);
  Time::Exploded then = {};
  then_time.UTCExplode(&then);

  if (!then.HasValidValues())
    return L"";

  string16 date_string;
  base::SStringPrintf(&date_string,
                      L"%ls, %02d %ls %d %02d:%02d:%02d GMT",
                      kDays[then.day_of_week],
                      then.day_of_month,
                      kMonths[then.month - 1],
                      then.year,
                      then.hour,
                      then.minute,
                      then.second);
  return date_string;
}

// Returns the number of weeks since 2/3/2003.
int GetCurrentRlzWeek() {
  Time::Exploded february_third_2003_exploded = {2003, 2, 1, 3, 0, 0, 0, 0};
  Time f = Time::FromUTCExploded(february_third_2003_exploded);
  TimeDelta delta = Time::Now() - f;
  return delta.InDays() / 7;
}

}  // namespace

bool SetOmahaExperimentLabel(const wchar_t* brand_code) {
  if (!brand_code) {
    return false;
  }

  int week_number = GetCurrentRlzWeek();
  if (week_number < 0 || week_number > 999)
    week_number = 999;

  string16 experiment_label;
  base::SStringPrintf(&experiment_label,
                      L"%ls_%d|%ls",
                      brand_code,
                      week_number,
                      BuildOmahaExperimentDateString().c_str());

  int successful_writes = 0;
  for (int i = 0; i < arraysize(kExperimentAppGuids); ++i) {
    string16 experiment_path(google_update::kRegPathClientState);
    experiment_path += L"\\";
    experiment_path += kExperimentAppGuids[i];

    RegKey client_state(HKEY_LOCAL_MACHINE, experiment_path.c_str(),
                        KEY_SET_VALUE);
    if (client_state.Valid()) {
      if (client_state.WriteValue(kExperimentLabels,
                                  experiment_label.c_str()) == ERROR_SUCCESS) {
        successful_writes++;
      }
    }
  }

  return (successful_writes == arraysize(kExperimentAppGuids));
}