| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/web/web_state/ui/crw_wk_simple_web_view_controller.h" |
| 6 |
| 7 #include "base/ios/weak_nsobject.h" |
| 8 #include "base/logging.h" |
| 9 #import "base/mac/scoped_nsobject.h" |
| 10 |
| 11 @interface CRWWKSimpleWebViewController () <WKNavigationDelegate> |
| 12 // Consults the delegate to decide whether a load request should be started. |
| 13 - (BOOL)shouldStartLoadWithRequest:(NSURLRequest*)request; |
| 14 @end |
| 15 |
| 16 @implementation CRWWKSimpleWebViewController { |
| 17 base::scoped_nsobject<WKWebView> _webView; |
| 18 base::WeakNSProtocol<id<CRWSimpleWebViewControllerDelegate>> _delegate; |
| 19 } |
| 20 |
| 21 - (instancetype)initWithWKWebView:(WKWebView*)webView { |
| 22 self = [super init]; |
| 23 if (self) { |
| 24 DCHECK(webView); |
| 25 _webView.reset([webView retain]); |
| 26 [_webView setNavigationDelegate:self]; |
| 27 [_webView addObserver:self forKeyPath:@"title" options:0 context:NULL]; |
| 28 } |
| 29 return self; |
| 30 } |
| 31 |
| 32 - (void)dealloc { |
| 33 [_webView removeObserver:self forKeyPath:@"title"]; |
| 34 [super dealloc]; |
| 35 } |
| 36 |
| 37 #pragma mark - |
| 38 #pragma mark CRWSimpleWebView implementation |
| 39 |
| 40 - (void)reload { |
| 41 [_webView reload]; |
| 42 } |
| 43 |
| 44 - (UIView*)view { |
| 45 return _webView.get(); |
| 46 } |
| 47 |
| 48 - (UIScrollView*)scrollView { |
| 49 return [_webView scrollView]; |
| 50 } |
| 51 |
| 52 - (NSString*)title { |
| 53 return [_webView title]; |
| 54 } |
| 55 |
| 56 - (void)loadRequest:(NSURLRequest*)request { |
| 57 [_webView loadRequest:request]; |
| 58 } |
| 59 |
| 60 - (void)loadHTMLString:(NSString*)html baseURL:(NSURL*)baseURL { |
| 61 [_webView loadHTMLString:html baseURL:baseURL]; |
| 62 } |
| 63 |
| 64 - (void)loadPDFAtFilePath:(NSString*)filePath { |
| 65 DCHECK([[NSFileManager defaultManager] fileExistsAtPath:filePath]); |
| 66 NSURLRequest* request = |
| 67 [NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]; |
| 68 [_webView loadRequest:request]; |
| 69 } |
| 70 |
| 71 - (void)setDelegate:(id<CRWSimpleWebViewControllerDelegate>)delegate { |
| 72 _delegate.reset(delegate); |
| 73 } |
| 74 |
| 75 - (id<CRWSimpleWebViewControllerDelegate>)delegate { |
| 76 return _delegate.get(); |
| 77 } |
| 78 |
| 79 - (void)evaluateJavaScript:(NSString*)script |
| 80 stringResultHandler:(web::JavaScriptCompletion)handler { |
| 81 web::EvaluateJavaScript(_webView, script, handler); |
| 82 } |
| 83 |
| 84 #pragma mark - |
| 85 #pragma mark KVO callback |
| 86 |
| 87 - (void)observeValueForKeyPath:(NSString*)keyPath |
| 88 ofObject:(id)object |
| 89 change:(NSDictionary*)change |
| 90 context:(void*)context { |
| 91 DCHECK([keyPath isEqualToString:@"title"]); |
| 92 if ([_delegate respondsToSelector:@selector(simpleWebViewController: |
| 93 titleMayHaveChanged:)]) { |
| 94 [_delegate simpleWebViewController:self titleMayHaveChanged:[self title]]; |
| 95 } |
| 96 } |
| 97 |
| 98 #pragma mark - |
| 99 #pragma mark Delegate forwarding methods |
| 100 |
| 101 - (BOOL)shouldStartLoadWithRequest:(NSURLRequest*)request { |
| 102 if ([_delegate respondsToSelector:@selector(simpleWebViewController: |
| 103 shouldStartLoadWithRequest:)]) { |
| 104 return [_delegate simpleWebViewController:self |
| 105 shouldStartLoadWithRequest:request]; |
| 106 } |
| 107 // By default all loads are allowed. |
| 108 return YES; |
| 109 } |
| 110 |
| 111 #pragma mark - |
| 112 #pragma mark WKNavigationDelegate methods |
| 113 |
| 114 - (void)webView:(WKWebView*)webView |
| 115 decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction |
| 116 decisionHandler: |
| 117 (void (^)(WKNavigationActionPolicy))decisionHandler { |
| 118 decisionHandler([self shouldStartLoadWithRequest:navigationAction.request] |
| 119 ? WKNavigationActionPolicyAllow |
| 120 : WKNavigationActionPolicyCancel); |
| 121 } |
| 122 |
| 123 @end |
| OLD | NEW |