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.
5 #include "extensions/browser/guest_view/web_view/javascript_dialog_helper.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/values.h"
9 #include "components/guest_view/common/guest_view_constants.h"
10 #include "extensions/browser/guest_view/web_view/web_view_constants.h"
11 #include "extensions/browser/guest_view/web_view/web_view_guest.h"
12 #include "extensions/browser/guest_view/web_view/web_view_permission_helper.h"
13 #include "extensions/browser/guest_view/web_view/web_view_permission_types.h"
15 namespace extensions
{
19 std::string
JavaScriptMessageTypeToString(
20 content::JavaScriptMessageType message_type
) {
21 switch (message_type
) {
22 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT
:
24 case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM
:
26 case content::JAVASCRIPT_MESSAGE_TYPE_PROMPT
:
29 NOTREACHED() << "Unknown JavaScript Message Type.";
36 JavaScriptDialogHelper::JavaScriptDialogHelper(WebViewGuest
* guest
)
37 : web_view_guest_(guest
) {
40 JavaScriptDialogHelper::~JavaScriptDialogHelper() {
43 void 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
;
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(guest_view::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
,
67 base::Bind(&JavaScriptDialogHelper::OnPermissionResponse
,
68 base::Unretained(this),
70 false /* allowed_by_default */);
73 void JavaScriptDialogHelper::RunBeforeUnloadDialog(
74 content::WebContents
* web_contents
,
75 const base::string16
& message_text
,
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());
83 bool JavaScriptDialogHelper::HandleJavaScriptDialog(
84 content::WebContents
* web_contents
,
86 const base::string16
* prompt_override
) {
90 void JavaScriptDialogHelper::CancelActiveAndPendingDialogs(
91 content::WebContents
* web_contents
) {
94 void JavaScriptDialogHelper::ResetDialogState(
95 content::WebContents
* web_contents
) {
98 void JavaScriptDialogHelper::OnPermissionResponse(
99 const DialogClosedCallback
& callback
,
101 const std::string
& user_input
) {
102 callback
.Run(allow
&& web_view_guest_
->attached(),
103 base::UTF8ToUTF16(user_input
));
106 } // namespace extensions