Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(922)

Side by Side Diff: chrome/test/chromedriver/server/http_handler.cc

Issue 18419003: [chromedriver] Remove dll build target. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/test/chromedriver/server/http_handler.h" 5 #include "chrome/test/chromedriver/server/http_handler.h"
6 6
7 #include "base/bind.h"
8 #include "base/callback.h"
7 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h" 10 #include "base/json/json_writer.h"
11 #include "base/logging.h" // For CHECK macros.
9 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop.h"
14 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/strings/string_split.h" 15 #include "base/strings/string_split.h"
11 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h" 17 #include "base/strings/stringprintf.h"
18 #include "base/sys_info.h"
13 #include "base/values.h" 19 #include "base/values.h"
20 #include "chrome/test/chromedriver/alert_commands.h"
21 #include "chrome/test/chromedriver/chrome/adb_impl.h"
22 #include "chrome/test/chromedriver/chrome/device_manager.h"
14 #include "chrome/test/chromedriver/chrome/log.h" 23 #include "chrome/test/chromedriver/chrome/log.h"
15 #include "chrome/test/chromedriver/chrome/status.h" 24 #include "chrome/test/chromedriver/chrome/status.h"
16 #include "chrome/test/chromedriver/command_executor.h" 25 #include "chrome/test/chromedriver/chrome/version.h"
17 #include "chrome/test/chromedriver/command_names.h" 26 #include "chrome/test/chromedriver/commands.h"
27 #include "chrome/test/chromedriver/element_commands.h"
28 #include "chrome/test/chromedriver/net/url_request_context_getter.h"
18 #include "chrome/test/chromedriver/server/http_response.h" 29 #include "chrome/test/chromedriver/server/http_response.h"
30 #include "chrome/test/chromedriver/session.h"
31 #include "chrome/test/chromedriver/session_commands.h"
32 #include "chrome/test/chromedriver/session_map.h"
19 #include "chrome/test/chromedriver/util.h" 33 #include "chrome/test/chromedriver/util.h"
34 #include "chrome/test/chromedriver/window_commands.h"
35
36 #if defined(OS_MACOSX)
37 #include "base/mac/scoped_nsautorelease_pool.h"
38 #endif
20 39
21 namespace { 40 namespace {
22 41
42 const char kLocalStorage[] = "localStorage";
43 const char kSessionStorage[] = "sessionStorage";
23 const char kShutdownPath[] = "shutdown"; 44 const char kShutdownPath[] = "shutdown";
24 45
46 Status UnimplementedCommand(
47 const base::DictionaryValue& params,
48 const std::string& session_id,
49 scoped_ptr<base::Value>* value,
50 std::string* out_session_id) {
51 return Status(kUnknownCommand);
52 }
53
25 } // namespace 54 } // namespace
26 55
27 HttpRequest::HttpRequest(HttpMethod method, 56 HttpRequest::HttpRequest(HttpMethod method,
28 const std::string& path, 57 const std::string& path,
29 const std::string& body) 58 const std::string& body)
30 : method(method), path(path), body(body) {} 59 : method(method), path(path), body(body) {}
31 60
32 HttpRequest::~HttpRequest() {} 61 HttpRequest::~HttpRequest() {}
33 62
34 CommandMapping::CommandMapping(HttpMethod method, 63 CommandMapping::CommandMapping(HttpMethod method,
35 const std::string& path_pattern, 64 const std::string& path_pattern,
36 const std::string& name) 65 const Command& command)
37 : method(method), path_pattern(path_pattern), name(name) {} 66 : method(method), path_pattern(path_pattern), command(command) {}
38 67
39 CommandMapping::~CommandMapping() {} 68 CommandMapping::~CommandMapping() {}
40 69
41 // static 70 HttpHandler::HttpHandler(Log* log, const std::string& url_base)
42 scoped_ptr<HttpHandler::CommandMap> HttpHandler::CreateCommandMap() { 71 : log_(log),
72 io_thread_("ChromeDriver IO"),
73 url_base_(url_base) {
74 #if defined(OS_MACOSX)
75 base::mac::ScopedNSAutoreleasePool autorelease_pool;
76 #endif
77 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0);
78 CHECK(io_thread_.StartWithOptions(options));
79 context_getter_ = new URLRequestContextGetter(
80 io_thread_.message_loop_proxy());
81 socket_factory_ = CreateSyncWebSocketFactory(context_getter_.get());
82 adb_.reset(new AdbImpl(io_thread_.message_loop_proxy(), log_));
83 device_manager_.reset(new DeviceManager(adb_.get()));
84
43 CommandMapping commands[] = { 85 CommandMapping commands[] = {
44 CommandMapping(kPost, "session", CommandNames::kNewSession), 86 CommandMapping(kPost, internal::kNewSessionPathPattern,
87 base::Bind(&ExecuteNewSession,
88 NewSessionParams(log_, &session_map_,
89 context_getter_, socket_factory_,
90 device_manager_.get()))),
45 CommandMapping(kGet, "session/:sessionId", 91 CommandMapping(kGet, "session/:sessionId",
46 CommandNames::kGetSessionCapabilities), 92 WrapToCommand(
47 CommandMapping(kDelete, "session/:sessionId", CommandNames::kQuit), 93 base::Bind(&ExecuteGetSessionCapabilities,
94 &session_map_))),
95 CommandMapping(kDelete, "session/:sessionId",
96 base::Bind(&ExecuteQuit, false, &session_map_)),
48 CommandMapping(kGet, "session/:sessionId/window_handle", 97 CommandMapping(kGet, "session/:sessionId/window_handle",
49 CommandNames::kGetCurrentWindowHandle), 98 WrapToCommand(base::Bind(&ExecuteGetCurrentWindowHandle))),
50 CommandMapping(kGet, "session/:sessionId/window_handles", 99 CommandMapping(kGet, "session/:sessionId/window_handles",
51 CommandNames::kGetWindowHandles), 100 WrapToCommand(base::Bind(&ExecuteGetWindowHandles))),
52 CommandMapping(kPost, "session/:sessionId/url", CommandNames::kGet), 101 CommandMapping(kPost, "session/:sessionId/url",
53 CommandMapping(kGet, "session/:sessionId/alert", CommandNames::kGetAlert), 102 WrapToCommand(base::Bind(&ExecuteGet))),
103 CommandMapping(kGet, "session/:sessionId/alert",
104 WrapToCommand(
105 base::Bind(&ExecuteAlertCommand,
106 base::Bind(&ExecuteGetAlert)))),
54 CommandMapping(kPost, "session/:sessionId/dismiss_alert", 107 CommandMapping(kPost, "session/:sessionId/dismiss_alert",
55 CommandNames::kDismissAlert), 108 WrapToCommand(
109 base::Bind(&ExecuteAlertCommand,
110 base::Bind(&ExecuteDismissAlert)))),
56 CommandMapping(kPost, "session/:sessionId/accept_alert", 111 CommandMapping(kPost, "session/:sessionId/accept_alert",
57 CommandNames::kAcceptAlert), 112 WrapToCommand(
113 base::Bind(&ExecuteAlertCommand,
114 base::Bind(&ExecuteAcceptAlert)))),
58 CommandMapping(kGet, "session/:sessionId/alert_text", 115 CommandMapping(kGet, "session/:sessionId/alert_text",
59 CommandNames::kGetAlertText), 116 WrapToCommand(
117 base::Bind(&ExecuteAlertCommand,
118 base::Bind(&ExecuteGetAlertText)))),
60 CommandMapping(kPost, "session/:sessionId/alert_text", 119 CommandMapping(kPost, "session/:sessionId/alert_text",
61 CommandNames::kSetAlertValue), 120 WrapToCommand(
121 base::Bind(&ExecuteAlertCommand,
122 base::Bind(&ExecuteSetAlertValue)))),
62 CommandMapping(kPost, "session/:sessionId/forward", 123 CommandMapping(kPost, "session/:sessionId/forward",
63 CommandNames::kGoForward), 124 WrapToCommand(base::Bind(&ExecuteGoForward))),
64 CommandMapping(kPost, "session/:sessionId/back", CommandNames::kGoBack), 125 CommandMapping(kPost, "session/:sessionId/back",
126 WrapToCommand(base::Bind(&ExecuteGoBack))),
65 CommandMapping(kPost, "session/:sessionId/refresh", 127 CommandMapping(kPost, "session/:sessionId/refresh",
66 CommandNames::kRefresh), 128 WrapToCommand(base::Bind(&ExecuteRefresh))),
67 CommandMapping(kPost, "session/:sessionId/execute", 129 CommandMapping(kPost, "session/:sessionId/execute",
68 CommandNames::kExecuteScript), 130 WrapToCommand(base::Bind(&ExecuteExecuteScript))),
69 CommandMapping(kPost, "session/:sessionId/execute_async", 131 CommandMapping(kPost, "session/:sessionId/execute_async",
70 CommandNames::kExecuteAsyncScript), 132 WrapToCommand(base::Bind(&ExecuteExecuteAsyncScript))),
71 CommandMapping(kGet, "session/:sessionId/url", 133 CommandMapping(kGet, "session/:sessionId/url",
72 CommandNames::kGetCurrentUrl), 134 WrapToCommand(base::Bind(&ExecuteGetCurrentUrl))),
73 CommandMapping(kGet, "session/:sessionId/title", CommandNames::kGetTitle), 135 CommandMapping(kGet, "session/:sessionId/title",
136 WrapToCommand(base::Bind(&ExecuteGetTitle))),
74 CommandMapping(kGet, "session/:sessionId/source", 137 CommandMapping(kGet, "session/:sessionId/source",
75 CommandNames::kGetPageSource), 138 WrapToCommand(base::Bind(&ExecuteGetPageSource))),
76 CommandMapping(kGet, "session/:sessionId/screenshot", 139 CommandMapping(kGet, "session/:sessionId/screenshot",
77 CommandNames::kScreenshot), 140 WrapToCommand(base::Bind(&ExecuteScreenshot))),
78 CommandMapping(kPost, "session/:sessionId/visible", 141 CommandMapping(kPost, "session/:sessionId/visible",
79 CommandNames::kSetBrowserVisible), 142 base::Bind(&UnimplementedCommand)),
80 CommandMapping(kGet, "session/:sessionId/visible", 143 CommandMapping(kGet, "session/:sessionId/visible",
81 CommandNames::kIsBrowserVisible), 144 base::Bind(&UnimplementedCommand)),
82 CommandMapping(kPost, "session/:sessionId/element", 145 CommandMapping(kPost, "session/:sessionId/element",
83 CommandNames::kFindElement), 146 WrapToCommand(base::Bind(&ExecuteFindElement, 50))),
84 CommandMapping(kPost, "session/:sessionId/elements", 147 CommandMapping(kPost, "session/:sessionId/elements",
85 CommandNames::kFindElements), 148 WrapToCommand(base::Bind(&ExecuteFindElements, 50))),
86 CommandMapping(kPost, "session/:sessionId/element/active", 149 CommandMapping(kPost, "session/:sessionId/element/active",
87 CommandNames::kGetActiveElement), 150 WrapToCommand(base::Bind(&ExecuteGetActiveElement))),
88 CommandMapping(kPost, "session/:sessionId/element/:id/element", 151 CommandMapping(kPost, "session/:sessionId/element/:id/element",
89 CommandNames::kFindChildElement), 152 WrapToCommand(base::Bind(&ExecuteFindChildElement, 50))),
90 CommandMapping(kPost, "session/:sessionId/element/:id/elements", 153 CommandMapping(kPost, "session/:sessionId/element/:id/elements",
91 CommandNames::kFindChildElements), 154 WrapToCommand(base::Bind(&ExecuteFindChildElements, 50))),
92 CommandMapping(kPost, "session/:sessionId/element/:id/click", 155 CommandMapping(kPost, "session/:sessionId/element/:id/click",
93 CommandNames::kClickElement), 156 WrapToCommand(base::Bind(&ExecuteClickElement))),
94 CommandMapping(kPost, "session/:sessionId/element/:id/clear", 157 CommandMapping(kPost, "session/:sessionId/element/:id/clear",
95 CommandNames::kClearElement), 158 WrapToCommand(base::Bind(&ExecuteClearElement))),
96 CommandMapping(kPost, "session/:sessionId/element/:id/submit", 159 CommandMapping(kPost, "session/:sessionId/element/:id/submit",
97 CommandNames::kSubmitElement), 160 WrapToCommand(base::Bind(&ExecuteSubmitElement))),
98 CommandMapping(kGet, "session/:sessionId/element/:id/text", 161 CommandMapping(kGet, "session/:sessionId/element/:id/text",
99 CommandNames::kGetElementText), 162 WrapToCommand(base::Bind(&ExecuteGetElementText))),
100 CommandMapping(kPost, "session/:sessionId/element/:id/value", 163 CommandMapping(kPost, "session/:sessionId/element/:id/value",
101 CommandNames::kSendKeysToElement), 164 WrapToCommand(base::Bind(&ExecuteSendKeysToElement))),
102 CommandMapping(kPost, "session/:sessionId/file", 165 CommandMapping(kPost, "session/:sessionId/file",
103 CommandNames::kUploadFile), 166 WrapToCommand(base::Bind(&ExecuteUploadFile))),
104 CommandMapping(kGet, "session/:sessionId/element/:id/value", 167 CommandMapping(kGet, "session/:sessionId/element/:id/value",
105 CommandNames::kGetElementValue), 168 WrapToCommand(base::Bind(&ExecuteGetElementValue))),
106 CommandMapping(kGet, "session/:sessionId/element/:id/name", 169 CommandMapping(kGet, "session/:sessionId/element/:id/name",
107 CommandNames::kGetElementTagName), 170 WrapToCommand(base::Bind(&ExecuteGetElementTagName))),
108 CommandMapping(kGet, "session/:sessionId/element/:id/selected", 171 CommandMapping(kGet, "session/:sessionId/element/:id/selected",
109 CommandNames::kIsElementSelected), 172 WrapToCommand(base::Bind(&ExecuteIsElementSelected))),
110 CommandMapping(kGet, "session/:sessionId/element/:id/enabled", 173 CommandMapping(kGet, "session/:sessionId/element/:id/enabled",
111 CommandNames::kIsElementEnabled), 174 WrapToCommand(base::Bind(&ExecuteIsElementEnabled))),
112 CommandMapping(kGet, "session/:sessionId/element/:id/displayed", 175 CommandMapping(kGet, "session/:sessionId/element/:id/displayed",
113 CommandNames::kIsElementDisplayed), 176 WrapToCommand(base::Bind(&ExecuteIsElementDisplayed))),
114 CommandMapping(kPost, "session/:sessionId/element/:id/hover", 177 CommandMapping(kPost, "session/:sessionId/element/:id/hover",
115 CommandNames::kHoverOverElement), 178 WrapToCommand(base::Bind(&ExecuteHoverOverElement))),
116 CommandMapping(kGet, "session/:sessionId/element/:id/location", 179 CommandMapping(kGet, "session/:sessionId/element/:id/location",
117 CommandNames::kGetElementLocation), 180 WrapToCommand(base::Bind(&ExecuteGetElementLocation))),
118 CommandMapping(kGet, "session/:sessionId/element/:id/location_in_view", 181 CommandMapping(kGet, "session/:sessionId/element/:id/location_in_view",
119 CommandNames::kGetElementLocationOnceScrolledIntoView), 182 WrapToCommand(
183 base::Bind(
184 &ExecuteGetElementLocationOnceScrolledIntoView))),
120 CommandMapping(kGet, "session/:sessionId/element/:id/size", 185 CommandMapping(kGet, "session/:sessionId/element/:id/size",
121 CommandNames::kGetElementSize), 186 WrapToCommand(base::Bind(&ExecuteGetElementSize))),
122 CommandMapping(kGet, "session/:sessionId/element/:id/attribute/:name", 187 CommandMapping(kGet, "session/:sessionId/element/:id/attribute/:name",
123 CommandNames::kGetElementAttribute), 188 WrapToCommand(base::Bind(&ExecuteGetElementAttribute))),
124 CommandMapping(kGet, "session/:sessionId/element/:id/equals/:other", 189 CommandMapping(kGet, "session/:sessionId/element/:id/equals/:other",
125 CommandNames::kElementEquals), 190 WrapToCommand(base::Bind(&ExecuteElementEquals))),
126 CommandMapping(kGet, "session/:sessionId/cookie", 191 CommandMapping(kGet, "session/:sessionId/cookie",
127 CommandNames::kGetCookies), 192 WrapToCommand(base::Bind(&ExecuteGetCookies))),
128 CommandMapping(kPost, "session/:sessionId/cookie", 193 CommandMapping(kPost, "session/:sessionId/cookie",
129 CommandNames::kAddCookie), 194 WrapToCommand(base::Bind(&ExecuteAddCookie))),
130 CommandMapping(kDelete, "session/:sessionId/cookie", 195 CommandMapping(kDelete, "session/:sessionId/cookie",
131 CommandNames::kDeleteAllCookies), 196 WrapToCommand(base::Bind(&ExecuteDeleteAllCookies))),
132 CommandMapping(kDelete, "session/:sessionId/cookie/:name", 197 CommandMapping(kDelete, "session/:sessionId/cookie/:name",
133 CommandNames::kDeleteCookie), 198 WrapToCommand(base::Bind(&ExecuteDeleteCookie))),
134 CommandMapping(kPost, "session/:sessionId/frame", 199 CommandMapping(kPost, "session/:sessionId/frame",
135 CommandNames::kSwitchToFrame), 200 WrapToCommand(base::Bind(&ExecuteSwitchToFrame))),
136 CommandMapping(kPost, "session/:sessionId/window", 201 CommandMapping(kPost, "session/:sessionId/window",
137 CommandNames::kSwitchToWindow), 202 WrapToCommand(base::Bind(&ExecuteSwitchToWindow))),
138 CommandMapping(kGet, "session/:sessionId/window/:windowHandle/size", 203 CommandMapping(kGet, "session/:sessionId/window/:windowHandle/size",
139 CommandNames::kGetWindowSize), 204 WrapToCommand(base::Bind(&ExecuteGetWindowSize))),
140 CommandMapping(kGet, "session/:sessionId/window/:windowHandle/position", 205 CommandMapping(kGet, "session/:sessionId/window/:windowHandle/position",
141 CommandNames::kGetWindowPosition), 206 WrapToCommand(base::Bind(&ExecuteGetWindowPosition))),
142 CommandMapping(kPost, "session/:sessionId/window/:windowHandle/size", 207 CommandMapping(kPost, "session/:sessionId/window/:windowHandle/size",
143 CommandNames::kSetWindowSize), 208 WrapToCommand(base::Bind(&ExecuteSetWindowSize))),
144 CommandMapping(kPost, "session/:sessionId/window/:windowHandle/position", 209 CommandMapping(kPost, "session/:sessionId/window/:windowHandle/position",
145 CommandNames::kSetWindowPosition), 210 WrapToCommand(base::Bind(&ExecuteSetWindowPosition))),
146 CommandMapping(kPost, "session/:sessionId/window/:windowHandle/maximize", 211 CommandMapping(kPost, "session/:sessionId/window/:windowHandle/maximize",
147 CommandNames::kMaximizeWindow), 212 WrapToCommand(base::Bind(&ExecuteMaximizeWindow))),
148 CommandMapping(kDelete, "session/:sessionId/window", 213 CommandMapping(kDelete, "session/:sessionId/window",
149 CommandNames::kClose), 214 WrapToCommand(base::Bind(&ExecuteClose, &session_map_))),
150 CommandMapping(kPost, "session/:sessionId/element/:id/drag", 215 CommandMapping(kPost, "session/:sessionId/element/:id/drag",
151 CommandNames::kDragElement), 216 base::Bind(&UnimplementedCommand)),
152 CommandMapping(kGet, "session/:sessionId/element/:id/css/:propertyName", 217 CommandMapping(kGet, "session/:sessionId/element/:id/css/:propertyName",
153 CommandNames::kGetElementValueOfCssProperty), 218 WrapToCommand(
219 base::Bind(&ExecuteGetElementValueOfCSSProperty))),
154 CommandMapping(kPost, "session/:sessionId/timeouts/implicit_wait", 220 CommandMapping(kPost, "session/:sessionId/timeouts/implicit_wait",
155 CommandNames::kImplicitlyWait), 221 WrapToCommand(base::Bind(&ExecuteImplicitlyWait))),
156 CommandMapping(kPost, "session/:sessionId/timeouts/async_script", 222 CommandMapping(kPost, "session/:sessionId/timeouts/async_script",
157 CommandNames::kSetScriptTimeout), 223 WrapToCommand(base::Bind(&ExecuteSetScriptTimeout))),
158 CommandMapping(kPost, "session/:sessionId/timeouts", 224 CommandMapping(kPost, "session/:sessionId/timeouts",
159 CommandNames::kSetTimeout), 225 WrapToCommand(base::Bind(&ExecuteSetTimeout))),
160 CommandMapping(kPost, "session/:sessionId/execute_sql", 226 CommandMapping(kPost, "session/:sessionId/execute_sql",
161 CommandNames::kExecuteSQL), 227 base::Bind(&UnimplementedCommand)),
162 CommandMapping(kGet, "session/:sessionId/location", 228 CommandMapping(kGet, "session/:sessionId/location",
163 CommandNames::kGetLocation), 229 WrapToCommand(base::Bind(&ExecuteGetLocation))),
164 CommandMapping(kPost, "session/:sessionId/location", 230 CommandMapping(kPost, "session/:sessionId/location",
165 CommandNames::kSetLocation), 231 WrapToCommand(base::Bind(&ExecuteSetLocation))),
166 CommandMapping(kGet, "session/:sessionId/application_cache/status", 232 CommandMapping(kGet, "session/:sessionId/application_cache/status",
167 CommandNames::kGetStatus), 233 base::Bind(&ExecuteGetStatus)),
168 CommandMapping(kGet, "session/:sessionId/browser_connection", 234 CommandMapping(kGet, "session/:sessionId/browser_connection",
169 CommandNames::kIsBrowserOnline), 235 base::Bind(&UnimplementedCommand)),
170 CommandMapping(kPost, "session/:sessionId/browser_connection", 236 CommandMapping(kPost, "session/:sessionId/browser_connection",
171 CommandNames::kSetBrowserOnline), 237 base::Bind(&UnimplementedCommand)),
172 CommandMapping(kGet, "session/:sessionId/local_storage/key/:key", 238 CommandMapping(kGet, "session/:sessionId/local_storage/key/:key",
173 CommandNames::kGetLocalStorageItem), 239 WrapToCommand(
240 base::Bind(&ExecuteGetStorageItem, kLocalStorage))),
174 CommandMapping(kDelete, "session/:sessionId/local_storage/key/:key", 241 CommandMapping(kDelete, "session/:sessionId/local_storage/key/:key",
175 CommandNames::kRemoveLocalStorageItem), 242 WrapToCommand(
243 base::Bind(&ExecuteRemoveStorageItem, kLocalStorage))),
176 CommandMapping(kGet, "session/:sessionId/local_storage", 244 CommandMapping(kGet, "session/:sessionId/local_storage",
177 CommandNames::kGetLocalStorageKeys), 245 WrapToCommand(
246 base::Bind(&ExecuteGetStorageKeys, kLocalStorage))),
178 CommandMapping(kPost, "session/:sessionId/local_storage", 247 CommandMapping(kPost, "session/:sessionId/local_storage",
179 CommandNames::kSetLocalStorageItem), 248 WrapToCommand(
249 base::Bind(&ExecuteSetStorageItem, kLocalStorage))),
180 CommandMapping(kDelete, "session/:sessionId/local_storage", 250 CommandMapping(kDelete, "session/:sessionId/local_storage",
181 CommandNames::kClearLocalStorage), 251 WrapToCommand(
252 base::Bind(&ExecuteClearStorage, kLocalStorage))),
182 CommandMapping(kGet, "session/:sessionId/local_storage/size", 253 CommandMapping(kGet, "session/:sessionId/local_storage/size",
183 CommandNames::kGetLocalStorageSize), 254 WrapToCommand(
255 base::Bind(&ExecuteGetStorageSize, kLocalStorage))),
184 CommandMapping(kGet, "session/:sessionId/session_storage/key/:key", 256 CommandMapping(kGet, "session/:sessionId/session_storage/key/:key",
185 CommandNames::kGetSessionStorageItem), 257 WrapToCommand(
258 base::Bind(&ExecuteGetStorageItem, kSessionStorage))),
186 CommandMapping(kDelete, "session/:sessionId/session_storage/key/:key", 259 CommandMapping(kDelete, "session/:sessionId/session_storage/key/:key",
187 CommandNames::kRemoveSessionStorageItem), 260 WrapToCommand(
261 base::Bind(
262 &ExecuteRemoveStorageItem, kSessionStorage))),
188 CommandMapping(kGet, "session/:sessionId/session_storage", 263 CommandMapping(kGet, "session/:sessionId/session_storage",
189 CommandNames::kGetSessionStorageKey), 264 WrapToCommand(
265 base::Bind(&ExecuteGetStorageKeys, kSessionStorage))),
190 CommandMapping(kPost, "session/:sessionId/session_storage", 266 CommandMapping(kPost, "session/:sessionId/session_storage",
191 CommandNames::kSetSessionStorageItem), 267 WrapToCommand(
268 base::Bind(&ExecuteSetStorageItem, kSessionStorage))),
192 CommandMapping(kDelete, "session/:sessionId/session_storage", 269 CommandMapping(kDelete, "session/:sessionId/session_storage",
193 CommandNames::kClearSessionStorage), 270 WrapToCommand(
271 base::Bind(&ExecuteClearStorage, kSessionStorage))),
194 CommandMapping(kGet, "session/:sessionId/session_storage/size", 272 CommandMapping(kGet, "session/:sessionId/session_storage/size",
195 CommandNames::kGetSessionStorageSize), 273 WrapToCommand(
274 base::Bind(&ExecuteGetStorageSize, kSessionStorage))),
196 CommandMapping(kGet, "session/:sessionId/orientation", 275 CommandMapping(kGet, "session/:sessionId/orientation",
197 CommandNames::kGetScreenOrientation), 276 base::Bind(&UnimplementedCommand)),
198 CommandMapping(kPost, "session/:sessionId/orientation", 277 CommandMapping(kPost, "session/:sessionId/orientation",
199 CommandNames::kSetScreenOrientation), 278 base::Bind(&UnimplementedCommand)),
200 CommandMapping(kPost, "session/:sessionId/click", 279 CommandMapping(kPost, "session/:sessionId/click",
201 CommandNames::kMouseClick), 280 WrapToCommand(base::Bind(&ExecuteMouseClick))),
202 CommandMapping(kPost, "session/:sessionId/doubleclick", 281 CommandMapping(kPost, "session/:sessionId/doubleclick",
203 CommandNames::kMouseDoubleClick), 282 WrapToCommand(base::Bind(&ExecuteMouseDoubleClick))),
204 CommandMapping(kPost, "session/:sessionId/buttondown", 283 CommandMapping(kPost, "session/:sessionId/buttondown",
205 CommandNames::kMouseButtonDown), 284 WrapToCommand(base::Bind(&ExecuteMouseButtonDown))),
206 CommandMapping(kPost, "session/:sessionId/buttonup", 285 CommandMapping(kPost, "session/:sessionId/buttonup",
207 CommandNames::kMouseButtonUp), 286 WrapToCommand(base::Bind(&ExecuteMouseButtonUp))),
208 CommandMapping(kPost, "session/:sessionId/moveto", 287 CommandMapping(kPost, "session/:sessionId/moveto",
209 CommandNames::kMouseMoveTo), 288 WrapToCommand(base::Bind(&ExecuteMouseMoveTo))),
210 CommandMapping(kPost, "session/:sessionId/keys", 289 CommandMapping(kPost, "session/:sessionId/keys",
211 CommandNames::kSendKeysToActiveElement), 290 WrapToCommand(
291 base::Bind(&ExecuteSendKeysToActiveElement))),
212 CommandMapping(kGet, "session/:sessionId/ime/available_engines", 292 CommandMapping(kGet, "session/:sessionId/ime/available_engines",
213 CommandNames::kImeGetAvailableEngines), 293 base::Bind(&UnimplementedCommand)),
214 CommandMapping(kGet, "session/:sessionId/ime/active_engine", 294 CommandMapping(kGet, "session/:sessionId/ime/active_engine",
215 CommandNames::kImeGetActiveEngine), 295 base::Bind(&UnimplementedCommand)),
216 CommandMapping(kGet, "session/:sessionId/ime/activated", 296 CommandMapping(kGet, "session/:sessionId/ime/activated",
217 CommandNames::kImeIsActivated), 297 base::Bind(&UnimplementedCommand)),
218 CommandMapping(kPost, "session/:sessionId/ime/deactivate", 298 CommandMapping(kPost, "session/:sessionId/ime/deactivate",
219 CommandNames::kImeDeactivate), 299 base::Bind(&UnimplementedCommand)),
220 CommandMapping(kPost, "session/:sessionId/ime/activate", 300 CommandMapping(kPost, "session/:sessionId/ime/activate",
221 CommandNames::kImeActivateEngine), 301 base::Bind(&UnimplementedCommand)),
222 CommandMapping(kPost, "session/:sessionId/touch/click", 302 CommandMapping(kPost, "session/:sessionId/touch/click",
223 CommandNames::kTouchSingleTap), 303 WrapToCommand(base::Bind(&ExecuteTouchSingleTap))),
224 CommandMapping(kPost, "session/:sessionId/touch/down", 304 CommandMapping(kPost, "session/:sessionId/touch/down",
225 CommandNames::kTouchDown), 305 base::Bind(&UnimplementedCommand)),
226 CommandMapping(kPost, "session/:sessionId/touch/up", 306 CommandMapping(kPost, "session/:sessionId/touch/up",
227 CommandNames::kTouchUp), 307 base::Bind(&UnimplementedCommand)),
228 CommandMapping(kPost, "session/:sessionId/touch/move", 308 CommandMapping(kPost, "session/:sessionId/touch/move",
229 CommandNames::kTouchMove), 309 base::Bind(&UnimplementedCommand)),
230 CommandMapping(kPost, "session/:sessionId/touch/scroll", 310 CommandMapping(kPost, "session/:sessionId/touch/scroll",
231 CommandNames::kTouchScroll), 311 base::Bind(&UnimplementedCommand)),
232 CommandMapping(kPost, "session/:sessionId/touch/doubleclick", 312 CommandMapping(kPost, "session/:sessionId/touch/doubleclick",
233 CommandNames::kTouchDoubleTap), 313 base::Bind(&UnimplementedCommand)),
234 CommandMapping(kPost, "session/:sessionId/touch/longclick", 314 CommandMapping(kPost, "session/:sessionId/touch/longclick",
235 CommandNames::kTouchLongPress), 315 base::Bind(&UnimplementedCommand)),
236 CommandMapping(kPost, "session/:sessionId/touch/flick", 316 CommandMapping(kPost, "session/:sessionId/touch/flick",
237 CommandNames::kTouchFlick), 317 base::Bind(&UnimplementedCommand)),
238 CommandMapping(kPost, "session/:sessionId/log", CommandNames::kGetLog), 318 CommandMapping(kPost, "session/:sessionId/log",
319 WrapToCommand(base::Bind(&ExecuteGetLog))),
239 CommandMapping(kGet, "session/:sessionId/log/types", 320 CommandMapping(kGet, "session/:sessionId/log/types",
240 CommandNames::kGetAvailableLogTypes), 321 WrapToCommand(base::Bind(&ExecuteGetAvailableLogTypes))),
241 CommandMapping(kPost, "logs", CommandNames::kGetSessionLogs), 322 CommandMapping(kPost, "logs", base::Bind(&UnimplementedCommand)),
242 CommandMapping(kGet, "status", CommandNames::kStatus), 323 CommandMapping(kGet, "status", base::Bind(&ExecuteGetStatus)),
243 324
244 // Custom Chrome commands: 325 // Custom Chrome commands:
245 // Allow quit all to be called with GET or POST. 326 // Allow quit all to be called with GET or POST.
246 CommandMapping(kGet, kShutdownPath, CommandNames::kQuitAll), 327 CommandMapping(kGet, kShutdownPath,
247 CommandMapping(kPost, kShutdownPath, CommandNames::kQuitAll), 328 base::Bind(&ExecuteQuitAll,
329 base::Bind(&ExecuteQuit, true, &session_map_),
330 &session_map_)),
331 CommandMapping(kPost, kShutdownPath,
332 base::Bind(&ExecuteQuitAll,
333 base::Bind(&ExecuteQuit, true, &session_map_),
334 &session_map_)),
248 CommandMapping(kGet, "session/:sessionId/is_loading", 335 CommandMapping(kGet, "session/:sessionId/is_loading",
249 CommandNames::kIsLoading), 336 WrapToCommand(base::Bind(&ExecuteIsLoading))),
250 }; 337 };
251 return scoped_ptr<CommandMap>( 338 this->command_map_.reset(
252 new CommandMap(commands, commands + arraysize(commands))); 339 new CommandMap(commands, commands + arraysize(commands)));
253 } 340 }
254 341
255 HttpHandler::HttpHandler(Log* log,
256 scoped_ptr<CommandExecutor> executor,
257 scoped_ptr<CommandMap> command_map,
258 const std::string& url_base)
259 : log_(log),
260 executor_(executor.Pass()),
261 command_map_(command_map.Pass()),
262 url_base_(url_base) {
263 executor_->Init();
264 }
265
266 HttpHandler::~HttpHandler() {} 342 HttpHandler::~HttpHandler() {}
267 343
268 void HttpHandler::Handle(const HttpRequest& request, 344 void HttpHandler::Handle(const HttpRequest& request,
269 HttpResponse* response) { 345 HttpResponse* response) {
270 const char* method = "GET"; 346 const char* method = "GET";
271 if (request.method == kPost) 347 if (request.method == kPost)
272 method = "POST"; 348 method = "POST";
273 else if (request.method == kDelete) 349 else if (request.method == kDelete)
274 method = "DELETE"; 350 method = "DELETE";
275 log_->AddEntry( 351 log_->AddEntry(
276 Log::kLog, 352 Log::kLog,
277 base::StringPrintf("received WebDriver request: %s %s %s", 353 base::StringPrintf("received WebDriver request: %s %s %s",
278 method, 354 method,
279 request.path.c_str(), 355 request.path.c_str(),
280 request.body.c_str())); 356 request.body.c_str()));
281 357
282 HandleInternal(request, response); 358 HandleInternal(request, response);
283 359
284 log_->AddEntry( 360 log_->AddEntry(
285 Log::kLog, 361 Log::kLog,
286 base::StringPrintf("sending WebDriver response: %d %s", 362 base::StringPrintf("sending WebDriver response: %d %s",
287 response->status(), 363 response->status(),
288 response->body().c_str())); 364 response->body().c_str()));
289 } 365 }
290 366
291 bool HttpHandler::ShouldShutdown(const HttpRequest& request) { 367 bool HttpHandler::ShouldShutdown(const HttpRequest& request) {
292 return request.path == url_base_ + kShutdownPath; 368 return request.path == url_base_ + kShutdownPath;
293 } 369 }
294 370
371 Command HttpHandler::WrapToCommand(
372 const SessionCommand& session_command) {
373 return base::Bind(&ExecuteSessionCommand, &session_map_, session_command);
374 }
375
376 Command HttpHandler::WrapToCommand(
377 const WindowCommand& window_command) {
378 return WrapToCommand(
379 base::Bind(&ExecuteWindowCommand, window_command));
380 }
381
382 Command HttpHandler::WrapToCommand(
383 const ElementCommand& element_command) {
384 return WrapToCommand(
385 base::Bind(&ExecuteElementCommand, element_command));
386 }
387
295 void HttpHandler::HandleInternal(const HttpRequest& request, 388 void HttpHandler::HandleInternal(const HttpRequest& request,
296 HttpResponse* response) { 389 HttpResponse* response) {
297 std::string path = request.path; 390 std::string path = request.path;
298 if (!StartsWithASCII(path, url_base_, true)) { 391 if (!StartsWithASCII(path, url_base_, true)) {
299 *response = HttpResponse(HttpResponse::kBadRequest); 392 *response = HttpResponse(HttpResponse::kBadRequest);
300 response->set_body("unhandled request"); 393 response->set_body("unhandled request");
301 return; 394 return;
302 } 395 }
303 396
304 path.erase(0, url_base_.length()); 397 path.erase(0, url_base_.length());
(...skipping 27 matching lines...) Expand all
332 base::DictionaryValue* body_params; 425 base::DictionaryValue* body_params;
333 scoped_ptr<base::Value> parsed_body(base::JSONReader::Read(request.body)); 426 scoped_ptr<base::Value> parsed_body(base::JSONReader::Read(request.body));
334 if (!parsed_body || !parsed_body->GetAsDictionary(&body_params)) { 427 if (!parsed_body || !parsed_body->GetAsDictionary(&body_params)) {
335 *response = HttpResponse(HttpResponse::kBadRequest); 428 *response = HttpResponse(HttpResponse::kBadRequest);
336 response->set_body("missing command parameters"); 429 response->set_body("missing command parameters");
337 return true; 430 return true;
338 } 431 }
339 params.MergeDictionary(body_params); 432 params.MergeDictionary(body_params);
340 } 433 }
341 434
342 StatusCode status = kOk;
343 scoped_ptr<base::Value> value; 435 scoped_ptr<base::Value> value;
344 std::string out_session_id; 436 std::string out_session_id;
345 executor_->ExecuteCommand( 437 Status status = iter->command.Run(
346 iter->name, params, session_id, &status, &value, &out_session_id); 438 params, session_id, &value, &out_session_id);
347 439
348 if (status == kUnknownCommand) { 440 if (status.code() == kUnknownCommand) {
349 *response = HttpResponse(HttpResponse::kNotImplemented); 441 *response = HttpResponse(HttpResponse::kNotImplemented);
350 response->set_body("unimplemented command: " + iter->name); 442 response->set_body("unimplemented command: " + trimmed_path);
351 return true; 443 return true;
352 } 444 }
353 445
354 if (iter->name == CommandNames::kNewSession && status == kOk) { 446 if (iter->path_pattern == internal::kNewSessionPathPattern && status.IsOk()) {
355 // Creating a session involves a HTTP request to /session, which is 447 // Creating a session involves a HTTP request to /session, which is
356 // supposed to redirect to /session/:sessionId, which returns the 448 // supposed to redirect to /session/:sessionId, which returns the
357 // session info. 449 // session info.
358 *response = HttpResponse(HttpResponse::kSeeOther); 450 *response = HttpResponse(HttpResponse::kSeeOther);
359 response->AddHeader("Location", url_base_ + "session/" + out_session_id); 451 response->AddHeader("Location", url_base_ + "session/" + out_session_id);
360 return true; 452 return true;
453 } else if (status.IsError()) {
454 status.AddDetails(base::StringPrintf(
455 "Driver info: chromedriver=%s,platform=%s %s %s",
456 kChromeDriverVersion,
457 base::SysInfo::OperatingSystemName().c_str(),
458 base::SysInfo::OperatingSystemVersion().c_str(),
459 base::SysInfo::OperatingSystemArchitecture().c_str()));
460 scoped_ptr<base::DictionaryValue> error(new base::DictionaryValue());
461 error->SetString("message", status.message());
462 value.reset(error.release());
361 } 463 }
464 if (!value)
465 value.reset(base::Value::CreateNullValue());
362 466
363 base::DictionaryValue body_params; 467 base::DictionaryValue body_params;
364 body_params.SetInteger("status", status); 468 body_params.SetInteger("status", status.code());
365 body_params.Set("value", value.release()); 469 body_params.Set("value", value.release());
366 body_params.SetString("sessionId", out_session_id); 470 body_params.SetString("sessionId", out_session_id);
367 std::string body; 471 std::string body;
368 base::JSONWriter::WriteWithOptions( 472 base::JSONWriter::WriteWithOptions(
369 &body_params, base::JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION, 473 &body_params, base::JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION,
370 &body); 474 &body);
371 *response = HttpResponse(HttpResponse::kOk); 475 *response = HttpResponse(HttpResponse::kOk);
372 response->SetMimeType("application/json; charset=utf-8"); 476 response->SetMimeType("application/json; charset=utf-8");
373 response->set_body(body); 477 response->set_body(body);
374 return true; 478 return true;
375 } 479 }
376 480
377 namespace internal { 481 namespace internal {
378 482
483 const char kNewSessionPathPattern[] = "session";
484
379 bool MatchesCommand(HttpMethod method, 485 bool MatchesCommand(HttpMethod method,
380 const std::string& path, 486 const std::string& path,
381 const CommandMapping& command, 487 const CommandMapping& command,
382 std::string* session_id, 488 std::string* session_id,
383 base::DictionaryValue* out_params) { 489 base::DictionaryValue* out_params) {
384 if (method != command.method) 490 if (method != command.method)
385 return false; 491 return false;
386 492
387 std::vector<std::string> path_parts; 493 std::vector<std::string> path_parts;
388 base::SplitString(path, '/', &path_parts); 494 base::SplitString(path, '/', &path_parts);
(...skipping 14 matching lines...) Expand all
403 else 509 else
404 params.SetString(name, path_parts[i]); 510 params.SetString(name, path_parts[i]);
405 } else if (command_path_parts[i] != path_parts[i]) { 511 } else if (command_path_parts[i] != path_parts[i]) {
406 return false; 512 return false;
407 } 513 }
408 } 514 }
409 out_params->MergeDictionary(&params); 515 out_params->MergeDictionary(&params);
410 return true; 516 return true;
411 } 517 }
412 518
413 } // namespace 519 } // namespace internal
OLDNEW
« no previous file with comments | « chrome/test/chromedriver/server/http_handler.h ('k') | chrome/test/chromedriver/server/http_handler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698