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

Unified Diff: chrome/test/chromedriver/server/http_handler.cc

Issue 19616008: [chromedriver] Allow commands to be async. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix 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 side-by-side diff with in-line comments
Download patch
Index: chrome/test/chromedriver/server/http_handler.cc
diff --git a/chrome/test/chromedriver/server/http_handler.cc b/chrome/test/chromedriver/server/http_handler.cc
index fb408b6c4f749d4b4719c5a28115dfb56bd6cbce..4699b93b56ace6cb776ec37f7847a118f755723a 100644
--- a/chrome/test/chromedriver/server/http_handler.cc
+++ b/chrome/test/chromedriver/server/http_handler.cc
@@ -23,15 +23,11 @@
#include "chrome/test/chromedriver/chrome/log.h"
#include "chrome/test/chromedriver/chrome/status.h"
#include "chrome/test/chromedriver/chrome/version.h"
-#include "chrome/test/chromedriver/commands.h"
-#include "chrome/test/chromedriver/element_commands.h"
#include "chrome/test/chromedriver/net/url_request_context_getter.h"
#include "chrome/test/chromedriver/server/http_response.h"
#include "chrome/test/chromedriver/session.h"
-#include "chrome/test/chromedriver/session_commands.h"
-#include "chrome/test/chromedriver/session_map.h"
+#include "chrome/test/chromedriver/session_thread_map.h"
#include "chrome/test/chromedriver/util.h"
-#include "chrome/test/chromedriver/window_commands.h"
#include "net/server/http_server_request_info.h"
#if defined(OS_MACOSX)
@@ -44,12 +40,11 @@ const char kLocalStorage[] = "localStorage";
const char kSessionStorage[] = "sessionStorage";
const char kShutdownPath[] = "shutdown";
-Status UnimplementedCommand(
+void UnimplementedCommand(
const base::DictionaryValue& params,
const std::string& session_id,
- scoped_ptr<base::Value>* value,
- std::string* out_session_id) {
- return Status(kUnknownCommand);
+ const CommandCallback& callback) {
+ callback.Run(Status(kUnknownCommand), scoped_ptr<base::Value>(), session_id);
}
} // namespace
@@ -63,294 +58,411 @@ CommandMapping::~CommandMapping() {}
HttpHandler::HttpHandler(Log* log, const std::string& url_base)
: log_(log),
- io_thread_("ChromeDriver IO"),
- url_base_(url_base) {
+ url_base_(url_base),
+ received_shutdown_(false),
+ command_map_(new CommandMap()),
+ weak_ptr_factory_(this) {}
+
+HttpHandler::HttpHandler(
+ const scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
+ Log* log,
+ const std::string& url_base)
+ : log_(log),
+ url_base_(url_base),
+ received_shutdown_(false),
+ weak_ptr_factory_(this) {
#if defined(OS_MACOSX)
base::mac::ScopedNSAutoreleasePool autorelease_pool;
#endif
- base::Thread::Options options(base::MessageLoop::TYPE_IO, 0);
- CHECK(io_thread_.StartWithOptions(options));
- context_getter_ = new URLRequestContextGetter(
- io_thread_.message_loop_proxy());
+ context_getter_ = new URLRequestContextGetter(io_task_runner);
socket_factory_ = CreateSyncWebSocketFactory(context_getter_.get());
- adb_.reset(new AdbImpl(io_thread_.message_loop_proxy(), log_));
+ adb_.reset(new AdbImpl(io_task_runner, log_));
device_manager_.reset(new DeviceManager(adb_.get()));
CommandMapping commands[] = {
- CommandMapping(kPost, internal::kNewSessionPathPattern,
+ CommandMapping(kPost,
+ internal::kNewSessionPathPattern,
base::Bind(&ExecuteNewSession,
- NewSessionParams(log_, &session_map_,
- context_getter_, socket_factory_,
- device_manager_.get()))),
- CommandMapping(kGet, "session/:sessionId",
- WrapToCommand(
- base::Bind(&ExecuteGetSessionCapabilities,
- &session_map_))),
- CommandMapping(kDelete, "session/:sessionId",
- base::Bind(&ExecuteQuit, false, &session_map_)),
- CommandMapping(kGet, "session/:sessionId/window_handle",
+ NewSessionParams(log_,
+ &session_thread_map_,
+ context_getter_,
+ socket_factory_,
+ device_manager_.get()))),
+ CommandMapping(kGet,
+ "session/:sessionId",
+ WrapToCommand(base::Bind(&ExecuteGetSessionCapabilities))),
+ CommandMapping(kDelete,
+ "session/:sessionId",
+ base::Bind(&ExecuteSessionCommand,
+ &session_thread_map_,
+ base::Bind(&ExecuteQuit, false),
+ true)),
+ CommandMapping(kGet,
+ "session/:sessionId/window_handle",
WrapToCommand(base::Bind(&ExecuteGetCurrentWindowHandle))),
- CommandMapping(kGet, "session/:sessionId/window_handles",
+ CommandMapping(kGet,
+ "session/:sessionId/window_handles",
WrapToCommand(base::Bind(&ExecuteGetWindowHandles))),
- CommandMapping(kPost, "session/:sessionId/url",
+ CommandMapping(kPost,
+ "session/:sessionId/url",
WrapToCommand(base::Bind(&ExecuteGet))),
- CommandMapping(kGet, "session/:sessionId/alert",
- WrapToCommand(
- base::Bind(&ExecuteAlertCommand,
- base::Bind(&ExecuteGetAlert)))),
- CommandMapping(kPost, "session/:sessionId/dismiss_alert",
- WrapToCommand(
- base::Bind(&ExecuteAlertCommand,
- base::Bind(&ExecuteDismissAlert)))),
- CommandMapping(kPost, "session/:sessionId/accept_alert",
- WrapToCommand(
- base::Bind(&ExecuteAlertCommand,
- base::Bind(&ExecuteAcceptAlert)))),
- CommandMapping(kGet, "session/:sessionId/alert_text",
- WrapToCommand(
- base::Bind(&ExecuteAlertCommand,
- base::Bind(&ExecuteGetAlertText)))),
- CommandMapping(kPost, "session/:sessionId/alert_text",
- WrapToCommand(
- base::Bind(&ExecuteAlertCommand,
- base::Bind(&ExecuteSetAlertValue)))),
- CommandMapping(kPost, "session/:sessionId/forward",
+ CommandMapping(kGet,
+ "session/:sessionId/alert",
+ WrapToCommand(base::Bind(&ExecuteAlertCommand,
+ base::Bind(&ExecuteGetAlert)))),
+ CommandMapping(
+ kPost,
+ "session/:sessionId/dismiss_alert",
+ WrapToCommand(base::Bind(&ExecuteAlertCommand,
+ base::Bind(&ExecuteDismissAlert)))),
+ CommandMapping(
+ kPost,
+ "session/:sessionId/accept_alert",
+ WrapToCommand(base::Bind(&ExecuteAlertCommand,
+ base::Bind(&ExecuteAcceptAlert)))),
+ CommandMapping(
+ kGet,
+ "session/:sessionId/alert_text",
+ WrapToCommand(base::Bind(&ExecuteAlertCommand,
+ base::Bind(&ExecuteGetAlertText)))),
+ CommandMapping(
+ kPost,
+ "session/:sessionId/alert_text",
+ WrapToCommand(base::Bind(&ExecuteAlertCommand,
+ base::Bind(&ExecuteSetAlertValue)))),
+ CommandMapping(kPost,
+ "session/:sessionId/forward",
WrapToCommand(base::Bind(&ExecuteGoForward))),
- CommandMapping(kPost, "session/:sessionId/back",
+ CommandMapping(kPost,
+ "session/:sessionId/back",
WrapToCommand(base::Bind(&ExecuteGoBack))),
- CommandMapping(kPost, "session/:sessionId/refresh",
+ CommandMapping(kPost,
+ "session/:sessionId/refresh",
WrapToCommand(base::Bind(&ExecuteRefresh))),
- CommandMapping(kPost, "session/:sessionId/execute",
+ CommandMapping(kPost,
+ "session/:sessionId/execute",
WrapToCommand(base::Bind(&ExecuteExecuteScript))),
- CommandMapping(kPost, "session/:sessionId/execute_async",
+ CommandMapping(kPost,
+ "session/:sessionId/execute_async",
WrapToCommand(base::Bind(&ExecuteExecuteAsyncScript))),
- CommandMapping(kGet, "session/:sessionId/url",
+ CommandMapping(kGet,
+ "session/:sessionId/url",
WrapToCommand(base::Bind(&ExecuteGetCurrentUrl))),
- CommandMapping(kGet, "session/:sessionId/title",
+ CommandMapping(kGet,
+ "session/:sessionId/title",
WrapToCommand(base::Bind(&ExecuteGetTitle))),
- CommandMapping(kGet, "session/:sessionId/source",
+ CommandMapping(kGet,
+ "session/:sessionId/source",
WrapToCommand(base::Bind(&ExecuteGetPageSource))),
- CommandMapping(kGet, "session/:sessionId/screenshot",
+ CommandMapping(kGet,
+ "session/:sessionId/screenshot",
WrapToCommand(base::Bind(&ExecuteScreenshot))),
- CommandMapping(kPost, "session/:sessionId/visible",
+ CommandMapping(kPost,
+ "session/:sessionId/visible",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kGet, "session/:sessionId/visible",
+ CommandMapping(kGet,
+ "session/:sessionId/visible",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kPost, "session/:sessionId/element",
+ CommandMapping(kPost,
+ "session/:sessionId/element",
WrapToCommand(base::Bind(&ExecuteFindElement, 50))),
- CommandMapping(kPost, "session/:sessionId/elements",
+ CommandMapping(kPost,
+ "session/:sessionId/elements",
WrapToCommand(base::Bind(&ExecuteFindElements, 50))),
- CommandMapping(kPost, "session/:sessionId/element/active",
+ CommandMapping(kPost,
+ "session/:sessionId/element/active",
WrapToCommand(base::Bind(&ExecuteGetActiveElement))),
- CommandMapping(kPost, "session/:sessionId/element/:id/element",
+ CommandMapping(kPost,
+ "session/:sessionId/element/:id/element",
WrapToCommand(base::Bind(&ExecuteFindChildElement, 50))),
- CommandMapping(kPost, "session/:sessionId/element/:id/elements",
+ CommandMapping(kPost,
+ "session/:sessionId/element/:id/elements",
WrapToCommand(base::Bind(&ExecuteFindChildElements, 50))),
- CommandMapping(kPost, "session/:sessionId/element/:id/click",
+ CommandMapping(kPost,
+ "session/:sessionId/element/:id/click",
WrapToCommand(base::Bind(&ExecuteClickElement))),
- CommandMapping(kPost, "session/:sessionId/element/:id/clear",
+ CommandMapping(kPost,
+ "session/:sessionId/element/:id/clear",
WrapToCommand(base::Bind(&ExecuteClearElement))),
- CommandMapping(kPost, "session/:sessionId/element/:id/submit",
+ CommandMapping(kPost,
+ "session/:sessionId/element/:id/submit",
WrapToCommand(base::Bind(&ExecuteSubmitElement))),
- CommandMapping(kGet, "session/:sessionId/element/:id/text",
+ CommandMapping(kGet,
+ "session/:sessionId/element/:id/text",
WrapToCommand(base::Bind(&ExecuteGetElementText))),
- CommandMapping(kPost, "session/:sessionId/element/:id/value",
+ CommandMapping(kPost,
+ "session/:sessionId/element/:id/value",
WrapToCommand(base::Bind(&ExecuteSendKeysToElement))),
- CommandMapping(kPost, "session/:sessionId/file",
+ CommandMapping(kPost,
+ "session/:sessionId/file",
WrapToCommand(base::Bind(&ExecuteUploadFile))),
- CommandMapping(kGet, "session/:sessionId/element/:id/value",
+ CommandMapping(kGet,
+ "session/:sessionId/element/:id/value",
WrapToCommand(base::Bind(&ExecuteGetElementValue))),
- CommandMapping(kGet, "session/:sessionId/element/:id/name",
+ CommandMapping(kGet,
+ "session/:sessionId/element/:id/name",
WrapToCommand(base::Bind(&ExecuteGetElementTagName))),
- CommandMapping(kGet, "session/:sessionId/element/:id/selected",
+ CommandMapping(kGet,
+ "session/:sessionId/element/:id/selected",
WrapToCommand(base::Bind(&ExecuteIsElementSelected))),
- CommandMapping(kGet, "session/:sessionId/element/:id/enabled",
+ CommandMapping(kGet,
+ "session/:sessionId/element/:id/enabled",
WrapToCommand(base::Bind(&ExecuteIsElementEnabled))),
- CommandMapping(kGet, "session/:sessionId/element/:id/displayed",
+ CommandMapping(kGet,
+ "session/:sessionId/element/:id/displayed",
WrapToCommand(base::Bind(&ExecuteIsElementDisplayed))),
- CommandMapping(kPost, "session/:sessionId/element/:id/hover",
+ CommandMapping(kPost,
+ "session/:sessionId/element/:id/hover",
WrapToCommand(base::Bind(&ExecuteHoverOverElement))),
- CommandMapping(kGet, "session/:sessionId/element/:id/location",
+ CommandMapping(kGet,
+ "session/:sessionId/element/:id/location",
WrapToCommand(base::Bind(&ExecuteGetElementLocation))),
- CommandMapping(kGet, "session/:sessionId/element/:id/location_in_view",
- WrapToCommand(
- base::Bind(
- &ExecuteGetElementLocationOnceScrolledIntoView))),
- CommandMapping(kGet, "session/:sessionId/element/:id/size",
+ CommandMapping(kGet,
+ "session/:sessionId/element/:id/location_in_view",
+ WrapToCommand(base::Bind(
+ &ExecuteGetElementLocationOnceScrolledIntoView))),
+ CommandMapping(kGet,
+ "session/:sessionId/element/:id/size",
WrapToCommand(base::Bind(&ExecuteGetElementSize))),
- CommandMapping(kGet, "session/:sessionId/element/:id/attribute/:name",
+ CommandMapping(kGet,
+ "session/:sessionId/element/:id/attribute/:name",
WrapToCommand(base::Bind(&ExecuteGetElementAttribute))),
- CommandMapping(kGet, "session/:sessionId/element/:id/equals/:other",
+ CommandMapping(kGet,
+ "session/:sessionId/element/:id/equals/:other",
WrapToCommand(base::Bind(&ExecuteElementEquals))),
- CommandMapping(kGet, "session/:sessionId/cookie",
+ CommandMapping(kGet,
+ "session/:sessionId/cookie",
WrapToCommand(base::Bind(&ExecuteGetCookies))),
- CommandMapping(kPost, "session/:sessionId/cookie",
+ CommandMapping(kPost,
+ "session/:sessionId/cookie",
WrapToCommand(base::Bind(&ExecuteAddCookie))),
- CommandMapping(kDelete, "session/:sessionId/cookie",
+ CommandMapping(kDelete,
+ "session/:sessionId/cookie",
WrapToCommand(base::Bind(&ExecuteDeleteAllCookies))),
- CommandMapping(kDelete, "session/:sessionId/cookie/:name",
+ CommandMapping(kDelete,
+ "session/:sessionId/cookie/:name",
WrapToCommand(base::Bind(&ExecuteDeleteCookie))),
- CommandMapping(kPost, "session/:sessionId/frame",
+ CommandMapping(kPost,
+ "session/:sessionId/frame",
WrapToCommand(base::Bind(&ExecuteSwitchToFrame))),
- CommandMapping(kPost, "session/:sessionId/window",
+ CommandMapping(kPost,
+ "session/:sessionId/window",
WrapToCommand(base::Bind(&ExecuteSwitchToWindow))),
- CommandMapping(kGet, "session/:sessionId/window/:windowHandle/size",
+ CommandMapping(kGet,
+ "session/:sessionId/window/:windowHandle/size",
WrapToCommand(base::Bind(&ExecuteGetWindowSize))),
- CommandMapping(kGet, "session/:sessionId/window/:windowHandle/position",
+ CommandMapping(kGet,
+ "session/:sessionId/window/:windowHandle/position",
WrapToCommand(base::Bind(&ExecuteGetWindowPosition))),
- CommandMapping(kPost, "session/:sessionId/window/:windowHandle/size",
+ CommandMapping(kPost,
+ "session/:sessionId/window/:windowHandle/size",
WrapToCommand(base::Bind(&ExecuteSetWindowSize))),
- CommandMapping(kPost, "session/:sessionId/window/:windowHandle/position",
+ CommandMapping(kPost,
+ "session/:sessionId/window/:windowHandle/position",
WrapToCommand(base::Bind(&ExecuteSetWindowPosition))),
- CommandMapping(kPost, "session/:sessionId/window/:windowHandle/maximize",
+ CommandMapping(kPost,
+ "session/:sessionId/window/:windowHandle/maximize",
WrapToCommand(base::Bind(&ExecuteMaximizeWindow))),
- CommandMapping(kDelete, "session/:sessionId/window",
- WrapToCommand(base::Bind(&ExecuteClose, &session_map_))),
- CommandMapping(kPost, "session/:sessionId/element/:id/drag",
+ CommandMapping(kDelete,
+ "session/:sessionId/window",
+ WrapToCommand(base::Bind(&ExecuteClose))),
+ CommandMapping(kPost,
+ "session/:sessionId/element/:id/drag",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kGet, "session/:sessionId/element/:id/css/:propertyName",
- WrapToCommand(
- base::Bind(&ExecuteGetElementValueOfCSSProperty))),
- CommandMapping(kPost, "session/:sessionId/timeouts/implicit_wait",
+ CommandMapping(
+ kGet,
+ "session/:sessionId/element/:id/css/:propertyName",
+ WrapToCommand(base::Bind(&ExecuteGetElementValueOfCSSProperty))),
+ CommandMapping(kPost,
+ "session/:sessionId/timeouts/implicit_wait",
WrapToCommand(base::Bind(&ExecuteImplicitlyWait))),
- CommandMapping(kPost, "session/:sessionId/timeouts/async_script",
+ CommandMapping(kPost,
+ "session/:sessionId/timeouts/async_script",
WrapToCommand(base::Bind(&ExecuteSetScriptTimeout))),
- CommandMapping(kPost, "session/:sessionId/timeouts",
+ CommandMapping(kPost,
+ "session/:sessionId/timeouts",
WrapToCommand(base::Bind(&ExecuteSetTimeout))),
- CommandMapping(kPost, "session/:sessionId/execute_sql",
+ CommandMapping(kPost,
+ "session/:sessionId/execute_sql",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kGet, "session/:sessionId/location",
+ CommandMapping(kGet,
+ "session/:sessionId/location",
WrapToCommand(base::Bind(&ExecuteGetLocation))),
- CommandMapping(kPost, "session/:sessionId/location",
+ CommandMapping(kPost,
+ "session/:sessionId/location",
WrapToCommand(base::Bind(&ExecuteSetLocation))),
- CommandMapping(kGet, "session/:sessionId/application_cache/status",
+ CommandMapping(kGet,
+ "session/:sessionId/application_cache/status",
base::Bind(&ExecuteGetStatus)),
- CommandMapping(kGet, "session/:sessionId/browser_connection",
+ CommandMapping(kGet,
+ "session/:sessionId/browser_connection",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kPost, "session/:sessionId/browser_connection",
+ CommandMapping(kPost,
+ "session/:sessionId/browser_connection",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kGet, "session/:sessionId/local_storage/key/:key",
- WrapToCommand(
- base::Bind(&ExecuteGetStorageItem, kLocalStorage))),
- CommandMapping(kDelete, "session/:sessionId/local_storage/key/:key",
- WrapToCommand(
- base::Bind(&ExecuteRemoveStorageItem, kLocalStorage))),
- CommandMapping(kGet, "session/:sessionId/local_storage",
- WrapToCommand(
- base::Bind(&ExecuteGetStorageKeys, kLocalStorage))),
- CommandMapping(kPost, "session/:sessionId/local_storage",
- WrapToCommand(
- base::Bind(&ExecuteSetStorageItem, kLocalStorage))),
- CommandMapping(kDelete, "session/:sessionId/local_storage",
- WrapToCommand(
- base::Bind(&ExecuteClearStorage, kLocalStorage))),
- CommandMapping(kGet, "session/:sessionId/local_storage/size",
- WrapToCommand(
- base::Bind(&ExecuteGetStorageSize, kLocalStorage))),
- CommandMapping(kGet, "session/:sessionId/session_storage/key/:key",
- WrapToCommand(
- base::Bind(&ExecuteGetStorageItem, kSessionStorage))),
- CommandMapping(kDelete, "session/:sessionId/session_storage/key/:key",
- WrapToCommand(
- base::Bind(
- &ExecuteRemoveStorageItem, kSessionStorage))),
- CommandMapping(kGet, "session/:sessionId/session_storage",
- WrapToCommand(
- base::Bind(&ExecuteGetStorageKeys, kSessionStorage))),
- CommandMapping(kPost, "session/:sessionId/session_storage",
- WrapToCommand(
- base::Bind(&ExecuteSetStorageItem, kSessionStorage))),
- CommandMapping(kDelete, "session/:sessionId/session_storage",
- WrapToCommand(
- base::Bind(&ExecuteClearStorage, kSessionStorage))),
- CommandMapping(kGet, "session/:sessionId/session_storage/size",
- WrapToCommand(
- base::Bind(&ExecuteGetStorageSize, kSessionStorage))),
- CommandMapping(kGet, "session/:sessionId/orientation",
+ CommandMapping(
+ kGet,
+ "session/:sessionId/local_storage/key/:key",
+ WrapToCommand(base::Bind(&ExecuteGetStorageItem, kLocalStorage))),
+ CommandMapping(
+ kDelete,
+ "session/:sessionId/local_storage/key/:key",
+ WrapToCommand(base::Bind(&ExecuteRemoveStorageItem, kLocalStorage))),
+ CommandMapping(
+ kGet,
+ "session/:sessionId/local_storage",
+ WrapToCommand(base::Bind(&ExecuteGetStorageKeys, kLocalStorage))),
+ CommandMapping(
+ kPost,
+ "session/:sessionId/local_storage",
+ WrapToCommand(base::Bind(&ExecuteSetStorageItem, kLocalStorage))),
+ CommandMapping(
+ kDelete,
+ "session/:sessionId/local_storage",
+ WrapToCommand(base::Bind(&ExecuteClearStorage, kLocalStorage))),
+ CommandMapping(
+ kGet,
+ "session/:sessionId/local_storage/size",
+ WrapToCommand(base::Bind(&ExecuteGetStorageSize, kLocalStorage))),
+ CommandMapping(
+ kGet,
+ "session/:sessionId/session_storage/key/:key",
+ WrapToCommand(base::Bind(&ExecuteGetStorageItem, kSessionStorage))),
+ CommandMapping(kDelete,
+ "session/:sessionId/session_storage/key/:key",
+ WrapToCommand(base::Bind(&ExecuteRemoveStorageItem,
+ kSessionStorage))),
+ CommandMapping(
+ kGet,
+ "session/:sessionId/session_storage",
+ WrapToCommand(base::Bind(&ExecuteGetStorageKeys, kSessionStorage))),
+ CommandMapping(
+ kPost,
+ "session/:sessionId/session_storage",
+ WrapToCommand(base::Bind(&ExecuteSetStorageItem, kSessionStorage))),
+ CommandMapping(
+ kDelete,
+ "session/:sessionId/session_storage",
+ WrapToCommand(base::Bind(&ExecuteClearStorage, kSessionStorage))),
+ CommandMapping(
+ kGet,
+ "session/:sessionId/session_storage/size",
+ WrapToCommand(base::Bind(&ExecuteGetStorageSize, kSessionStorage))),
+ CommandMapping(kGet,
+ "session/:sessionId/orientation",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kPost, "session/:sessionId/orientation",
+ CommandMapping(kPost,
+ "session/:sessionId/orientation",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kPost, "session/:sessionId/click",
+ CommandMapping(kPost,
+ "session/:sessionId/click",
WrapToCommand(base::Bind(&ExecuteMouseClick))),
- CommandMapping(kPost, "session/:sessionId/doubleclick",
+ CommandMapping(kPost,
+ "session/:sessionId/doubleclick",
WrapToCommand(base::Bind(&ExecuteMouseDoubleClick))),
- CommandMapping(kPost, "session/:sessionId/buttondown",
+ CommandMapping(kPost,
+ "session/:sessionId/buttondown",
WrapToCommand(base::Bind(&ExecuteMouseButtonDown))),
- CommandMapping(kPost, "session/:sessionId/buttonup",
+ CommandMapping(kPost,
+ "session/:sessionId/buttonup",
WrapToCommand(base::Bind(&ExecuteMouseButtonUp))),
- CommandMapping(kPost, "session/:sessionId/moveto",
+ CommandMapping(kPost,
+ "session/:sessionId/moveto",
WrapToCommand(base::Bind(&ExecuteMouseMoveTo))),
- CommandMapping(kPost, "session/:sessionId/keys",
- WrapToCommand(
- base::Bind(&ExecuteSendKeysToActiveElement))),
- CommandMapping(kGet, "session/:sessionId/ime/available_engines",
+ CommandMapping(
+ kPost,
+ "session/:sessionId/keys",
+ WrapToCommand(base::Bind(&ExecuteSendKeysToActiveElement))),
+ CommandMapping(kGet,
+ "session/:sessionId/ime/available_engines",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kGet, "session/:sessionId/ime/active_engine",
+ CommandMapping(kGet,
+ "session/:sessionId/ime/active_engine",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kGet, "session/:sessionId/ime/activated",
+ CommandMapping(kGet,
+ "session/:sessionId/ime/activated",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kPost, "session/:sessionId/ime/deactivate",
+ CommandMapping(kPost,
+ "session/:sessionId/ime/deactivate",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kPost, "session/:sessionId/ime/activate",
+ CommandMapping(kPost,
+ "session/:sessionId/ime/activate",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kPost, "session/:sessionId/touch/click",
+ CommandMapping(kPost,
+ "session/:sessionId/touch/click",
WrapToCommand(base::Bind(&ExecuteTouchSingleTap))),
- CommandMapping(kPost, "session/:sessionId/touch/down",
+ CommandMapping(kPost,
+ "session/:sessionId/touch/down",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kPost, "session/:sessionId/touch/up",
+ CommandMapping(kPost,
+ "session/:sessionId/touch/up",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kPost, "session/:sessionId/touch/move",
+ CommandMapping(kPost,
+ "session/:sessionId/touch/move",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kPost, "session/:sessionId/touch/scroll",
+ CommandMapping(kPost,
+ "session/:sessionId/touch/scroll",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kPost, "session/:sessionId/touch/doubleclick",
+ CommandMapping(kPost,
+ "session/:sessionId/touch/doubleclick",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kPost, "session/:sessionId/touch/longclick",
+ CommandMapping(kPost,
+ "session/:sessionId/touch/longclick",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kPost, "session/:sessionId/touch/flick",
+ CommandMapping(kPost,
+ "session/:sessionId/touch/flick",
base::Bind(&UnimplementedCommand)),
- CommandMapping(kPost, "session/:sessionId/log",
+ CommandMapping(kPost,
+ "session/:sessionId/log",
WrapToCommand(base::Bind(&ExecuteGetLog))),
- CommandMapping(kGet, "session/:sessionId/log/types",
+ CommandMapping(kGet,
+ "session/:sessionId/log/types",
WrapToCommand(base::Bind(&ExecuteGetAvailableLogTypes))),
CommandMapping(kPost, "logs", base::Bind(&UnimplementedCommand)),
CommandMapping(kGet, "status", base::Bind(&ExecuteGetStatus)),
// Custom Chrome commands:
// Allow quit all to be called with GET or POST.
- CommandMapping(kGet, kShutdownPath,
+ CommandMapping(kGet,
+ kShutdownPath,
base::Bind(&ExecuteQuitAll,
- base::Bind(&ExecuteQuit, true, &session_map_),
- &session_map_)),
- CommandMapping(kPost, kShutdownPath,
+ WrapToCommand(base::Bind(&ExecuteQuit, true)),
+ &session_thread_map_)),
+ CommandMapping(kPost,
+ kShutdownPath,
base::Bind(&ExecuteQuitAll,
- base::Bind(&ExecuteQuit, true, &session_map_),
- &session_map_)),
- CommandMapping(kGet, "session/:sessionId/is_loading",
- WrapToCommand(base::Bind(&ExecuteIsLoading))),
- };
- this->command_map_.reset(
+ WrapToCommand(base::Bind(&ExecuteQuit, true)),
+ &session_thread_map_)),
+ CommandMapping(kGet,
+ "session/:sessionId/is_loading",
+ WrapToCommand(base::Bind(&ExecuteIsLoading))), };
+ command_map_.reset(
new CommandMap(commands, commands + arraysize(commands)));
}
HttpHandler::~HttpHandler() {}
void HttpHandler::Handle(const net::HttpServerRequestInfo& request,
- HttpResponse* response) {
- log_->AddEntry(
- Log::kLog,
- base::StringPrintf("received WebDriver request: %s %s %s",
- request.method.c_str(),
- request.path.c_str(),
- request.data.c_str()));
-
- HandleInternal(request, response);
-
- log_->AddEntry(
- Log::kLog,
- base::StringPrintf("sending WebDriver response: %d %s",
- response->status(),
- response->body().c_str()));
+ const HttpResponseSenderFunc& send_response_func) {
+ CHECK(thread_checker_.CalledOnValidThread());
+
+ if (received_shutdown_)
+ return;
+ if (ShouldShutdown(request))
+ received_shutdown_ = true;
+
+ std::string path = request.path;
+ if (!StartsWithASCII(path, url_base_, true)) {
+ scoped_ptr<HttpResponse> response(
+ new HttpResponse(HttpResponse::kBadRequest));
+ response->set_body("unhandled request");
+ send_response_func.Run(response.Pass());
+ return;
+ }
+
+ path.erase(0, url_base_.length());
+
+ HandleCommand(request, path, send_response_func);
}
bool HttpHandler::ShouldShutdown(const net::HttpServerRequestInfo& request) {
@@ -359,7 +471,8 @@ bool HttpHandler::ShouldShutdown(const net::HttpServerRequestInfo& request) {
Command HttpHandler::WrapToCommand(
const SessionCommand& session_command) {
- return base::Bind(&ExecuteSessionCommand, &session_map_, session_command);
+ return base::Bind(
+ &ExecuteSessionCommand, &session_thread_map_, session_command, false);
}
Command HttpHandler::WrapToCommand(
@@ -374,34 +487,26 @@ Command HttpHandler::WrapToCommand(
base::Bind(&ExecuteElementCommand, element_command));
}
-void HttpHandler::HandleInternal(const net::HttpServerRequestInfo& request,
- HttpResponse* response) {
- std::string path = request.path;
- if (!StartsWithASCII(path, url_base_, true)) {
- *response = HttpResponse(HttpResponse::kBadRequest);
- response->set_body("unhandled request");
- return;
- }
-
- path.erase(0, url_base_.length());
-
- if (!HandleWebDriverCommand(request, path, response)) {
- *response = HttpResponse(HttpResponse::kNotFound);
- response->set_body("unknown command: " + path);
- return;
- }
-}
-
-bool HttpHandler::HandleWebDriverCommand(
+void HttpHandler::HandleCommand(
const net::HttpServerRequestInfo& request,
const std::string& trimmed_path,
- HttpResponse* response) {
+ const HttpResponseSenderFunc& send_response_func) {
+ log_->AddEntry(Log::kLog,
+ base::StringPrintf("handling command: %s %s %s",
+ request.method.c_str(),
+ trimmed_path.c_str(),
+ request.data.c_str()));
+
base::DictionaryValue params;
std::string session_id;
CommandMap::const_iterator iter = command_map_->begin();
while (true) {
if (iter == command_map_->end()) {
- return false;
+ scoped_ptr<HttpResponse> response(
+ new HttpResponse(HttpResponse::kNotFound));
+ response->set_body("unknown command: " + trimmed_path);
+ send_response_func.Run(response.Pass());
+ return;
}
if (internal::MatchesCommand(
request.method, trimmed_path, *iter, &session_id, &params)) {
@@ -414,40 +519,69 @@ bool HttpHandler::HandleWebDriverCommand(
base::DictionaryValue* body_params;
scoped_ptr<base::Value> parsed_body(base::JSONReader::Read(request.data));
if (!parsed_body || !parsed_body->GetAsDictionary(&body_params)) {
- *response = HttpResponse(HttpResponse::kBadRequest);
+ scoped_ptr<HttpResponse> response(
+ new HttpResponse(HttpResponse::kBadRequest));
response->set_body("missing command parameters");
- return true;
+ send_response_func.Run(response.Pass());
+ return;
}
params.MergeDictionary(body_params);
}
- scoped_ptr<base::Value> value;
- std::string out_session_id;
- Status status = iter->command.Run(
- params, session_id, &value, &out_session_id);
+ iter->command.Run(params,
+ session_id,
+ base::Bind(&HttpHandler::PrepareResponse,
+ weak_ptr_factory_.GetWeakPtr(),
+ trimmed_path,
+ send_response_func));
+}
+
+void HttpHandler::PrepareResponse(
+ const std::string& trimmed_path,
+ const HttpResponseSenderFunc& send_response_func,
+ const Status& status,
+ scoped_ptr<base::Value> value,
+ const std::string& session_id) {
+ CHECK(thread_checker_.CalledOnValidThread());
+ scoped_ptr<HttpResponse> response =
+ PrepareResponseHelper(trimmed_path, status, value.Pass(), session_id);
+ log_->AddEntry(Log::kLog,
+ base::StringPrintf("sending response: %d %s",
+ response->status(),
+ response->body().c_str()));
+ send_response_func.Run(response.Pass());
+}
+scoped_ptr<HttpResponse> HttpHandler::PrepareResponseHelper(
+ const std::string& trimmed_path,
+ const Status& status,
+ scoped_ptr<base::Value> value,
+ const std::string& session_id) {
if (status.code() == kUnknownCommand) {
- *response = HttpResponse(HttpResponse::kNotImplemented);
+ scoped_ptr<HttpResponse> response(
+ new HttpResponse(HttpResponse::kNotImplemented));
response->set_body("unimplemented command: " + trimmed_path);
- return true;
+ return response.Pass();
}
- if (iter->path_pattern == internal::kNewSessionPathPattern && status.IsOk()) {
+ if (trimmed_path == internal::kNewSessionPathPattern && status.IsOk()) {
// Creating a session involves a HTTP request to /session, which is
// supposed to redirect to /session/:sessionId, which returns the
// session info.
- *response = HttpResponse(HttpResponse::kSeeOther);
- response->AddHeader("Location", url_base_ + "session/" + out_session_id);
- return true;
+ scoped_ptr<HttpResponse> response(
+ new HttpResponse(HttpResponse::kSeeOther));
+ response->AddHeader("Location", url_base_ + "session/" + session_id);
+ return response.Pass();
} else if (status.IsError()) {
- status.AddDetails(base::StringPrintf(
+ Status full_status(status);
+ full_status.AddDetails(base::StringPrintf(
"Driver info: chromedriver=%s,platform=%s %s %s",
kChromeDriverVersion,
base::SysInfo::OperatingSystemName().c_str(),
base::SysInfo::OperatingSystemVersion().c_str(),
base::SysInfo::OperatingSystemArchitecture().c_str()));
scoped_ptr<base::DictionaryValue> error(new base::DictionaryValue());
- error->SetString("message", status.message());
+ error->SetString("message", full_status.message());
value.reset(error.release());
}
if (!value)
@@ -456,15 +590,15 @@ bool HttpHandler::HandleWebDriverCommand(
base::DictionaryValue body_params;
body_params.SetInteger("status", status.code());
body_params.Set("value", value.release());
- body_params.SetString("sessionId", out_session_id);
+ body_params.SetString("sessionId", session_id);
std::string body;
base::JSONWriter::WriteWithOptions(
&body_params, base::JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION,
&body);
- *response = HttpResponse(HttpResponse::kOk);
+ scoped_ptr<HttpResponse> response(new HttpResponse(HttpResponse::kOk));
response->SetMimeType("application/json; charset=utf-8");
response->set_body(body);
- return true;
+ return response.Pass();
}
namespace internal {
« 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