-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathcommands.cc
418 lines (371 loc) · 15.5 KB
/
commands.cc
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/test/chromedriver/commands.h"
#include <stddef.h>
#include <algorithm>
#include <functional>
#include <list>
#include <utility>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/system/sys_info.h"
#include "base/task/current_thread.h"
#include "base/task/single_thread_task_runner.h"
#include "base/values.h"
#include "chrome/test/chromedriver/capabilities.h"
#include "chrome/test/chromedriver/chrome/browser_info.h"
#include "chrome/test/chromedriver/chrome/chrome.h"
#include "chrome/test/chromedriver/chrome/status.h"
#include "chrome/test/chromedriver/constants/version.h"
#include "chrome/test/chromedriver/logging.h"
#include "chrome/test/chromedriver/session.h"
#include "chrome/test/chromedriver/session_commands.h"
#include "chrome/test/chromedriver/session_thread_map.h"
#include "chrome/test/chromedriver/util.h"
namespace {
void WriteChromeDriverExtendedStatus(base::Value::Dict& info) {
base::Value::Dict build;
build.Set("version", kChromeDriverVersion);
info.Set("build", std::move(build));
base::Value::Dict os;
os.Set("name", base::SysInfo::OperatingSystemName());
os.Set("version", base::SysInfo::OperatingSystemVersion());
os.Set("arch", base::SysInfo::OperatingSystemArchitecture());
info.Set("os", std::move(os));
}
} // namespace
void ExecuteGetStatus(const base::Value::Dict& params,
const std::string& session_id,
const CommandCallback& callback) {
// W3C defined data:
// ChromeDriver doesn't have a preset limit on number of active sessions,
// so we are always ready.
base::Value::Dict info;
info.Set("ready", true);
info.Set("message", base::StringPrintf("%s ready for new sessions.",
kChromeDriverProductShortName));
// ChromeDriver specific data:
WriteChromeDriverExtendedStatus(info);
callback.Run(Status(kOk), std::make_unique<base::Value>(std::move(info)),
std::string(), kW3CDefault);
}
void ExecuteBidiSessionStatus(const base::Value::Dict& params,
const std::string& session_id,
const CommandCallback& callback) {
base::Value::Dict info;
if (session_id.empty()) {
info.Set("ready", true);
info.Set("message", base::StringPrintf("%s ready for new sessions.",
kChromeDriverProductShortName));
} else {
info.Set("ready", false);
// The error message is borrowed from BiDiMapper code.
// See bidiMapper/domains/session/SessionProcessor.ts of chromium-bidi
// repository.
info.Set("message", "already connected");
}
// ChromeDriver specific data:
WriteChromeDriverExtendedStatus(info);
callback.Run(Status(kOk), std::make_unique<base::Value>(std::move(info)),
session_id, kW3CDefault);
}
void ExecuteCreateSession(SessionThreadMap* session_thread_map,
const Command& init_session_cmd,
const base::Value::Dict& params,
const std::string& host,
const CommandCallback& callback) {
std::string new_id = GenerateId();
std::unique_ptr<Session> session = std::make_unique<Session>(new_id, host);
std::unique_ptr<SessionThreadInfo> thread_info =
std::make_unique<SessionThreadInfo>(new_id, GetW3CSetting(params));
if (!thread_info->thread()->Start()) {
callback.Run(
Status(kUnknownError, "failed to start a thread for the new session"),
std::unique_ptr<base::Value>(), std::string(),
session->w3c_compliant);
return;
}
thread_info->thread()->task_runner()->PostTask(
FROM_HERE, base::BindOnce(&SetThreadLocalSession, std::move(session)));
session_thread_map->emplace(new_id, std::move(thread_info));
init_session_cmd.Run(params, new_id, callback);
}
void ExecuteBidiSessionNew(SessionThreadMap* session_thread_map,
const Command& init_session_cmd,
const base::Value::Dict& params,
const std::string& resource,
const CommandCallback& callback) {
if (!resource.empty()) {
callback.Run(Status{kSessionNotCreated, "session already exists"}, nullptr,
resource, kW3CDefault);
return;
}
base::Value::Dict new_params;
const base::Value::Dict* capabilities =
params.FindDictByDottedPath("params.capabilities");
if (capabilities) {
new_params.Set("capabilities", capabilities->Clone());
}
new_params.SetByDottedPath("capabilities.alwaysMatch.webSocketUrl", true);
ExecuteCreateSession(session_thread_map, init_session_cmd, new_params,
resource, callback);
}
namespace {
void OnGetSession(const base::WeakPtr<size_t>& session_remaining_count,
const base::RepeatingClosure& all_get_session_func,
base::Value::List& session_list,
const Status& status,
std::unique_ptr<base::Value> value,
const std::string& session_id,
bool w3c_compliant) {
if (!session_remaining_count)
return;
(*session_remaining_count)--;
if (value) {
base::Value::Dict session;
session.Set("id", session_id);
session.Set("capabilities",
base::Value::FromUniquePtrValue(std::move(value)));
session_list.Append(std::move(session));
}
if (!*session_remaining_count) {
all_get_session_func.Run();
}
}
} // namespace
void ExecuteGetSessions(const Command& session_capabilities_command,
SessionThreadMap* session_thread_map,
const base::Value::Dict& params,
const std::string& session_id,
const CommandCallback& callback) {
size_t get_remaining_count = session_thread_map->size();
base::WeakPtrFactory<size_t> weak_ptr_factory(&get_remaining_count);
base::Value::List session_list;
if (!get_remaining_count) {
callback.Run(Status(kOk),
std::make_unique<base::Value>(std::move(session_list)),
session_id, false);
return;
}
base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
for (auto iter = session_thread_map->begin();
iter != session_thread_map->end(); ++iter) {
session_capabilities_command.Run(
params, iter->first,
base::BindRepeating(&OnGetSession, weak_ptr_factory.GetWeakPtr(),
run_loop.QuitClosure(), std::ref(session_list)));
}
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(), base::Seconds(10));
run_loop.Run();
callback.Run(Status(kOk),
std::make_unique<base::Value>(std::move(session_list)),
session_id, false);
}
namespace {
void OnSessionQuit(const base::WeakPtr<size_t>& quit_remaining_count,
const base::RepeatingClosure& all_quit_func,
const Status& status,
std::unique_ptr<base::Value> value,
const std::string& session_id,
bool w3c_compliant) {
// |quit_remaining_count| may no longer be valid if a timeout occurred.
if (!quit_remaining_count)
return;
(*quit_remaining_count)--;
if (!*quit_remaining_count)
all_quit_func.Run();
}
} // namespace
void ExecuteQuitAll(const Command& quit_command,
SessionThreadMap* session_thread_map,
const base::Value::Dict& params,
const std::string& session_id,
const CommandCallback& callback) {
size_t quit_remaining_count = session_thread_map->size();
base::WeakPtrFactory<size_t> weak_ptr_factory(&quit_remaining_count);
if (!quit_remaining_count) {
callback.Run(Status(kOk), std::unique_ptr<base::Value>(),
session_id, false);
return;
}
base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
for (auto iter = session_thread_map->begin();
iter != session_thread_map->end(); ++iter) {
quit_command.Run(
params, iter->first,
base::BindRepeating(&OnSessionQuit, weak_ptr_factory.GetWeakPtr(),
run_loop.QuitClosure()));
}
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(), base::Seconds(10));
// Uses a nested run loop to block this thread until all the quit
// commands have executed, or the timeout expires.
run_loop.Run();
callback.Run(Status(kOk), std::unique_ptr<base::Value>(),
session_id, false);
}
namespace {
void ExecuteSessionCommandOnSessionThread(
const char* command_name,
const std::string& session_id,
const SessionCommand& command,
bool w3c_standard_command,
bool return_ok_without_session,
const base::Value::Dict& params,
scoped_refptr<base::SingleThreadTaskRunner> cmd_task_runner,
const CommandCallback& callback_on_cmd) {
Session* session = GetThreadLocalSession();
if (!session) {
cmd_task_runner->PostTask(
FROM_HERE,
base::BindOnce(
callback_on_cmd,
Status(return_ok_without_session ? kOk : kInvalidSessionId),
std::unique_ptr<base::Value>(), session_id, kW3CDefault));
return;
}
if (IsVLogOn(0)) {
if (!session->driver_log ||
session->driver_log->min_level() != Log::Level::kOff) {
// Note: ChromeDriver log-replay depends on the format of this logging.
// see chromedriver/log_replay/client_replay.py
VLOG(0) << "[" << session->id << "] "
<< "COMMAND " << command_name << " "
<< PrettyPrintValue(base::Value(params.Clone()));
}
}
// Notify |session|'s |CommandListener|s of the command.
// Will mark |session| for deletion if an error is encountered.
Status status = NotifyCommandListenersBeforeCommand(session, command_name);
std::unique_ptr<base::Value> value;
if (session->w3c_compliant && !w3c_standard_command) {
status = Status(kUnknownCommand,
"Cannot call non W3C standard command while in W3C mode");
if (IsVLogOn(0)) {
std::string result;
result = "ERROR " + status.message();
if (!session->driver_log ||
session->driver_log->min_level() != Log::Level::kOff) {
// Note: ChromeDriver log-replay depends on the format of this
// logging. see chromedriver/log_replay/client_replay.py
VLOG(0) << "[" << session->id << "] "
<< "RESPONSE " << command_name
<< (result.length() ? " " + result : "");
}
}
} else {
// Only run the command if we were able to notify all listeners
// successfully.
// Otherwise, pass error to callback, delete |session|, and do not continue.
if (status.IsError()) {
LOG(ERROR) << status.message();
} else {
status = command.Run(session, params, &value);
if (status.IsError() && session->chrome) {
if (!session->quit && session->chrome->HasCrashedWebView()) {
session->quit = true;
std::string message("session deleted because of page crash");
if (!session->detach) {
Status quit_status = session->chrome->Quit();
if (quit_status.IsError())
message +=
", but failed to kill browser:" + quit_status.message();
}
status = Status(kUnknownError, message, status);
} else if (status.code() == kDisconnected) {
session->quit = true;
std::string message(
"session deleted as the browser has closed the connection");
if (!session->detach) {
// Even though the connection was lost that makes the graceful
// shutdown impossible the Quit procedure falls back on killing the
// process in case if it is still alive.
Status quit_status = session->chrome->Quit();
if (quit_status.IsError()) {
message +=
", but failed to kill browser:" + quit_status.message();
}
}
status = Status(kInvalidSessionId, message, status);
} else if (status.code() == kTargetDetached) {
// Some commands, like clicking a button or link which closes the
// window, may result in a kTargetDetached error code.
std::list<std::string> tab_view_ids;
Status status_tmp = session->chrome->GetTopLevelWebViewIds(
&tab_view_ids, session->w3c_compliant);
if (status_tmp.IsError()) {
status.AddDetails("failed to check if window was closed: " +
status_tmp.message());
} else if (!base::Contains(tab_view_ids, session->window)) {
status = Status(kOk);
}
}
if (status.IsError()) {
const BrowserInfo* browser_info = session->chrome->GetBrowserInfo();
status.AddDetails("Session info: " + browser_info->browser_name +
"=" + browser_info->browser_version);
}
}
if (IsVLogOn(0)) {
std::string result;
if (status.IsError()) {
result = "ERROR " + status.message();
} else if (value) {
result = FormatValueForDisplay(*value);
}
if (!session->driver_log ||
session->driver_log->min_level() != Log::Level::kOff) {
// Note: ChromeDriver log-replay depends on the format of this
// logging. see chromedriver/log_replay/client_replay.py
VLOG(0) << "[" << session->id << "] "
<< "RESPONSE " << command_name
<< (result.length() ? " " + result : "");
}
}
}
}
cmd_task_runner->PostTask(
FROM_HERE, base::BindOnce(callback_on_cmd, status, std::move(value),
session->id, session->w3c_compliant));
if (session->quit) {
Session::Terminate();
}
}
} // namespace
void ExecuteSessionCommand(SessionThreadMap* session_thread_map,
const char* command_name,
const SessionCommand& command,
bool w3c_standard_command,
bool return_ok_without_session,
const base::Value::Dict& params,
const std::string& session_id,
const CommandCallback& callback) {
auto iter = session_thread_map->find(session_id);
if (iter == session_thread_map->end()) {
Status status(return_ok_without_session ? kOk : kInvalidSessionId);
callback.Run(status, std::unique_ptr<base::Value>(), session_id,
kW3CDefault);
return;
}
iter->second->thread()->task_runner()->PostTask(
FROM_HERE,
base::BindOnce(&ExecuteSessionCommandOnSessionThread, command_name,
session_id, command, w3c_standard_command,
return_ok_without_session, params.Clone(),
base::SingleThreadTaskRunner::GetCurrentDefault(),
callback));
}
namespace internal {
void CreateSessionOnSessionThreadForTesting(const std::string& id) {
SetThreadLocalSession(std::make_unique<Session>(id));
}
} // namespace internal