summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/websocket_experiment/websocket_experiment_runner.cc
blob: 675f494117db96916f010addd55e1ea9b8c0cada (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (c) 2010 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/browser/net/websocket_experiment/websocket_experiment_runner.h"

#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/message_loop.h"
#include "base/metrics/field_trial.h"
#include "base/task.h"
#include "base/string_util.h"
#include "chrome/common/chrome_switches.h"
#include "content/browser/browser_thread.h"
#include "net/base/host_resolver.h"
#include "net/base/net_errors.h"
#include "net/websockets/websocket.h"

namespace chrome_browser_net_websocket_experiment {

static const char *kExperimentHost = "websocket-experiment.chromium.org";
static const int kAlternativePort = 61985;

// Hold reference while experiment is running.
static scoped_refptr<WebSocketExperimentRunner> runner;

/* static */
void WebSocketExperimentRunner::Start() {
  DCHECK(!runner.get());

  // After June 30, 2011 builds, it will always be in default group.
  scoped_refptr<base::FieldTrial> trial(
      new base::FieldTrial(
          "WebSocketExperiment", 1000, "default", 2011, 6, 30));
  int active = trial->AppendGroup("active", 5);  // 0.5% in active group.

  bool run_experiment = (trial->group() == active);
#ifndef NDEBUG
  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
  std::string experiment_host = command_line.GetSwitchValueASCII(
      switches::kWebSocketLiveExperimentHost);
  if (!experiment_host.empty())
    run_experiment = true;
#else
  run_experiment = false;
#endif
  if (!run_experiment)
    return;

  runner = new WebSocketExperimentRunner;
  runner->Run();
}

/* static */
void WebSocketExperimentRunner::Stop() {
  if (runner.get())
    runner->Cancel();
  runner = NULL;
}

WebSocketExperimentRunner::WebSocketExperimentRunner()
    : next_state_(STATE_NONE),
      task_state_(STATE_NONE),
      ALLOW_THIS_IN_INITIALIZER_LIST(
          task_callback_(this, &WebSocketExperimentRunner::OnTaskCompleted)) {
  WebSocketExperimentTask::InitHistogram();
  InitConfig();
}

WebSocketExperimentRunner::~WebSocketExperimentRunner() {
  DCHECK(!task_.get());
  WebSocketExperimentTask::ReleaseHistogram();
}

void WebSocketExperimentRunner::Run() {
  DCHECK_EQ(next_state_, STATE_NONE);
  next_state_ = STATE_RUN_WS;
  BrowserThread::PostDelayedTask(
      BrowserThread::IO,
      FROM_HERE,
      NewRunnableMethod(this, &WebSocketExperimentRunner::DoLoop),
      config_.initial_delay_ms);
}

void WebSocketExperimentRunner::Cancel() {
  next_state_ = STATE_NONE;
  BrowserThread::PostTask(
      BrowserThread::IO,
      FROM_HERE,
      NewRunnableMethod(this, &WebSocketExperimentRunner::DoLoop));
}

void WebSocketExperimentRunner::InitConfig() {
  config_.initial_delay_ms = 5 * 60 * 1000;  // 5 mins
  config_.next_delay_ms = 12 * 60 * 60 * 1000;  // 12 hours

  std::string experiment_host = kExperimentHost;
#ifndef NDEBUG
  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
  std::string experiment_host_override = command_line.GetSwitchValueASCII(
      switches::kWebSocketLiveExperimentHost);
  if (!experiment_host_override.empty()) {
    experiment_host = experiment_host_override;
    config_.initial_delay_ms = 5 * 1000;  // 5 secs.
  }
#endif

  WebSocketExperimentTask::Config* config;
  WebSocketExperimentTask::Config task_config;

  task_config.protocol_version = net::WebSocket::DEFAULT_VERSION;
  config = &config_.ws_config[STATE_RUN_WS - STATE_RUN_WS];
  *config = task_config;
  config->url =
      GURL(StringPrintf("ws://%s/live_exp", experiment_host.c_str()));
  config->ws_location =
      StringPrintf("ws://%s/live_exp", experiment_host.c_str());
  config->http_url =
      GURL(StringPrintf("http://%s/", experiment_host.c_str()));

  config = &config_.ws_config[STATE_RUN_WSS - STATE_RUN_WS];
  *config = task_config;
  config->url =
      GURL(StringPrintf("wss://%s/live_exp", experiment_host.c_str()));
  config->ws_location =
      StringPrintf("wss://%s/live_exp", experiment_host.c_str());
  config->http_url =
      GURL(StringPrintf("https://%s/", experiment_host.c_str()));

  config = &config_.ws_config[STATE_RUN_WS_NODEFAULT_PORT -
                              STATE_RUN_WS];
  *config = task_config;
  config->url =
      GURL(StringPrintf("ws://%s:%d/live_exp",
                        experiment_host.c_str(), kAlternativePort));
  config->ws_location =
      StringPrintf("ws://%s:%d/live_exp",
                   experiment_host.c_str(), kAlternativePort);
  config->http_url =
      GURL(StringPrintf("http://%s:%d/",
                        experiment_host.c_str(), kAlternativePort));

  task_config.protocol_version = net::WebSocket::DRAFT75;
  config = &config_.ws_config[STATE_RUN_WS_DRAFT75 - STATE_RUN_WS];
  *config = task_config;
  config->url =
      GURL(StringPrintf("ws://%s/live_exp", experiment_host.c_str()));
  config->ws_location =
      StringPrintf("ws://%s/live_exp", experiment_host.c_str());
  config->http_url =
      GURL(StringPrintf("http://%s/", experiment_host.c_str()));

  config = &config_.ws_config[STATE_RUN_WSS_DRAFT75 - STATE_RUN_WS];
  *config = task_config;
  config->url =
      GURL(StringPrintf("wss://%s/live_exp", experiment_host.c_str()));
  config->ws_location =
      StringPrintf("wss://%s/live_exp", experiment_host.c_str());
  config->http_url =
      GURL(StringPrintf("https://%s/", experiment_host.c_str()));

  config = &config_.ws_config[STATE_RUN_WS_NODEFAULT_PORT_DRAFT75 -
                              STATE_RUN_WS];
  *config = task_config;
  config->url =
      GURL(StringPrintf("ws://%s:%d/live_exp",
                        experiment_host.c_str(), kAlternativePort));
  config->ws_location =
      StringPrintf("ws://%s:%d/live_exp",
                   experiment_host.c_str(), kAlternativePort);
  config->http_url =
      GURL(StringPrintf("http://%s:%d/",
                        experiment_host.c_str(), kAlternativePort));

}

void WebSocketExperimentRunner::DoLoop() {
  if (next_state_ == STATE_NONE) {
    if (task_.get()) {
      AddRef();  // Release in OnTaskCompleted after Cancelled.
      task_->Cancel();
    }
    return;
  }

  State state = next_state_;
  task_state_ = STATE_NONE;
  next_state_ = STATE_NONE;

  switch (state) {
    case STATE_IDLE:
      task_.reset();
      next_state_ = STATE_RUN_WS;
      BrowserThread::PostDelayedTask(
          BrowserThread::IO,
          FROM_HERE,
          NewRunnableMethod(this, &WebSocketExperimentRunner::DoLoop),
          config_.next_delay_ms);
      break;
    case STATE_RUN_WS:
    case STATE_RUN_WSS:
    case STATE_RUN_WS_NODEFAULT_PORT:
    case STATE_RUN_WS_DRAFT75:
    case STATE_RUN_WSS_DRAFT75:
    case STATE_RUN_WS_NODEFAULT_PORT_DRAFT75:
      task_.reset(new WebSocketExperimentTask(
          config_.ws_config[state - STATE_RUN_WS], &task_callback_));
      task_state_ = state;
      if (static_cast<State>(state + 1) == NUM_STATES)
        next_state_ = STATE_IDLE;
      else
        next_state_ = static_cast<State>(state + 1);
      break;
    default:
      NOTREACHED();
      break;
  }
  if (task_.get())
    task_->Run();
}

void WebSocketExperimentRunner::OnTaskCompleted(int result) {
  if (next_state_ == STATE_NONE) {
    task_.reset();
    // Task is Canceled.
    DVLOG(1) << "WebSocketExperiment Task is canceled.";
    Release();
    return;
  }
  task_->SaveResult();
  task_.reset();

  DoLoop();
}

}  // namespace chrome_browser_net_websocket_experiment