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 24 matching lines...) Expand all Loading... |
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 | 40 |
41 - (void)back; | 41 - (void)back; |
42 - (void)forward; | 42 - (void)forward; |
43 - (void)stopLoading; | 43 - (void)stopLoading; |
44 // Disconnects and release the |webView|. | 44 // Disconnects and release the |webView|. |
45 - (void)resetWebView; | 45 - (void)removeWebView; |
46 @end | 46 @end |
47 | 47 |
48 @implementation ShellViewController | 48 @implementation ShellViewController |
49 | 49 |
50 @synthesize backButton = _backButton; | 50 @synthesize backButton = _backButton; |
51 @synthesize containerView = _containerView; | 51 @synthesize containerView = _containerView; |
52 @synthesize field = _field; | 52 @synthesize field = _field; |
53 @synthesize forwardButton = _forwardButton; | 53 @synthesize forwardButton = _forwardButton; |
54 @synthesize toolbar = _toolbar; | 54 @synthesize toolbar = _toolbar; |
55 @synthesize webView = _webView; | 55 @synthesize webView = _webView; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 forControlEvents:UIControlEventTouchUpInside]; | 150 forControlEvents:UIControlEventTouchUpInside]; |
151 | 151 |
152 [_toolbar addSubview:_backButton]; | 152 [_toolbar addSubview:_backButton]; |
153 [_toolbar addSubview:_forwardButton]; | 153 [_toolbar addSubview:_forwardButton]; |
154 [_toolbar addSubview:stop]; | 154 [_toolbar addSubview:stop]; |
155 [_toolbar addSubview:menu]; | 155 [_toolbar addSubview:menu]; |
156 [_toolbar addSubview:_field]; | 156 [_toolbar addSubview:_field]; |
157 | 157 |
158 [CWVWebView setUserAgentProduct:@"Dummy/1.0"]; | 158 [CWVWebView setUserAgentProduct:@"Dummy/1.0"]; |
159 | 159 |
160 CWVWebViewConfiguration* configuration = | 160 [self createWebViewWithConfiguration:[CWVWebViewConfiguration |
161 [CWVWebViewConfiguration defaultConfiguration]; | 161 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 } | 162 } |
188 | 163 |
189 - (void)observeValueForKeyPath:(NSString*)keyPath | 164 - (void)observeValueForKeyPath:(NSString*)keyPath |
190 ofObject:(id)object | 165 ofObject:(id)object |
191 change:(NSDictionary<NSKeyValueChangeKey, id>*)change | 166 change:(NSDictionary<NSKeyValueChangeKey, id>*)change |
192 context:(void*)context { | 167 context:(void*)context { |
193 if ([keyPath isEqualToString:@"canGoBack"]) { | 168 if ([keyPath isEqualToString:@"canGoBack"]) { |
194 _backButton.enabled = [_webView canGoBack]; | 169 _backButton.enabled = [_webView canGoBack]; |
195 } else if ([keyPath isEqualToString:@"canGoForward"]) { | 170 } else if ([keyPath isEqualToString:@"canGoForward"]) { |
196 _forwardButton.enabled = [_webView canGoForward]; | 171 _forwardButton.enabled = [_webView canGoForward]; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 | 207 |
233 __weak ShellViewController* weakSelf = self; | 208 __weak ShellViewController* weakSelf = self; |
234 | 209 |
235 [alertController | 210 [alertController |
236 addAction:[UIAlertAction actionWithTitle:@"Reload" | 211 addAction:[UIAlertAction actionWithTitle:@"Reload" |
237 style:UIAlertActionStyleDefault | 212 style:UIAlertActionStyleDefault |
238 handler:^(UIAlertAction* action) { | 213 handler:^(UIAlertAction* action) { |
239 [weakSelf.webView reload]; | 214 [weakSelf.webView reload]; |
240 }]]; | 215 }]]; |
241 | 216 |
242 // Removes the web view from the view hierarchy and releases it. For | 217 // Toggles the incognito mode. |
243 // testing deallocation behavior, because there have been multiple crash bugs | 218 NSString* incognitoActionTitle = _webView.configuration.persistent |
244 // on deallocation of CWVWebView. | 219 ? @"Enter incognito" |
| 220 : @"Exit incognito"; |
245 [alertController | 221 [alertController |
246 addAction:[UIAlertAction actionWithTitle:@"Deallocate web view" | 222 addAction:[UIAlertAction actionWithTitle:incognitoActionTitle |
247 style:UIAlertActionStyleDefault | 223 style:UIAlertActionStyleDefault |
248 handler:^(UIAlertAction* action) { | 224 handler:^(UIAlertAction* action) { |
249 [weakSelf resetWebView]; | 225 [weakSelf toggleIncognito]; |
250 }]]; | 226 }]]; |
251 | 227 |
| 228 // Removes the web view from the view hierarchy, releases it, and recreates |
| 229 // the web view with the same configuration. This is for testing deallocation |
| 230 // and sharing configuration. |
| 231 [alertController |
| 232 addAction:[UIAlertAction |
| 233 actionWithTitle:@"Recreate web view" |
| 234 style:UIAlertActionStyleDefault |
| 235 handler:^(UIAlertAction* action) { |
| 236 CWVWebViewConfiguration* configuration = |
| 237 weakSelf.webView.configuration; |
| 238 [weakSelf removeWebView]; |
| 239 [weakSelf |
| 240 createWebViewWithConfiguration:configuration]; |
| 241 }]]; |
| 242 |
252 [self presentViewController:alertController animated:YES completion:nil]; | 243 [self presentViewController:alertController animated:YES completion:nil]; |
253 } | 244 } |
254 | 245 |
255 - (void)resetWebView { | 246 - (void)toggleIncognito { |
| 247 BOOL wasPersistent = _webView.configuration.persistent; |
| 248 [self removeWebView]; |
| 249 CWVWebViewConfiguration* newConfiguration = |
| 250 wasPersistent ? [CWVWebViewConfiguration incognitoConfiguration] |
| 251 : [CWVWebViewConfiguration defaultConfiguration]; |
| 252 [self createWebViewWithConfiguration:newConfiguration]; |
| 253 } |
| 254 |
| 255 - (void)createWebViewWithConfiguration:(CWVWebViewConfiguration*)configuration { |
| 256 self.webView = [[CWVWebView alloc] initWithFrame:[_containerView bounds] |
| 257 configuration:configuration]; |
| 258 // Gives a restoration identifier so that state restoration works. |
| 259 _webView.restorationIdentifier = @"webView"; |
| 260 _webView.navigationDelegate = self; |
| 261 _webView.UIDelegate = self; |
| 262 _translationDelegate = [[ShellTranslationDelegate alloc] init]; |
| 263 _webView.translationController.delegate = _translationDelegate; |
| 264 |
| 265 [_webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | |
| 266 UIViewAutoresizingFlexibleHeight]; |
| 267 [_containerView addSubview:_webView]; |
| 268 |
| 269 [_webView addObserver:self |
| 270 forKeyPath:@"canGoBack" |
| 271 options:NSKeyValueObservingOptionNew |
| 272 context:nil]; |
| 273 [_webView addObserver:self |
| 274 forKeyPath:@"canGoForward" |
| 275 options:NSKeyValueObservingOptionNew |
| 276 context:nil]; |
| 277 |
| 278 NSURLRequest* request = [NSURLRequest |
| 279 requestWithURL:[NSURL URLWithString:@"https://www.google.com/"]]; |
| 280 [_webView loadRequest:request]; |
| 281 } |
| 282 |
| 283 - (void)removeWebView { |
256 [_webView removeFromSuperview]; | 284 [_webView removeFromSuperview]; |
257 [_webView removeObserver:self forKeyPath:@"canGoBack"]; | 285 [_webView removeObserver:self forKeyPath:@"canGoBack"]; |
258 [_webView removeObserver:self forKeyPath:@"canGoForward"]; | 286 [_webView removeObserver:self forKeyPath:@"canGoForward"]; |
259 _webView = nil; | 287 _webView = nil; |
260 } | 288 } |
261 | 289 |
262 - (void)dealloc { | 290 - (void)dealloc { |
263 [_webView removeObserver:self forKeyPath:@"canGoBack"]; | 291 [_webView removeObserver:self forKeyPath:@"canGoBack"]; |
264 [_webView removeObserver:self forKeyPath:@"canGoForward"]; | 292 [_webView removeObserver:self forKeyPath:@"canGoForward"]; |
265 } | 293 } |
(...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 | 464 // TODO(crbug.com/679895): Add some visual indication that the page load has |
437 // finished. | 465 // finished. |
438 [self updateToolbar]; | 466 [self updateToolbar]; |
439 } | 467 } |
440 | 468 |
441 - (void)webViewWebContentProcessDidTerminate:(CWVWebView*)webView { | 469 - (void)webViewWebContentProcessDidTerminate:(CWVWebView*)webView { |
442 NSLog(@"webViewWebContentProcessDidTerminate"); | 470 NSLog(@"webViewWebContentProcessDidTerminate"); |
443 } | 471 } |
444 | 472 |
445 @end | 473 @end |
OLD | NEW |