OLD | NEW |
---|---|
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/ios/weak_nsobject.h" | 9 #include "base/ios/weak_nsobject.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 | 44 |
45 namespace { | 45 namespace { |
46 // Extracts Referer value from WKNavigationAction request header. | 46 // Extracts Referer value from WKNavigationAction request header. |
47 NSString* GetRefererFromNavigationAction(WKNavigationAction* action) { | 47 NSString* GetRefererFromNavigationAction(WKNavigationAction* action) { |
48 return [action.request valueForHTTPHeaderField:@"Referer"]; | 48 return [action.request valueForHTTPHeaderField:@"Referer"]; |
49 } | 49 } |
50 | 50 |
51 NSString* const kScriptMessageName = @"crwebinvoke"; | 51 NSString* const kScriptMessageName = @"crwebinvoke"; |
52 NSString* const kScriptImmediateName = @"crwebinvokeimmediate"; | 52 NSString* const kScriptImmediateName = @"crwebinvokeimmediate"; |
53 | 53 |
54 // Utility functions for storing the source of NSErrors received by WKWebViews: | |
55 // - Errors received by |-webView:didFailProvisionalNavigation:withError:| are | |
56 // recorded using WKWebViewErrorSource::PROVISIONAL_LOAD. These should be | |
57 // aborted. | |
58 // - Errors received by |-webView:didFailNavigation:withError:| are recorded | |
59 // using WKWebViewsource::NAVIGATION. These errors should not be aborted, as | |
60 // the WKWebView will automatically retry the load. | |
61 NSString* const kWKWebViewErrorSourceKey = @"ErrorSource"; | |
62 typedef enum { NONE = 0, PROVISIONAL_LOAD, NAVIGATION } WKWebViewErrorSource; | |
63 NSError* WKWebViewErrorWithSource(NSError* error, WKWebViewErrorSource source) { | |
64 DCHECK(error); | |
65 base::scoped_nsobject<NSMutableDictionary> userInfo( | |
66 [error.userInfo mutableCopy]); | |
67 [userInfo setValue:@(source) forKey:kWKWebViewErrorSourceKey]; | |
Eugene But (OOO till 7-30)
2015/06/23 21:27:14
s/setValue/setObject
kkhorimoto
2015/06/24 20:01:42
Done.
| |
68 return [NSError errorWithDomain:error.domain | |
69 code:error.code | |
70 userInfo:userInfo]; | |
71 } | |
72 WKWebViewErrorSource WKWebViewErrorSourceFromError(NSError* error) { | |
73 DCHECK(error); | |
74 return static_cast<WKWebViewErrorSource>( | |
75 [error.userInfo[kWKWebViewErrorSourceKey] integerValue]); | |
76 } | |
77 | |
54 } // namespace | 78 } // namespace |
55 | 79 |
56 @interface CRWWKWebViewWebController () <WKNavigationDelegate, | 80 @interface CRWWKWebViewWebController () <WKNavigationDelegate, |
57 WKScriptMessageHandler, | 81 WKScriptMessageHandler, |
58 WKUIDelegate> { | 82 WKUIDelegate> { |
59 // The WKWebView managed by this instance. | 83 // The WKWebView managed by this instance. |
60 base::scoped_nsobject<WKWebView> _wkWebView; | 84 base::scoped_nsobject<WKWebView> _wkWebView; |
61 | 85 |
62 // The Watch Dog that detects and reports WKWebView crashes. | 86 // The Watch Dog that detects and reports WKWebView crashes. |
63 base::scoped_nsobject<CRWWKWebViewCrashDetector> _crashDetector; | 87 base::scoped_nsobject<CRWWKWebViewCrashDetector> _crashDetector; |
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
405 if (zoomState.IsLegacyFormat()) | 429 if (zoomState.IsLegacyFormat()) |
406 return; | 430 return; |
407 CGFloat zoomScale = zoomState.zoom_scale(); | 431 CGFloat zoomScale = zoomState.zoom_scale(); |
408 if (zoomScale < self.webScrollView.minimumZoomScale) | 432 if (zoomScale < self.webScrollView.minimumZoomScale) |
409 zoomScale = self.webScrollView.minimumZoomScale; | 433 zoomScale = self.webScrollView.minimumZoomScale; |
410 if (zoomScale > self.webScrollView.maximumZoomScale) | 434 if (zoomScale > self.webScrollView.maximumZoomScale) |
411 zoomScale = self.webScrollView.maximumZoomScale; | 435 zoomScale = self.webScrollView.maximumZoomScale; |
412 self.webScrollView.zoomScale = zoomScale; | 436 self.webScrollView.zoomScale = zoomScale; |
413 } | 437 } |
414 | 438 |
415 - (BOOL)shouldAbortLoadForCancelledURL:(const GURL&)cancelledURL { | 439 - (BOOL)shouldAbortLoadForCancelledError:(NSError*)cancelledError { |
440 DCHECK_EQ(cancelledError.code, NSURLErrorCancelled); | |
416 // Do not abort the load if it is for an app specific URL, as such errors | 441 // Do not abort the load if it is for an app specific URL, as such errors |
417 // are produced during the app specific URL load process. | 442 // are produced during the app specific URL load process. |
418 return !web::GetWebClient()->IsAppSpecificURL(cancelledURL); | 443 const GURL errorURL = |
444 net::GURLWithNSURL(cancelledError.userInfo[NSURLErrorFailingURLErrorKey]); | |
445 BOOL isAppSpecificURL = web::GetWebClient()->IsAppSpecificURL(errorURL); | |
446 // Don't abort NSURLErrorCancelled errors originating from navigation, as the | |
Eugene But (OOO till 7-30)
2015/06/23 21:27:14
Early return here:
if (isAppSpecificURL)
return
kkhorimoto
2015/06/24 20:01:42
Done.
| |
447 // WKWebView will automatically retry these loads. | |
448 WKWebViewErrorSource source = WKWebViewErrorSourceFromError(cancelledError); | |
449 BOOL isFromNavigation = source == NAVIGATION; | |
450 return !isFromNavigation && !isAppSpecificURL; | |
419 } | 451 } |
420 | 452 |
421 #pragma mark Private methods | 453 #pragma mark Private methods |
422 | 454 |
423 - (NSString*)documentMIMEType { | 455 - (NSString*)documentMIMEType { |
424 return _documentMIMEType.get(); | 456 return _documentMIMEType.get(); |
425 } | 457 } |
426 | 458 |
427 - (void)setDocumentMIMEType:(NSString*)type { | 459 - (void)setDocumentMIMEType:(NSString*)type { |
428 _documentMIMEType.reset([type copy]); | 460 _documentMIMEType.reset([type copy]); |
(...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1006 [self registerLoadRequest:net::GURLWithNSURL(webView.URL) | 1038 [self registerLoadRequest:net::GURLWithNSURL(webView.URL) |
1007 referrer:[self currentReferrer] | 1039 referrer:[self currentReferrer] |
1008 transition:ui::PAGE_TRANSITION_SERVER_REDIRECT]; | 1040 transition:ui::PAGE_TRANSITION_SERVER_REDIRECT]; |
1009 } | 1041 } |
1010 | 1042 |
1011 - (void)webView:(WKWebView *)webView | 1043 - (void)webView:(WKWebView *)webView |
1012 didFailProvisionalNavigation:(WKNavigation *)navigation | 1044 didFailProvisionalNavigation:(WKNavigation *)navigation |
1013 withError:(NSError *)error { | 1045 withError:(NSError *)error { |
1014 [self discardPendingReferrerString]; | 1046 [self discardPendingReferrerString]; |
1015 | 1047 |
1048 error = WKWebViewErrorWithSource(error, PROVISIONAL_LOAD); | |
1016 #if !defined(ENABLE_CHROME_NET_STACK_FOR_WKWEBVIEW) | 1049 #if !defined(ENABLE_CHROME_NET_STACK_FOR_WKWEBVIEW) |
1017 if (web::IsWKWebViewSSLError(error)) | 1050 if (web::IsWKWebViewSSLError(error)) |
1018 [self handleSSLError:error]; | 1051 [self handleSSLError:error]; |
1019 else | 1052 else |
1020 #endif | 1053 #endif |
1021 [self handleLoadError:error inMainFrame:YES]; | 1054 [self handleLoadError:error inMainFrame:YES]; |
1022 } | 1055 } |
1023 | 1056 |
1024 - (void)webView:(WKWebView *)webView | 1057 - (void)webView:(WKWebView *)webView |
1025 didCommitNavigation:(WKNavigation *)navigation { | 1058 didCommitNavigation:(WKNavigation *)navigation { |
(...skipping 23 matching lines...) Expand all Loading... | |
1049 // TODO(jyquinn): Investigate using WKUserScriptInjectionTimeAtDocumentEnd to | 1082 // TODO(jyquinn): Investigate using WKUserScriptInjectionTimeAtDocumentEnd to |
1050 // inject this material at the appropriate time rather than invoking here. | 1083 // inject this material at the appropriate time rather than invoking here. |
1051 web::EvaluateJavaScript(webView, | 1084 web::EvaluateJavaScript(webView, |
1052 @"__gCrWeb.didFinishNavigation()", nil); | 1085 @"__gCrWeb.didFinishNavigation()", nil); |
1053 [self didFinishNavigation]; | 1086 [self didFinishNavigation]; |
1054 } | 1087 } |
1055 | 1088 |
1056 - (void)webView:(WKWebView *)webView | 1089 - (void)webView:(WKWebView *)webView |
1057 didFailNavigation:(WKNavigation *)navigation | 1090 didFailNavigation:(WKNavigation *)navigation |
1058 withError:(NSError *)error { | 1091 withError:(NSError *)error { |
1059 [self handleLoadError:error inMainFrame:YES]; | 1092 [self handleLoadError:WKWebViewErrorWithSource(error, NAVIGATION) |
1093 inMainFrame:YES]; | |
1060 } | 1094 } |
1061 | 1095 |
1062 - (void)webView:(WKWebView *)webView | 1096 - (void)webView:(WKWebView *)webView |
1063 didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge | 1097 didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge |
1064 completionHandler: | 1098 completionHandler: |
1065 (void (^)(NSURLSessionAuthChallengeDisposition disposition, | 1099 (void (^)(NSURLSessionAuthChallengeDisposition disposition, |
1066 NSURLCredential *credential))completionHandler { | 1100 NSURLCredential *credential))completionHandler { |
1067 NOTIMPLEMENTED(); | 1101 NOTIMPLEMENTED(); |
1068 completionHandler(NSURLSessionAuthChallengeRejectProtectionSpace, nil); | 1102 completionHandler(NSURLSessionAuthChallengeRejectProtectionSpace, nil); |
1069 } | 1103 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1153 placeholderText:defaultText | 1187 placeholderText:defaultText |
1154 requestURL: | 1188 requestURL: |
1155 net::GURLWithNSURL(frame.request.URL) | 1189 net::GURLWithNSURL(frame.request.URL) |
1156 completionHandler:completionHandler]; | 1190 completionHandler:completionHandler]; |
1157 } else if (completionHandler) { | 1191 } else if (completionHandler) { |
1158 completionHandler(nil); | 1192 completionHandler(nil); |
1159 } | 1193 } |
1160 } | 1194 } |
1161 | 1195 |
1162 @end | 1196 @end |
OLD | NEW |