|
| 1 | +#include "commands/browser_connection_command.h" |
| 2 | + |
| 3 | +#include "base/bind.h" |
| 4 | +#include "commands/response.h" |
| 5 | +#include "webdriver_error.h" |
| 6 | +#include "webdriver_session.h" |
| 7 | +#include "webdriver_view_executor.h" |
| 8 | +#include "webdriver_view_enumerator.h" |
| 9 | + |
| 10 | +namespace webdriver { |
| 11 | + |
| 12 | +typedef scoped_ptr<ViewCmdExecutor> ExecutorPtr; |
| 13 | + |
| 14 | +BrowserConnectionCommand::BrowserConnectionCommand(const std::vector<std::string>& path_segments, |
| 15 | + const base::DictionaryValue* const parameters) |
| 16 | + : WebDriverCommand(path_segments, parameters) { |
| 17 | +} |
| 18 | + |
| 19 | +BrowserConnectionCommand::~BrowserConnectionCommand() { |
| 20 | +} |
| 21 | + |
| 22 | +bool BrowserConnectionCommand::DoesPost() const { |
| 23 | + return true; |
| 24 | +} |
| 25 | + |
| 26 | +bool BrowserConnectionCommand::DoesGet() const { |
| 27 | + return true; |
| 28 | +} |
| 29 | + |
| 30 | +void BrowserConnectionCommand::ExecuteGet(Response* const response) { |
| 31 | + bool online; |
| 32 | + Error* error = NULL; |
| 33 | + |
| 34 | + ExecutorPtr executor(ViewCmdExecutorFactory::GetInstance()->CreateExecutor(session_)); |
| 35 | + if (NULL == executor.get()) { |
| 36 | + response->SetError(new Error(kBadRequest, "cant get view executor.")); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + session_->RunSessionTask(base::Bind( |
| 41 | + &ViewCmdExecutor::IsOnline, |
| 42 | + base::Unretained(executor.get()), |
| 43 | + &online, |
| 44 | + &error)); |
| 45 | + |
| 46 | + if (error) { |
| 47 | + response->SetError(error); |
| 48 | + return; |
| 49 | + } |
| 50 | + response->SetValue(base::Value::CreateBooleanValue(online)); |
| 51 | +} |
| 52 | + |
| 53 | +void BrowserConnectionCommand::ExecutePost(Response* const response) { |
| 54 | + const char kStateKey[] = "state"; |
| 55 | + |
| 56 | + if (!HasParameter(kStateKey)) { |
| 57 | + response->SetError(new Error(kBadRequest, "Request missing 'state' parameter")); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + bool online; |
| 62 | + if (!GetBooleanParameter("state", &online)) { |
| 63 | + response->SetError(new Error( |
| 64 | + kBadRequest, "'state' is invalid")); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // update online state for all views |
| 69 | + std::vector<ViewId> views; |
| 70 | + |
| 71 | + session_->RunSessionTask(base::Bind( |
| 72 | + &ViewEnumerator::EnumerateViews, |
| 73 | + session_, |
| 74 | + &views)); |
| 75 | + |
| 76 | + for (size_t i = 0; i < views.size(); ++i) { |
| 77 | + Error* error = NULL; |
| 78 | + ExecutorPtr executor(ViewCmdExecutorFactory::GetInstance()->CreateExecutor(session_, views.at(i))); |
| 79 | + if (NULL == executor.get()) { |
| 80 | + GlobalLogger::Log(kWarningLogLevel, "Cant update online mode for view(no executor), skip."); |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + session_->RunSessionTask(base::Bind( |
| 85 | + &ViewCmdExecutor::SetOnline, |
| 86 | + base::Unretained(executor.get()), |
| 87 | + online, |
| 88 | + &error)); |
| 89 | + |
| 90 | + if (error) { |
| 91 | + response->SetError(error); |
| 92 | + return; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +} |
0 commit comments