| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 #import "ios/web_view/internal/web_view_java_script_dialog_presenter.h" |
| 6 |
| 7 #import "ios/web_view/public/cwv_ui_delegate.h" |
| 8 #import "net/base/mac/url_conversions.h" |
| 9 |
| 10 namespace ios_web_view { |
| 11 |
| 12 WebViewJavaScriptDialogPresenter::WebViewJavaScriptDialogPresenter( |
| 13 CWVWebView* web_view, |
| 14 id<CWVUIDelegate> ui_delegate) |
| 15 : ui_delegate_(ui_delegate), web_view_(web_view) {} |
| 16 |
| 17 WebViewJavaScriptDialogPresenter::~WebViewJavaScriptDialogPresenter() = default; |
| 18 |
| 19 void WebViewJavaScriptDialogPresenter::RunJavaScriptDialog( |
| 20 web::WebState* web_state, |
| 21 const GURL& origin_url, |
| 22 web::JavaScriptDialogType dialog_type, |
| 23 NSString* message_text, |
| 24 NSString* default_prompt_text, |
| 25 const web::DialogClosedCallback& callback) { |
| 26 switch (dialog_type) { |
| 27 case web::JAVASCRIPT_DIALOG_TYPE_ALERT: |
| 28 HandleJavaScriptAlert(origin_url, message_text, callback); |
| 29 break; |
| 30 case web::JAVASCRIPT_DIALOG_TYPE_CONFIRM: |
| 31 HandleJavaScriptConfirmDialog(origin_url, message_text, callback); |
| 32 break; |
| 33 case web::JAVASCRIPT_DIALOG_TYPE_PROMPT: |
| 34 HandleJavaScriptTextPrompt(origin_url, message_text, default_prompt_text, |
| 35 callback); |
| 36 break; |
| 37 } |
| 38 } |
| 39 |
| 40 void WebViewJavaScriptDialogPresenter::HandleJavaScriptAlert( |
| 41 const GURL& origin_url, |
| 42 NSString* message_text, |
| 43 const web::DialogClosedCallback& callback) { |
| 44 if (![ui_delegate_ respondsToSelector:@selector |
| 45 (webView:runJavaScriptAlertPanelWithMessage:pageURL |
| 46 :completionHandler:)]) { |
| 47 callback.Run(NO, nil); |
| 48 return; |
| 49 } |
| 50 web::DialogClosedCallback scoped_callback = callback; |
| 51 [ui_delegate_ webView:web_view_ |
| 52 runJavaScriptAlertPanelWithMessage:message_text |
| 53 pageURL:net::NSURLWithGURL(origin_url) |
| 54 completionHandler:^{ |
| 55 if (!scoped_callback.is_null()) { |
| 56 scoped_callback.Run(YES, nil); |
| 57 } |
| 58 }]; |
| 59 } |
| 60 |
| 61 void WebViewJavaScriptDialogPresenter::HandleJavaScriptConfirmDialog( |
| 62 const GURL& origin_url, |
| 63 NSString* message_text, |
| 64 const web::DialogClosedCallback& callback) { |
| 65 if (![ui_delegate_ respondsToSelector:@selector |
| 66 (webView:runJavaScriptConfirmPanelWithMessage:pageURL |
| 67 :completionHandler:)]) { |
| 68 callback.Run(NO, nil); |
| 69 return; |
| 70 } |
| 71 web::DialogClosedCallback scoped_callback = callback; |
| 72 [ui_delegate_ webView:web_view_ |
| 73 runJavaScriptConfirmPanelWithMessage:message_text |
| 74 pageURL:net::NSURLWithGURL(origin_url) |
| 75 completionHandler:^(BOOL is_confirmed) { |
| 76 if (!scoped_callback.is_null()) { |
| 77 scoped_callback.Run(is_confirmed, nil); |
| 78 } |
| 79 }]; |
| 80 } |
| 81 |
| 82 void WebViewJavaScriptDialogPresenter::HandleJavaScriptTextPrompt( |
| 83 const GURL& origin_url, |
| 84 NSString* message_text, |
| 85 NSString* default_prompt_text, |
| 86 const web::DialogClosedCallback& callback) { |
| 87 if (![ui_delegate_ respondsToSelector:@selector |
| 88 (webView:runJavaScriptTextInputPanelWithPrompt:defaultText |
| 89 :pageURL:completionHandler:)]) { |
| 90 callback.Run(NO, nil); |
| 91 return; |
| 92 } |
| 93 web::DialogClosedCallback scoped_callback = callback; |
| 94 [ui_delegate_ webView:web_view_ |
| 95 runJavaScriptTextInputPanelWithPrompt:message_text |
| 96 defaultText:default_prompt_text |
| 97 pageURL:net::NSURLWithGURL(origin_url) |
| 98 completionHandler:^(NSString* text_input) { |
| 99 if (!scoped_callback.is_null()) { |
| 100 if (text_input == nil) { |
| 101 scoped_callback.Run(NO, nil); |
| 102 } else { |
| 103 scoped_callback.Run(YES, text_input); |
| 104 } |
| 105 } |
| 106 }]; |
| 107 } |
| 108 |
| 109 void WebViewJavaScriptDialogPresenter::CancelDialogs(web::WebState* web_state) { |
| 110 } |
| 111 |
| 112 void WebViewJavaScriptDialogPresenter::SetUIDelegate( |
| 113 id<CWVUIDelegate> ui_delegate) { |
| 114 ui_delegate_.reset(ui_delegate); |
| 115 } |
| 116 |
| 117 } // namespace ios_web_view |
| OLD | NEW |