This source file includes following definitions.
- IsDialogOpen
- GetDialogMessage
- HandleDialog
- OnConnected
- OnEvent
#include "chrome/test/chromedriver/chrome/javascript_dialog_manager.h"
#include "base/values.h"
#include "chrome/test/chromedriver/chrome/devtools_client.h"
#include "chrome/test/chromedriver/chrome/status.h"
JavaScriptDialogManager::JavaScriptDialogManager(DevToolsClient* client)
: client_(client) {
client_->AddListener(this);
}
JavaScriptDialogManager::~JavaScriptDialogManager() {}
bool JavaScriptDialogManager::IsDialogOpen() {
return !unhandled_dialog_queue_.empty();
}
Status JavaScriptDialogManager::GetDialogMessage(std::string* message) {
if (!IsDialogOpen())
return Status(kNoAlertOpen);
*message = unhandled_dialog_queue_.front();
return Status(kOk);
}
Status JavaScriptDialogManager::HandleDialog(bool accept,
const std::string* text) {
if (!IsDialogOpen())
return Status(kNoAlertOpen);
base::DictionaryValue params;
params.SetBoolean("accept", accept);
if (text)
params.SetString("promptText", *text);
Status status = client_->SendCommand("Page.handleJavaScriptDialog", params);
if (status.IsError())
return status;
if (unhandled_dialog_queue_.size())
unhandled_dialog_queue_.pop_front();
return Status(kOk);
}
Status JavaScriptDialogManager::OnConnected(DevToolsClient* client) {
unhandled_dialog_queue_.clear();
base::DictionaryValue params;
return client_->SendCommand("Page.enable", params);
}
Status JavaScriptDialogManager::OnEvent(DevToolsClient* client,
const std::string& method,
const base::DictionaryValue& params) {
if (method == "Page.javascriptDialogOpening") {
std::string message;
if (!params.GetString("message", &message))
return Status(kUnknownError, "dialog event missing or invalid 'message'");
unhandled_dialog_queue_.push_back(message);
} else if (method == "Page.javascriptDialogClosing") {
unhandled_dialog_queue_.clear();
}
return Status(kOk);
}