| 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_view/public/criwv_web_view.h" |  | 
|    6  |  | 
|    7 #include <memory> |  | 
|    8 #include <utility> |  | 
|    9  |  | 
|   10 #import "base/ios/weak_nsobject.h" |  | 
|   11 #include "base/memory/ptr_util.h" |  | 
|   12 #include "base/strings/sys_string_conversions.h" |  | 
|   13 #import "ios/web/public/navigation_manager.h" |  | 
|   14 #include "ios/web/public/referrer.h" |  | 
|   15 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |  | 
|   16 #import "ios/web/public/web_state/ui/crw_web_delegate.h" |  | 
|   17 #import "ios/web/public/web_state/web_state.h" |  | 
|   18 #import "ios/web/public/web_state/web_state_delegate_bridge.h" |  | 
|   19 #import "ios/web/public/web_state/web_state_observer_bridge.h" |  | 
|   20 #include "ios/web_view/internal/criwv_browser_state.h" |  | 
|   21 #import "ios/web_view/internal/criwv_website_data_store_internal.h" |  | 
|   22 #import "ios/web_view/internal/translate/criwv_translate_client.h" |  | 
|   23 #import "ios/web_view/public/criwv_web_view_configuration.h" |  | 
|   24 #import "ios/web_view/public/criwv_web_view_delegate.h" |  | 
|   25 #import "ios/web_view/public/criwv_website_data_store.h" |  | 
|   26 #import "net/base/mac/url_conversions.h" |  | 
|   27 #include "ui/base/page_transition_types.h" |  | 
|   28 #include "url/gurl.h" |  | 
|   29  |  | 
|   30 #if !defined(__has_feature) || !__has_feature(objc_arc) |  | 
|   31 #error "This file requires ARC support." |  | 
|   32 #endif |  | 
|   33  |  | 
|   34 @interface CRIWVWebView ()<CRWWebStateDelegate, CRWWebStateObserver> { |  | 
|   35   CRIWVWebViewConfiguration* _configuration; |  | 
|   36   std::unique_ptr<web::WebState> _webState; |  | 
|   37   std::unique_ptr<web::WebStateDelegateBridge> _webStateDelegate; |  | 
|   38   std::unique_ptr<web::WebStateObserverBridge> _webStateObserver; |  | 
|   39   CGFloat _loadProgress; |  | 
|   40 } |  | 
|   41  |  | 
|   42 @end |  | 
|   43  |  | 
|   44 @implementation CRIWVWebView |  | 
|   45  |  | 
|   46 @synthesize delegate = _delegate; |  | 
|   47 @synthesize loadProgress = _loadProgress; |  | 
|   48  |  | 
|   49 - (instancetype)initWithFrame:(CGRect)frame |  | 
|   50                 configuration:(CRIWVWebViewConfiguration*)configuration { |  | 
|   51   self = [super initWithFrame:frame]; |  | 
|   52   if (self) { |  | 
|   53     _configuration = [configuration copy]; |  | 
|   54  |  | 
|   55     web::WebState::CreateParams webStateCreateParams( |  | 
|   56         [configuration.websiteDataStore browserState]); |  | 
|   57     _webState = web::WebState::Create(webStateCreateParams); |  | 
|   58     _webState->SetWebUsageEnabled(true); |  | 
|   59  |  | 
|   60     _webStateObserver = |  | 
|   61         base::MakeUnique<web::WebStateObserverBridge>(_webState.get(), self); |  | 
|   62     _webStateDelegate = base::MakeUnique<web::WebStateDelegateBridge>(self); |  | 
|   63     _webState->SetDelegate(_webStateDelegate.get()); |  | 
|   64  |  | 
|   65     // Initialize Translate. |  | 
|   66     ios_web_view::CRIWVTranslateClient::CreateForWebState(_webState.get()); |  | 
|   67   } |  | 
|   68   return self; |  | 
|   69 } |  | 
|   70  |  | 
|   71 - (void)willMoveToSuperview:(UIView*)newSuperview { |  | 
|   72   [super willMoveToSuperview:newSuperview]; |  | 
|   73   UIView* subview = _webState->GetView(); |  | 
|   74   if (subview.superview == self) { |  | 
|   75     return; |  | 
|   76   } |  | 
|   77   subview.frame = self.frame; |  | 
|   78   subview.autoresizingMask = |  | 
|   79       UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; |  | 
|   80   [self addSubview:subview]; |  | 
|   81 } |  | 
|   82  |  | 
|   83 - (UIView*)view { |  | 
|   84   return _webState->GetView(); |  | 
|   85 } |  | 
|   86  |  | 
|   87 - (BOOL)canGoBack { |  | 
|   88   return _webState && _webState->GetNavigationManager()->CanGoBack(); |  | 
|   89 } |  | 
|   90  |  | 
|   91 - (BOOL)canGoForward { |  | 
|   92   return _webState && _webState->GetNavigationManager()->CanGoForward(); |  | 
|   93 } |  | 
|   94  |  | 
|   95 - (BOOL)isLoading { |  | 
|   96   return _webState->IsLoading(); |  | 
|   97 } |  | 
|   98  |  | 
|   99 - (NSURL*)visibleURL { |  | 
|  100   return net::NSURLWithGURL(_webState->GetVisibleURL()); |  | 
|  101 } |  | 
|  102  |  | 
|  103 - (NSString*)pageTitle { |  | 
|  104   return base::SysUTF16ToNSString(_webState->GetTitle()); |  | 
|  105 } |  | 
|  106  |  | 
|  107 - (void)goBack { |  | 
|  108   if (_webState->GetNavigationManager()) |  | 
|  109     _webState->GetNavigationManager()->GoBack(); |  | 
|  110 } |  | 
|  111  |  | 
|  112 - (void)goForward { |  | 
|  113   if (_webState->GetNavigationManager()) |  | 
|  114     _webState->GetNavigationManager()->GoForward(); |  | 
|  115 } |  | 
|  116  |  | 
|  117 - (void)reload { |  | 
|  118   _webState->GetNavigationManager()->Reload(true); |  | 
|  119 } |  | 
|  120  |  | 
|  121 - (void)stopLoading { |  | 
|  122   _webState->Stop(); |  | 
|  123 } |  | 
|  124  |  | 
|  125 - (void)loadRequest:(NSURLRequest*)request { |  | 
|  126   DCHECK_EQ(nil, request.HTTPBodyStream) |  | 
|  127       << "request.HTTPBodyStream is not supported."; |  | 
|  128  |  | 
|  129   web::NavigationManager::WebLoadParams params(net::GURLWithNSURL(request.URL)); |  | 
|  130   params.transition_type = ui::PAGE_TRANSITION_TYPED; |  | 
|  131   params.extra_headers.reset([request.allHTTPHeaderFields copy]); |  | 
|  132   params.post_data.reset([request.HTTPBody copy]); |  | 
|  133   _webState->GetNavigationManager()->LoadURLWithParams(params); |  | 
|  134 } |  | 
|  135  |  | 
|  136 - (void)evaluateJavaScript:(NSString*)javaScriptString |  | 
|  137          completionHandler:(void (^)(id, NSError*))completionHandler { |  | 
|  138   [_webState->GetJSInjectionReceiver() executeJavaScript:javaScriptString |  | 
|  139                                        completionHandler:completionHandler]; |  | 
|  140 } |  | 
|  141  |  | 
|  142 - (void)setDelegate:(id<CRIWVWebViewDelegate>)delegate { |  | 
|  143   _delegate = delegate; |  | 
|  144  |  | 
|  145   // Set up the translate delegate. |  | 
|  146   ios_web_view::CRIWVTranslateClient* translateClient = |  | 
|  147       ios_web_view::CRIWVTranslateClient::FromWebState(_webState.get()); |  | 
|  148   id<CRIWVTranslateDelegate> translateDelegate = nil; |  | 
|  149   if ([_delegate respondsToSelector:@selector(translateDelegate)]) |  | 
|  150     translateDelegate = [_delegate translateDelegate]; |  | 
|  151   translateClient->set_translate_delegate(translateDelegate); |  | 
|  152 } |  | 
|  153  |  | 
|  154 - (void)notifyDidUpdateWithChanges:(CRIWVWebViewUpdateType)changes { |  | 
|  155   SEL selector = @selector(webView:didUpdateWithChanges:); |  | 
|  156   if ([_delegate respondsToSelector:selector]) { |  | 
|  157     [_delegate webView:self didUpdateWithChanges:changes]; |  | 
|  158   } |  | 
|  159 } |  | 
|  160  |  | 
|  161 // ----------------------------------------------------------------------- |  | 
|  162 // WebStateObserver implementation. |  | 
|  163  |  | 
|  164 - (void)didStartProvisionalNavigationForURL:(const GURL&)URL { |  | 
|  165   [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; |  | 
|  166 } |  | 
|  167  |  | 
|  168 - (void)didCommitNavigationWithDetails: |  | 
|  169     (const web::LoadCommittedDetails&)details { |  | 
|  170   [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; |  | 
|  171 } |  | 
|  172  |  | 
|  173 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success { |  | 
|  174   DCHECK_EQ(_webState.get(), webState); |  | 
|  175   SEL selector = @selector(webView:didFinishLoadingWithURL:loadSuccess:); |  | 
|  176   if ([_delegate respondsToSelector:selector]) { |  | 
|  177     [_delegate webView:self |  | 
|  178         didFinishLoadingWithURL:[self visibleURL] |  | 
|  179                     loadSuccess:success]; |  | 
|  180   } |  | 
|  181 } |  | 
|  182  |  | 
|  183 - (void)webState:(web::WebState*)webState |  | 
|  184     didChangeLoadingProgress:(double)progress { |  | 
|  185   [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeProgress]; |  | 
|  186 } |  | 
|  187  |  | 
|  188 @end |  | 
| OLD | NEW |