OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/command_executor_impl.h" | 5 #include "chrome/test/chromedriver/command_executor_impl.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/message_loop.h" |
| 10 #include "base/message_loop_proxy.h" |
9 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/test/chromedriver/chrome_launcher_impl.h" |
10 #include "chrome/test/chromedriver/commands.h" | 13 #include "chrome/test/chromedriver/commands.h" |
| 14 #include "chrome/test/chromedriver/net/url_request_context_getter.h" |
11 #include "chrome/test/chromedriver/session.h" | 15 #include "chrome/test/chromedriver/session.h" |
12 #include "chrome/test/chromedriver/session_command.h" | 16 #include "chrome/test/chromedriver/session_command.h" |
13 #include "chrome/test/chromedriver/session_map.h" | 17 #include "chrome/test/chromedriver/session_map.h" |
14 #include "chrome/test/chromedriver/status.h" | 18 #include "chrome/test/chromedriver/status.h" |
15 | 19 |
16 CommandExecutorImpl::CommandExecutorImpl() { | 20 CommandExecutorImpl::CommandExecutorImpl() |
| 21 : io_thread_("ChromeDriver IO") {} |
| 22 |
| 23 CommandExecutorImpl::~CommandExecutorImpl() {} |
| 24 |
| 25 void CommandExecutorImpl::Init() { |
| 26 base::Thread::Options options(MessageLoop::TYPE_IO, 0); |
| 27 CHECK(io_thread_.StartWithOptions(options)); |
| 28 context_getter_ = new URLRequestContextGetter( |
| 29 io_thread_.message_loop_proxy()); |
| 30 launcher_.reset(new ChromeLauncherImpl(context_getter_)); |
| 31 |
| 32 // Session commands. |
17 base::Callback<Status( | 33 base::Callback<Status( |
18 const SessionCommand&, | 34 const SessionCommand&, |
19 const base::DictionaryValue&, | 35 const base::DictionaryValue&, |
20 const std::string&, | 36 const std::string&, |
21 scoped_ptr<base::Value>*, | 37 scoped_ptr<base::Value>*, |
22 std::string*)> execute_session_command = base::Bind( | 38 std::string*)> execute_session_command = base::Bind( |
23 &ExecuteSessionCommand, | 39 &ExecuteSessionCommand, |
24 &session_map_); | 40 &session_map_); |
| 41 command_map_.Set("get", base::Bind(execute_session_command, |
| 42 base::Bind(&ExecuteGet))); |
25 Command quit_command = base::Bind(execute_session_command, | 43 Command quit_command = base::Bind(execute_session_command, |
26 base::Bind(&ExecuteQuit, &session_map_)); | 44 base::Bind(&ExecuteQuit, &session_map_)); |
27 command_map_.Set("quit", quit_command); | 45 command_map_.Set("quit", quit_command); |
28 | 46 |
29 // Non-session commands. | 47 // Non-session commands. |
30 command_map_.Set("newSession", | 48 command_map_.Set("newSession", |
31 base::Bind(&ExecuteNewSession, &session_map_, &launcher_)); | 49 base::Bind(&ExecuteNewSession, &session_map_, launcher_.get())); |
32 command_map_.Set("quitAll", | 50 command_map_.Set("quitAll", |
33 base::Bind(&ExecuteQuitAll, quit_command, &session_map_)); | 51 base::Bind(&ExecuteQuitAll, quit_command, &session_map_)); |
34 } | 52 } |
35 | 53 |
36 CommandExecutorImpl::~CommandExecutorImpl() {} | |
37 | |
38 void CommandExecutorImpl::ExecuteCommand( | 54 void CommandExecutorImpl::ExecuteCommand( |
39 const std::string& name, | 55 const std::string& name, |
40 const base::DictionaryValue& params, | 56 const base::DictionaryValue& params, |
41 const std::string& session_id, | 57 const std::string& session_id, |
42 StatusCode* status_code, | 58 StatusCode* status_code, |
43 scoped_ptr<base::Value>* value, | 59 scoped_ptr<base::Value>* value, |
44 std::string* out_session_id) { | 60 std::string* out_session_id) { |
45 Command cmd; | 61 Command cmd; |
46 Status status(kOk); | 62 Status status(kOk); |
47 if (command_map_.Get(name, &cmd)) { | 63 if (command_map_.Get(name, &cmd)) { |
48 status = cmd.Run(params, session_id, value, out_session_id); | 64 status = cmd.Run(params, session_id, value, out_session_id); |
49 } else { | 65 } else { |
50 status = Status(kUnknownCommand, name); | 66 status = Status(kUnknownCommand, name); |
51 *out_session_id = session_id; | 67 *out_session_id = session_id; |
52 } | 68 } |
53 *status_code = status.code(); | 69 *status_code = status.code(); |
54 if (status.IsError()) { | 70 if (status.IsError()) { |
55 scoped_ptr<base::DictionaryValue> error(new base::DictionaryValue()); | 71 scoped_ptr<base::DictionaryValue> error(new base::DictionaryValue()); |
56 error->SetString("message", status.message()); | 72 error->SetString("message", status.message()); |
57 value->reset(error.release()); | 73 value->reset(error.release()); |
58 } | 74 } |
59 if (!*value) | 75 if (!*value) |
60 value->reset(base::Value::CreateNullValue()); | 76 value->reset(base::Value::CreateNullValue()); |
61 } | 77 } |
OLD | NEW |