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_view/shell/shell_view_controller.h" | 5 #import "ios/web_view/shell/shell_view_controller.h" |
6 | 6 |
7 #import <ChromeWebView/ChromeWebView.h> | 7 #import <ChromeWebView/ChromeWebView.h> |
8 #import <MobileCoreServices/MobileCoreServices.h> | 8 #import <MobileCoreServices/MobileCoreServices.h> |
9 | 9 |
10 #import "ios/web_view/shell/shell_translation_delegate.h" | 10 #import "ios/web_view/shell/shell_translation_delegate.h" |
(...skipping 19 matching lines...) Expand all Loading... | |
30 // Toolbar button to navigate backwards. | 30 // Toolbar button to navigate backwards. |
31 @property(nonatomic, strong) UIButton* backButton; | 31 @property(nonatomic, strong) UIButton* backButton; |
32 // Toolbar button to navigate forwards. | 32 // Toolbar button to navigate forwards. |
33 @property(nonatomic, strong) UIButton* forwardButton; | 33 @property(nonatomic, strong) UIButton* forwardButton; |
34 // Toolbar containing navigation buttons and |field|. | 34 // Toolbar containing navigation buttons and |field|. |
35 @property(nonatomic, strong) UIToolbar* toolbar; | 35 @property(nonatomic, strong) UIToolbar* toolbar; |
36 // CWV view which renders the web page. | 36 // CWV view which renders the web page. |
37 @property(nonatomic, strong) CWVWebView* webView; | 37 @property(nonatomic, strong) CWVWebView* webView; |
38 // Handles the translation of the content displayed in |webView|. | 38 // Handles the translation of the content displayed in |webView|. |
39 @property(nonatomic, strong) ShellTranslationDelegate* translationDelegate; | 39 @property(nonatomic, strong) ShellTranslationDelegate* translationDelegate; |
40 // Whether it's in the incognito mode. | |
41 @property(nonatomic) BOOL isIncognito; | |
michaeldo
2017/05/31 21:21:10
I actually think it would be better to either use
Hiroshi Ichikawa
2017/06/01 01:20:37
Done.
| |
40 | 42 |
41 - (void)back; | 43 - (void)back; |
42 - (void)forward; | 44 - (void)forward; |
43 - (void)stopLoading; | 45 - (void)stopLoading; |
44 // Disconnects and release the |webView|. | 46 // Disconnects and release the |webView|. |
45 - (void)resetWebView; | 47 - (void)removeWebView; |
46 @end | 48 @end |
47 | 49 |
48 @implementation ShellViewController | 50 @implementation ShellViewController |
49 | 51 |
50 @synthesize backButton = _backButton; | 52 @synthesize backButton = _backButton; |
51 @synthesize containerView = _containerView; | 53 @synthesize containerView = _containerView; |
52 @synthesize field = _field; | 54 @synthesize field = _field; |
53 @synthesize forwardButton = _forwardButton; | 55 @synthesize forwardButton = _forwardButton; |
54 @synthesize toolbar = _toolbar; | 56 @synthesize toolbar = _toolbar; |
55 @synthesize webView = _webView; | 57 @synthesize webView = _webView; |
56 @synthesize translationDelegate = _translationDelegate; | 58 @synthesize translationDelegate = _translationDelegate; |
59 @synthesize isIncognito = _isIncognito; | |
57 | 60 |
58 - (void)viewDidLoad { | 61 - (void)viewDidLoad { |
59 [super viewDidLoad]; | 62 [super viewDidLoad]; |
60 | 63 |
61 CGRect bounds = self.view.bounds; | 64 CGRect bounds = self.view.bounds; |
62 | 65 |
63 // Set up the toolbar. | 66 // Set up the toolbar. |
64 self.toolbar = [[UIToolbar alloc] init]; | 67 self.toolbar = [[UIToolbar alloc] init]; |
65 [_toolbar setBarTintColor:[UIColor colorWithRed:0.337 | 68 [_toolbar setBarTintColor:[UIColor colorWithRed:0.337 |
66 green:0.467 | 69 green:0.467 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 forControlEvents:UIControlEventTouchUpInside]; | 153 forControlEvents:UIControlEventTouchUpInside]; |
151 | 154 |
152 [_toolbar addSubview:_backButton]; | 155 [_toolbar addSubview:_backButton]; |
153 [_toolbar addSubview:_forwardButton]; | 156 [_toolbar addSubview:_forwardButton]; |
154 [_toolbar addSubview:stop]; | 157 [_toolbar addSubview:stop]; |
155 [_toolbar addSubview:menu]; | 158 [_toolbar addSubview:menu]; |
156 [_toolbar addSubview:_field]; | 159 [_toolbar addSubview:_field]; |
157 | 160 |
158 [CWVWebView setUserAgentProduct:@"Dummy/1.0"]; | 161 [CWVWebView setUserAgentProduct:@"Dummy/1.0"]; |
159 | 162 |
160 CWVWebViewConfiguration* configuration = | 163 [self createWebViewWithConfiguration:[self createConfiguration]]; |
michaeldo
2017/05/31 21:21:09
I think explicitly using [CWVWebViewConfiguration
Hiroshi Ichikawa
2017/06/01 01:20:37
Done.
| |
161 [CWVWebViewConfiguration defaultConfiguration]; | |
162 self.webView = [[CWVWebView alloc] initWithFrame:[_containerView bounds] | |
163 configuration:configuration]; | |
164 // Gives a restoration identifier so that state restoration works. | |
165 _webView.restorationIdentifier = @"webView"; | |
166 _webView.navigationDelegate = self; | |
167 _webView.UIDelegate = self; | |
168 _translationDelegate = [[ShellTranslationDelegate alloc] init]; | |
169 _webView.translationController.delegate = _translationDelegate; | |
170 | |
171 [_webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | | |
172 UIViewAutoresizingFlexibleHeight]; | |
173 [_containerView addSubview:_webView]; | |
174 | |
175 [_webView addObserver:self | |
176 forKeyPath:@"canGoBack" | |
177 options:NSKeyValueObservingOptionNew | |
178 context:nil]; | |
179 [_webView addObserver:self | |
180 forKeyPath:@"canGoForward" | |
181 options:NSKeyValueObservingOptionNew | |
182 context:nil]; | |
183 | |
184 NSURLRequest* request = [NSURLRequest | |
185 requestWithURL:[NSURL URLWithString:@"https://www.google.com/"]]; | |
186 [_webView loadRequest:request]; | |
187 } | 164 } |
188 | 165 |
189 - (void)observeValueForKeyPath:(NSString*)keyPath | 166 - (void)observeValueForKeyPath:(NSString*)keyPath |
190 ofObject:(id)object | 167 ofObject:(id)object |
191 change:(NSDictionary<NSKeyValueChangeKey, id>*)change | 168 change:(NSDictionary<NSKeyValueChangeKey, id>*)change |
192 context:(void*)context { | 169 context:(void*)context { |
193 if ([keyPath isEqualToString:@"canGoBack"]) { | 170 if ([keyPath isEqualToString:@"canGoBack"]) { |
194 _backButton.enabled = [_webView canGoBack]; | 171 _backButton.enabled = [_webView canGoBack]; |
195 } else if ([keyPath isEqualToString:@"canGoForward"]) { | 172 } else if ([keyPath isEqualToString:@"canGoForward"]) { |
196 _forwardButton.enabled = [_webView canGoForward]; | 173 _forwardButton.enabled = [_webView canGoForward]; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
232 | 209 |
233 __weak ShellViewController* weakSelf = self; | 210 __weak ShellViewController* weakSelf = self; |
234 | 211 |
235 [alertController | 212 [alertController |
236 addAction:[UIAlertAction actionWithTitle:@"Reload" | 213 addAction:[UIAlertAction actionWithTitle:@"Reload" |
237 style:UIAlertActionStyleDefault | 214 style:UIAlertActionStyleDefault |
238 handler:^(UIAlertAction* action) { | 215 handler:^(UIAlertAction* action) { |
239 [weakSelf.webView reload]; | 216 [weakSelf.webView reload]; |
240 }]]; | 217 }]]; |
241 | 218 |
242 // Removes the web view from the view hierarchy and releases it. For | 219 // Toggles the incognito mode. |
243 // testing deallocation behavior, because there have been multiple crash bugs | 220 NSString* incognitoActionTitle = |
244 // on deallocation of CWVWebView. | 221 _isIncognito ? @"Exit incognito" : @"Enter incognito"; |
245 [alertController | 222 [alertController |
246 addAction:[UIAlertAction actionWithTitle:@"Deallocate web view" | 223 addAction:[UIAlertAction |
247 style:UIAlertActionStyleDefault | 224 actionWithTitle:incognitoActionTitle |
248 handler:^(UIAlertAction* action) { | 225 style:UIAlertActionStyleDefault |
249 [weakSelf resetWebView]; | 226 handler:^(UIAlertAction* action) { |
250 }]]; | 227 [weakSelf removeWebView]; |
228 weakSelf.isIncognito = !weakSelf.isIncognito; | |
229 [weakSelf createWebViewWithConfiguration: | |
230 [weakSelf createConfiguration]]; | |
231 }]]; | |
232 | |
233 // Removes the web view from the view hierarchy, releases it, and recreates | |
234 // the web view with the same configuration. This is for testing deallocation | |
235 // and sharing configuration, because there have been multiple bugs on these | |
michaeldo
2017/05/31 21:21:10
I think you can remove the ", because there have b
Hiroshi Ichikawa
2017/06/01 01:20:36
Done.
| |
236 // features. | |
237 [alertController | |
238 addAction:[UIAlertAction | |
239 actionWithTitle:@"Recreate web view" | |
240 style:UIAlertActionStyleDefault | |
241 handler:^(UIAlertAction* action) { | |
242 CWVWebViewConfiguration* configuration = | |
243 weakSelf.webView.configuration; | |
244 [weakSelf removeWebView]; | |
245 [weakSelf | |
246 createWebViewWithConfiguration:configuration]; | |
247 }]]; | |
251 | 248 |
252 [self presentViewController:alertController animated:YES completion:nil]; | 249 [self presentViewController:alertController animated:YES completion:nil]; |
253 } | 250 } |
254 | 251 |
255 - (void)resetWebView { | 252 - (CWVWebViewConfiguration*)createConfiguration { |
michaeldo
2017/05/31 21:21:10
Per comment above, I don't think we need this. We
Hiroshi Ichikawa
2017/06/01 01:20:37
Done. I also extracted -toggleIncognito as a metho
| |
253 return _isIncognito ? [CWVWebViewConfiguration incognitoConfiguration] | |
254 : [CWVWebViewConfiguration defaultConfiguration]; | |
255 } | |
256 | |
257 - (void)createWebViewWithConfiguration:(CWVWebViewConfiguration*)configuration { | |
258 self.webView = [[CWVWebView alloc] initWithFrame:[_containerView bounds] | |
259 configuration:configuration]; | |
260 // Gives a restoration identifier so that state restoration works. | |
261 _webView.restorationIdentifier = @"webView"; | |
262 _webView.navigationDelegate = self; | |
263 _webView.UIDelegate = self; | |
264 _translationDelegate = [[ShellTranslationDelegate alloc] init]; | |
265 _webView.translationController.delegate = _translationDelegate; | |
266 | |
267 [_webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | | |
268 UIViewAutoresizingFlexibleHeight]; | |
269 [_containerView addSubview:_webView]; | |
270 | |
271 [_webView addObserver:self | |
272 forKeyPath:@"canGoBack" | |
273 options:NSKeyValueObservingOptionNew | |
274 context:nil]; | |
275 [_webView addObserver:self | |
276 forKeyPath:@"canGoForward" | |
277 options:NSKeyValueObservingOptionNew | |
278 context:nil]; | |
279 | |
280 NSURLRequest* request = [NSURLRequest | |
281 requestWithURL:[NSURL URLWithString:@"https://www.google.com/"]]; | |
282 [_webView loadRequest:request]; | |
283 } | |
284 | |
285 - (void)removeWebView { | |
256 [_webView removeFromSuperview]; | 286 [_webView removeFromSuperview]; |
257 [_webView removeObserver:self forKeyPath:@"canGoBack"]; | 287 [_webView removeObserver:self forKeyPath:@"canGoBack"]; |
258 [_webView removeObserver:self forKeyPath:@"canGoForward"]; | 288 [_webView removeObserver:self forKeyPath:@"canGoForward"]; |
259 _webView = nil; | 289 _webView = nil; |
260 } | 290 } |
261 | 291 |
262 - (void)dealloc { | 292 - (void)dealloc { |
263 [_webView removeObserver:self forKeyPath:@"canGoBack"]; | 293 [_webView removeObserver:self forKeyPath:@"canGoBack"]; |
264 [_webView removeObserver:self forKeyPath:@"canGoForward"]; | 294 [_webView removeObserver:self forKeyPath:@"canGoForward"]; |
265 } | 295 } |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
436 // TODO(crbug.com/679895): Add some visual indication that the page load has | 466 // TODO(crbug.com/679895): Add some visual indication that the page load has |
437 // finished. | 467 // finished. |
438 [self updateToolbar]; | 468 [self updateToolbar]; |
439 } | 469 } |
440 | 470 |
441 - (void)webViewWebContentProcessDidTerminate:(CWVWebView*)webView { | 471 - (void)webViewWebContentProcessDidTerminate:(CWVWebView*)webView { |
442 NSLog(@"webViewWebContentProcessDidTerminate"); | 472 NSLog(@"webViewWebContentProcessDidTerminate"); |
443 } | 473 } |
444 | 474 |
445 @end | 475 @end |
OLD | NEW |