- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / chromedriver / chrome / javascript_dialog_manager.cc
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/chrome/javascript_dialog_manager.h"
6
7 #include "base/values.h"
8 #include "chrome/test/chromedriver/chrome/devtools_client.h"
9 #include "chrome/test/chromedriver/chrome/status.h"
10
11 JavaScriptDialogManager::JavaScriptDialogManager(DevToolsClient* client)
12     : client_(client) {
13   client_->AddListener(this);
14 }
15
16 JavaScriptDialogManager::~JavaScriptDialogManager() {}
17
18 bool JavaScriptDialogManager::IsDialogOpen() {
19   return !unhandled_dialog_queue_.empty();
20 }
21
22 Status JavaScriptDialogManager::GetDialogMessage(std::string* message) {
23   if (!IsDialogOpen())
24     return Status(kNoAlertOpen);
25
26   *message = unhandled_dialog_queue_.front();
27   return Status(kOk);
28 }
29
30 Status JavaScriptDialogManager::HandleDialog(bool accept,
31                                              const std::string* text) {
32   if (!IsDialogOpen())
33     return Status(kNoAlertOpen);
34
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;
42
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);
49 }
50
51 Status JavaScriptDialogManager::OnConnected(DevToolsClient* client) {
52   unhandled_dialog_queue_.clear();
53   base::DictionaryValue params;
54   return client_->SendCommand("Page.enable", params);
55 }
56
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'");
64
65     unhandled_dialog_queue_.push_back(message);
66   } else if (method == "Page.javascriptDialogClosing") {
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();
70   }
71   return Status(kOk);
72 }