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 |
| 216 |
| 217 - (BOOL)webView:(WKWebView*)webView |
| 218 handleContextMenu:(const web::ContextMenuParams&)params { |
| 219 if ([delegate_ |
| 220 respondsToSelector:@selector(nativeContent:handleContextMenu:)]) { |
| 221 return [delegate_ nativeContent:self handleContextMenu:params]; |
| 222 } |
| 223 return NO; |
| 224 } |
| 225 |
| 226 #pragma mark - |
213 #pragma mark KVO callback | 227 #pragma mark KVO callback |
214 | 228 |
215 - (void)observeValueForKeyPath:(NSString*)keyPath | 229 - (void)observeValueForKeyPath:(NSString*)keyPath |
216 ofObject:(id)object | 230 ofObject:(id)object |
217 change:(NSDictionary*)change | 231 change:(NSDictionary*)change |
218 context:(void*)context { | 232 context:(void*)context { |
219 DCHECK([keyPath isEqualToString:@"title"]); | 233 DCHECK([keyPath isEqualToString:@"title"]); |
220 if ([delegate_ respondsToSelector:@selector(nativeContent:titleDidChange:)]) { | 234 if ([delegate_ respondsToSelector:@selector(nativeContent:titleDidChange:)]) { |
221 // WKWebView's |title| changes to nil when its web process crashes. | 235 // WKWebView's |title| changes to nil when its web process crashes. |
222 if ([webView_ title]) | 236 if ([webView_ title]) |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 [components setScheme:@"file"]; | 285 [components setScheme:@"file"]; |
272 [components setHost:@""]; | 286 [components setHost:@""]; |
273 [components setPath:path]; | 287 [components setPath:path]; |
274 resourceUrl_.reset([[components URL] retain]); | 288 resourceUrl_.reset([[components URL] retain]); |
275 resourcesRootDirectory_.reset([resourceUrl_ copy]); | 289 resourcesRootDirectory_.reset([resourceUrl_ copy]); |
276 return resourceUrl_; | 290 return resourceUrl_; |
277 } | 291 } |
278 | 292 |
279 - (void)ensureWebViewCreated { | 293 - (void)ensureWebViewCreated { |
280 if (!webView_) { | 294 if (!webView_) { |
281 WKWebView* webView = web::BuildWKWebView(CGRectZero, browserState_); | 295 WKWebView* webView = web::BuildWKWebViewWithCustomContextMenu( |
| 296 CGRectZero, browserState_, self); |
282 [webView addObserver:self forKeyPath:@"title" options:0 context:nullptr]; | 297 [webView addObserver:self forKeyPath:@"title" options:0 context:nullptr]; |
283 [webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | | 298 [webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | |
284 UIViewAutoresizingFlexibleHeight]; | 299 UIViewAutoresizingFlexibleHeight]; |
285 [self loadWebViewContents:webView]; | 300 [self loadWebViewContents:webView]; |
286 [webView setNavigationDelegate:self]; | 301 [webView setNavigationDelegate:self]; |
287 webView_.reset([webView retain]); | 302 webView_.reset([webView retain]); |
288 } | 303 } |
289 } | 304 } |
290 | 305 |
291 - (void)loadWebViewContents:(WKWebView*)webView { | 306 - (void)loadWebViewContents:(WKWebView*)webView { |
292 if (!generator_) { | 307 if (!generator_) { |
293 [webView loadFileURL:[self resourceURL] | 308 [webView loadFileURL:[self resourceURL] |
294 allowingReadAccessToURL:resourcesRootDirectory_]; | 309 allowingReadAccessToURL:resourcesRootDirectory_]; |
295 } else { | 310 } else { |
296 NSURL* resourceURL = [self resourceURL]; | 311 NSURL* resourceURL = [self resourceURL]; |
297 [generator_ generateHtml:^(NSString* HTML) { | 312 [generator_ generateHtml:^(NSString* HTML) { |
298 [webView loadHTMLString:HTML baseURL:resourceURL]; | 313 [webView loadHTMLString:HTML baseURL:resourceURL]; |
299 }]; | 314 }]; |
300 } | 315 } |
301 } | 316 } |
302 | 317 |
303 @end | 318 @end |
OLD | NEW |