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.
5 #import "ios/web/web_state/ui/crw_wk_simple_web_view_controller.h"
7 #include "base/ios/weak_nsobject.h"
8 #include "base/logging.h"
9 #import "base/mac/scoped_nsobject.h"
11 @interface CRWWKSimpleWebViewController () <WKNavigationDelegate>
12 // Consults the delegate to decide whether a load request should be started.
13 - (BOOL)shouldStartLoadWithRequest:(NSURLRequest*)request;
16 @implementation CRWWKSimpleWebViewController {
17 base::scoped_nsobject<WKWebView> _webView;
18 base::WeakNSProtocol<id<CRWSimpleWebViewControllerDelegate>> _delegate;
21 - (instancetype)initWithWKWebView:(WKWebView*)webView {
25 _webView.reset([webView retain]);
26 [_webView setNavigationDelegate:self];
27 [_webView addObserver:self forKeyPath:@"title" options:0 context:NULL];
33 [_webView removeObserver:self forKeyPath:@"title"];
38 #pragma mark CRWSimpleWebView implementation
45 return _webView.get();
48 - (UIScrollView*)scrollView {
49 return [_webView scrollView];
53 return [_webView title];
56 - (void)loadRequest:(NSURLRequest*)request {
57 [_webView loadRequest:request];
60 - (void)loadHTMLString:(NSString*)html baseURL:(NSURL*)baseURL {
61 [_webView loadHTMLString:html baseURL:baseURL];
64 - (void)loadPDFAtFilePath:(NSString*)filePath {
65 DCHECK([[NSFileManager defaultManager] fileExistsAtPath:filePath]);
66 NSURLRequest* request =
67 [NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]];
68 [_webView loadRequest:request];
71 - (void)setDelegate:(id<CRWSimpleWebViewControllerDelegate>)delegate {
72 _delegate.reset(delegate);
75 - (id<CRWSimpleWebViewControllerDelegate>)delegate {
76 return _delegate.get();
79 - (void)evaluateJavaScript:(NSString*)script
80 stringResultHandler:(web::JavaScriptCompletion)handler {
81 web::EvaluateJavaScript(_webView, script, handler);
85 #pragma mark KVO callback
87 - (void)observeValueForKeyPath:(NSString*)keyPath
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]];
99 #pragma mark Delegate forwarding methods
101 - (BOOL)shouldStartLoadWithRequest:(NSURLRequest*)request {
102 if ([_delegate respondsToSelector:@selector(simpleWebViewController:
103 shouldStartLoadWithRequest:)]) {
104 return [_delegate simpleWebViewController:self
105 shouldStartLoadWithRequest:request];
107 // By default all loads are allowed.
112 #pragma mark WKNavigationDelegate methods
114 - (void)webView:(WKWebView*)webView
115 decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction
117 (void (^)(WKNavigationActionPolicy))decisionHandler {
118 decisionHandler([self shouldStartLoadWithRequest:navigationAction.request]
119 ? WKNavigationActionPolicyAllow
120 : WKNavigationActionPolicyCancel);