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
)
13 client_
->AddListener(this);
16 JavaScriptDialogManager::~JavaScriptDialogManager() {}
18 bool JavaScriptDialogManager::IsDialogOpen() {
19 return !unhandled_dialog_queue_
.empty();
22 Status
JavaScriptDialogManager::GetDialogMessage(std::string
* message
) {
24 return Status(kNoAlertOpen
);
26 *message
= unhandled_dialog_queue_
.front();
30 Status
JavaScriptDialogManager::HandleDialog(bool accept
,
31 const std::string
* text
) {
33 return Status(kNoAlertOpen
);
35 base::DictionaryValue params
;
36 params
.SetBoolean("accept", accept
);
38 params
.SetString("promptText", *text
);
39 Status status
= client_
->SendCommand("Page.handleJavaScriptDialog", params
);
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
46 if (unhandled_dialog_queue_
.size())
47 unhandled_dialog_queue_
.pop_front();
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") {
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();