Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / chrome / test / chromedriver / chrome / javascript_dialog_manager.cc
bloba1b1631ff5a74097c6deb87383e2f8d035227c7c
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
3 // found in the LICENSE file.
5 #include "chrome/test/chromedriver/chrome/javascript_dialog_manager.h"
7 #include "base/values.h"
8 #include "chrome/test/chromedriver/chrome/devtools_client.h"
9 #include "chrome/test/chromedriver/chrome/status.h"
11 JavaScriptDialogManager::JavaScriptDialogManager(DevToolsClient* client)
12 : client_(client) {
13 client_->AddListener(this);
16 JavaScriptDialogManager::~JavaScriptDialogManager() {}
18 bool JavaScriptDialogManager::IsDialogOpen() {
19 return !unhandled_dialog_queue_.empty();
22 Status JavaScriptDialogManager::GetDialogMessage(std::string* message) {
23 if (!IsDialogOpen())
24 return Status(kNoAlertOpen);
26 *message = unhandled_dialog_queue_.front();
27 return Status(kOk);
30 Status JavaScriptDialogManager::HandleDialog(bool accept,
31 const std::string* text) {
32 if (!IsDialogOpen())
33 return Status(kNoAlertOpen);
35 base::DictionaryValue params;
36 params.SetBoolean("accept", accept);
37 if (text)
38 params.SetString("promptText", *text);
39 Status status = client_->SendCommand("Page.handleJavaScriptDialog", params);
40 if (status.IsError())
41 return status;
43 // Remove a dialog from the queue. Need to check the queue is not empty here,
44 // because it could have been cleared during waiting for the command
45 // response.
46 if (unhandled_dialog_queue_.size())
47 unhandled_dialog_queue_.pop_front();
48 return Status(kOk);
51 Status JavaScriptDialogManager::OnConnected(DevToolsClient* client) {
52 unhandled_dialog_queue_.clear();
53 base::DictionaryValue params;
54 return client_->SendCommand("Page.enable", params);
57 Status JavaScriptDialogManager::OnEvent(DevToolsClient* client,
58 const std::string& method,
59 const base::DictionaryValue& params) {
60 if (method == "Page.javascriptDialogOpening") {
61 std::string message;
62 if (!params.GetString("message", &message))
63 return Status(kUnknownError, "dialog event missing or invalid 'message'");
65 unhandled_dialog_queue_.push_back(message);
66 } else if (method == "Page.javascriptDialogClosed") {
67 // Inspector only sends this event when all dialogs have been closed.
68 // Clear the unhandled queue in case the user closed a dialog manually.
69 unhandled_dialog_queue_.clear();
71 return Status(kOk);