1// Copyright 2014 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 "extensions/browser/guest_view/web_view/javascript_dialog_helper.h"
6
7#include "base/strings/utf_string_conversions.h"
8#include "base/values.h"
9#include "extensions/browser/guest_view/web_view/web_view_constants.h"
10#include "extensions/browser/guest_view/web_view/web_view_guest.h"
11#include "extensions/browser/guest_view/web_view/web_view_permission_helper.h"
12#include "extensions/browser/guest_view/web_view/web_view_permission_types.h"
13#include "extensions/common/guest_view/guest_view_constants.h"
14
15namespace extensions {
16
17namespace {
18
19std::string JavaScriptMessageTypeToString(
20    content::JavaScriptMessageType message_type) {
21  switch (message_type) {
22    case content::JAVASCRIPT_MESSAGE_TYPE_ALERT:
23      return "alert";
24    case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM:
25      return "confirm";
26    case content::JAVASCRIPT_MESSAGE_TYPE_PROMPT:
27      return "prompt";
28    default:
29      NOTREACHED() << "Unknown JavaScript Message Type.";
30      return "unknown";
31  }
32}
33
34}  // namespace
35
36JavaScriptDialogHelper::JavaScriptDialogHelper(WebViewGuest* guest)
37    : web_view_guest_(guest) {
38}
39
40JavaScriptDialogHelper::~JavaScriptDialogHelper() {
41}
42
43void JavaScriptDialogHelper::RunJavaScriptDialog(
44    content::WebContents* web_contents,
45    const GURL& origin_url,
46    const std::string& accept_lang,
47    content::JavaScriptMessageType javascript_message_type,
48    const base::string16& message_text,
49    const base::string16& default_prompt_text,
50    const DialogClosedCallback& callback,
51    bool* did_suppress_message) {
52  base::DictionaryValue request_info;
53  request_info.Set(
54      webview::kDefaultPromptText,
55      new base::StringValue(base::UTF16ToUTF8(default_prompt_text)));
56  request_info.Set(webview::kMessageText,
57                   new base::StringValue(base::UTF16ToUTF8(message_text)));
58  request_info.Set(webview::kMessageType,
59                   new base::StringValue(
60                       JavaScriptMessageTypeToString(javascript_message_type)));
61  request_info.Set(guestview::kUrl, new base::StringValue(origin_url.spec()));
62  WebViewPermissionHelper* web_view_permission_helper =
63      WebViewPermissionHelper::FromWebContents(web_contents);
64  web_view_permission_helper->RequestPermission(
65      WEB_VIEW_PERMISSION_TYPE_JAVASCRIPT_DIALOG,
66      request_info,
67      base::Bind(&JavaScriptDialogHelper::OnPermissionResponse,
68                 base::Unretained(this),
69                 callback),
70      false /* allowed_by_default */);
71}
72
73void JavaScriptDialogHelper::RunBeforeUnloadDialog(
74    content::WebContents* web_contents,
75    const base::string16& message_text,
76    bool is_reload,
77    const DialogClosedCallback& callback) {
78  // This is called if the guest has a beforeunload event handler.
79  // This callback allows navigation to proceed.
80  callback.Run(true, base::string16());
81}
82
83bool JavaScriptDialogHelper::HandleJavaScriptDialog(
84    content::WebContents* web_contents,
85    bool accept,
86    const base::string16* prompt_override) {
87  return false;
88}
89
90void JavaScriptDialogHelper::CancelActiveAndPendingDialogs(
91    content::WebContents* web_contents) {
92}
93
94void JavaScriptDialogHelper::WebContentsDestroyed(
95    content::WebContents* web_contents) {
96}
97
98void JavaScriptDialogHelper::OnPermissionResponse(
99    const DialogClosedCallback& callback,
100    bool allow,
101    const std::string& user_input) {
102  callback.Run(allow && web_view_guest_->attached(),
103               base::UTF8ToUTF16(user_input));
104}
105
106}  // namespace extensions
107