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