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_native_content.h" | 16 #import "ios/web/public/web_state/ui/crw_native_content.h" |
17 #import "ios/web/public/web_view_creation_util.h" | 17 #import "ios/web/public/web_view_creation_util.h" |
18 #import "ios/web/public/web_state/ui/crw_context_menu_controller.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; |
26 encoding_ = encoding; | 27 encoding_ = encoding; |
27 } | 28 } |
28 return self; | 29 return self; |
29 } | 30 } |
30 | 31 |
31 - (id)initWithResourceId:(int)resourceId { | 32 - (id)initWithResourceId:(int)resourceId { |
32 return [self initWithResourceId:resourceId encoding:NSUTF8StringEncoding]; | 33 return [self initWithResourceId:resourceId encoding:NSUTF8StringEncoding]; |
33 } | 34 } |
34 | 35 |
35 - (void)generateHtml:(HtmlCallback)callback { | 36 - (void)generateHtml:(HtmlCallback)callback { |
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 ()<CRWContextMenuControllerWebView, |
48 WKNavigationDelegate, | |
49 UIGestureRecognizerDelegate> { | |
jif
2017/01/12 17:13:12
remove UIGestureRecognizerDelegate?
Olivier
2017/01/13 10:20:25
Done.
| |
47 @private | 50 @private |
48 // The referrer that will be passed when navigating from this page. | 51 // The referrer that will be passed when navigating from this page. |
49 web::Referrer referrer_; | 52 web::Referrer referrer_; |
50 | 53 |
51 // The URL that will be passed to the web page when loading. | 54 // 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 | 55 // If the page is displaying a local HTML file, it contains the file URL to |
53 // the file. | 56 // the file. |
54 // If the page is a generated HTML, it contains a random resource URL. | 57 // If the page is a generated HTML, it contains a random resource URL. |
55 base::scoped_nsobject<NSURL> resourceUrl_; | 58 base::scoped_nsobject<NSURL> resourceUrl_; |
56 | 59 |
(...skipping 12 matching lines...) Expand all Loading... | |
69 // Browser state associated with the view controller, used to create the | 72 // Browser state associated with the view controller, used to create the |
70 // WKWebView. | 73 // WKWebView. |
71 web::BrowserState* browserState_; // Weak. | 74 web::BrowserState* browserState_; // Weak. |
72 | 75 |
73 // The web view that is used to display the content. | 76 // The web view that is used to display the content. |
74 base::scoped_nsobject<WKWebView> webView_; | 77 base::scoped_nsobject<WKWebView> webView_; |
75 | 78 |
76 // The delegate of the native content. | 79 // The delegate of the native content. |
77 id<CRWNativeContentDelegate> delegate_; // weak | 80 id<CRWNativeContentDelegate> delegate_; // weak |
78 | 81 |
82 // The controller that will recognize the long press and trigger the context | |
83 // menu. | |
84 base::scoped_nsobject<CRWContextMenuController> contextMenuController_; | |
85 | |
79 // The loader to navigate from the page. | 86 // The loader to navigate from the page. |
80 id<UrlLoader> loader_; // weak | 87 id<UrlLoader> loader_; // weak |
81 } | 88 } |
82 | 89 |
83 // Returns the URL of the static page to display. | 90 // Returns the URL of the static page to display. |
84 - (NSURL*)resourceURL; | 91 - (NSURL*)resourceURL; |
85 // Ensures that webView_ has been created, creating it if necessary. | 92 // Ensures that webView_ has been created, creating it if necessary. |
86 - (void)ensureWebViewCreated; | 93 - (void)ensureWebViewCreated; |
87 // Determines if the page load should begin based on the current |resourceURL| | 94 // Determines if the page load should begin based on the current |resourceURL| |
88 // and if the request is issued by the main frame (|fromMainFrame|). | 95 // and if the request is issued by the main frame (|fromMainFrame|). |
(...skipping 20 matching lines...) Expand all Loading... | |
109 DCHECK(browserState); | 116 DCHECK(browserState); |
110 if ((self = [super init])) { | 117 if ((self = [super init])) { |
111 generator_.reset([generator retain]); | 118 generator_.reset([generator retain]); |
112 browserState_ = browserState; | 119 browserState_ = browserState; |
113 } | 120 } |
114 return self; | 121 return self; |
115 } | 122 } |
116 | 123 |
117 - (instancetype)initWithFileURL:(const GURL&)URL | 124 - (instancetype)initWithFileURL:(const GURL&)URL |
118 allowingReadAccessToURL:(const GURL&)resourcesRoot | 125 allowingReadAccessToURL:(const GURL&)resourcesRoot |
119 browserState:(web::BrowserState*)browserState { | 126 browserState:(web::BrowserState*)browserState |
127 contextMenuDelegate: | |
128 (id<CRWContextMenuControllerDelegate>)contextMenuDelegate { | |
120 DCHECK(URL.is_valid()); | 129 DCHECK(URL.is_valid()); |
121 DCHECK(URL.SchemeIsFile()); | 130 DCHECK(URL.SchemeIsFile()); |
122 DCHECK(browserState); | 131 DCHECK(browserState); |
123 if ((self = [super init])) { | 132 if ((self = [super init])) { |
124 resourceUrl_.reset([net::NSURLWithGURL(URL) retain]); | 133 resourceUrl_.reset([net::NSURLWithGURL(URL) retain]); |
125 resourcesRootDirectory_.reset([net::NSURLWithGURL(resourcesRoot) retain]); | 134 resourcesRootDirectory_.reset([net::NSURLWithGURL(resourcesRoot) retain]); |
126 browserState_ = browserState; | 135 browserState_ = browserState; |
136 if (contextMenuDelegate) { | |
137 contextMenuController_.reset([[CRWContextMenuController alloc] | |
138 initWithWebView:self | |
139 delegate:contextMenuDelegate]); | |
140 } | |
127 } | 141 } |
128 return self; | 142 return self; |
129 } | 143 } |
130 | 144 |
131 - (void)dealloc { | 145 - (void)dealloc { |
132 [self removeWebViewObservers]; | 146 [self removeWebViewObservers]; |
133 [super dealloc]; | 147 [super dealloc]; |
134 } | 148 } |
135 | 149 |
136 - (void)removeWebViewObservers { | 150 - (void)removeWebViewObservers { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
203 (void (^)(WKNavigationActionPolicy))decisionHandler { | 217 (void (^)(WKNavigationActionPolicy))decisionHandler { |
204 decisionHandler( | 218 decisionHandler( |
205 [self | 219 [self |
206 shouldStartLoadWithRequest:navigationAction.request | 220 shouldStartLoadWithRequest:navigationAction.request |
207 fromMainFrame:[navigationAction.sourceFrame isMainFrame]] | 221 fromMainFrame:[navigationAction.sourceFrame isMainFrame]] |
208 ? WKNavigationActionPolicyAllow | 222 ? WKNavigationActionPolicyAllow |
209 : WKNavigationActionPolicyCancel); | 223 : WKNavigationActionPolicyCancel); |
210 } | 224 } |
211 | 225 |
212 #pragma mark - | 226 #pragma mark - |
227 #pragma mark CRWWKContextMenuWebView implementation | |
228 - (UIView*)webScrollView { | |
229 return self.scrollView; | |
230 } | |
231 | |
232 #pragma mark - | |
213 #pragma mark KVO callback | 233 #pragma mark KVO callback |
214 | 234 |
215 - (void)observeValueForKeyPath:(NSString*)keyPath | 235 - (void)observeValueForKeyPath:(NSString*)keyPath |
216 ofObject:(id)object | 236 ofObject:(id)object |
217 change:(NSDictionary*)change | 237 change:(NSDictionary*)change |
218 context:(void*)context { | 238 context:(void*)context { |
219 DCHECK([keyPath isEqualToString:@"title"]); | 239 DCHECK([keyPath isEqualToString:@"title"]); |
220 if ([delegate_ respondsToSelector:@selector(nativeContent:titleDidChange:)]) { | 240 if ([delegate_ respondsToSelector:@selector(nativeContent:titleDidChange:)]) { |
221 // WKWebView's |title| changes to nil when its web process crashes. | 241 // WKWebView's |title| changes to nil when its web process crashes. |
222 if ([webView_ title]) | 242 if ([webView_ title]) |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
294 allowingReadAccessToURL:resourcesRootDirectory_]; | 314 allowingReadAccessToURL:resourcesRootDirectory_]; |
295 } else { | 315 } else { |
296 NSURL* resourceURL = [self resourceURL]; | 316 NSURL* resourceURL = [self resourceURL]; |
297 [generator_ generateHtml:^(NSString* HTML) { | 317 [generator_ generateHtml:^(NSString* HTML) { |
298 [webView loadHTMLString:HTML baseURL:resourceURL]; | 318 [webView loadHTMLString:HTML baseURL:resourceURL]; |
299 }]; | 319 }]; |
300 } | 320 } |
301 } | 321 } |
302 | 322 |
303 @end | 323 @end |
OLD | NEW |