1 // Copyright 2015 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/interstitials/html_web_interstitial_impl.h"
7 #include "base/logging.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "ios/web/interstitials/web_interstitial_facade_delegate.h"
10 #include "ios/web/public/interstitials/web_interstitial_delegate.h"
11 #include "ios/web/public/web_state/ui/crw_web_view_content_view.h"
12 #import "ios/web/web_state/ui/crw_simple_web_view_controller.h"
13 #include "ios/web/web_state/web_state_impl.h"
14 #import "ios/web/web_state/web_view_internal_creation_util.h"
15 #import "net/base/mac/url_conversions.h"
16 #include "ui/gfx/geometry/size.h"
18 // The delegate of the simple web view controller that is used to display the
19 // HTML content. It intercepts JavaScript-triggered commands and forwards them
20 // to the interstitial.
21 @interface CRWWebInterstitialImplCRWSimpleWebViewDelegate
22 : NSObject<CRWSimpleWebViewControllerDelegate>
23 // Initializes a CRWWebInterstitialImplCRWSimpleWebViewDelegate which will
24 // forward JavaScript commands from its CRWSimpleWebView to |interstitial|.
25 - (instancetype)initWithInterstitial:
26 (web::HtmlWebInterstitialImpl*)interstitial;
29 @implementation CRWWebInterstitialImplCRWSimpleWebViewDelegate {
30 web::HtmlWebInterstitialImpl* _interstitial;
33 - (instancetype)initWithInterstitial:
34 (web::HtmlWebInterstitialImpl*)interstitial {
37 _interstitial = interstitial;
41 - (BOOL)simpleWebViewController:(id<CRWSimpleWebViewController>)controller
42 shouldStartLoadWithRequest:(NSURLRequest*)request {
43 NSString* requestString = [[request URL] absoluteString];
44 // If the request is a JavaScript-triggered command, parse it and forward the
45 // command to |interstitial_|.
46 NSString* const commandPrefix = @"js-command:";
47 if ([requestString hasPrefix:commandPrefix]) {
48 DCHECK(_interstitial);
49 _interstitial->CommandReceivedFromWebView(
50 [requestString substringFromIndex:commandPrefix.length]);
63 WebInterstitial* WebInterstitial::CreateHtmlInterstitial(
66 scoped_ptr<HtmlWebInterstitialDelegate> delegate) {
67 WebStateImpl* web_state_impl = static_cast<WebStateImpl*>(web_state);
68 return new HtmlWebInterstitialImpl(web_state_impl, url, delegate.Pass());
71 HtmlWebInterstitialImpl::HtmlWebInterstitialImpl(
72 WebStateImpl* web_state,
74 scoped_ptr<HtmlWebInterstitialDelegate> delegate)
75 : WebInterstitialImpl(web_state, url), delegate_(delegate.Pass()) {
79 HtmlWebInterstitialImpl::~HtmlWebInterstitialImpl() {
82 void HtmlWebInterstitialImpl::CommandReceivedFromWebView(NSString* command) {
83 delegate_->CommandReceived(base::SysNSStringToUTF8(command));
86 CRWContentView* HtmlWebInterstitialImpl::GetContentView() const {
87 return content_view_.get();
90 void HtmlWebInterstitialImpl::PrepareForDisplay() {
92 web_view_controller_delegate_.reset(
93 [[CRWWebInterstitialImplCRWSimpleWebViewDelegate alloc]
94 initWithInterstitial:this]);
95 web_view_controller_.reset(web::CreateSimpleWebViewController(
96 CGRectZero, GetWebStateImpl()->GetBrowserState(),
97 GetWebStateImpl()->GetWebViewType()));
98 [web_view_controller_ setDelegate:web_view_controller_delegate_];
99 [[web_view_controller_ view]
100 setAutoresizingMask:(UIViewAutoresizingFlexibleWidth |
101 UIViewAutoresizingFlexibleHeight)];
102 NSString* html = base::SysUTF8ToNSString(delegate_->GetHtmlContents());
103 [web_view_controller_ loadHTMLString:html
104 baseURL:net::NSURLWithGURL(GetUrl())];
105 content_view_.reset([[CRWWebViewContentView alloc]
106 initWithWebView:[web_view_controller_ view]
107 scrollView:[web_view_controller_ scrollView]]);
111 WebInterstitialDelegate* HtmlWebInterstitialImpl::GetDelegate() const {
112 return delegate_.get();
115 void HtmlWebInterstitialImpl::EvaluateJavaScript(
117 JavaScriptCompletion completionHandler) {
118 [web_view_controller_ evaluateJavaScript:script
119 stringResultHandler:completionHandler];