OLD | NEW |
---|---|
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 #include "ios/chrome/browser/ui/static_content/static_html_view_controller.h" | 5 #include "ios/chrome/browser/ui/static_content/static_html_view_controller.h" |
6 | 6 |
7 #include <WebKit/WebKit.h> | 7 #include <WebKit/WebKit.h> |
8 | 8 |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 | 10 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 id<CRWNativeContentDelegate> delegate_; // weak | 77 id<CRWNativeContentDelegate> delegate_; // weak |
78 | 78 |
79 // The loader to navigate from the page. | 79 // The loader to navigate from the page. |
80 id<UrlLoader> loader_; // weak | 80 id<UrlLoader> loader_; // weak |
81 } | 81 } |
82 | 82 |
83 // Returns the URL of the static page to display. | 83 // Returns the URL of the static page to display. |
84 - (NSURL*)resourceURL; | 84 - (NSURL*)resourceURL; |
85 // Ensures that webView_ has been created, creating it if necessary. | 85 // Ensures that webView_ has been created, creating it if necessary. |
86 - (void)ensureWebViewCreated; | 86 - (void)ensureWebViewCreated; |
87 // Determines if the page load should begin based on the current |resourceURL|. | 87 // Determines if the page load should begin based on the current |resourceURL| |
88 - (BOOL)shouldStartLoadWithRequest:(NSURLRequest*)request; | 88 // and the navigation type |navigationType|. |
89 - (BOOL)shouldStartLoadWithRequest:(NSURLRequest*)request | |
90 type:(WKNavigationType)navigationType; | |
89 @end | 91 @end |
90 | 92 |
91 @implementation StaticHtmlViewController | 93 @implementation StaticHtmlViewController |
92 | 94 |
93 - (instancetype)initWithResource:(NSString*)resource | 95 - (instancetype)initWithResource:(NSString*)resource |
94 browserState:(web::BrowserState*)browserState { | 96 browserState:(web::BrowserState*)browserState { |
95 DCHECK(resource); | 97 DCHECK(resource); |
96 DCHECK(browserState); | 98 DCHECK(browserState); |
97 if ((self = [super init])) { | 99 if ((self = [super init])) { |
98 resource_.reset([resource copy]); | 100 resource_.reset([resource copy]); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 } | 191 } |
190 | 192 |
191 - (void)setScrollEnabled:(BOOL)enabled { | 193 - (void)setScrollEnabled:(BOOL)enabled { |
192 [[self scrollView] setScrollEnabled:enabled]; | 194 [[self scrollView] setScrollEnabled:enabled]; |
193 } | 195 } |
194 | 196 |
195 #pragma mark - | 197 #pragma mark - |
196 #pragma mark WKNavigationDelegate implementation | 198 #pragma mark WKNavigationDelegate implementation |
197 | 199 |
198 - (void)webView:(WKWebView*)webView | 200 - (void)webView:(WKWebView*)webView |
199 decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction | 201 decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction |
Eugene But (OOO till 7-30)
2016/12/22 17:37:51
Per offline conversation, you probably want to use
| |
200 decisionHandler: | 202 decisionHandler: |
201 (void (^)(WKNavigationActionPolicy))decisionHandler { | 203 (void (^)(WKNavigationActionPolicy))decisionHandler { |
202 decisionHandler([self shouldStartLoadWithRequest:navigationAction.request] | 204 decisionHandler( |
203 ? WKNavigationActionPolicyAllow | 205 [self shouldStartLoadWithRequest:navigationAction.request |
204 : WKNavigationActionPolicyCancel); | 206 type:navigationAction.navigationType] |
207 ? WKNavigationActionPolicyAllow | |
208 : WKNavigationActionPolicyCancel); | |
205 } | 209 } |
206 | 210 |
207 #pragma mark - | 211 #pragma mark - |
208 #pragma mark KVO callback | 212 #pragma mark KVO callback |
209 | 213 |
210 - (void)observeValueForKeyPath:(NSString*)keyPath | 214 - (void)observeValueForKeyPath:(NSString*)keyPath |
211 ofObject:(id)object | 215 ofObject:(id)object |
212 change:(NSDictionary*)change | 216 change:(NSDictionary*)change |
213 context:(void*)context { | 217 context:(void*)context { |
214 DCHECK([keyPath isEqualToString:@"title"]); | 218 DCHECK([keyPath isEqualToString:@"title"]); |
215 if ([delegate_ respondsToSelector:@selector(nativeContent:titleDidChange:)]) { | 219 if ([delegate_ respondsToSelector:@selector(nativeContent:titleDidChange:)]) { |
216 // WKWebView's |title| changes to nil when its web process crashes. | 220 // WKWebView's |title| changes to nil when its web process crashes. |
217 if ([webView_ title]) | 221 if ([webView_ title]) |
218 [delegate_ nativeContent:self titleDidChange:[webView_ title]]; | 222 [delegate_ nativeContent:self titleDidChange:[webView_ title]]; |
219 } | 223 } |
220 } | 224 } |
221 | 225 |
222 #pragma mark - | 226 #pragma mark - |
223 #pragma mark Private | 227 #pragma mark Private |
224 | 228 |
225 - (BOOL)shouldStartLoadWithRequest:(NSURLRequest*)request { | 229 - (BOOL)shouldStartLoadWithRequest:(NSURLRequest*)request |
230 type:(WKNavigationType)navigationType { | |
226 // Only allow displaying the URL which correspond to the authorized resource. | 231 // Only allow displaying the URL which correspond to the authorized resource. |
227 if ([[request URL] isEqual:[self resourceURL]]) | 232 if ([[request URL] isEqual:[self resourceURL]]) |
228 return YES; | 233 return YES; |
229 | 234 |
230 // All other URLs will be loaded by our UrlLoader if we have one. | 235 // All other navigation URLs will be loaded by our UrlLoader if one exists. |
231 if (loader_) { | 236 // If type is |WKNavigationTypeOther|, the URL corresponds to an iframe. |
237 if (loader_ && navigationType != WKNavigationTypeOther) { | |
232 dispatch_async(dispatch_get_main_queue(), ^{ | 238 dispatch_async(dispatch_get_main_queue(), ^{ |
233 [loader_ loadURL:net::GURLWithNSURL([request URL]) | 239 [loader_ loadURL:net::GURLWithNSURL([request URL]) |
234 referrer:referrer_ | 240 referrer:referrer_ |
235 transition:ui::PAGE_TRANSITION_LINK | 241 transition:ui::PAGE_TRANSITION_LINK |
236 rendererInitiated:YES]; | 242 rendererInitiated:YES]; |
237 }); | 243 }); |
238 } | 244 } |
239 return NO; | 245 return NO; |
240 } | 246 } |
241 | 247 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
287 allowingReadAccessToURL:resourcesRootDirectory_]; | 293 allowingReadAccessToURL:resourcesRootDirectory_]; |
288 } else { | 294 } else { |
289 NSURL* resourceURL = [self resourceURL]; | 295 NSURL* resourceURL = [self resourceURL]; |
290 [generator_ generateHtml:^(NSString* HTML) { | 296 [generator_ generateHtml:^(NSString* HTML) { |
291 [webView loadHTMLString:HTML baseURL:resourceURL]; | 297 [webView loadHTMLString:HTML baseURL:resourceURL]; |
292 }]; | 298 }]; |
293 } | 299 } |
294 } | 300 } |
295 | 301 |
296 @end | 302 @end |
OLD | NEW |