OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/test/chromedriver/javascript_dialog_manager.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/values.h" | |
9 #include "chrome/test/chromedriver/devtools_client.h" | |
10 #include "chrome/test/chromedriver/status.h" | |
11 | |
12 JavaScriptDialogManager::JavaScriptDialogManager(DevToolsClient* client) | |
13 : client_(client) { | |
14 client_->AddListener(this); | |
15 } | |
16 | |
17 JavaScriptDialogManager::~JavaScriptDialogManager() { | |
18 } | |
19 | |
20 bool JavaScriptDialogManager::IsDialogOpen() { | |
21 client_->HandleReceivedEvents(); | |
22 return !unhandled_dialog_queue_.empty(); | |
23 } | |
24 | |
25 Status JavaScriptDialogManager::GetDialogMessage(std::string* message) { | |
26 if (!IsDialogOpen()) | |
27 return Status(kNoAlertOpen); | |
28 | |
29 *message = unhandled_dialog_queue_.front(); | |
30 return Status(kOk); | |
31 } | |
32 | |
33 Status JavaScriptDialogManager::HandleDialog(bool accept, | |
34 const std::string& text) { | |
35 if (!IsDialogOpen()) | |
36 return Status(kNoAlertOpen); | |
37 | |
38 base::DictionaryValue params; | |
39 params.SetBoolean("accept", accept); | |
40 params.SetString("promptText", text); | |
41 Status status = client_->SendCommand("Page.handleJavaScriptDialog", params); | |
42 if (status.IsError()) | |
43 return status; | |
44 | |
45 // Remove a dialog from the queue. Need to check the queue is not empty here, | |
46 // because it could have been cleared during waiting for the command | |
47 // response. | |
48 if (unhandled_dialog_queue_.size()) | |
49 unhandled_dialog_queue_.pop_front(); | |
50 return Status(kOk); | |
51 } | |
52 | |
53 Status JavaScriptDialogManager::OnConnected() { | |
54 unhandled_dialog_queue_.clear(); | |
55 base::DictionaryValue params; | |
56 return client_->SendCommand("Page.enable", params); | |
57 } | |
58 | |
59 void JavaScriptDialogManager::OnEvent(const std::string& method, | |
60 const base::DictionaryValue& params) { | |
61 if (method == "Page.javascriptDialogOpening") { | |
62 std::string message; | |
63 if (!params.GetString("message", &message)) { | |
64 LOG(ERROR) << "dialog event missing or invalid 'message'"; | |
65 } | |
66 unhandled_dialog_queue_.push_back(message); | |
67 } else if (method == "Page.javascriptDialogClosing") { | |
68 // Inspector only sends this event when all dialogs have been closed. | |
69 // Clear the unhandled queue in case the user closed a dialog manually. | |
70 unhandled_dialog_queue_.clear(); | |
71 } | |
72 } | |
OLD | NEW |