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

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: Cleanup. Created 4 years, 6 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 819 matching lines...) Expand 10 before | Expand all | Expand 10 after
845 completionHandler: 846 completionHandler:
846 (void (^)(NSURLSessionAuthChallengeDisposition, 847 (void (^)(NSURLSessionAuthChallengeDisposition,
847 NSURLCredential*))completionHandler; 848 NSURLCredential*))completionHandler;
848 // Used in webView:didReceiveAuthenticationChallenge:completionHandler: to reply 849 // Used in webView:didReceiveAuthenticationChallenge:completionHandler: to reply
849 // with NSURLSessionAuthChallengeDisposition and credentials. 850 // with NSURLSessionAuthChallengeDisposition and credentials.
850 + (void)processHTTPAuthForUser:(NSString*)user 851 + (void)processHTTPAuthForUser:(NSString*)user
851 password:(NSString*)password 852 password:(NSString*)password
852 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, 853 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition,
853 NSURLCredential*))completionHandler; 854 NSURLCredential*))completionHandler;
854 855
856 // Helper to respond to |webView:runJavaScript...| delegate methods.
Eugene But (OOO till 7-30) 2016/06/24 17:04:52 Please document whether or not |completionHandler|
michaeldo 2016/06/27 20:30:10 you're right, made it non-nil.
857 - (void)runJavaScriptDialogOfType:(web::JavaScriptMessageType)type
858 initiatedByFrame:(WKFrameInfo*)frame
859 message:(NSString*)message
860 defaultText:(NSString*)defaultText
861 completion:(void (^)(BOOL, NSString*))completionHandler;
862
855 // Called when WKWebView estimatedProgress has been changed. 863 // Called when WKWebView estimatedProgress has been changed.
856 - (void)webViewEstimatedProgressDidChange; 864 - (void)webViewEstimatedProgressDidChange;
857 // Called when WKWebView certificateChain or hasOnlySecureContent property has 865 // Called when WKWebView certificateChain or hasOnlySecureContent property has
858 // changed. 866 // changed.
859 - (void)webViewSecurityFeaturesDidChange; 867 - (void)webViewSecurityFeaturesDidChange;
860 // Called when WKWebView loading state has been changed. 868 // Called when WKWebView loading state has been changed.
861 - (void)webViewLoadingStateDidChange; 869 - (void)webViewLoadingStateDidChange;
862 // Called when WKWebView title has been changed. 870 // Called when WKWebView title has been changed.
863 - (void)webViewTitleDidChange; 871 - (void)webViewTitleDidChange;
864 // Called when WKWebView URL has been changed. 872 // Called when WKWebView URL has been changed.
(...skipping 2845 matching lines...) Expand 10 before | Expand all | Expand 10 after
3710 if (self.shouldSuppressDialogs) { 3718 if (self.shouldSuppressDialogs) {
3711 [self didSuppressDialog]; 3719 [self didSuppressDialog];
3712 completionHandler(NSURLSessionAuthChallengeRejectProtectionSpace, nil); 3720 completionHandler(NSURLSessionAuthChallengeRejectProtectionSpace, nil);
3713 return; 3721 return;
3714 } 3722 }
3715 3723
3716 SEL selector = @selector(webController: 3724 SEL selector = @selector(webController:
3717 runAuthDialogForProtectionSpace: 3725 runAuthDialogForProtectionSpace:
3718 proposedCredential: 3726 proposedCredential:
3719 completionHandler:); 3727 completionHandler:);
3720 if (![self.UIDelegate respondsToSelector:selector]) { 3728 if ([self.UIDelegate respondsToSelector:selector]) {
3721 // Embedder does not support HTTP Authentication. 3729 [self.UIDelegate webController:self
3722 completionHandler(NSURLSessionAuthChallengeRejectProtectionSpace, nil); 3730 runAuthDialogForProtectionSpace:space
3723 return; 3731 proposedCredential:challenge.proposedCredential
3732 completionHandler:^(NSString* user, NSString* password) {
3733 [CRWWebController
3734 processHTTPAuthForUser:user
3735 password:password
3736 completionHandler:completionHandler];
3737 }];
3738 } else {
3739 void (^callback)(NSURLSessionAuthChallengeDisposition, NSURLCredential*) =
3740 nil;
3741 if (completionHandler) {
3742 callback = [completionHandler copy];
3743 }
3744 self.webStateImpl->RunAuthDialog(
3745 space, challenge.proposedCredential,
3746 base::BindBlock(^(const net::AuthCredentials& credentials) {
3747 if (callback) {
3748 [CRWWebController processHTTPAuthForUser:base::SysUTF16ToNSString(
3749 credentials.username())
3750 password:base::SysUTF16ToNSString(
3751 credentials.password())
3752 completionHandler:callback];
3753 [callback release];
3754 }
3755 }));
3724 } 3756 }
3725
3726 [self.UIDelegate webController:self
3727 runAuthDialogForProtectionSpace:space
3728 proposedCredential:challenge.proposedCredential
3729 completionHandler:^(NSString* user, NSString* password) {
3730 [CRWWebController
3731 processHTTPAuthForUser:user
3732 password:password
3733 completionHandler:completionHandler];
3734 }];
3735 } 3757 }
3736 3758
3737 + (void)processHTTPAuthForUser:(NSString*)user 3759 + (void)processHTTPAuthForUser:(NSString*)user
3738 password:(NSString*)password 3760 password:(NSString*)password
3739 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, 3761 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition,
3740 NSURLCredential*))completionHandler { 3762 NSURLCredential*))completionHandler {
3741 DCHECK_EQ(user == nil, password == nil);
3742 if (!user || !password) { 3763 if (!user || !password) {
3743 // Embedder cancelled authentication. 3764 // Embedder cancelled authentication or embedder does not support HTTP
3765 // Authentication.
3744 completionHandler(NSURLSessionAuthChallengeRejectProtectionSpace, nil); 3766 completionHandler(NSURLSessionAuthChallengeRejectProtectionSpace, nil);
3745 return; 3767 return;
3746 } 3768 }
3747 completionHandler( 3769 completionHandler(
3748 NSURLSessionAuthChallengeUseCredential, 3770 NSURLSessionAuthChallengeUseCredential,
3749 [NSURLCredential 3771 [NSURLCredential
3750 credentialWithUser:user 3772 credentialWithUser:user
3751 password:password 3773 password:password
3752 persistence:NSURLCredentialPersistenceForSession]); 3774 persistence:NSURLCredentialPersistenceForSession]);
3753 } 3775 }
3754 3776
3755 #pragma mark - 3777 #pragma mark -
3778 #pragma mark JavaScript Dialog
3779
3780 - (void)runJavaScriptDialogOfType:(web::JavaScriptMessageType)type
3781 initiatedByFrame:(WKFrameInfo*)frame
3782 message:(NSString*)message
3783 defaultText:(NSString*)defaultText
3784 completion:(void (^)(BOOL, NSString*))completionHandler {
Eugene But (OOO till 7-30) 2016/06/24 17:04:52 I think if you always pass |completionHandler| the
michaeldo 2016/06/27 20:30:10 Agreed, made the completion handler required per y
3785 void (^callback)(BOOL, NSString*) = nil;
3786 if (completionHandler) {
3787 callback = [completionHandler copy];
Eugene But (OOO till 7-30) 2016/06/24 17:04:52 Why do you need a separate |callback| variable? Ca
michaeldo 2016/06/27 20:30:10 Done.
3788 }
3789 self.webStateImpl->RunJavaScriptDialog(
3790 net::GURLWithNSURL(frame.request.URL), type, message, defaultText,
3791 base::BindBlock(^(bool success, NSString* input) {
3792 if (callback) {
3793 callback(success, input);
3794 [callback release];
3795 }
3796 }));
3797 }
3798
3799 #pragma mark -
3756 #pragma mark TouchTracking 3800 #pragma mark TouchTracking
3757 3801
3758 - (void)touched:(BOOL)touched { 3802 - (void)touched:(BOOL)touched {
3759 _clickInProgress = touched; 3803 _clickInProgress = touched;
3760 if (touched) { 3804 if (touched) {
3761 self.userInteractionRegistered = YES; 3805 self.userInteractionRegistered = YES;
3762 _userInteractedWithWebController = YES; 3806 _userInteractedWithWebController = YES;
3763 if (_isBeingDestroyed) 3807 if (_isBeingDestroyed)
3764 return; 3808 return;
3765 const web::NavigationManagerImpl& navigationManager = 3809 const web::NavigationManagerImpl& navigationManager =
(...skipping 913 matching lines...) Expand 10 before | Expand all | Expand 10 after
4679 [self setDocumentURL:_defaultURL]; 4723 [self setDocumentURL:_defaultURL];
4680 } 4724 }
4681 4725
4682 - (void)removeWebViewAllowingCachedReconstruction:(BOOL)allowCache { 4726 - (void)removeWebViewAllowingCachedReconstruction:(BOOL)allowCache {
4683 if (!_webView) 4727 if (!_webView)
4684 return; 4728 return;
4685 4729
4686 SEL cancelDialogsSelector = @selector(cancelDialogsForWebController:); 4730 SEL cancelDialogsSelector = @selector(cancelDialogsForWebController:);
4687 if ([self.UIDelegate respondsToSelector:cancelDialogsSelector]) 4731 if ([self.UIDelegate respondsToSelector:cancelDialogsSelector])
4688 [self.UIDelegate cancelDialogsForWebController:self]; 4732 [self.UIDelegate cancelDialogsForWebController:self];
4733 _webStateImpl->CancelActiveAndPendingDialogs();
4689 4734
4690 if (allowCache) 4735 if (allowCache)
4691 _expectedReconstructionURL = [self currentNavigationURL]; 4736 _expectedReconstructionURL = [self currentNavigationURL];
4692 else 4737 else
4693 _expectedReconstructionURL = GURL(); 4738 _expectedReconstructionURL = GURL();
4694 4739
4695 [self abortLoad]; 4740 [self abortLoad];
4696 [_webView removeFromSuperview]; 4741 [_webView removeFromSuperview];
4697 [_containerView resetContent]; 4742 [_containerView resetContent];
4698 [self setWebView:nil]; 4743 [self setWebView:nil];
4699 } 4744 }
4700 4745
4701 - (void)webViewWebProcessDidCrash { 4746 - (void)webViewWebProcessDidCrash {
4702 _webProcessIsDead = YES; 4747 _webProcessIsDead = YES;
4703 4748
4704 SEL cancelDialogsSelector = @selector(cancelDialogsForWebController:); 4749 SEL cancelDialogsSelector = @selector(cancelDialogsForWebController:);
4705 if ([self.UIDelegate respondsToSelector:cancelDialogsSelector]) 4750 if ([self.UIDelegate respondsToSelector:cancelDialogsSelector])
4706 [self.UIDelegate cancelDialogsForWebController:self]; 4751 [self.UIDelegate cancelDialogsForWebController:self];
4752 _webStateImpl->CancelActiveAndPendingDialogs();
4707 4753
4708 SEL rendererCrashSelector = @selector(webControllerWebProcessDidCrash:); 4754 SEL rendererCrashSelector = @selector(webControllerWebProcessDidCrash:);
4709 if ([self.delegate respondsToSelector:rendererCrashSelector]) 4755 if ([self.delegate respondsToSelector:rendererCrashSelector])
4710 [self.delegate webControllerWebProcessDidCrash:self]; 4756 [self.delegate webControllerWebProcessDidCrash:self];
4711 } 4757 }
4712 4758
4713 - (web::WKWebViewConfigurationProvider&)webViewConfigurationProvider { 4759 - (web::WKWebViewConfigurationProvider&)webViewConfigurationProvider {
4714 web::BrowserState* browserState = self.webStateImpl->GetBrowserState(); 4760 web::BrowserState* browserState = self.webStateImpl->GetBrowserState();
4715 return web::WKWebViewConfigurationProvider::FromBrowserState(browserState); 4761 return web::WKWebViewConfigurationProvider::FromBrowserState(browserState);
4716 } 4762 }
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
4824 [self.delegate webPageOrderedClose]; 4870 [self.delegate webPageOrderedClose];
4825 } 4871 }
4826 } 4872 }
4827 4873
4828 - (void)webView:(WKWebView*)webView 4874 - (void)webView:(WKWebView*)webView
4829 runJavaScriptAlertPanelWithMessage:(NSString*)message 4875 runJavaScriptAlertPanelWithMessage:(NSString*)message
4830 initiatedByFrame:(WKFrameInfo*)frame 4876 initiatedByFrame:(WKFrameInfo*)frame
4831 completionHandler:(void (^)())completionHandler { 4877 completionHandler:(void (^)())completionHandler {
4832 if (self.shouldSuppressDialogs) { 4878 if (self.shouldSuppressDialogs) {
4833 [self didSuppressDialog]; 4879 [self didSuppressDialog];
4834 completionHandler(); 4880 if (completionHandler) {
4881 completionHandler();
4882 }
4835 return; 4883 return;
4836 } 4884 }
4837 4885
4838 SEL alertSelector = @selector(webController: 4886 SEL alertSelector = @selector(webController:
4839 runJavaScriptAlertPanelWithMessage: 4887 runJavaScriptAlertPanelWithMessage:
4840 requestURL: 4888 requestURL:
4841 completionHandler:); 4889 completionHandler:);
4842 if ([self.UIDelegate respondsToSelector:alertSelector]) { 4890 if ([self.UIDelegate respondsToSelector:alertSelector]) {
4843 [self.UIDelegate webController:self 4891 [self.UIDelegate webController:self
4844 runJavaScriptAlertPanelWithMessage:message 4892 runJavaScriptAlertPanelWithMessage:message
4845 requestURL:net::GURLWithNSURL(frame.request.URL) 4893 requestURL:net::GURLWithNSURL(frame.request.URL)
4846 completionHandler:completionHandler]; 4894 completionHandler:completionHandler];
4847 } else if (completionHandler) { 4895 } else {
4848 completionHandler(); 4896 void (^completion)(BOOL, NSString*) = nil;
4897 if (completionHandler) {
4898 completion = ^(BOOL, NSString*) {
4899 completionHandler();
4900 };
4901 }
4902 [self runJavaScriptDialogOfType:web::JAVASCRIPT_MESSAGE_TYPE_ALERT
4903 initiatedByFrame:frame
4904 message:message
4905 defaultText:nil
4906 completion:completion];
4849 } 4907 }
4850 } 4908 }
4851 4909
4852 - (void)webView:(WKWebView*)webView 4910 - (void)webView:(WKWebView*)webView
4853 runJavaScriptConfirmPanelWithMessage:(NSString*)message 4911 runJavaScriptConfirmPanelWithMessage:(NSString*)message
4854 initiatedByFrame:(WKFrameInfo*)frame 4912 initiatedByFrame:(WKFrameInfo*)frame
4855 completionHandler: 4913 completionHandler:
4856 (void (^)(BOOL result))completionHandler { 4914 (void (^)(BOOL result))completionHandler {
4857 if (self.shouldSuppressDialogs) { 4915 if (self.shouldSuppressDialogs) {
4858 [self didSuppressDialog]; 4916 [self didSuppressDialog];
4859 completionHandler(NO); 4917 completionHandler(NO);
4860 return; 4918 return;
4861 } 4919 }
4862 4920
4863 SEL confirmationSelector = @selector(webController: 4921 SEL confirmationSelector = @selector(webController:
4864 runJavaScriptConfirmPanelWithMessage: 4922 runJavaScriptConfirmPanelWithMessage:
4865 requestURL: 4923 requestURL:
4866 completionHandler:); 4924 completionHandler:);
4867 if ([self.UIDelegate respondsToSelector:confirmationSelector]) { 4925 if ([self.UIDelegate respondsToSelector:confirmationSelector]) {
4868 [self.UIDelegate webController:self 4926 [self.UIDelegate webController:self
4869 runJavaScriptConfirmPanelWithMessage:message 4927 runJavaScriptConfirmPanelWithMessage:message
4870 requestURL:net::GURLWithNSURL( 4928 requestURL:net::GURLWithNSURL(
4871 frame.request.URL) 4929 frame.request.URL)
4872 completionHandler:completionHandler]; 4930 completionHandler:completionHandler];
4873 } else if (completionHandler) { 4931 } else {
4874 completionHandler(NO); 4932 void (^completion)(BOOL, NSString*) = nil;
4933 if (completionHandler) {
4934 completion = ^(BOOL success, NSString*) {
4935 completionHandler(success);
4936 };
4937 }
4938 [self runJavaScriptDialogOfType:web::JAVASCRIPT_MESSAGE_TYPE_CONFIRM
4939 initiatedByFrame:frame
4940 message:message
4941 defaultText:nil
4942 completion:completion];
4875 } 4943 }
4876 } 4944 }
4877 4945
4878 - (void)webView:(WKWebView*)webView 4946 - (void)webView:(WKWebView*)webView
4879 runJavaScriptTextInputPanelWithPrompt:(NSString*)prompt 4947 runJavaScriptTextInputPanelWithPrompt:(NSString*)prompt
4880 defaultText:(NSString*)defaultText 4948 defaultText:(NSString*)defaultText
4881 initiatedByFrame:(WKFrameInfo*)frame 4949 initiatedByFrame:(WKFrameInfo*)frame
4882 completionHandler: 4950 completionHandler:
4883 (void (^)(NSString* result))completionHandler { 4951 (void (^)(NSString* result))completionHandler {
4884 GURL origin(web::GURLOriginWithWKSecurityOrigin(frame.securityOrigin)); 4952 GURL origin(web::GURLOriginWithWKSecurityOrigin(frame.securityOrigin));
(...skipping 16 matching lines...) Expand all
4901 defaultText: 4969 defaultText:
4902 requestURL: 4970 requestURL:
4903 completionHandler:); 4971 completionHandler:);
4904 if ([self.UIDelegate respondsToSelector:textInputSelector]) { 4972 if ([self.UIDelegate respondsToSelector:textInputSelector]) {
4905 GURL requestURL = net::GURLWithNSURL(frame.request.URL); 4973 GURL requestURL = net::GURLWithNSURL(frame.request.URL);
4906 [self.UIDelegate webController:self 4974 [self.UIDelegate webController:self
4907 runJavaScriptTextInputPanelWithPrompt:prompt 4975 runJavaScriptTextInputPanelWithPrompt:prompt
4908 defaultText:defaultText 4976 defaultText:defaultText
4909 requestURL:requestURL 4977 requestURL:requestURL
4910 completionHandler:completionHandler]; 4978 completionHandler:completionHandler];
4911 } else if (completionHandler) { 4979 } else {
4912 completionHandler(nil); 4980 void (^completion)(BOOL, NSString*) = nil;
4981 if (completionHandler) {
4982 completion = ^(BOOL, NSString* input) {
4983 completionHandler(input);
4984 };
4985 }
4986 [self runJavaScriptDialogOfType:web::JAVASCRIPT_MESSAGE_TYPE_PROMPT
4987 initiatedByFrame:frame
4988 message:prompt
4989 defaultText:defaultText
4990 completion:completion];
4913 } 4991 }
4914 } 4992 }
4915 4993
4916 #pragma mark - 4994 #pragma mark -
4917 #pragma mark WKNavigationDelegate Methods 4995 #pragma mark WKNavigationDelegate Methods
4918 4996
4919 - (void)webView:(WKWebView*)webView 4997 - (void)webView:(WKWebView*)webView
4920 decidePolicyForNavigationAction:(WKNavigationAction*)action 4998 decidePolicyForNavigationAction:(WKNavigationAction*)action
4921 decisionHandler: 4999 decisionHandler:
4922 (void (^)(WKNavigationActionPolicy))decisionHandler { 5000 (void (^)(WKNavigationActionPolicy))decisionHandler {
(...skipping 666 matching lines...) Expand 10 before | Expand all | Expand 10 after
5589 } 5667 }
5590 5668
5591 return web::WEB_VIEW_DOCUMENT_TYPE_GENERIC; 5669 return web::WEB_VIEW_DOCUMENT_TYPE_GENERIC;
5592 } 5670 }
5593 5671
5594 - (NSString*)refererFromNavigationAction:(WKNavigationAction*)action { 5672 - (NSString*)refererFromNavigationAction:(WKNavigationAction*)action {
5595 return [action.request valueForHTTPHeaderField:@"Referer"]; 5673 return [action.request valueForHTTPHeaderField:@"Referer"];
5596 } 5674 }
5597 5675
5598 @end 5676 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698