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
|
// 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/test/chromedriver/commands.h"
#include "base/callback.h"
#include "base/file_util.h"
#include "base/stringprintf.h"
#include "base/sys_info.h"
#include "base/values.h"
#include "chrome/test/chromedriver/chrome.h"
#include "chrome/test/chromedriver/chrome_android_impl.h"
#include "chrome/test/chromedriver/chrome_desktop_impl.h"
#include "chrome/test/chromedriver/net/net_util.h"
#include "chrome/test/chromedriver/net/url_request_context_getter.h"
#include "chrome/test/chromedriver/session.h"
#include "chrome/test/chromedriver/session_map.h"
#include "chrome/test/chromedriver/status.h"
#include "chrome/test/chromedriver/util.h"
#include "chrome/test/chromedriver/version.h"
#include "chrome/test/chromedriver/web_view.h"
Status ExecuteGetStatus(
const base::DictionaryValue& params,
const std::string& session_id,
scoped_ptr<base::Value>* out_value,
std::string* out_session_id) {
base::DictionaryValue build;
build.SetString("version", "alpha");
base::DictionaryValue os;
os.SetString("name", base::SysInfo::OperatingSystemName());
os.SetString("version", base::SysInfo::OperatingSystemVersion());
os.SetString("arch", base::SysInfo::OperatingSystemArchitecture());
base::DictionaryValue info;
info.Set("build", build.DeepCopy());
info.Set("os", os.DeepCopy());
out_value->reset(info.DeepCopy());
return Status(kOk);
}
Status ExecuteNewSession(
SessionMap* session_map,
scoped_refptr<URLRequestContextGetter> context_getter,
const SyncWebSocketFactory& socket_factory,
const base::DictionaryValue& params,
const std::string& session_id,
scoped_ptr<base::Value>* out_value,
std::string* out_session_id) {
int port;
if (!FindOpenPort(&port))
return Status(kUnknownError, "failed to find an open port for Chrome");
const base::DictionaryValue* desired_caps;
if (!params.GetDictionary("desiredCapabilities", &desired_caps))
return Status(kUnknownError, "cannot find dict 'desiredCapabilities'");
scoped_ptr<Chrome> chrome;
Status status(kOk);
std::string android_package;
if (desired_caps->GetString("chromeOptions.android_package",
&android_package)) {
scoped_ptr<ChromeAndroidImpl> chrome_android(new ChromeAndroidImpl(
context_getter, port, socket_factory));
status = chrome_android->Launch(android_package);
chrome.reset(chrome_android.release());
} else {
base::FilePath::StringType path_str;
base::FilePath chrome_exe;
if (desired_caps->GetString("chromeOptions.binary", &path_str)) {
chrome_exe = base::FilePath(path_str);
if (!file_util::PathExists(chrome_exe)) {
std::string message = base::StringPrintf(
"no chrome binary at %" PRFilePath,
path_str.c_str());
return Status(kUnknownError, message);
}
}
const base::Value* args = NULL;
const base::ListValue* args_list = NULL;
if (desired_caps->Get("chromeOptions.args", &args) &&
!args->GetAsList(&args_list)) {
return Status(kUnknownError,
"command line arguments for chrome must be a list");
}
const base::Value* prefs = NULL;
const base::DictionaryValue* prefs_dict = NULL;
if (desired_caps->Get("chromeOptions.prefs", &prefs) &&
!prefs->GetAsDictionary(&prefs_dict)) {
return Status(kUnknownError, "'prefs' must be a dictionary");
}
const base::Value* local_state = NULL;
const base::DictionaryValue* local_state_dict = NULL;
if (desired_caps->Get("chromeOptions.localState", &local_state) &&
!prefs->GetAsDictionary(&prefs_dict)) {
return Status(kUnknownError, "'localState' must be a dictionary");
}
const base::Value* extensions = NULL;
const base::ListValue* extensions_list = NULL;
if (desired_caps->Get("chromeOptions.extensions", &extensions)
&& !extensions->GetAsList(&extensions_list)) {
return Status(kUnknownError,
"chrome extensions must be a list");
}
scoped_ptr<ChromeDesktopImpl> chrome_desktop(new ChromeDesktopImpl(
context_getter, port, socket_factory));
status = chrome_desktop->Launch(chrome_exe, args_list, extensions_list,
prefs_dict, local_state_dict);
chrome.reset(chrome_desktop.release());
}
if (status.IsError())
return Status(kSessionNotCreatedException, status.message());
std::list<WebView*> web_views;
status = chrome->GetWebViews(&web_views);
if (status.IsError() || web_views.empty()) {
chrome->Quit();
return status.IsError() ? status :
Status(kUnknownError, "unable to discover open window in chrome");
}
WebView* default_web_view = web_views.front();
std::string new_id = session_id;
if (new_id.empty())
new_id = GenerateId();
scoped_ptr<Session> session(new Session(new_id, chrome.Pass()));
session->window = default_web_view->GetId();
out_value->reset(session->capabilities->DeepCopy());
*out_session_id = new_id;
scoped_refptr<SessionAccessor> accessor(
new SessionAccessorImpl(session.Pass()));
session_map->Set(new_id, accessor);
return Status(kOk);
}
Status ExecuteQuitAll(
Command quit_command,
SessionMap* session_map,
const base::DictionaryValue& params,
const std::string& session_id,
scoped_ptr<base::Value>* out_value,
std::string* out_session_id) {
std::vector<std::string> session_ids;
session_map->GetKeys(&session_ids);
for (size_t i = 0; i < session_ids.size(); ++i) {
scoped_ptr<base::Value> unused_value;
std::string unused_session_id;
quit_command.Run(params, session_ids[i], &unused_value, &unused_session_id);
}
return Status(kOk);
}
|