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

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

Issue 1516303002: Adds support for POST request with bodies on WKWebView. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2564
Patch Set: Created 5 years 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
« no previous file with comments | « ios/web/web_state/ui/crw_wk_script_message_router.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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_wk_web_view_web_controller.h" 5 #import "ios/web/web_state/ui/crw_wk_web_view_web_controller.h"
6 6
7 #import <WebKit/WebKit.h> 7 #import <WebKit/WebKit.h>
8 8
9 #include "base/containers/mru_cache.h" 9 #include "base/containers/mru_cache.h"
10 #include "base/ios/ios_util.h" 10 #include "base/ios/ios_util.h"
11 #include "base/ios/weak_nsobject.h" 11 #include "base/ios/weak_nsobject.h"
12 #include "base/json/json_reader.h" 12 #include "base/json/json_reader.h"
13 #include "base/mac/objc_property_releaser.h" 13 #import "base/mac/objc_property_releaser.h"
14 #import "base/mac/scoped_nsobject.h" 14 #import "base/mac/scoped_nsobject.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/metrics/histogram_macros.h" 16 #include "base/metrics/histogram_macros.h"
17 #include "base/strings/sys_string_conversions.h" 17 #include "base/strings/sys_string_conversions.h"
18 #include "base/values.h" 18 #include "base/values.h"
19 #import "ios/net/http_response_headers_util.h" 19 #import "ios/net/http_response_headers_util.h"
20 #import "ios/web/crw_network_activity_indicator_manager.h" 20 #import "ios/web/crw_network_activity_indicator_manager.h"
21 #import "ios/web/navigation/crw_session_controller.h" 21 #import "ios/web/navigation/crw_session_controller.h"
22 #import "ios/web/navigation/crw_session_entry.h" 22 #import "ios/web/navigation/crw_session_entry.h"
23 #include "ios/web/navigation/navigation_item_impl.h" 23 #include "ios/web/navigation/navigation_item_impl.h"
24 #include "ios/web/navigation/web_load_params.h" 24 #include "ios/web/navigation/web_load_params.h"
25 #include "ios/web/net/cert_host_pair.h" 25 #include "ios/web/net/cert_host_pair.h"
26 #import "ios/web/net/crw_cert_verification_controller.h" 26 #import "ios/web/net/crw_cert_verification_controller.h"
27 #include "ios/web/public/browser_state.h" 27 #include "ios/web/public/browser_state.h"
28 #include "ios/web/public/cert_store.h" 28 #include "ios/web/public/cert_store.h"
29 #include "ios/web/public/navigation_item.h" 29 #include "ios/web/public/navigation_item.h"
30 #include "ios/web/public/ssl_status.h" 30 #include "ios/web/public/ssl_status.h"
31 #include "ios/web/public/url_util.h" 31 #include "ios/web/public/url_util.h"
32 #include "ios/web/public/web_client.h" 32 #include "ios/web/public/web_client.h"
33 #import "ios/web/public/web_state/crw_web_view_scroll_view_proxy.h" 33 #import "ios/web/public/web_state/crw_web_view_scroll_view_proxy.h"
34 #import "ios/web/public/web_state/js/crw_js_injection_manager.h" 34 #import "ios/web/public/web_state/js/crw_js_injection_manager.h"
35 #import "ios/web/public/web_state/ui/crw_native_content_provider.h" 35 #import "ios/web/public/web_state/ui/crw_native_content_provider.h"
36 #import "ios/web/public/web_state/ui/crw_web_view_content_view.h" 36 #import "ios/web/public/web_state/ui/crw_web_view_content_view.h"
37 #import "ios/web/ui_web_view_util.h" 37 #import "ios/web/ui_web_view_util.h"
38 #include "ios/web/web_state/blocked_popup_info.h" 38 #include "ios/web/web_state/blocked_popup_info.h"
39 #import "ios/web/web_state/crw_pass_kit_downloader.h" 39 #import "ios/web/web_state/crw_pass_kit_downloader.h"
40 #import "ios/web/web_state/error_translation_util.h" 40 #import "ios/web/web_state/error_translation_util.h"
41 #include "ios/web/web_state/frame_info.h" 41 #include "ios/web/web_state/frame_info.h"
42 #import "ios/web/web_state/js/crw_js_post_request_loader.h"
42 #import "ios/web/web_state/js/crw_js_window_id_manager.h" 43 #import "ios/web/web_state/js/crw_js_window_id_manager.h"
43 #import "ios/web/web_state/ui/crw_web_controller+protected.h" 44 #import "ios/web/web_state/ui/crw_web_controller+protected.h"
44 #import "ios/web/web_state/ui/crw_wk_script_message_router.h" 45 #import "ios/web/web_state/ui/crw_wk_script_message_router.h"
45 #import "ios/web/web_state/ui/crw_wk_web_view_crash_detector.h" 46 #import "ios/web/web_state/ui/crw_wk_web_view_crash_detector.h"
46 #import "ios/web/web_state/ui/web_view_js_utils.h" 47 #import "ios/web/web_state/ui/web_view_js_utils.h"
47 #import "ios/web/web_state/ui/wk_back_forward_list_item_holder.h" 48 #import "ios/web/web_state/ui/wk_back_forward_list_item_holder.h"
48 #import "ios/web/web_state/ui/wk_web_view_configuration_provider.h" 49 #import "ios/web/web_state/ui/wk_web_view_configuration_provider.h"
49 #import "ios/web/web_state/web_state_impl.h" 50 #import "ios/web/web_state/web_state_impl.h"
50 #import "ios/web/web_state/web_view_internal_creation_util.h" 51 #import "ios/web/web_state/web_view_internal_creation_util.h"
51 #import "ios/web/web_state/wk_web_view_security_util.h" 52 #import "ios/web/web_state/wk_web_view_security_util.h"
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 // |didFailProvisionalNavigation|. 181 // |didFailProvisionalNavigation|.
181 base::scoped_nsobject<CRWWebControllerPendingNavigationInfo> 182 base::scoped_nsobject<CRWWebControllerPendingNavigationInfo>
182 _pendingNavigationInfo; 183 _pendingNavigationInfo;
183 184
184 // Referrer for the current page. 185 // Referrer for the current page.
185 base::scoped_nsobject<NSString> _currentReferrerString; 186 base::scoped_nsobject<NSString> _currentReferrerString;
186 187
187 // Handles downloading PassKit data for WKWebView. Lazy initialized. 188 // Handles downloading PassKit data for WKWebView. Lazy initialized.
188 base::scoped_nsobject<CRWPassKitDownloader> _passKitDownloader; 189 base::scoped_nsobject<CRWPassKitDownloader> _passKitDownloader;
189 190
191 // Object for loading POST requests with body.
192 base::scoped_nsobject<CRWJSPOSTRequestLoader> _POSTRequestLoader;
193
190 // Whether the web page is currently performing window.history.pushState or 194 // Whether the web page is currently performing window.history.pushState or
191 // window.history.replaceState 195 // window.history.replaceState
192 // Set to YES on window.history.willChangeState message. To NO on 196 // Set to YES on window.history.willChangeState message. To NO on
193 // window.history.didPushState or window.history.didReplaceState. 197 // window.history.didPushState or window.history.didReplaceState.
194 BOOL _changingHistoryState; 198 BOOL _changingHistoryState;
195 199
196 // CRWWebUIManager object for loading WebUI pages. 200 // CRWWebUIManager object for loading WebUI pages.
197 base::scoped_nsobject<CRWWebUIManager> _webUIManager; 201 base::scoped_nsobject<CRWWebUIManager> _webUIManager;
198 202
199 // Controller used for certs verification to help with blocking requests with 203 // Controller used for certs verification to help with blocking requests with
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 @property(nonatomic, readonly) NSString* activityIndicatorGroupID; 236 @property(nonatomic, readonly) NSString* activityIndicatorGroupID;
233 237
234 #if !defined(ENABLE_CHROME_NET_STACK_FOR_WKWEBVIEW) 238 #if !defined(ENABLE_CHROME_NET_STACK_FOR_WKWEBVIEW)
235 // Identifier used for storing and retrieving certificates. 239 // Identifier used for storing and retrieving certificates.
236 @property(nonatomic, readonly) int certGroupID; 240 @property(nonatomic, readonly) int certGroupID;
237 #endif // #if !defined(ENABLE_CHROME_NET_STACK_FOR_WKWEBVIEW) 241 #endif // #if !defined(ENABLE_CHROME_NET_STACK_FOR_WKWEBVIEW)
238 242
239 // Downloader for PassKit files. Lazy initialized. 243 // Downloader for PassKit files. Lazy initialized.
240 @property(nonatomic, readonly) CRWPassKitDownloader* passKitDownloader; 244 @property(nonatomic, readonly) CRWPassKitDownloader* passKitDownloader;
241 245
246 // Loads POST request with body in |_wkWebView| by constructing an HTML page
247 // that executes the request through JavaScript and replaces document with the
248 // result.
249 // Note that this approach includes multiple body encodings and decodings, plus
250 // the data is passed to |_wkWebView| on main thread.
251 // This is necessary because WKWebView ignores POST request body.
252 // Workaround for https://bugs.webkit.org/show_bug.cgi?id=145410
253 - (void)loadPOSTRequest:(NSMutableURLRequest*)request;
254
242 // Returns the WKWebViewConfigurationProvider associated with the web 255 // Returns the WKWebViewConfigurationProvider associated with the web
243 // controller's BrowserState. 256 // controller's BrowserState.
244 - (web::WKWebViewConfigurationProvider&)webViewConfigurationProvider; 257 - (web::WKWebViewConfigurationProvider&)webViewConfigurationProvider;
245 258
246 // Creates a web view with given |config|. No-op if web view is already created. 259 // Creates a web view with given |config|. No-op if web view is already created.
247 - (void)ensureWebViewCreatedWithConfiguration:(WKWebViewConfiguration*)config; 260 - (void)ensureWebViewCreatedWithConfiguration:(WKWebViewConfiguration*)config;
248 261
249 // Returns a new autoreleased web view created with given configuration. 262 // Returns a new autoreleased web view created with given configuration.
250 - (WKWebView*)createWebViewWithConfiguration:(WKWebViewConfiguration*)config; 263 - (WKWebView*)createWebViewWithConfiguration:(WKWebViewConfiguration*)config;
251 264
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
566 [_injectedScriptManagers addObject:JSInjectionManagerClass]; 579 [_injectedScriptManagers addObject:JSInjectionManagerClass];
567 } 580 }
568 581
569 - (void)willLoadCurrentURLInWebView { 582 - (void)willLoadCurrentURLInWebView {
570 // TODO(stuartmorgan): Get a WKWebView version of the request ID verification 583 // TODO(stuartmorgan): Get a WKWebView version of the request ID verification
571 // code working for debug builds. 584 // code working for debug builds.
572 } 585 }
573 586
574 - (void)loadRequestForCurrentNavigationItem { 587 - (void)loadRequestForCurrentNavigationItem {
575 DCHECK(self.webView && !self.nativeController); 588 DCHECK(self.webView && !self.nativeController);
589 DCHECK([self currentSessionEntry]);
590
591 web::WKBackForwardListItemHolder* holder =
592 [self currentBackForwardListItemHolder];
593 BOOL isFormResubmission =
594 (holder->navigation_type() == WKNavigationTypeFormResubmitted ||
595 holder->navigation_type() == WKNavigationTypeFormSubmitted);
596 web::NavigationItemImpl* currentItem =
597 [self currentSessionEntry].navigationItemImpl;
598 NSData* POSTData = currentItem->GetPostData();
599 NSMutableURLRequest* request = [self requestForCurrentNavigationItem];
600
601 // If the request has POST data and is not a form resubmission, configure and
602 // run the POST request.
603 if (POSTData.length && !isFormResubmission) {
604 [request setHTTPMethod:@"POST"];
605 [request setHTTPBody:POSTData];
606 [request setAllHTTPHeaderFields:[self currentHTTPHeaders]];
607 [self registerLoadRequest:[self currentNavigationURL]
608 referrer:[self currentSessionEntryReferrer]
609 transition:[self currentTransition]];
610 [self loadPOSTRequest:request];
611 return;
612 }
576 613
577 ProceduralBlock defaultNavigationBlock = ^{ 614 ProceduralBlock defaultNavigationBlock = ^{
578 [self registerLoadRequest:[self currentNavigationURL] 615 [self registerLoadRequest:[self currentNavigationURL]
579 referrer:[self currentSessionEntryReferrer] 616 referrer:[self currentSessionEntryReferrer]
580 transition:[self currentTransition]]; 617 transition:[self currentTransition]];
581 [self loadRequest:[self requestForCurrentNavigationItem]]; 618 [self loadRequest:request];
582 }; 619 };
583 620
584 // If there is no corresponding WKBackForwardListItem, or the item is not in 621 // If there is no corresponding WKBackForwardListItem, or the item is not in
585 // the current WKWebView's back-forward list, navigating using WKWebView API 622 // the current WKWebView's back-forward list, navigating using WKWebView API
586 // is not possible. In this case, fall back to the default navigation 623 // is not possible. In this case, fall back to the default navigation
587 // mechanism. 624 // mechanism.
588 web::WKBackForwardListItemHolder* holder =
589 [self currentBackForwardListItemHolder];
590 if (!holder->back_forward_list_item() || 625 if (!holder->back_forward_list_item() ||
591 ![self isBackForwardListItemValid:holder->back_forward_list_item()]) { 626 ![self isBackForwardListItemValid:holder->back_forward_list_item()]) {
592 defaultNavigationBlock(); 627 defaultNavigationBlock();
593 return; 628 return;
594 } 629 }
595 630
596 ProceduralBlock webViewNavigationBlock = ^{ 631 ProceduralBlock webViewNavigationBlock = ^{
597 // If the current navigation URL is the same as the URL of the visible 632 // If the current navigation URL is the same as the URL of the visible
598 // page, that means the user requested a reload. |goToBackForwardListItem| 633 // page, that means the user requested a reload. |goToBackForwardListItem|
599 // will be a no-op when it is passed the current back forward list item, 634 // will be a no-op when it is passed the current back forward list item,
600 // so |reload| must be explicitly called. 635 // so |reload| must be explicitly called.
601 [self registerLoadRequest:[self currentNavigationURL] 636 [self registerLoadRequest:[self currentNavigationURL]
602 referrer:[self currentSessionEntryReferrer] 637 referrer:[self currentSessionEntryReferrer]
603 transition:[self currentTransition]]; 638 transition:[self currentTransition]];
604 if ([self currentNavigationURL] == net::GURLWithNSURL([_wkWebView URL])) { 639 if ([self currentNavigationURL] == net::GURLWithNSURL([_wkWebView URL])) {
605 [_wkWebView reload]; 640 [_wkWebView reload];
606 } else { 641 } else {
607 [_wkWebView goToBackForwardListItem:holder->back_forward_list_item()]; 642 [_wkWebView goToBackForwardListItem:holder->back_forward_list_item()];
608 } 643 }
609 }; 644 };
610 645
611 // If the request is not a form submission or resubmission, or the user 646 // If the request is not a form submission or resubmission, or the user
612 // doesn't need to confirm the load, then continue right away. 647 // doesn't need to confirm the load, then continue right away.
613 web::NavigationItemImpl* currentItem = 648
614 [self currentSessionEntry].navigationItemImpl; 649 if (!isFormResubmission ||
615 if ((holder->navigation_type() != WKNavigationTypeFormResubmitted &&
616 holder->navigation_type() != WKNavigationTypeFormSubmitted) ||
617 currentItem->ShouldSkipResubmitDataConfirmation()) { 650 currentItem->ShouldSkipResubmitDataConfirmation()) {
618 webViewNavigationBlock(); 651 webViewNavigationBlock();
619 return; 652 return;
620 } 653 }
621 654
622 // If the request is form submission or resubmission, then prompt the 655 // If the request is form submission or resubmission, then prompt the
623 // user before proceeding. 656 // user before proceeding.
657 DCHECK(isFormResubmission);
624 [self.delegate webController:self 658 [self.delegate webController:self
625 onFormResubmissionForRequest:nil 659 onFormResubmissionForRequest:nil
626 continueBlock:webViewNavigationBlock 660 continueBlock:webViewNavigationBlock
627 cancelBlock:defaultNavigationBlock]; 661 cancelBlock:defaultNavigationBlock];
628 } 662 }
629 663
630 // Overrides the hashchange workaround in the super class that manually 664 // Overrides the hashchange workaround in the super class that manually
631 // triggers Javascript hashchange events. If navigating with native API, 665 // triggers Javascript hashchange events. If navigating with native API,
632 // i.e. using a back forward list item, hashchange events will be triggered 666 // i.e. using a back forward list item, hashchange events will be triggered
633 // automatically, so no URL tampering is required. 667 // automatically, so no URL tampering is required.
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
737 } 771 }
738 772
739 #if !defined(ENABLE_CHROME_NET_STACK_FOR_WKWEBVIEW) 773 #if !defined(ENABLE_CHROME_NET_STACK_FOR_WKWEBVIEW)
740 - (int)certGroupID { 774 - (int)certGroupID {
741 DCHECK(self.webStateImpl); 775 DCHECK(self.webStateImpl);
742 // Request tracker IDs are used as certificate groups. 776 // Request tracker IDs are used as certificate groups.
743 return self.webStateImpl->GetRequestTracker()->identifier(); 777 return self.webStateImpl->GetRequestTracker()->identifier();
744 } 778 }
745 #endif 779 #endif
746 780
781 - (void)loadPOSTRequest:(NSMutableURLRequest*)request {
782 if (!_POSTRequestLoader) {
783 _POSTRequestLoader.reset([[CRWJSPOSTRequestLoader alloc] init]);
784 }
785
786 CRWWKScriptMessageRouter* messageRouter =
787 [self webViewConfigurationProvider].GetScriptMessageRouter();
788
789 [_POSTRequestLoader loadPOSTRequest:request
790 inWebView:_wkWebView
791 messageRouter:messageRouter
792 completionHandler:^(NSError* loadError) {
793 if (loadError)
794 [self handleLoadError:loadError inMainFrame:YES];
795 else
796 self.webStateImpl->SetContentsMimeType("text/html");
797 }];
798 }
799
747 - (web::WKWebViewConfigurationProvider&)webViewConfigurationProvider { 800 - (web::WKWebViewConfigurationProvider&)webViewConfigurationProvider {
748 DCHECK(self.webStateImpl); 801 DCHECK(self.webStateImpl);
749 web::BrowserState* browserState = self.webStateImpl->GetBrowserState(); 802 web::BrowserState* browserState = self.webStateImpl->GetBrowserState();
750 return web::WKWebViewConfigurationProvider::FromBrowserState(browserState); 803 return web::WKWebViewConfigurationProvider::FromBrowserState(browserState);
751 } 804 }
752 805
753 - (void)ensureWebViewCreatedWithConfiguration:(WKWebViewConfiguration*)config { 806 - (void)ensureWebViewCreatedWithConfiguration:(WKWebViewConfiguration*)config {
754 if (!_wkWebView) { 807 if (!_wkWebView) {
755 [self setWebView:[self createWebViewWithConfiguration:config]]; 808 [self setWebView:[self createWebViewWithConfiguration:config]];
756 // Notify super class about created web view. -webViewDidChange is not 809 // Notify super class about created web view. -webViewDidChange is not
(...skipping 1178 matching lines...) Expand 10 before | Expand all | Expand 10 after
1935 runJavaScriptTextInputPanelWithPrompt:prompt 1988 runJavaScriptTextInputPanelWithPrompt:prompt
1936 defaultText:defaultText 1989 defaultText:defaultText
1937 requestURL:requestURL 1990 requestURL:requestURL
1938 completionHandler:completionHandler]; 1991 completionHandler:completionHandler];
1939 } else if (completionHandler) { 1992 } else if (completionHandler) {
1940 completionHandler(nil); 1993 completionHandler(nil);
1941 } 1994 }
1942 } 1995 }
1943 1996
1944 @end 1997 @end
OLDNEW
« no previous file with comments | « ios/web/web_state/ui/crw_wk_script_message_router.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698