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

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

Issue 1375023002: Adds support for POST request with bodies on WKWebView. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Noyau's comments 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 26 matching lines...) Expand all
226 230
227 // Activity indicator group ID for this web controller. 231 // Activity indicator group ID for this web controller.
228 @property(nonatomic, readonly) NSString* activityIndicatorGroupID; 232 @property(nonatomic, readonly) NSString* activityIndicatorGroupID;
229 233
230 // Identifier used for storing and retrieving certificates. 234 // Identifier used for storing and retrieving certificates.
231 @property(nonatomic, readonly) int certGroupID; 235 @property(nonatomic, readonly) int certGroupID;
232 236
233 // Downloader for PassKit files. Lazy initialized. 237 // Downloader for PassKit files. Lazy initialized.
234 @property(nonatomic, readonly) CRWPassKitDownloader* passKitDownloader; 238 @property(nonatomic, readonly) CRWPassKitDownloader* passKitDownloader;
235 239
240 // Loads POST request with body in |_wkWebView| by constructing an HTML page
241 // that executes the request through JavaScript and replaces document with the
242 // result.
243 // Note that this approach includes multiple body encodings and decodings, plus
244 // the data is passed to |_wkWebView| on main thread.
245 // This is necessary because WKWebView ignores POST request body.
246 // Workaround for https://bugs.webkit.org/show_bug.cgi?id=145410
247 - (void)loadPOSTRequest:(NSMutableURLRequest*)request;
248
236 // Returns the WKWebViewConfigurationProvider associated with the web 249 // Returns the WKWebViewConfigurationProvider associated with the web
237 // controller's BrowserState. 250 // controller's BrowserState.
238 - (web::WKWebViewConfigurationProvider&)webViewConfigurationProvider; 251 - (web::WKWebViewConfigurationProvider&)webViewConfigurationProvider;
239 252
240 // Creates a web view with given |config|. No-op if web view is already created. 253 // Creates a web view with given |config|. No-op if web view is already created.
241 - (void)ensureWebViewCreatedWithConfiguration:(WKWebViewConfiguration*)config; 254 - (void)ensureWebViewCreatedWithConfiguration:(WKWebViewConfiguration*)config;
242 255
243 // Returns a new autoreleased web view created with given configuration. 256 // Returns a new autoreleased web view created with given configuration.
244 - (WKWebView*)createWebViewWithConfiguration:(WKWebViewConfiguration*)config; 257 - (WKWebView*)createWebViewWithConfiguration:(WKWebViewConfiguration*)config;
245 258
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
550 [_injectedScriptManagers addObject:JSInjectionManagerClass]; 563 [_injectedScriptManagers addObject:JSInjectionManagerClass];
551 } 564 }
552 565
553 - (void)willLoadCurrentURLInWebView { 566 - (void)willLoadCurrentURLInWebView {
554 // TODO(stuartmorgan): Get a WKWebView version of the request ID verification 567 // TODO(stuartmorgan): Get a WKWebView version of the request ID verification
555 // code working for debug builds. 568 // code working for debug builds.
556 } 569 }
557 570
558 - (void)loadRequestForCurrentNavigationItem { 571 - (void)loadRequestForCurrentNavigationItem {
559 DCHECK(self.webView && !self.nativeController); 572 DCHECK(self.webView && !self.nativeController);
573 DCHECK([self currentSessionEntry]);
574
575 web::WKBackForwardListItemHolder* holder =
576 [self currentBackForwardListItemHolder];
577 BOOL isFormResubmission =
578 (holder->navigation_type() == WKNavigationTypeFormResubmitted ||
579 holder->navigation_type() == WKNavigationTypeFormSubmitted);
580 web::NavigationItemImpl* currentItem =
581 [self currentSessionEntry].navigationItemImpl;
582 NSData* POSTData = currentItem->GetPostData();
583 NSMutableURLRequest* request = [self requestForCurrentNavigationItem];
584
585 // If the request has POST data and is not a form resubmission, configure and
586 // run the POST request.
587 if (POSTData.length && !isFormResubmission) {
588 [request setHTTPMethod:@"POST"];
589 [request setHTTPBody:POSTData];
590 [request setAllHTTPHeaderFields:[self currentHTTPHeaders]];
591 [self registerLoadRequest:[self currentNavigationURL]
592 referrer:[self currentSessionEntryReferrer]
593 transition:[self currentTransition]];
594 [self loadPOSTRequest:request];
595 return;
596 }
560 597
561 ProceduralBlock defaultNavigationBlock = ^{ 598 ProceduralBlock defaultNavigationBlock = ^{
562 [self registerLoadRequest:[self currentNavigationURL] 599 [self registerLoadRequest:[self currentNavigationURL]
563 referrer:[self currentSessionEntryReferrer] 600 referrer:[self currentSessionEntryReferrer]
564 transition:[self currentTransition]]; 601 transition:[self currentTransition]];
565 [self loadRequest:[self requestForCurrentNavigationItem]]; 602 [self loadRequest:request];
566 }; 603 };
567 604
568 // If there is no corresponding WKBackForwardListItem, or the item is not in 605 // If there is no corresponding WKBackForwardListItem, or the item is not in
569 // the current WKWebView's back-forward list, navigating using WKWebView API 606 // the current WKWebView's back-forward list, navigating using WKWebView API
570 // is not possible. In this case, fall back to the default navigation 607 // is not possible. In this case, fall back to the default navigation
571 // mechanism. 608 // mechanism.
572 web::WKBackForwardListItemHolder* holder =
573 [self currentBackForwardListItemHolder];
574 if (!holder->back_forward_list_item() || 609 if (!holder->back_forward_list_item() ||
575 ![self isBackForwardListItemValid:holder->back_forward_list_item()]) { 610 ![self isBackForwardListItemValid:holder->back_forward_list_item()]) {
576 defaultNavigationBlock(); 611 defaultNavigationBlock();
577 return; 612 return;
578 } 613 }
579 614
580 ProceduralBlock webViewNavigationBlock = ^{ 615 ProceduralBlock webViewNavigationBlock = ^{
581 // If the current navigation URL is the same as the URL of the visible 616 // If the current navigation URL is the same as the URL of the visible
582 // page, that means the user requested a reload. |goToBackForwardListItem| 617 // page, that means the user requested a reload. |goToBackForwardListItem|
583 // will be a no-op when it is passed the current back forward list item, 618 // will be a no-op when it is passed the current back forward list item,
584 // so |reload| must be explicitly called. 619 // so |reload| must be explicitly called.
585 [self registerLoadRequest:[self currentNavigationURL] 620 [self registerLoadRequest:[self currentNavigationURL]
586 referrer:[self currentSessionEntryReferrer] 621 referrer:[self currentSessionEntryReferrer]
587 transition:[self currentTransition]]; 622 transition:[self currentTransition]];
588 if ([self currentNavigationURL] == net::GURLWithNSURL([_wkWebView URL])) { 623 if ([self currentNavigationURL] == net::GURLWithNSURL([_wkWebView URL])) {
589 [_wkWebView reload]; 624 [_wkWebView reload];
590 } else { 625 } else {
591 [_wkWebView goToBackForwardListItem:holder->back_forward_list_item()]; 626 [_wkWebView goToBackForwardListItem:holder->back_forward_list_item()];
592 } 627 }
593 }; 628 };
594 629
595 // If the request is not a form submission or resubmission, or the user 630 // If the request is not a form submission or resubmission, or the user
596 // doesn't need to confirm the load, then continue right away. 631 // doesn't need to confirm the load, then continue right away.
597 web::NavigationItemImpl* currentItem = 632
598 [self currentSessionEntry].navigationItemImpl; 633 if (!isFormResubmission ||
599 if ((holder->navigation_type() != WKNavigationTypeFormResubmitted &&
600 holder->navigation_type() != WKNavigationTypeFormSubmitted) ||
601 currentItem->ShouldSkipResubmitDataConfirmation()) { 634 currentItem->ShouldSkipResubmitDataConfirmation()) {
602 webViewNavigationBlock(); 635 webViewNavigationBlock();
603 return; 636 return;
604 } 637 }
605 638
606 // If the request is form submission or resubmission, then prompt the 639 // If the request is form submission or resubmission, then prompt the
607 // user before proceeding. 640 // user before proceeding.
641 DCHECK(isFormResubmission);
608 [self.delegate webController:self 642 [self.delegate webController:self
609 onFormResubmissionForRequest:nil 643 onFormResubmissionForRequest:nil
610 continueBlock:webViewNavigationBlock 644 continueBlock:webViewNavigationBlock
611 cancelBlock:defaultNavigationBlock]; 645 cancelBlock:defaultNavigationBlock];
612 } 646 }
613 647
614 // Overrides the hashchange workaround in the super class that manually 648 // Overrides the hashchange workaround in the super class that manually
615 // triggers Javascript hashchange events. If navigating with native API, 649 // triggers Javascript hashchange events. If navigating with native API,
616 // i.e. using a back forward list item, hashchange events will be triggered 650 // i.e. using a back forward list item, hashchange events will be triggered
617 // automatically, so no URL tampering is required. 651 // automatically, so no URL tampering is required.
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
708 @"WKWebViewWebController.NetworkActivityIndicatorKey.%@", 742 @"WKWebViewWebController.NetworkActivityIndicatorKey.%@",
709 self.webStateImpl->GetRequestGroupID()]; 743 self.webStateImpl->GetRequestGroupID()];
710 } 744 }
711 745
712 - (int)certGroupID { 746 - (int)certGroupID {
713 DCHECK(self.webStateImpl); 747 DCHECK(self.webStateImpl);
714 // Request tracker IDs are used as certificate groups. 748 // Request tracker IDs are used as certificate groups.
715 return self.webStateImpl->GetRequestTracker()->identifier(); 749 return self.webStateImpl->GetRequestTracker()->identifier();
716 } 750 }
717 751
752 - (void)loadPOSTRequest:(NSMutableURLRequest*)request {
753 if (!_POSTRequestLoader) {
754 _POSTRequestLoader.reset([[CRWJSPOSTRequestLoader alloc] init]);
755 }
756
757 CRWWKScriptMessageRouter* messageRouter =
758 [self webViewConfigurationProvider].GetScriptMessageRouter();
759
760 [_POSTRequestLoader loadPOSTRequest:request
761 inWebView:_wkWebView
762 messageRouter:messageRouter
763 completionHandler:^(NSError* loadError) {
764 if (loadError)
765 [self handleLoadError:loadError inMainFrame:YES];
766 else
767 self.webStateImpl->SetContentsMimeType("text/html");
768 }];
769 }
770
718 - (web::WKWebViewConfigurationProvider&)webViewConfigurationProvider { 771 - (web::WKWebViewConfigurationProvider&)webViewConfigurationProvider {
719 DCHECK(self.webStateImpl); 772 DCHECK(self.webStateImpl);
720 web::BrowserState* browserState = self.webStateImpl->GetBrowserState(); 773 web::BrowserState* browserState = self.webStateImpl->GetBrowserState();
721 return web::WKWebViewConfigurationProvider::FromBrowserState(browserState); 774 return web::WKWebViewConfigurationProvider::FromBrowserState(browserState);
722 } 775 }
723 776
724 - (void)ensureWebViewCreatedWithConfiguration:(WKWebViewConfiguration*)config { 777 - (void)ensureWebViewCreatedWithConfiguration:(WKWebViewConfiguration*)config {
725 if (!_wkWebView) { 778 if (!_wkWebView) {
726 [self setWebView:[self createWebViewWithConfiguration:config]]; 779 [self setWebView:[self createWebViewWithConfiguration:config]];
727 // Notify super class about created web view. -webViewDidChange is not 780 // Notify super class about created web view. -webViewDidChange is not
(...skipping 1149 matching lines...) Expand 10 before | Expand all | Expand 10 after
1877 runJavaScriptTextInputPanelWithPrompt:prompt 1930 runJavaScriptTextInputPanelWithPrompt:prompt
1878 defaultText:defaultText 1931 defaultText:defaultText
1879 requestURL:requestURL 1932 requestURL:requestURL
1880 completionHandler:completionHandler]; 1933 completionHandler:completionHandler];
1881 } else if (completionHandler) { 1934 } else if (completionHandler) {
1882 completionHandler(nil); 1935 completionHandler(nil);
1883 } 1936 }
1884 } 1937 }
1885 1938
1886 @end 1939 @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