Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: ios/web/web_state/ui/crw_web_controller.mm

Issue 2074733002: Add public API for handling Javascript alerts and prompt. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add unittests. Address missed comments. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "ios/web/web_state/ui/crw_web_controller.h" 5 #import "ios/web/web_state/ui/crw_web_controller.h"
6 6
7 #import <WebKit/WebKit.h> 7 #import <WebKit/WebKit.h>
8 8
9 #import <objc/runtime.h> 9 #import <objc/runtime.h>
10 #include <stddef.h> 10 #include <stddef.h>
11 11
12 #include <cmath> 12 #include <cmath>
13 #include <memory> 13 #include <memory>
14 #include <utility> 14 #include <utility>
15 15
16 #include "base/callback.h"
16 #include "base/containers/mru_cache.h" 17 #include "base/containers/mru_cache.h"
17 #include "base/ios/block_types.h" 18 #include "base/ios/block_types.h"
18 #include "base/ios/ios_util.h" 19 #include "base/ios/ios_util.h"
19 #import "base/ios/ns_error_util.h" 20 #import "base/ios/ns_error_util.h"
20 #include "base/ios/weak_nsobject.h" 21 #include "base/ios/weak_nsobject.h"
21 #include "base/json/json_reader.h" 22 #include "base/json/json_reader.h"
22 #include "base/json/json_writer.h" 23 #include "base/json/json_writer.h"
23 #include "base/json/string_escape.h" 24 #include "base/json/string_escape.h"
24 #include "base/logging.h" 25 #include "base/logging.h"
25 #include "base/mac/bind_objc_block.h" 26 #include "base/mac/bind_objc_block.h"
(...skipping 21 matching lines...) Expand all
47 #import "ios/web/navigation/crw_session_entry.h" 48 #import "ios/web/navigation/crw_session_entry.h"
48 #import "ios/web/navigation/navigation_item_impl.h" 49 #import "ios/web/navigation/navigation_item_impl.h"
49 #import "ios/web/navigation/navigation_manager_impl.h" 50 #import "ios/web/navigation/navigation_manager_impl.h"
50 #include "ios/web/net/cert_host_pair.h" 51 #include "ios/web/net/cert_host_pair.h"
51 #import "ios/web/net/crw_cert_verification_controller.h" 52 #import "ios/web/net/crw_cert_verification_controller.h"
52 #import "ios/web/net/crw_ssl_status_updater.h" 53 #import "ios/web/net/crw_ssl_status_updater.h"
53 #include "ios/web/net/request_group_util.h" 54 #include "ios/web/net/request_group_util.h"
54 #include "ios/web/public/browser_state.h" 55 #include "ios/web/public/browser_state.h"
55 #include "ios/web/public/cert_store.h" 56 #include "ios/web/public/cert_store.h"
56 #include "ios/web/public/favicon_url.h" 57 #include "ios/web/public/favicon_url.h"
58 #import "ios/web/public/java_script_dialog_presenter.h"
57 #include "ios/web/public/navigation_item.h" 59 #include "ios/web/public/navigation_item.h"
58 #import "ios/web/public/navigation_manager.h" 60 #import "ios/web/public/navigation_manager.h"
59 #import "ios/web/public/origin_util.h" 61 #import "ios/web/public/origin_util.h"
60 #include "ios/web/public/referrer.h" 62 #include "ios/web/public/referrer.h"
61 #include "ios/web/public/referrer_util.h" 63 #include "ios/web/public/referrer_util.h"
62 #include "ios/web/public/ssl_status.h" 64 #include "ios/web/public/ssl_status.h"
63 #import "ios/web/public/url_scheme_util.h" 65 #import "ios/web/public/url_scheme_util.h"
64 #include "ios/web/public/url_util.h" 66 #include "ios/web/public/url_util.h"
65 #include "ios/web/public/user_metrics.h" 67 #include "ios/web/public/user_metrics.h"
66 #include "ios/web/public/web_client.h" 68 #include "ios/web/public/web_client.h"
(...skipping 778 matching lines...) Expand 10 before | Expand all | Expand 10 after
845 completionHandler: 847 completionHandler:
846 (void (^)(NSURLSessionAuthChallengeDisposition, 848 (void (^)(NSURLSessionAuthChallengeDisposition,
847 NSURLCredential*))completionHandler; 849 NSURLCredential*))completionHandler;
848 // Used in webView:didReceiveAuthenticationChallenge:completionHandler: to reply 850 // Used in webView:didReceiveAuthenticationChallenge:completionHandler: to reply
849 // with NSURLSessionAuthChallengeDisposition and credentials. 851 // with NSURLSessionAuthChallengeDisposition and credentials.
850 + (void)processHTTPAuthForUser:(NSString*)user 852 + (void)processHTTPAuthForUser:(NSString*)user
851 password:(NSString*)password 853 password:(NSString*)password
852 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, 854 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition,
853 NSURLCredential*))completionHandler; 855 NSURLCredential*))completionHandler;
854 856
857 // Helper to respond to |webView:runJavaScript...| delegate methods.
858 // |completionHandler| must not be nil.
859 - (void)runJavaScriptDialogOfType:(web::JavaScriptDialogType)type
860 initiatedByFrame:(WKFrameInfo*)frame
861 message:(NSString*)message
862 defaultText:(NSString*)defaultText
863 completion:(void (^)(BOOL, NSString*))completionHandler;
864
855 // Called when WKWebView estimatedProgress has been changed. 865 // Called when WKWebView estimatedProgress has been changed.
856 - (void)webViewEstimatedProgressDidChange; 866 - (void)webViewEstimatedProgressDidChange;
857 // Called when WKWebView certificateChain or hasOnlySecureContent property has 867 // Called when WKWebView certificateChain or hasOnlySecureContent property has
858 // changed. 868 // changed.
859 - (void)webViewSecurityFeaturesDidChange; 869 - (void)webViewSecurityFeaturesDidChange;
860 // Called when WKWebView loading state has been changed. 870 // Called when WKWebView loading state has been changed.
861 - (void)webViewLoadingStateDidChange; 871 - (void)webViewLoadingStateDidChange;
862 // Called when WKWebView title has been changed. 872 // Called when WKWebView title has been changed.
863 - (void)webViewTitleDidChange; 873 - (void)webViewTitleDidChange;
864 // Called when WKWebView URL has been changed. 874 // Called when WKWebView URL has been changed.
(...skipping 2888 matching lines...) Expand 10 before | Expand all | Expand 10 after
3753 } 3763 }
3754 completionHandler( 3764 completionHandler(
3755 NSURLSessionAuthChallengeUseCredential, 3765 NSURLSessionAuthChallengeUseCredential,
3756 [NSURLCredential 3766 [NSURLCredential
3757 credentialWithUser:user 3767 credentialWithUser:user
3758 password:password 3768 password:password
3759 persistence:NSURLCredentialPersistenceForSession]); 3769 persistence:NSURLCredentialPersistenceForSession]);
3760 } 3770 }
3761 3771
3762 #pragma mark - 3772 #pragma mark -
3773 #pragma mark JavaScript Dialog
3774
3775 - (void)runJavaScriptDialogOfType:(web::JavaScriptDialogType)type
3776 initiatedByFrame:(WKFrameInfo*)frame
3777 message:(NSString*)message
3778 defaultText:(NSString*)defaultText
3779 completion:(void (^)(BOOL, NSString*))completionHandler {
3780 self.webStateImpl->RunJavaScriptDialog(
3781 net::GURLWithNSURL(frame.request.URL), type, message, defaultText,
Eugene But (OOO till 7-30) 2016/06/28 18:14:48 Could you please test that this works correctly fo
michaeldo 2016/06/28 21:58:27 Yes, I will test that it works correctly.
3782 base::BindBlock(^(bool success, NSString* input) {
3783 completionHandler(success, input);
3784 }));
3785 }
3786
3787 #pragma mark -
3763 #pragma mark TouchTracking 3788 #pragma mark TouchTracking
3764 3789
3765 - (void)touched:(BOOL)touched { 3790 - (void)touched:(BOOL)touched {
3766 _clickInProgress = touched; 3791 _clickInProgress = touched;
3767 if (touched) { 3792 if (touched) {
3768 self.userInteractionRegistered = YES; 3793 self.userInteractionRegistered = YES;
3769 _userInteractedWithWebController = YES; 3794 _userInteractedWithWebController = YES;
3770 if (_isBeingDestroyed) 3795 if (_isBeingDestroyed)
3771 return; 3796 return;
3772 const web::NavigationManagerImpl& navigationManager = 3797 const web::NavigationManagerImpl& navigationManager =
(...skipping 913 matching lines...) Expand 10 before | Expand all | Expand 10 after
4686 [self setDocumentURL:_defaultURL]; 4711 [self setDocumentURL:_defaultURL];
4687 } 4712 }
4688 4713
4689 - (void)removeWebViewAllowingCachedReconstruction:(BOOL)allowCache { 4714 - (void)removeWebViewAllowingCachedReconstruction:(BOOL)allowCache {
4690 if (!_webView) 4715 if (!_webView)
4691 return; 4716 return;
4692 4717
4693 SEL cancelDialogsSelector = @selector(cancelDialogsForWebController:); 4718 SEL cancelDialogsSelector = @selector(cancelDialogsForWebController:);
4694 if ([self.UIDelegate respondsToSelector:cancelDialogsSelector]) 4719 if ([self.UIDelegate respondsToSelector:cancelDialogsSelector])
4695 [self.UIDelegate cancelDialogsForWebController:self]; 4720 [self.UIDelegate cancelDialogsForWebController:self];
4721 _webStateImpl->CancelActiveAndPendingDialogs();
4696 4722
4697 if (allowCache) 4723 if (allowCache)
4698 _expectedReconstructionURL = [self currentNavigationURL]; 4724 _expectedReconstructionURL = [self currentNavigationURL];
4699 else 4725 else
4700 _expectedReconstructionURL = GURL(); 4726 _expectedReconstructionURL = GURL();
4701 4727
4702 [self abortLoad]; 4728 [self abortLoad];
4703 [_webView removeFromSuperview]; 4729 [_webView removeFromSuperview];
4704 [_containerView resetContent]; 4730 [_containerView resetContent];
4705 [self setWebView:nil]; 4731 [self setWebView:nil];
4706 } 4732 }
4707 4733
4708 - (void)webViewWebProcessDidCrash { 4734 - (void)webViewWebProcessDidCrash {
4709 _webProcessIsDead = YES; 4735 _webProcessIsDead = YES;
4710 4736
4711 SEL cancelDialogsSelector = @selector(cancelDialogsForWebController:); 4737 SEL cancelDialogsSelector = @selector(cancelDialogsForWebController:);
4712 if ([self.UIDelegate respondsToSelector:cancelDialogsSelector]) 4738 if ([self.UIDelegate respondsToSelector:cancelDialogsSelector])
4713 [self.UIDelegate cancelDialogsForWebController:self]; 4739 [self.UIDelegate cancelDialogsForWebController:self];
4740 _webStateImpl->CancelActiveAndPendingDialogs();
4714 4741
4715 SEL rendererCrashSelector = @selector(webControllerWebProcessDidCrash:); 4742 SEL rendererCrashSelector = @selector(webControllerWebProcessDidCrash:);
4716 if ([self.delegate respondsToSelector:rendererCrashSelector]) 4743 if ([self.delegate respondsToSelector:rendererCrashSelector])
4717 [self.delegate webControllerWebProcessDidCrash:self]; 4744 [self.delegate webControllerWebProcessDidCrash:self];
4718 } 4745 }
4719 4746
4720 - (web::WKWebViewConfigurationProvider&)webViewConfigurationProvider { 4747 - (web::WKWebViewConfigurationProvider&)webViewConfigurationProvider {
4721 web::BrowserState* browserState = self.webStateImpl->GetBrowserState(); 4748 web::BrowserState* browserState = self.webStateImpl->GetBrowserState();
4722 return web::WKWebViewConfigurationProvider::FromBrowserState(browserState); 4749 return web::WKWebViewConfigurationProvider::FromBrowserState(browserState);
4723 } 4750 }
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
4844 4871
4845 SEL alertSelector = @selector(webController: 4872 SEL alertSelector = @selector(webController:
4846 runJavaScriptAlertPanelWithMessage: 4873 runJavaScriptAlertPanelWithMessage:
4847 requestURL: 4874 requestURL:
4848 completionHandler:); 4875 completionHandler:);
4849 if ([self.UIDelegate respondsToSelector:alertSelector]) { 4876 if ([self.UIDelegate respondsToSelector:alertSelector]) {
4850 [self.UIDelegate webController:self 4877 [self.UIDelegate webController:self
4851 runJavaScriptAlertPanelWithMessage:message 4878 runJavaScriptAlertPanelWithMessage:message
4852 requestURL:net::GURLWithNSURL(frame.request.URL) 4879 requestURL:net::GURLWithNSURL(frame.request.URL)
4853 completionHandler:completionHandler]; 4880 completionHandler:completionHandler];
4854 } else if (completionHandler) { 4881 } else {
4855 completionHandler(); 4882 [self runJavaScriptDialogOfType:web::JAVASCRIPT_DIALOG_TYPE_ALERT
4883 initiatedByFrame:frame
4884 message:message
4885 defaultText:nil
4886 completion:^(BOOL, NSString*) {
4887 completionHandler();
4888 }];
4856 } 4889 }
4857 } 4890 }
4858 4891
4859 - (void)webView:(WKWebView*)webView 4892 - (void)webView:(WKWebView*)webView
4860 runJavaScriptConfirmPanelWithMessage:(NSString*)message 4893 runJavaScriptConfirmPanelWithMessage:(NSString*)message
4861 initiatedByFrame:(WKFrameInfo*)frame 4894 initiatedByFrame:(WKFrameInfo*)frame
4862 completionHandler: 4895 completionHandler:
4863 (void (^)(BOOL result))completionHandler { 4896 (void (^)(BOOL result))completionHandler {
4864 if (self.shouldSuppressDialogs) { 4897 if (self.shouldSuppressDialogs) {
4865 [self didSuppressDialog]; 4898 [self didSuppressDialog];
4866 completionHandler(NO); 4899 completionHandler(NO);
4867 return; 4900 return;
4868 } 4901 }
4869 4902
4870 SEL confirmationSelector = @selector(webController: 4903 SEL confirmationSelector = @selector(webController:
4871 runJavaScriptConfirmPanelWithMessage: 4904 runJavaScriptConfirmPanelWithMessage:
4872 requestURL: 4905 requestURL:
4873 completionHandler:); 4906 completionHandler:);
4874 if ([self.UIDelegate respondsToSelector:confirmationSelector]) { 4907 if ([self.UIDelegate respondsToSelector:confirmationSelector]) {
4875 [self.UIDelegate webController:self 4908 [self.UIDelegate webController:self
4876 runJavaScriptConfirmPanelWithMessage:message 4909 runJavaScriptConfirmPanelWithMessage:message
4877 requestURL:net::GURLWithNSURL( 4910 requestURL:net::GURLWithNSURL(
4878 frame.request.URL) 4911 frame.request.URL)
4879 completionHandler:completionHandler]; 4912 completionHandler:completionHandler];
4880 } else if (completionHandler) { 4913 } else {
4881 completionHandler(NO); 4914 [self runJavaScriptDialogOfType:web::JAVASCRIPT_DIALOG_TYPE_CONFIRM
4915 initiatedByFrame:frame
4916 message:message
4917 defaultText:nil
4918 completion:^(BOOL success, NSString*) {
4919 if (completionHandler) {
4920 completionHandler(success);
4921 }
4922 }];
4882 } 4923 }
4883 } 4924 }
4884 4925
4885 - (void)webView:(WKWebView*)webView 4926 - (void)webView:(WKWebView*)webView
4886 runJavaScriptTextInputPanelWithPrompt:(NSString*)prompt 4927 runJavaScriptTextInputPanelWithPrompt:(NSString*)prompt
4887 defaultText:(NSString*)defaultText 4928 defaultText:(NSString*)defaultText
4888 initiatedByFrame:(WKFrameInfo*)frame 4929 initiatedByFrame:(WKFrameInfo*)frame
4889 completionHandler: 4930 completionHandler:
4890 (void (^)(NSString* result))completionHandler { 4931 (void (^)(NSString* result))completionHandler {
4891 GURL origin(web::GURLOriginWithWKSecurityOrigin(frame.securityOrigin)); 4932 GURL origin(web::GURLOriginWithWKSecurityOrigin(frame.securityOrigin));
(...skipping 16 matching lines...) Expand all
4908 defaultText: 4949 defaultText:
4909 requestURL: 4950 requestURL:
4910 completionHandler:); 4951 completionHandler:);
4911 if ([self.UIDelegate respondsToSelector:textInputSelector]) { 4952 if ([self.UIDelegate respondsToSelector:textInputSelector]) {
4912 GURL requestURL = net::GURLWithNSURL(frame.request.URL); 4953 GURL requestURL = net::GURLWithNSURL(frame.request.URL);
4913 [self.UIDelegate webController:self 4954 [self.UIDelegate webController:self
4914 runJavaScriptTextInputPanelWithPrompt:prompt 4955 runJavaScriptTextInputPanelWithPrompt:prompt
4915 defaultText:defaultText 4956 defaultText:defaultText
4916 requestURL:requestURL 4957 requestURL:requestURL
4917 completionHandler:completionHandler]; 4958 completionHandler:completionHandler];
4918 } else if (completionHandler) { 4959 } else {
4919 completionHandler(nil); 4960 [self runJavaScriptDialogOfType:web::JAVASCRIPT_DIALOG_TYPE_PROMPT
4961 initiatedByFrame:frame
4962 message:prompt
4963 defaultText:defaultText
4964 completion:^(BOOL, NSString* input) {
4965 if (completionHandler) {
4966 completionHandler(input);
4967 }
4968 }];
4920 } 4969 }
4921 } 4970 }
4922 4971
4923 #pragma mark - 4972 #pragma mark -
4924 #pragma mark WKNavigationDelegate Methods 4973 #pragma mark WKNavigationDelegate Methods
4925 4974
4926 - (void)webView:(WKWebView*)webView 4975 - (void)webView:(WKWebView*)webView
4927 decidePolicyForNavigationAction:(WKNavigationAction*)action 4976 decidePolicyForNavigationAction:(WKNavigationAction*)action
4928 decisionHandler: 4977 decisionHandler:
4929 (void (^)(WKNavigationActionPolicy))decisionHandler { 4978 (void (^)(WKNavigationActionPolicy))decisionHandler {
(...skipping 676 matching lines...) Expand 10 before | Expand all | Expand 10 after
5606 } 5655 }
5607 5656
5608 return web::WEB_VIEW_DOCUMENT_TYPE_GENERIC; 5657 return web::WEB_VIEW_DOCUMENT_TYPE_GENERIC;
5609 } 5658 }
5610 5659
5611 - (NSString*)refererFromNavigationAction:(WKNavigationAction*)action { 5660 - (NSString*)refererFromNavigationAction:(WKNavigationAction*)action {
5612 return [action.request valueForHTTPHeaderField:@"Referer"]; 5661 return [action.request valueForHTTPHeaderField:@"Referer"];
5613 } 5662 }
5614 5663
5615 @end 5664 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698