OLD | NEW |
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" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
11 #include "base/logging.h" // For CHECK macros. | 11 #include "base/logging.h" // For CHECK macros. |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/message_loop/message_loop_proxy.h" | 14 #include "base/message_loop/message_loop_proxy.h" |
15 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
18 #include "base/sys_info.h" | 18 #include "base/sys_info.h" |
19 #include "base/values.h" | 19 #include "base/values.h" |
20 #include "chrome/test/chromedriver/alert_commands.h" | 20 #include "chrome/test/chromedriver/alert_commands.h" |
21 #include "chrome/test/chromedriver/chrome/adb_impl.h" | 21 #include "chrome/test/chromedriver/chrome/adb_impl.h" |
22 #include "chrome/test/chromedriver/chrome/device_manager.h" | 22 #include "chrome/test/chromedriver/chrome/device_manager.h" |
23 #include "chrome/test/chromedriver/chrome/log.h" | 23 #include "chrome/test/chromedriver/chrome/log.h" |
24 #include "chrome/test/chromedriver/chrome/status.h" | 24 #include "chrome/test/chromedriver/chrome/status.h" |
25 #include "chrome/test/chromedriver/chrome/version.h" | 25 #include "chrome/test/chromedriver/chrome/version.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" | 26 #include "chrome/test/chromedriver/net/url_request_context_getter.h" |
29 #include "chrome/test/chromedriver/server/http_response.h" | 27 #include "chrome/test/chromedriver/server/http_response.h" |
30 #include "chrome/test/chromedriver/session.h" | 28 #include "chrome/test/chromedriver/session.h" |
31 #include "chrome/test/chromedriver/session_commands.h" | 29 #include "chrome/test/chromedriver/session_thread_map.h" |
32 #include "chrome/test/chromedriver/session_map.h" | |
33 #include "chrome/test/chromedriver/util.h" | 30 #include "chrome/test/chromedriver/util.h" |
34 #include "chrome/test/chromedriver/window_commands.h" | |
35 #include "net/server/http_server_request_info.h" | 31 #include "net/server/http_server_request_info.h" |
36 | 32 |
37 #if defined(OS_MACOSX) | 33 #if defined(OS_MACOSX) |
38 #include "base/mac/scoped_nsautorelease_pool.h" | 34 #include "base/mac/scoped_nsautorelease_pool.h" |
39 #endif | 35 #endif |
40 | 36 |
41 namespace { | 37 namespace { |
42 | 38 |
43 const char kLocalStorage[] = "localStorage"; | 39 const char kLocalStorage[] = "localStorage"; |
44 const char kSessionStorage[] = "sessionStorage"; | 40 const char kSessionStorage[] = "sessionStorage"; |
45 const char kShutdownPath[] = "shutdown"; | 41 const char kShutdownPath[] = "shutdown"; |
46 | 42 |
47 Status UnimplementedCommand( | 43 void UnimplementedCommand( |
48 const base::DictionaryValue& params, | 44 const base::DictionaryValue& params, |
49 const std::string& session_id, | 45 const std::string& session_id, |
50 scoped_ptr<base::Value>* value, | 46 const CommandCallback& callback) { |
51 std::string* out_session_id) { | 47 callback.Run(Status(kUnknownCommand), scoped_ptr<base::Value>(), session_id); |
52 return Status(kUnknownCommand); | |
53 } | 48 } |
54 | 49 |
55 } // namespace | 50 } // namespace |
56 | 51 |
57 CommandMapping::CommandMapping(HttpMethod method, | 52 CommandMapping::CommandMapping(HttpMethod method, |
58 const std::string& path_pattern, | 53 const std::string& path_pattern, |
59 const Command& command) | 54 const Command& command) |
60 : method(method), path_pattern(path_pattern), command(command) {} | 55 : method(method), path_pattern(path_pattern), command(command) {} |
61 | 56 |
62 CommandMapping::~CommandMapping() {} | 57 CommandMapping::~CommandMapping() {} |
63 | 58 |
64 HttpHandler::HttpHandler(Log* log, const std::string& url_base) | 59 HttpHandler::HttpHandler(Log* log, const std::string& url_base) |
65 : log_(log), | 60 : log_(log), |
66 io_thread_("ChromeDriver IO"), | 61 url_base_(url_base), |
67 url_base_(url_base) { | 62 received_shutdown_(false), |
| 63 command_map_(new CommandMap()), |
| 64 weak_ptr_factory_(this) {} |
| 65 |
| 66 HttpHandler::HttpHandler( |
| 67 const scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
| 68 Log* log, |
| 69 const std::string& url_base) |
| 70 : log_(log), |
| 71 url_base_(url_base), |
| 72 received_shutdown_(false), |
| 73 weak_ptr_factory_(this) { |
68 #if defined(OS_MACOSX) | 74 #if defined(OS_MACOSX) |
69 base::mac::ScopedNSAutoreleasePool autorelease_pool; | 75 base::mac::ScopedNSAutoreleasePool autorelease_pool; |
70 #endif | 76 #endif |
71 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); | 77 context_getter_ = new URLRequestContextGetter(io_task_runner); |
72 CHECK(io_thread_.StartWithOptions(options)); | |
73 context_getter_ = new URLRequestContextGetter( | |
74 io_thread_.message_loop_proxy()); | |
75 socket_factory_ = CreateSyncWebSocketFactory(context_getter_.get()); | 78 socket_factory_ = CreateSyncWebSocketFactory(context_getter_.get()); |
76 adb_.reset(new AdbImpl(io_thread_.message_loop_proxy(), log_)); | 79 adb_.reset(new AdbImpl(io_task_runner, log_)); |
77 device_manager_.reset(new DeviceManager(adb_.get())); | 80 device_manager_.reset(new DeviceManager(adb_.get())); |
78 | 81 |
79 CommandMapping commands[] = { | 82 CommandMapping commands[] = { |
80 CommandMapping(kPost, internal::kNewSessionPathPattern, | 83 CommandMapping(kPost, |
| 84 internal::kNewSessionPathPattern, |
81 base::Bind(&ExecuteNewSession, | 85 base::Bind(&ExecuteNewSession, |
82 NewSessionParams(log_, &session_map_, | 86 NewSessionParams(log_, |
83 context_getter_, socket_factory_, | 87 &session_thread_map_, |
84 device_manager_.get()))), | 88 context_getter_, |
85 CommandMapping(kGet, "session/:sessionId", | 89 socket_factory_, |
86 WrapToCommand( | 90 device_manager_.get()))), |
87 base::Bind(&ExecuteGetSessionCapabilities, | 91 CommandMapping(kGet, |
88 &session_map_))), | 92 "session/:sessionId", |
89 CommandMapping(kDelete, "session/:sessionId", | 93 WrapToCommand(base::Bind(&ExecuteGetSessionCapabilities))), |
90 base::Bind(&ExecuteQuit, false, &session_map_)), | 94 CommandMapping(kDelete, |
91 CommandMapping(kGet, "session/:sessionId/window_handle", | 95 "session/:sessionId", |
| 96 base::Bind(&ExecuteSessionCommand, |
| 97 &session_thread_map_, |
| 98 base::Bind(&ExecuteQuit, false), |
| 99 true)), |
| 100 CommandMapping(kGet, |
| 101 "session/:sessionId/window_handle", |
92 WrapToCommand(base::Bind(&ExecuteGetCurrentWindowHandle))), | 102 WrapToCommand(base::Bind(&ExecuteGetCurrentWindowHandle))), |
93 CommandMapping(kGet, "session/:sessionId/window_handles", | 103 CommandMapping(kGet, |
| 104 "session/:sessionId/window_handles", |
94 WrapToCommand(base::Bind(&ExecuteGetWindowHandles))), | 105 WrapToCommand(base::Bind(&ExecuteGetWindowHandles))), |
95 CommandMapping(kPost, "session/:sessionId/url", | 106 CommandMapping(kPost, |
| 107 "session/:sessionId/url", |
96 WrapToCommand(base::Bind(&ExecuteGet))), | 108 WrapToCommand(base::Bind(&ExecuteGet))), |
97 CommandMapping(kGet, "session/:sessionId/alert", | 109 CommandMapping(kGet, |
98 WrapToCommand( | 110 "session/:sessionId/alert", |
99 base::Bind(&ExecuteAlertCommand, | 111 WrapToCommand(base::Bind(&ExecuteAlertCommand, |
100 base::Bind(&ExecuteGetAlert)))), | 112 base::Bind(&ExecuteGetAlert)))), |
101 CommandMapping(kPost, "session/:sessionId/dismiss_alert", | 113 CommandMapping( |
102 WrapToCommand( | 114 kPost, |
103 base::Bind(&ExecuteAlertCommand, | 115 "session/:sessionId/dismiss_alert", |
104 base::Bind(&ExecuteDismissAlert)))), | 116 WrapToCommand(base::Bind(&ExecuteAlertCommand, |
105 CommandMapping(kPost, "session/:sessionId/accept_alert", | 117 base::Bind(&ExecuteDismissAlert)))), |
106 WrapToCommand( | 118 CommandMapping( |
107 base::Bind(&ExecuteAlertCommand, | 119 kPost, |
108 base::Bind(&ExecuteAcceptAlert)))), | 120 "session/:sessionId/accept_alert", |
109 CommandMapping(kGet, "session/:sessionId/alert_text", | 121 WrapToCommand(base::Bind(&ExecuteAlertCommand, |
110 WrapToCommand( | 122 base::Bind(&ExecuteAcceptAlert)))), |
111 base::Bind(&ExecuteAlertCommand, | 123 CommandMapping( |
112 base::Bind(&ExecuteGetAlertText)))), | 124 kGet, |
113 CommandMapping(kPost, "session/:sessionId/alert_text", | 125 "session/:sessionId/alert_text", |
114 WrapToCommand( | 126 WrapToCommand(base::Bind(&ExecuteAlertCommand, |
115 base::Bind(&ExecuteAlertCommand, | 127 base::Bind(&ExecuteGetAlertText)))), |
116 base::Bind(&ExecuteSetAlertValue)))), | 128 CommandMapping( |
117 CommandMapping(kPost, "session/:sessionId/forward", | 129 kPost, |
| 130 "session/:sessionId/alert_text", |
| 131 WrapToCommand(base::Bind(&ExecuteAlertCommand, |
| 132 base::Bind(&ExecuteSetAlertValue)))), |
| 133 CommandMapping(kPost, |
| 134 "session/:sessionId/forward", |
118 WrapToCommand(base::Bind(&ExecuteGoForward))), | 135 WrapToCommand(base::Bind(&ExecuteGoForward))), |
119 CommandMapping(kPost, "session/:sessionId/back", | 136 CommandMapping(kPost, |
| 137 "session/:sessionId/back", |
120 WrapToCommand(base::Bind(&ExecuteGoBack))), | 138 WrapToCommand(base::Bind(&ExecuteGoBack))), |
121 CommandMapping(kPost, "session/:sessionId/refresh", | 139 CommandMapping(kPost, |
| 140 "session/:sessionId/refresh", |
122 WrapToCommand(base::Bind(&ExecuteRefresh))), | 141 WrapToCommand(base::Bind(&ExecuteRefresh))), |
123 CommandMapping(kPost, "session/:sessionId/execute", | 142 CommandMapping(kPost, |
| 143 "session/:sessionId/execute", |
124 WrapToCommand(base::Bind(&ExecuteExecuteScript))), | 144 WrapToCommand(base::Bind(&ExecuteExecuteScript))), |
125 CommandMapping(kPost, "session/:sessionId/execute_async", | 145 CommandMapping(kPost, |
| 146 "session/:sessionId/execute_async", |
126 WrapToCommand(base::Bind(&ExecuteExecuteAsyncScript))), | 147 WrapToCommand(base::Bind(&ExecuteExecuteAsyncScript))), |
127 CommandMapping(kGet, "session/:sessionId/url", | 148 CommandMapping(kGet, |
| 149 "session/:sessionId/url", |
128 WrapToCommand(base::Bind(&ExecuteGetCurrentUrl))), | 150 WrapToCommand(base::Bind(&ExecuteGetCurrentUrl))), |
129 CommandMapping(kGet, "session/:sessionId/title", | 151 CommandMapping(kGet, |
| 152 "session/:sessionId/title", |
130 WrapToCommand(base::Bind(&ExecuteGetTitle))), | 153 WrapToCommand(base::Bind(&ExecuteGetTitle))), |
131 CommandMapping(kGet, "session/:sessionId/source", | 154 CommandMapping(kGet, |
| 155 "session/:sessionId/source", |
132 WrapToCommand(base::Bind(&ExecuteGetPageSource))), | 156 WrapToCommand(base::Bind(&ExecuteGetPageSource))), |
133 CommandMapping(kGet, "session/:sessionId/screenshot", | 157 CommandMapping(kGet, |
| 158 "session/:sessionId/screenshot", |
134 WrapToCommand(base::Bind(&ExecuteScreenshot))), | 159 WrapToCommand(base::Bind(&ExecuteScreenshot))), |
135 CommandMapping(kPost, "session/:sessionId/visible", | 160 CommandMapping(kPost, |
136 base::Bind(&UnimplementedCommand)), | 161 "session/:sessionId/visible", |
137 CommandMapping(kGet, "session/:sessionId/visible", | 162 base::Bind(&UnimplementedCommand)), |
138 base::Bind(&UnimplementedCommand)), | 163 CommandMapping(kGet, |
139 CommandMapping(kPost, "session/:sessionId/element", | 164 "session/:sessionId/visible", |
| 165 base::Bind(&UnimplementedCommand)), |
| 166 CommandMapping(kPost, |
| 167 "session/:sessionId/element", |
140 WrapToCommand(base::Bind(&ExecuteFindElement, 50))), | 168 WrapToCommand(base::Bind(&ExecuteFindElement, 50))), |
141 CommandMapping(kPost, "session/:sessionId/elements", | 169 CommandMapping(kPost, |
| 170 "session/:sessionId/elements", |
142 WrapToCommand(base::Bind(&ExecuteFindElements, 50))), | 171 WrapToCommand(base::Bind(&ExecuteFindElements, 50))), |
143 CommandMapping(kPost, "session/:sessionId/element/active", | 172 CommandMapping(kPost, |
| 173 "session/:sessionId/element/active", |
144 WrapToCommand(base::Bind(&ExecuteGetActiveElement))), | 174 WrapToCommand(base::Bind(&ExecuteGetActiveElement))), |
145 CommandMapping(kPost, "session/:sessionId/element/:id/element", | 175 CommandMapping(kPost, |
| 176 "session/:sessionId/element/:id/element", |
146 WrapToCommand(base::Bind(&ExecuteFindChildElement, 50))), | 177 WrapToCommand(base::Bind(&ExecuteFindChildElement, 50))), |
147 CommandMapping(kPost, "session/:sessionId/element/:id/elements", | 178 CommandMapping(kPost, |
| 179 "session/:sessionId/element/:id/elements", |
148 WrapToCommand(base::Bind(&ExecuteFindChildElements, 50))), | 180 WrapToCommand(base::Bind(&ExecuteFindChildElements, 50))), |
149 CommandMapping(kPost, "session/:sessionId/element/:id/click", | 181 CommandMapping(kPost, |
| 182 "session/:sessionId/element/:id/click", |
150 WrapToCommand(base::Bind(&ExecuteClickElement))), | 183 WrapToCommand(base::Bind(&ExecuteClickElement))), |
151 CommandMapping(kPost, "session/:sessionId/element/:id/clear", | 184 CommandMapping(kPost, |
| 185 "session/:sessionId/element/:id/clear", |
152 WrapToCommand(base::Bind(&ExecuteClearElement))), | 186 WrapToCommand(base::Bind(&ExecuteClearElement))), |
153 CommandMapping(kPost, "session/:sessionId/element/:id/submit", | 187 CommandMapping(kPost, |
| 188 "session/:sessionId/element/:id/submit", |
154 WrapToCommand(base::Bind(&ExecuteSubmitElement))), | 189 WrapToCommand(base::Bind(&ExecuteSubmitElement))), |
155 CommandMapping(kGet, "session/:sessionId/element/:id/text", | 190 CommandMapping(kGet, |
| 191 "session/:sessionId/element/:id/text", |
156 WrapToCommand(base::Bind(&ExecuteGetElementText))), | 192 WrapToCommand(base::Bind(&ExecuteGetElementText))), |
157 CommandMapping(kPost, "session/:sessionId/element/:id/value", | 193 CommandMapping(kPost, |
| 194 "session/:sessionId/element/:id/value", |
158 WrapToCommand(base::Bind(&ExecuteSendKeysToElement))), | 195 WrapToCommand(base::Bind(&ExecuteSendKeysToElement))), |
159 CommandMapping(kPost, "session/:sessionId/file", | 196 CommandMapping(kPost, |
| 197 "session/:sessionId/file", |
160 WrapToCommand(base::Bind(&ExecuteUploadFile))), | 198 WrapToCommand(base::Bind(&ExecuteUploadFile))), |
161 CommandMapping(kGet, "session/:sessionId/element/:id/value", | 199 CommandMapping(kGet, |
| 200 "session/:sessionId/element/:id/value", |
162 WrapToCommand(base::Bind(&ExecuteGetElementValue))), | 201 WrapToCommand(base::Bind(&ExecuteGetElementValue))), |
163 CommandMapping(kGet, "session/:sessionId/element/:id/name", | 202 CommandMapping(kGet, |
| 203 "session/:sessionId/element/:id/name", |
164 WrapToCommand(base::Bind(&ExecuteGetElementTagName))), | 204 WrapToCommand(base::Bind(&ExecuteGetElementTagName))), |
165 CommandMapping(kGet, "session/:sessionId/element/:id/selected", | 205 CommandMapping(kGet, |
| 206 "session/:sessionId/element/:id/selected", |
166 WrapToCommand(base::Bind(&ExecuteIsElementSelected))), | 207 WrapToCommand(base::Bind(&ExecuteIsElementSelected))), |
167 CommandMapping(kGet, "session/:sessionId/element/:id/enabled", | 208 CommandMapping(kGet, |
| 209 "session/:sessionId/element/:id/enabled", |
168 WrapToCommand(base::Bind(&ExecuteIsElementEnabled))), | 210 WrapToCommand(base::Bind(&ExecuteIsElementEnabled))), |
169 CommandMapping(kGet, "session/:sessionId/element/:id/displayed", | 211 CommandMapping(kGet, |
| 212 "session/:sessionId/element/:id/displayed", |
170 WrapToCommand(base::Bind(&ExecuteIsElementDisplayed))), | 213 WrapToCommand(base::Bind(&ExecuteIsElementDisplayed))), |
171 CommandMapping(kPost, "session/:sessionId/element/:id/hover", | 214 CommandMapping(kPost, |
| 215 "session/:sessionId/element/:id/hover", |
172 WrapToCommand(base::Bind(&ExecuteHoverOverElement))), | 216 WrapToCommand(base::Bind(&ExecuteHoverOverElement))), |
173 CommandMapping(kGet, "session/:sessionId/element/:id/location", | 217 CommandMapping(kGet, |
| 218 "session/:sessionId/element/:id/location", |
174 WrapToCommand(base::Bind(&ExecuteGetElementLocation))), | 219 WrapToCommand(base::Bind(&ExecuteGetElementLocation))), |
175 CommandMapping(kGet, "session/:sessionId/element/:id/location_in_view", | 220 CommandMapping(kGet, |
176 WrapToCommand( | 221 "session/:sessionId/element/:id/location_in_view", |
177 base::Bind( | 222 WrapToCommand(base::Bind( |
178 &ExecuteGetElementLocationOnceScrolledIntoView))), | 223 &ExecuteGetElementLocationOnceScrolledIntoView))), |
179 CommandMapping(kGet, "session/:sessionId/element/:id/size", | 224 CommandMapping(kGet, |
| 225 "session/:sessionId/element/:id/size", |
180 WrapToCommand(base::Bind(&ExecuteGetElementSize))), | 226 WrapToCommand(base::Bind(&ExecuteGetElementSize))), |
181 CommandMapping(kGet, "session/:sessionId/element/:id/attribute/:name", | 227 CommandMapping(kGet, |
| 228 "session/:sessionId/element/:id/attribute/:name", |
182 WrapToCommand(base::Bind(&ExecuteGetElementAttribute))), | 229 WrapToCommand(base::Bind(&ExecuteGetElementAttribute))), |
183 CommandMapping(kGet, "session/:sessionId/element/:id/equals/:other", | 230 CommandMapping(kGet, |
| 231 "session/:sessionId/element/:id/equals/:other", |
184 WrapToCommand(base::Bind(&ExecuteElementEquals))), | 232 WrapToCommand(base::Bind(&ExecuteElementEquals))), |
185 CommandMapping(kGet, "session/:sessionId/cookie", | 233 CommandMapping(kGet, |
| 234 "session/:sessionId/cookie", |
186 WrapToCommand(base::Bind(&ExecuteGetCookies))), | 235 WrapToCommand(base::Bind(&ExecuteGetCookies))), |
187 CommandMapping(kPost, "session/:sessionId/cookie", | 236 CommandMapping(kPost, |
| 237 "session/:sessionId/cookie", |
188 WrapToCommand(base::Bind(&ExecuteAddCookie))), | 238 WrapToCommand(base::Bind(&ExecuteAddCookie))), |
189 CommandMapping(kDelete, "session/:sessionId/cookie", | 239 CommandMapping(kDelete, |
| 240 "session/:sessionId/cookie", |
190 WrapToCommand(base::Bind(&ExecuteDeleteAllCookies))), | 241 WrapToCommand(base::Bind(&ExecuteDeleteAllCookies))), |
191 CommandMapping(kDelete, "session/:sessionId/cookie/:name", | 242 CommandMapping(kDelete, |
| 243 "session/:sessionId/cookie/:name", |
192 WrapToCommand(base::Bind(&ExecuteDeleteCookie))), | 244 WrapToCommand(base::Bind(&ExecuteDeleteCookie))), |
193 CommandMapping(kPost, "session/:sessionId/frame", | 245 CommandMapping(kPost, |
| 246 "session/:sessionId/frame", |
194 WrapToCommand(base::Bind(&ExecuteSwitchToFrame))), | 247 WrapToCommand(base::Bind(&ExecuteSwitchToFrame))), |
195 CommandMapping(kPost, "session/:sessionId/window", | 248 CommandMapping(kPost, |
| 249 "session/:sessionId/window", |
196 WrapToCommand(base::Bind(&ExecuteSwitchToWindow))), | 250 WrapToCommand(base::Bind(&ExecuteSwitchToWindow))), |
197 CommandMapping(kGet, "session/:sessionId/window/:windowHandle/size", | 251 CommandMapping(kGet, |
| 252 "session/:sessionId/window/:windowHandle/size", |
198 WrapToCommand(base::Bind(&ExecuteGetWindowSize))), | 253 WrapToCommand(base::Bind(&ExecuteGetWindowSize))), |
199 CommandMapping(kGet, "session/:sessionId/window/:windowHandle/position", | 254 CommandMapping(kGet, |
| 255 "session/:sessionId/window/:windowHandle/position", |
200 WrapToCommand(base::Bind(&ExecuteGetWindowPosition))), | 256 WrapToCommand(base::Bind(&ExecuteGetWindowPosition))), |
201 CommandMapping(kPost, "session/:sessionId/window/:windowHandle/size", | 257 CommandMapping(kPost, |
| 258 "session/:sessionId/window/:windowHandle/size", |
202 WrapToCommand(base::Bind(&ExecuteSetWindowSize))), | 259 WrapToCommand(base::Bind(&ExecuteSetWindowSize))), |
203 CommandMapping(kPost, "session/:sessionId/window/:windowHandle/position", | 260 CommandMapping(kPost, |
| 261 "session/:sessionId/window/:windowHandle/position", |
204 WrapToCommand(base::Bind(&ExecuteSetWindowPosition))), | 262 WrapToCommand(base::Bind(&ExecuteSetWindowPosition))), |
205 CommandMapping(kPost, "session/:sessionId/window/:windowHandle/maximize", | 263 CommandMapping(kPost, |
| 264 "session/:sessionId/window/:windowHandle/maximize", |
206 WrapToCommand(base::Bind(&ExecuteMaximizeWindow))), | 265 WrapToCommand(base::Bind(&ExecuteMaximizeWindow))), |
207 CommandMapping(kDelete, "session/:sessionId/window", | 266 CommandMapping(kDelete, |
208 WrapToCommand(base::Bind(&ExecuteClose, &session_map_))), | 267 "session/:sessionId/window", |
209 CommandMapping(kPost, "session/:sessionId/element/:id/drag", | 268 WrapToCommand(base::Bind(&ExecuteClose))), |
210 base::Bind(&UnimplementedCommand)), | 269 CommandMapping(kPost, |
211 CommandMapping(kGet, "session/:sessionId/element/:id/css/:propertyName", | 270 "session/:sessionId/element/:id/drag", |
212 WrapToCommand( | 271 base::Bind(&UnimplementedCommand)), |
213 base::Bind(&ExecuteGetElementValueOfCSSProperty))), | 272 CommandMapping( |
214 CommandMapping(kPost, "session/:sessionId/timeouts/implicit_wait", | 273 kGet, |
| 274 "session/:sessionId/element/:id/css/:propertyName", |
| 275 WrapToCommand(base::Bind(&ExecuteGetElementValueOfCSSProperty))), |
| 276 CommandMapping(kPost, |
| 277 "session/:sessionId/timeouts/implicit_wait", |
215 WrapToCommand(base::Bind(&ExecuteImplicitlyWait))), | 278 WrapToCommand(base::Bind(&ExecuteImplicitlyWait))), |
216 CommandMapping(kPost, "session/:sessionId/timeouts/async_script", | 279 CommandMapping(kPost, |
| 280 "session/:sessionId/timeouts/async_script", |
217 WrapToCommand(base::Bind(&ExecuteSetScriptTimeout))), | 281 WrapToCommand(base::Bind(&ExecuteSetScriptTimeout))), |
218 CommandMapping(kPost, "session/:sessionId/timeouts", | 282 CommandMapping(kPost, |
| 283 "session/:sessionId/timeouts", |
219 WrapToCommand(base::Bind(&ExecuteSetTimeout))), | 284 WrapToCommand(base::Bind(&ExecuteSetTimeout))), |
220 CommandMapping(kPost, "session/:sessionId/execute_sql", | 285 CommandMapping(kPost, |
221 base::Bind(&UnimplementedCommand)), | 286 "session/:sessionId/execute_sql", |
222 CommandMapping(kGet, "session/:sessionId/location", | 287 base::Bind(&UnimplementedCommand)), |
| 288 CommandMapping(kGet, |
| 289 "session/:sessionId/location", |
223 WrapToCommand(base::Bind(&ExecuteGetLocation))), | 290 WrapToCommand(base::Bind(&ExecuteGetLocation))), |
224 CommandMapping(kPost, "session/:sessionId/location", | 291 CommandMapping(kPost, |
| 292 "session/:sessionId/location", |
225 WrapToCommand(base::Bind(&ExecuteSetLocation))), | 293 WrapToCommand(base::Bind(&ExecuteSetLocation))), |
226 CommandMapping(kGet, "session/:sessionId/application_cache/status", | 294 CommandMapping(kGet, |
| 295 "session/:sessionId/application_cache/status", |
227 base::Bind(&ExecuteGetStatus)), | 296 base::Bind(&ExecuteGetStatus)), |
228 CommandMapping(kGet, "session/:sessionId/browser_connection", | 297 CommandMapping(kGet, |
229 base::Bind(&UnimplementedCommand)), | 298 "session/:sessionId/browser_connection", |
230 CommandMapping(kPost, "session/:sessionId/browser_connection", | 299 base::Bind(&UnimplementedCommand)), |
231 base::Bind(&UnimplementedCommand)), | 300 CommandMapping(kPost, |
232 CommandMapping(kGet, "session/:sessionId/local_storage/key/:key", | 301 "session/:sessionId/browser_connection", |
233 WrapToCommand( | 302 base::Bind(&UnimplementedCommand)), |
234 base::Bind(&ExecuteGetStorageItem, kLocalStorage))), | 303 CommandMapping( |
235 CommandMapping(kDelete, "session/:sessionId/local_storage/key/:key", | 304 kGet, |
236 WrapToCommand( | 305 "session/:sessionId/local_storage/key/:key", |
237 base::Bind(&ExecuteRemoveStorageItem, kLocalStorage))), | 306 WrapToCommand(base::Bind(&ExecuteGetStorageItem, kLocalStorage))), |
238 CommandMapping(kGet, "session/:sessionId/local_storage", | 307 CommandMapping( |
239 WrapToCommand( | 308 kDelete, |
240 base::Bind(&ExecuteGetStorageKeys, kLocalStorage))), | 309 "session/:sessionId/local_storage/key/:key", |
241 CommandMapping(kPost, "session/:sessionId/local_storage", | 310 WrapToCommand(base::Bind(&ExecuteRemoveStorageItem, kLocalStorage))), |
242 WrapToCommand( | 311 CommandMapping( |
243 base::Bind(&ExecuteSetStorageItem, kLocalStorage))), | 312 kGet, |
244 CommandMapping(kDelete, "session/:sessionId/local_storage", | 313 "session/:sessionId/local_storage", |
245 WrapToCommand( | 314 WrapToCommand(base::Bind(&ExecuteGetStorageKeys, kLocalStorage))), |
246 base::Bind(&ExecuteClearStorage, kLocalStorage))), | 315 CommandMapping( |
247 CommandMapping(kGet, "session/:sessionId/local_storage/size", | 316 kPost, |
248 WrapToCommand( | 317 "session/:sessionId/local_storage", |
249 base::Bind(&ExecuteGetStorageSize, kLocalStorage))), | 318 WrapToCommand(base::Bind(&ExecuteSetStorageItem, kLocalStorage))), |
250 CommandMapping(kGet, "session/:sessionId/session_storage/key/:key", | 319 CommandMapping( |
251 WrapToCommand( | 320 kDelete, |
252 base::Bind(&ExecuteGetStorageItem, kSessionStorage))), | 321 "session/:sessionId/local_storage", |
253 CommandMapping(kDelete, "session/:sessionId/session_storage/key/:key", | 322 WrapToCommand(base::Bind(&ExecuteClearStorage, kLocalStorage))), |
254 WrapToCommand( | 323 CommandMapping( |
255 base::Bind( | 324 kGet, |
256 &ExecuteRemoveStorageItem, kSessionStorage))), | 325 "session/:sessionId/local_storage/size", |
257 CommandMapping(kGet, "session/:sessionId/session_storage", | 326 WrapToCommand(base::Bind(&ExecuteGetStorageSize, kLocalStorage))), |
258 WrapToCommand( | 327 CommandMapping( |
259 base::Bind(&ExecuteGetStorageKeys, kSessionStorage))), | 328 kGet, |
260 CommandMapping(kPost, "session/:sessionId/session_storage", | 329 "session/:sessionId/session_storage/key/:key", |
261 WrapToCommand( | 330 WrapToCommand(base::Bind(&ExecuteGetStorageItem, kSessionStorage))), |
262 base::Bind(&ExecuteSetStorageItem, kSessionStorage))), | 331 CommandMapping(kDelete, |
263 CommandMapping(kDelete, "session/:sessionId/session_storage", | 332 "session/:sessionId/session_storage/key/:key", |
264 WrapToCommand( | 333 WrapToCommand(base::Bind(&ExecuteRemoveStorageItem, |
265 base::Bind(&ExecuteClearStorage, kSessionStorage))), | 334 kSessionStorage))), |
266 CommandMapping(kGet, "session/:sessionId/session_storage/size", | 335 CommandMapping( |
267 WrapToCommand( | 336 kGet, |
268 base::Bind(&ExecuteGetStorageSize, kSessionStorage))), | 337 "session/:sessionId/session_storage", |
269 CommandMapping(kGet, "session/:sessionId/orientation", | 338 WrapToCommand(base::Bind(&ExecuteGetStorageKeys, kSessionStorage))), |
270 base::Bind(&UnimplementedCommand)), | 339 CommandMapping( |
271 CommandMapping(kPost, "session/:sessionId/orientation", | 340 kPost, |
272 base::Bind(&UnimplementedCommand)), | 341 "session/:sessionId/session_storage", |
273 CommandMapping(kPost, "session/:sessionId/click", | 342 WrapToCommand(base::Bind(&ExecuteSetStorageItem, kSessionStorage))), |
| 343 CommandMapping( |
| 344 kDelete, |
| 345 "session/:sessionId/session_storage", |
| 346 WrapToCommand(base::Bind(&ExecuteClearStorage, kSessionStorage))), |
| 347 CommandMapping( |
| 348 kGet, |
| 349 "session/:sessionId/session_storage/size", |
| 350 WrapToCommand(base::Bind(&ExecuteGetStorageSize, kSessionStorage))), |
| 351 CommandMapping(kGet, |
| 352 "session/:sessionId/orientation", |
| 353 base::Bind(&UnimplementedCommand)), |
| 354 CommandMapping(kPost, |
| 355 "session/:sessionId/orientation", |
| 356 base::Bind(&UnimplementedCommand)), |
| 357 CommandMapping(kPost, |
| 358 "session/:sessionId/click", |
274 WrapToCommand(base::Bind(&ExecuteMouseClick))), | 359 WrapToCommand(base::Bind(&ExecuteMouseClick))), |
275 CommandMapping(kPost, "session/:sessionId/doubleclick", | 360 CommandMapping(kPost, |
| 361 "session/:sessionId/doubleclick", |
276 WrapToCommand(base::Bind(&ExecuteMouseDoubleClick))), | 362 WrapToCommand(base::Bind(&ExecuteMouseDoubleClick))), |
277 CommandMapping(kPost, "session/:sessionId/buttondown", | 363 CommandMapping(kPost, |
| 364 "session/:sessionId/buttondown", |
278 WrapToCommand(base::Bind(&ExecuteMouseButtonDown))), | 365 WrapToCommand(base::Bind(&ExecuteMouseButtonDown))), |
279 CommandMapping(kPost, "session/:sessionId/buttonup", | 366 CommandMapping(kPost, |
| 367 "session/:sessionId/buttonup", |
280 WrapToCommand(base::Bind(&ExecuteMouseButtonUp))), | 368 WrapToCommand(base::Bind(&ExecuteMouseButtonUp))), |
281 CommandMapping(kPost, "session/:sessionId/moveto", | 369 CommandMapping(kPost, |
| 370 "session/:sessionId/moveto", |
282 WrapToCommand(base::Bind(&ExecuteMouseMoveTo))), | 371 WrapToCommand(base::Bind(&ExecuteMouseMoveTo))), |
283 CommandMapping(kPost, "session/:sessionId/keys", | 372 CommandMapping( |
284 WrapToCommand( | 373 kPost, |
285 base::Bind(&ExecuteSendKeysToActiveElement))), | 374 "session/:sessionId/keys", |
286 CommandMapping(kGet, "session/:sessionId/ime/available_engines", | 375 WrapToCommand(base::Bind(&ExecuteSendKeysToActiveElement))), |
287 base::Bind(&UnimplementedCommand)), | 376 CommandMapping(kGet, |
288 CommandMapping(kGet, "session/:sessionId/ime/active_engine", | 377 "session/:sessionId/ime/available_engines", |
289 base::Bind(&UnimplementedCommand)), | 378 base::Bind(&UnimplementedCommand)), |
290 CommandMapping(kGet, "session/:sessionId/ime/activated", | 379 CommandMapping(kGet, |
291 base::Bind(&UnimplementedCommand)), | 380 "session/:sessionId/ime/active_engine", |
292 CommandMapping(kPost, "session/:sessionId/ime/deactivate", | 381 base::Bind(&UnimplementedCommand)), |
293 base::Bind(&UnimplementedCommand)), | 382 CommandMapping(kGet, |
294 CommandMapping(kPost, "session/:sessionId/ime/activate", | 383 "session/:sessionId/ime/activated", |
295 base::Bind(&UnimplementedCommand)), | 384 base::Bind(&UnimplementedCommand)), |
296 CommandMapping(kPost, "session/:sessionId/touch/click", | 385 CommandMapping(kPost, |
| 386 "session/:sessionId/ime/deactivate", |
| 387 base::Bind(&UnimplementedCommand)), |
| 388 CommandMapping(kPost, |
| 389 "session/:sessionId/ime/activate", |
| 390 base::Bind(&UnimplementedCommand)), |
| 391 CommandMapping(kPost, |
| 392 "session/:sessionId/touch/click", |
297 WrapToCommand(base::Bind(&ExecuteTouchSingleTap))), | 393 WrapToCommand(base::Bind(&ExecuteTouchSingleTap))), |
298 CommandMapping(kPost, "session/:sessionId/touch/down", | 394 CommandMapping(kPost, |
299 base::Bind(&UnimplementedCommand)), | 395 "session/:sessionId/touch/down", |
300 CommandMapping(kPost, "session/:sessionId/touch/up", | 396 base::Bind(&UnimplementedCommand)), |
301 base::Bind(&UnimplementedCommand)), | 397 CommandMapping(kPost, |
302 CommandMapping(kPost, "session/:sessionId/touch/move", | 398 "session/:sessionId/touch/up", |
303 base::Bind(&UnimplementedCommand)), | 399 base::Bind(&UnimplementedCommand)), |
304 CommandMapping(kPost, "session/:sessionId/touch/scroll", | 400 CommandMapping(kPost, |
305 base::Bind(&UnimplementedCommand)), | 401 "session/:sessionId/touch/move", |
306 CommandMapping(kPost, "session/:sessionId/touch/doubleclick", | 402 base::Bind(&UnimplementedCommand)), |
307 base::Bind(&UnimplementedCommand)), | 403 CommandMapping(kPost, |
308 CommandMapping(kPost, "session/:sessionId/touch/longclick", | 404 "session/:sessionId/touch/scroll", |
309 base::Bind(&UnimplementedCommand)), | 405 base::Bind(&UnimplementedCommand)), |
310 CommandMapping(kPost, "session/:sessionId/touch/flick", | 406 CommandMapping(kPost, |
311 base::Bind(&UnimplementedCommand)), | 407 "session/:sessionId/touch/doubleclick", |
312 CommandMapping(kPost, "session/:sessionId/log", | 408 base::Bind(&UnimplementedCommand)), |
| 409 CommandMapping(kPost, |
| 410 "session/:sessionId/touch/longclick", |
| 411 base::Bind(&UnimplementedCommand)), |
| 412 CommandMapping(kPost, |
| 413 "session/:sessionId/touch/flick", |
| 414 base::Bind(&UnimplementedCommand)), |
| 415 CommandMapping(kPost, |
| 416 "session/:sessionId/log", |
313 WrapToCommand(base::Bind(&ExecuteGetLog))), | 417 WrapToCommand(base::Bind(&ExecuteGetLog))), |
314 CommandMapping(kGet, "session/:sessionId/log/types", | 418 CommandMapping(kGet, |
| 419 "session/:sessionId/log/types", |
315 WrapToCommand(base::Bind(&ExecuteGetAvailableLogTypes))), | 420 WrapToCommand(base::Bind(&ExecuteGetAvailableLogTypes))), |
316 CommandMapping(kPost, "logs", base::Bind(&UnimplementedCommand)), | 421 CommandMapping(kPost, "logs", base::Bind(&UnimplementedCommand)), |
317 CommandMapping(kGet, "status", base::Bind(&ExecuteGetStatus)), | 422 CommandMapping(kGet, "status", base::Bind(&ExecuteGetStatus)), |
318 | 423 |
319 // Custom Chrome commands: | 424 // Custom Chrome commands: |
320 // Allow quit all to be called with GET or POST. | 425 // Allow quit all to be called with GET or POST. |
321 CommandMapping(kGet, kShutdownPath, | 426 CommandMapping(kGet, |
| 427 kShutdownPath, |
322 base::Bind(&ExecuteQuitAll, | 428 base::Bind(&ExecuteQuitAll, |
323 base::Bind(&ExecuteQuit, true, &session_map_), | 429 WrapToCommand(base::Bind(&ExecuteQuit, true)), |
324 &session_map_)), | 430 &session_thread_map_)), |
325 CommandMapping(kPost, kShutdownPath, | 431 CommandMapping(kPost, |
| 432 kShutdownPath, |
326 base::Bind(&ExecuteQuitAll, | 433 base::Bind(&ExecuteQuitAll, |
327 base::Bind(&ExecuteQuit, true, &session_map_), | 434 WrapToCommand(base::Bind(&ExecuteQuit, true)), |
328 &session_map_)), | 435 &session_thread_map_)), |
329 CommandMapping(kGet, "session/:sessionId/is_loading", | 436 CommandMapping(kGet, |
330 WrapToCommand(base::Bind(&ExecuteIsLoading))), | 437 "session/:sessionId/is_loading", |
331 }; | 438 WrapToCommand(base::Bind(&ExecuteIsLoading))), }; |
332 this->command_map_.reset( | 439 command_map_.reset( |
333 new CommandMap(commands, commands + arraysize(commands))); | 440 new CommandMap(commands, commands + arraysize(commands))); |
334 } | 441 } |
335 | 442 |
336 HttpHandler::~HttpHandler() {} | 443 HttpHandler::~HttpHandler() {} |
337 | 444 |
338 void HttpHandler::Handle(const net::HttpServerRequestInfo& request, | 445 void HttpHandler::Handle(const net::HttpServerRequestInfo& request, |
339 HttpResponse* response) { | 446 const HttpResponseSenderFunc& send_response_func) { |
340 log_->AddEntry( | 447 CHECK(thread_checker_.CalledOnValidThread()); |
341 Log::kLog, | 448 |
342 base::StringPrintf("received WebDriver request: %s %s %s", | 449 if (received_shutdown_) |
343 request.method.c_str(), | 450 return; |
344 request.path.c_str(), | 451 if (ShouldShutdown(request)) |
345 request.data.c_str())); | 452 received_shutdown_ = true; |
346 | 453 |
347 HandleInternal(request, response); | 454 std::string path = request.path; |
348 | 455 if (!StartsWithASCII(path, url_base_, true)) { |
349 log_->AddEntry( | 456 scoped_ptr<HttpResponse> response( |
350 Log::kLog, | 457 new HttpResponse(HttpResponse::kBadRequest)); |
351 base::StringPrintf("sending WebDriver response: %d %s", | 458 response->set_body("unhandled request"); |
352 response->status(), | 459 send_response_func.Run(response.Pass()); |
353 response->body().c_str())); | 460 return; |
| 461 } |
| 462 |
| 463 path.erase(0, url_base_.length()); |
| 464 |
| 465 HandleCommand(request, path, send_response_func); |
354 } | 466 } |
355 | 467 |
356 bool HttpHandler::ShouldShutdown(const net::HttpServerRequestInfo& request) { | 468 bool HttpHandler::ShouldShutdown(const net::HttpServerRequestInfo& request) { |
357 return request.path == url_base_ + kShutdownPath; | 469 return request.path == url_base_ + kShutdownPath; |
358 } | 470 } |
359 | 471 |
360 Command HttpHandler::WrapToCommand( | 472 Command HttpHandler::WrapToCommand( |
361 const SessionCommand& session_command) { | 473 const SessionCommand& session_command) { |
362 return base::Bind(&ExecuteSessionCommand, &session_map_, session_command); | 474 return base::Bind( |
| 475 &ExecuteSessionCommand, &session_thread_map_, session_command, false); |
363 } | 476 } |
364 | 477 |
365 Command HttpHandler::WrapToCommand( | 478 Command HttpHandler::WrapToCommand( |
366 const WindowCommand& window_command) { | 479 const WindowCommand& window_command) { |
367 return WrapToCommand( | 480 return WrapToCommand( |
368 base::Bind(&ExecuteWindowCommand, window_command)); | 481 base::Bind(&ExecuteWindowCommand, window_command)); |
369 } | 482 } |
370 | 483 |
371 Command HttpHandler::WrapToCommand( | 484 Command HttpHandler::WrapToCommand( |
372 const ElementCommand& element_command) { | 485 const ElementCommand& element_command) { |
373 return WrapToCommand( | 486 return WrapToCommand( |
374 base::Bind(&ExecuteElementCommand, element_command)); | 487 base::Bind(&ExecuteElementCommand, element_command)); |
375 } | 488 } |
376 | 489 |
377 void HttpHandler::HandleInternal(const net::HttpServerRequestInfo& request, | 490 void HttpHandler::HandleCommand( |
378 HttpResponse* response) { | |
379 std::string path = request.path; | |
380 if (!StartsWithASCII(path, url_base_, true)) { | |
381 *response = HttpResponse(HttpResponse::kBadRequest); | |
382 response->set_body("unhandled request"); | |
383 return; | |
384 } | |
385 | |
386 path.erase(0, url_base_.length()); | |
387 | |
388 if (!HandleWebDriverCommand(request, path, response)) { | |
389 *response = HttpResponse(HttpResponse::kNotFound); | |
390 response->set_body("unknown command: " + path); | |
391 return; | |
392 } | |
393 } | |
394 | |
395 bool HttpHandler::HandleWebDriverCommand( | |
396 const net::HttpServerRequestInfo& request, | 491 const net::HttpServerRequestInfo& request, |
397 const std::string& trimmed_path, | 492 const std::string& trimmed_path, |
398 HttpResponse* response) { | 493 const HttpResponseSenderFunc& send_response_func) { |
| 494 log_->AddEntry(Log::kLog, |
| 495 base::StringPrintf("handling command: %s %s %s", |
| 496 request.method.c_str(), |
| 497 trimmed_path.c_str(), |
| 498 request.data.c_str())); |
| 499 |
399 base::DictionaryValue params; | 500 base::DictionaryValue params; |
400 std::string session_id; | 501 std::string session_id; |
401 CommandMap::const_iterator iter = command_map_->begin(); | 502 CommandMap::const_iterator iter = command_map_->begin(); |
402 while (true) { | 503 while (true) { |
403 if (iter == command_map_->end()) { | 504 if (iter == command_map_->end()) { |
404 return false; | 505 scoped_ptr<HttpResponse> response( |
| 506 new HttpResponse(HttpResponse::kNotFound)); |
| 507 response->set_body("unknown command: " + trimmed_path); |
| 508 send_response_func.Run(response.Pass()); |
| 509 return; |
405 } | 510 } |
406 if (internal::MatchesCommand( | 511 if (internal::MatchesCommand( |
407 request.method, trimmed_path, *iter, &session_id, ¶ms)) { | 512 request.method, trimmed_path, *iter, &session_id, ¶ms)) { |
408 break; | 513 break; |
409 } | 514 } |
410 ++iter; | 515 ++iter; |
411 } | 516 } |
412 | 517 |
413 if (request.data.length()) { | 518 if (request.data.length()) { |
414 base::DictionaryValue* body_params; | 519 base::DictionaryValue* body_params; |
415 scoped_ptr<base::Value> parsed_body(base::JSONReader::Read(request.data)); | 520 scoped_ptr<base::Value> parsed_body(base::JSONReader::Read(request.data)); |
416 if (!parsed_body || !parsed_body->GetAsDictionary(&body_params)) { | 521 if (!parsed_body || !parsed_body->GetAsDictionary(&body_params)) { |
417 *response = HttpResponse(HttpResponse::kBadRequest); | 522 scoped_ptr<HttpResponse> response( |
| 523 new HttpResponse(HttpResponse::kBadRequest)); |
418 response->set_body("missing command parameters"); | 524 response->set_body("missing command parameters"); |
419 return true; | 525 send_response_func.Run(response.Pass()); |
| 526 return; |
420 } | 527 } |
421 params.MergeDictionary(body_params); | 528 params.MergeDictionary(body_params); |
422 } | 529 } |
423 | 530 |
424 scoped_ptr<base::Value> value; | 531 iter->command.Run(params, |
425 std::string out_session_id; | 532 session_id, |
426 Status status = iter->command.Run( | 533 base::Bind(&HttpHandler::PrepareResponse, |
427 params, session_id, &value, &out_session_id); | 534 weak_ptr_factory_.GetWeakPtr(), |
| 535 trimmed_path, |
| 536 send_response_func)); |
| 537 } |
428 | 538 |
| 539 void HttpHandler::PrepareResponse( |
| 540 const std::string& trimmed_path, |
| 541 const HttpResponseSenderFunc& send_response_func, |
| 542 const Status& status, |
| 543 scoped_ptr<base::Value> value, |
| 544 const std::string& session_id) { |
| 545 CHECK(thread_checker_.CalledOnValidThread()); |
| 546 scoped_ptr<HttpResponse> response = |
| 547 PrepareResponseHelper(trimmed_path, status, value.Pass(), session_id); |
| 548 log_->AddEntry(Log::kLog, |
| 549 base::StringPrintf("sending response: %d %s", |
| 550 response->status(), |
| 551 response->body().c_str())); |
| 552 send_response_func.Run(response.Pass()); |
| 553 } |
| 554 |
| 555 scoped_ptr<HttpResponse> HttpHandler::PrepareResponseHelper( |
| 556 const std::string& trimmed_path, |
| 557 const Status& status, |
| 558 scoped_ptr<base::Value> value, |
| 559 const std::string& session_id) { |
429 if (status.code() == kUnknownCommand) { | 560 if (status.code() == kUnknownCommand) { |
430 *response = HttpResponse(HttpResponse::kNotImplemented); | 561 scoped_ptr<HttpResponse> response( |
| 562 new HttpResponse(HttpResponse::kNotImplemented)); |
431 response->set_body("unimplemented command: " + trimmed_path); | 563 response->set_body("unimplemented command: " + trimmed_path); |
432 return true; | 564 return response.Pass(); |
433 } | 565 } |
434 | 566 |
435 if (iter->path_pattern == internal::kNewSessionPathPattern && status.IsOk()) { | 567 if (trimmed_path == internal::kNewSessionPathPattern && status.IsOk()) { |
436 // Creating a session involves a HTTP request to /session, which is | 568 // Creating a session involves a HTTP request to /session, which is |
437 // supposed to redirect to /session/:sessionId, which returns the | 569 // supposed to redirect to /session/:sessionId, which returns the |
438 // session info. | 570 // session info. |
439 *response = HttpResponse(HttpResponse::kSeeOther); | 571 scoped_ptr<HttpResponse> response( |
440 response->AddHeader("Location", url_base_ + "session/" + out_session_id); | 572 new HttpResponse(HttpResponse::kSeeOther)); |
441 return true; | 573 response->AddHeader("Location", url_base_ + "session/" + session_id); |
| 574 return response.Pass(); |
442 } else if (status.IsError()) { | 575 } else if (status.IsError()) { |
443 status.AddDetails(base::StringPrintf( | 576 Status full_status(status); |
| 577 full_status.AddDetails(base::StringPrintf( |
444 "Driver info: chromedriver=%s,platform=%s %s %s", | 578 "Driver info: chromedriver=%s,platform=%s %s %s", |
445 kChromeDriverVersion, | 579 kChromeDriverVersion, |
446 base::SysInfo::OperatingSystemName().c_str(), | 580 base::SysInfo::OperatingSystemName().c_str(), |
447 base::SysInfo::OperatingSystemVersion().c_str(), | 581 base::SysInfo::OperatingSystemVersion().c_str(), |
448 base::SysInfo::OperatingSystemArchitecture().c_str())); | 582 base::SysInfo::OperatingSystemArchitecture().c_str())); |
449 scoped_ptr<base::DictionaryValue> error(new base::DictionaryValue()); | 583 scoped_ptr<base::DictionaryValue> error(new base::DictionaryValue()); |
450 error->SetString("message", status.message()); | 584 error->SetString("message", full_status.message()); |
451 value.reset(error.release()); | 585 value.reset(error.release()); |
452 } | 586 } |
453 if (!value) | 587 if (!value) |
454 value.reset(base::Value::CreateNullValue()); | 588 value.reset(base::Value::CreateNullValue()); |
455 | 589 |
456 base::DictionaryValue body_params; | 590 base::DictionaryValue body_params; |
457 body_params.SetInteger("status", status.code()); | 591 body_params.SetInteger("status", status.code()); |
458 body_params.Set("value", value.release()); | 592 body_params.Set("value", value.release()); |
459 body_params.SetString("sessionId", out_session_id); | 593 body_params.SetString("sessionId", session_id); |
460 std::string body; | 594 std::string body; |
461 base::JSONWriter::WriteWithOptions( | 595 base::JSONWriter::WriteWithOptions( |
462 &body_params, base::JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION, | 596 &body_params, base::JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION, |
463 &body); | 597 &body); |
464 *response = HttpResponse(HttpResponse::kOk); | 598 scoped_ptr<HttpResponse> response(new HttpResponse(HttpResponse::kOk)); |
465 response->SetMimeType("application/json; charset=utf-8"); | 599 response->SetMimeType("application/json; charset=utf-8"); |
466 response->set_body(body); | 600 response->set_body(body); |
467 return true; | 601 return response.Pass(); |
468 } | 602 } |
469 | 603 |
470 namespace internal { | 604 namespace internal { |
471 | 605 |
472 const char kNewSessionPathPattern[] = "session"; | 606 const char kNewSessionPathPattern[] = "session"; |
473 | 607 |
474 bool MatchesMethod(HttpMethod command_method, const std::string& method) { | 608 bool MatchesMethod(HttpMethod command_method, const std::string& method) { |
475 std::string lower_method = StringToLowerASCII(method); | 609 std::string lower_method = StringToLowerASCII(method); |
476 switch (command_method) { | 610 switch (command_method) { |
477 case kGet: | 611 case kGet: |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 params.SetString(name, path_parts[i]); | 646 params.SetString(name, path_parts[i]); |
513 } else if (command_path_parts[i] != path_parts[i]) { | 647 } else if (command_path_parts[i] != path_parts[i]) { |
514 return false; | 648 return false; |
515 } | 649 } |
516 } | 650 } |
517 out_params->MergeDictionary(¶ms); | 651 out_params->MergeDictionary(¶ms); |
518 return true; | 652 return true; |
519 } | 653 } |
520 | 654 |
521 } // namespace internal | 655 } // namespace internal |
OLD | NEW |