Chromium Code Reviews| 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 #import <WebKit/WebKit.h> | 7 #import <WebKit/WebKit.h> |
| 8 | 8 |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/mac/bundle_locations.h" | 12 #include "base/mac/bundle_locations.h" |
| 13 #include "base/mac/foundation_util.h" | 13 #include "base/mac/foundation_util.h" |
| 14 #include "base/mac/scoped_nsobject.h" | 14 #include "base/mac/scoped_nsobject.h" |
| 15 #include "ios/web/public/referrer.h" | 15 #include "ios/web/public/referrer.h" |
| 16 #import "ios/web/public/web_state/ui/crw_context_menu_delegate.h" | |
| 16 #import "ios/web/public/web_state/ui/crw_native_content.h" | 17 #import "ios/web/public/web_state/ui/crw_native_content.h" |
| 17 #import "ios/web/public/web_view_creation_util.h" | 18 #import "ios/web/public/web_view_creation_util.h" |
| 18 #import "net/base/mac/url_conversions.h" | 19 #import "net/base/mac/url_conversions.h" |
| 19 #include "ui/base/page_transition_types.h" | 20 #include "ui/base/page_transition_types.h" |
| 20 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
| 21 | 22 |
| 22 @implementation IdrHtmlGenerator | 23 @implementation IdrHtmlGenerator |
| 23 - (id)initWithResourceId:(int)resourceId encoding:(NSStringEncoding)encoding { | 24 - (id)initWithResourceId:(int)resourceId encoding:(NSStringEncoding)encoding { |
| 24 if ((self = [super init])) { | 25 if ((self = [super init])) { |
| 25 resourceId_ = resourceId; | 26 resourceId_ = resourceId; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 36 base::StringPiece html( | 37 base::StringPiece html( |
| 37 ResourceBundle::GetSharedInstance().GetRawDataResource(resourceId_)); | 38 ResourceBundle::GetSharedInstance().GetRawDataResource(resourceId_)); |
| 38 base::scoped_nsobject<NSString> result([[NSString alloc] | 39 base::scoped_nsobject<NSString> result([[NSString alloc] |
| 39 initWithBytes:html.data() | 40 initWithBytes:html.data() |
| 40 length:html.size() | 41 length:html.size() |
| 41 encoding:encoding_]); | 42 encoding:encoding_]); |
| 42 callback(result); | 43 callback(result); |
| 43 } | 44 } |
| 44 @end | 45 @end |
| 45 | 46 |
| 46 @interface StaticHtmlViewController ()<WKNavigationDelegate> { | 47 @interface StaticHtmlViewController ()<CRWContextMenuDelegate, |
| 48 WKNavigationDelegate> { | |
| 47 @private | 49 @private |
| 48 // The referrer that will be passed when navigating from this page. | 50 // The referrer that will be passed when navigating from this page. |
| 49 web::Referrer referrer_; | 51 web::Referrer referrer_; |
| 50 | 52 |
| 51 // The URL that will be passed to the web page when loading. | 53 // The URL that will be passed to the web page when loading. |
| 52 // If the page is displaying a local HTML file, it contains the file URL to | 54 // If the page is displaying a local HTML file, it contains the file URL to |
| 53 // the file. | 55 // the file. |
| 54 // If the page is a generated HTML, it contains a random resource URL. | 56 // If the page is a generated HTML, it contains a random resource URL. |
| 55 base::scoped_nsobject<NSURL> resourceUrl_; | 57 base::scoped_nsobject<NSURL> resourceUrl_; |
| 56 | 58 |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 203 (void (^)(WKNavigationActionPolicy))decisionHandler { | 205 (void (^)(WKNavigationActionPolicy))decisionHandler { |
| 204 decisionHandler( | 206 decisionHandler( |
| 205 [self | 207 [self |
| 206 shouldStartLoadWithRequest:navigationAction.request | 208 shouldStartLoadWithRequest:navigationAction.request |
| 207 fromMainFrame:[navigationAction.sourceFrame isMainFrame]] | 209 fromMainFrame:[navigationAction.sourceFrame isMainFrame]] |
| 208 ? WKNavigationActionPolicyAllow | 210 ? WKNavigationActionPolicyAllow |
| 209 : WKNavigationActionPolicyCancel); | 211 : WKNavigationActionPolicyCancel); |
| 210 } | 212 } |
| 211 | 213 |
| 212 #pragma mark - | 214 #pragma mark - |
| 215 #pragma mark CRWContextMenuDelegate implementation | |
|
Eugene But (OOO till 7-30)
2017/01/13 16:19:41
nit: Add a linebreak ?
Olivier
2017/01/13 18:08:58
Done.
| |
| 216 - (BOOL)webView:(WKWebView*)webView | |
| 217 handleContextMenu:(const web::ContextMenuParams&)params { | |
| 218 if ([delegate_ | |
| 219 respondsToSelector:@selector(nativeContent:handleContextMenu:)]) { | |
| 220 return [delegate_ nativeContent:self handleContextMenu:params]; | |
|
Olivier
2017/01/13 10:20:26
I kept the current behavior of the delegate to pas
Eugene But (OOO till 7-30)
2017/01/13 16:19:41
Looks consistent with -[CRWWebController nativeCon
| |
| 221 } | |
| 222 return NO; | |
| 223 } | |
| 224 | |
| 225 #pragma mark - | |
| 213 #pragma mark KVO callback | 226 #pragma mark KVO callback |
| 214 | 227 |
| 215 - (void)observeValueForKeyPath:(NSString*)keyPath | 228 - (void)observeValueForKeyPath:(NSString*)keyPath |
| 216 ofObject:(id)object | 229 ofObject:(id)object |
| 217 change:(NSDictionary*)change | 230 change:(NSDictionary*)change |
| 218 context:(void*)context { | 231 context:(void*)context { |
| 219 DCHECK([keyPath isEqualToString:@"title"]); | 232 DCHECK([keyPath isEqualToString:@"title"]); |
| 220 if ([delegate_ respondsToSelector:@selector(nativeContent:titleDidChange:)]) { | 233 if ([delegate_ respondsToSelector:@selector(nativeContent:titleDidChange:)]) { |
| 221 // WKWebView's |title| changes to nil when its web process crashes. | 234 // WKWebView's |title| changes to nil when its web process crashes. |
| 222 if ([webView_ title]) | 235 if ([webView_ title]) |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 271 [components setScheme:@"file"]; | 284 [components setScheme:@"file"]; |
| 272 [components setHost:@""]; | 285 [components setHost:@""]; |
| 273 [components setPath:path]; | 286 [components setPath:path]; |
| 274 resourceUrl_.reset([[components URL] retain]); | 287 resourceUrl_.reset([[components URL] retain]); |
| 275 resourcesRootDirectory_.reset([resourceUrl_ copy]); | 288 resourcesRootDirectory_.reset([resourceUrl_ copy]); |
| 276 return resourceUrl_; | 289 return resourceUrl_; |
| 277 } | 290 } |
| 278 | 291 |
| 279 - (void)ensureWebViewCreated { | 292 - (void)ensureWebViewCreated { |
| 280 if (!webView_) { | 293 if (!webView_) { |
| 281 WKWebView* webView = web::BuildWKWebView(CGRectZero, browserState_); | 294 WKWebView* webView = web::BuildWKWebViewWithCustomContextMenu( |
| 295 CGRectZero, browserState_, self); | |
| 282 [webView addObserver:self forKeyPath:@"title" options:0 context:nullptr]; | 296 [webView addObserver:self forKeyPath:@"title" options:0 context:nullptr]; |
| 283 [webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | | 297 [webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | |
| 284 UIViewAutoresizingFlexibleHeight]; | 298 UIViewAutoresizingFlexibleHeight]; |
| 285 [self loadWebViewContents:webView]; | 299 [self loadWebViewContents:webView]; |
| 286 [webView setNavigationDelegate:self]; | 300 [webView setNavigationDelegate:self]; |
| 287 webView_.reset([webView retain]); | 301 webView_.reset([webView retain]); |
| 288 } | 302 } |
| 289 } | 303 } |
| 290 | 304 |
| 291 - (void)loadWebViewContents:(WKWebView*)webView { | 305 - (void)loadWebViewContents:(WKWebView*)webView { |
| 292 if (!generator_) { | 306 if (!generator_) { |
| 293 [webView loadFileURL:[self resourceURL] | 307 [webView loadFileURL:[self resourceURL] |
| 294 allowingReadAccessToURL:resourcesRootDirectory_]; | 308 allowingReadAccessToURL:resourcesRootDirectory_]; |
| 295 } else { | 309 } else { |
| 296 NSURL* resourceURL = [self resourceURL]; | 310 NSURL* resourceURL = [self resourceURL]; |
| 297 [generator_ generateHtml:^(NSString* HTML) { | 311 [generator_ generateHtml:^(NSString* HTML) { |
| 298 [webView loadHTMLString:HTML baseURL:resourceURL]; | 312 [webView loadHTMLString:HTML baseURL:resourceURL]; |
| 299 }]; | 313 }]; |
| 300 } | 314 } |
| 301 } | 315 } |
| 302 | 316 |
| 303 @end | 317 @end |
| OLD | NEW |