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_ui_simple_web_view_controller.h"
7 #include "base/ios/weak_nsobject.h"
8 #import "base/logging.h"
9 #import "base/mac/scoped_nsobject.h"
11 @interface CRWUISimpleWebViewController () <UIWebViewDelegate>
14 @implementation CRWUISimpleWebViewController {
15 base::scoped_nsobject<UIWebView> _webView;
16 base::WeakNSProtocol<id<CRWSimpleWebViewControllerDelegate>> _delegate;
19 - (instancetype)initWithUIWebView:(UIWebView*)webView {
23 _webView.reset([webView retain]);
24 [_webView setDelegate:self];
30 [_webView setDelegate:nil];
35 #pragma mark CRWSimpleWebView implementation
42 return _webView.get();
45 - (UIScrollView*)scrollView {
46 return [_webView scrollView];
50 return [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
53 - (void)loadRequest:(NSURLRequest*)request {
54 [_webView loadRequest:request];
57 - (void)loadHTMLString:(NSString*)html baseURL:(NSURL*)baseURL {
58 [_webView loadHTMLString:html baseURL:baseURL];
61 - (void)setDelegate:(id<CRWSimpleWebViewControllerDelegate>)delegate {
62 _delegate.reset(delegate);
65 - (id<CRWSimpleWebViewControllerDelegate>)delegate {
66 return _delegate.get();
69 - (void)loadPDFAtFilePath:(NSString*)filePath {
70 DCHECK([[NSFileManager defaultManager] fileExistsAtPath:filePath]);
71 NSData* data = [NSData dataWithContentsOfFile:filePath];
72 [_webView loadData:data
73 MIMEType:@"application/pdf"
74 textEncodingName:@"utf-8"
75 baseURL:[NSURL URLWithString:@""]];
78 - (void)evaluateJavaScript:(NSString*)script
79 stringResultHandler:(web::JavaScriptCompletion)handler {
80 web::EvaluateJavaScript(_webView, script, handler);
84 #pragma mark UIWebViewDelegateMethods
86 - (BOOL)webView:(UIWebView*)webView
87 shouldStartLoadWithRequest:(NSURLRequest*)request
88 navigationType:(UIWebViewNavigationType)navigationType {
89 if ([_delegate respondsToSelector:@selector(simpleWebViewController:
90 shouldStartLoadWithRequest:)]) {
91 return [_delegate simpleWebViewController:self
92 shouldStartLoadWithRequest:request];
94 // By default all loads are allowed.
98 - (void)webViewDidFinishLoad:(UIWebView*)webView {
99 if (![_delegate respondsToSelector:@selector(simpleWebViewController:
100 titleMayHaveChanged:)]) {
103 [_delegate simpleWebViewController:self titleMayHaveChanged:self.title];