| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/web_view/internal/criwv_web_view_impl.h" | 5 #import "ios/web_view/internal/criwv_web_view_impl.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #import "base/ios/weak_nsobject.h" | 10 #import "base/ios/weak_nsobject.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
| 13 #import "ios/web/navigation/crw_session_controller.h" | |
| 14 #include "ios/web/public/referrer.h" | 13 #include "ios/web/public/referrer.h" |
| 15 #import "ios/web/public/navigation_manager.h" | 14 #import "ios/web/public/navigation_manager.h" |
| 16 #import "ios/web/public/web_state/ui/crw_web_delegate.h" | 15 #import "ios/web/public/web_state/ui/crw_web_delegate.h" |
| 17 #import "ios/web/public/web_state/web_state.h" | 16 #import "ios/web/public/web_state/web_state.h" |
| 18 #import "ios/web/public/web_state/web_state_delegate_bridge.h" | 17 #import "ios/web/public/web_state/web_state_delegate_bridge.h" |
| 19 #import "ios/web/web_state/ui/crw_web_controller.h" | 18 #import "ios/web/public/web_state/web_state_observer_bridge.h" |
| 20 #import "ios/web/web_state/web_state_impl.h" | |
| 21 #include "ios/web_view/internal/criwv_browser_state.h" | 19 #include "ios/web_view/internal/criwv_browser_state.h" |
| 22 #import "ios/web_view/internal/translate/criwv_translate_client.h" | 20 #import "ios/web_view/internal/translate/criwv_translate_client.h" |
| 23 #import "ios/web_view/public/criwv_web_view_delegate.h" | 21 #import "ios/web_view/public/criwv_web_view_delegate.h" |
| 24 #import "net/base/mac/url_conversions.h" | 22 #import "net/base/mac/url_conversions.h" |
| 25 #include "ui/base/page_transition_types.h" | 23 #include "ui/base/page_transition_types.h" |
| 26 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 27 | 25 |
| 28 @interface CRIWVWebViewImpl ()<CRWWebDelegate, CRWWebStateDelegate> { | 26 @interface CRIWVWebViewImpl ()<CRWWebStateDelegate, CRWWebStateObserver> { |
| 29 id<CRIWVWebViewDelegate> _delegate; | 27 id<CRIWVWebViewDelegate> _delegate; |
| 30 ios_web_view::CRIWVBrowserState* _browserState; | 28 ios_web_view::CRIWVBrowserState* _browserState; |
| 31 std::unique_ptr<web::WebStateImpl> _webStateImpl; | 29 std::unique_ptr<web::WebState> _webState; |
| 32 base::WeakNSObject<CRWWebController> _webController; | |
| 33 std::unique_ptr<web::WebStateDelegateBridge> _webStateDelegate; | 30 std::unique_ptr<web::WebStateDelegateBridge> _webStateDelegate; |
| 34 | 31 std::unique_ptr<web::WebStateObserverBridge> _webStateObserver; |
| 35 CGFloat _loadProgress; | 32 CGFloat _loadProgress; |
| 36 } | 33 } |
| 37 | 34 |
| 38 @end | 35 @end |
| 39 | 36 |
| 40 @implementation CRIWVWebViewImpl | 37 @implementation CRIWVWebViewImpl |
| 41 | 38 |
| 42 @synthesize delegate = _delegate; | 39 @synthesize delegate = _delegate; |
| 43 @synthesize loadProgress = _loadProgress; | 40 @synthesize loadProgress = _loadProgress; |
| 44 | 41 |
| 45 - (instancetype)initWithBrowserState: | 42 - (instancetype)initWithBrowserState: |
| 46 (ios_web_view::CRIWVBrowserState*)browserState { | 43 (ios_web_view::CRIWVBrowserState*)browserState { |
| 47 self = [super init]; | 44 self = [super init]; |
| 48 if (self) { | 45 if (self) { |
| 49 _browserState = browserState; | 46 _browserState = browserState; |
| 50 _webStateImpl = base::MakeUnique<web::WebStateImpl>(_browserState); | 47 |
| 51 _webStateImpl->GetNavigationManagerImpl().InitializeSession(nil, nil, NO, | 48 web::WebState::CreateParams webStateCreateParams(_browserState); |
| 52 0); | 49 _webState = web::WebState::Create(webStateCreateParams); |
| 50 _webState->SetWebUsageEnabled(true); |
| 51 |
| 52 _webStateObserver = |
| 53 base::MakeUnique<web::WebStateObserverBridge>(_webState.get(), self); |
| 53 _webStateDelegate = base::MakeUnique<web::WebStateDelegateBridge>(self); | 54 _webStateDelegate = base::MakeUnique<web::WebStateDelegateBridge>(self); |
| 54 _webStateImpl->SetDelegate(_webStateDelegate.get()); | 55 _webState->SetDelegate(_webStateDelegate.get()); |
| 55 _webController.reset(_webStateImpl->GetWebController()); | |
| 56 [_webController setDelegate:self]; | |
| 57 [_webController setWebUsageEnabled:YES]; | |
| 58 | 56 |
| 59 // Initialize Translate. | 57 // Initialize Translate. |
| 60 ios_web_view::CRIWVTranslateClient::CreateForWebState(_webStateImpl.get()); | 58 ios_web_view::CRIWVTranslateClient::CreateForWebState(_webState.get()); |
| 61 } | 59 } |
| 62 return self; | 60 return self; |
| 63 } | 61 } |
| 64 | 62 |
| 65 - (UIView*)view { | 63 - (UIView*)view { |
| 66 return [_webController view]; | 64 return _webState->GetView(); |
| 67 } | 65 } |
| 68 | 66 |
| 69 - (BOOL)canGoBack { | 67 - (BOOL)canGoBack { |
| 70 return _webStateImpl && _webStateImpl->GetNavigationManager()->CanGoBack(); | 68 return _webState && _webState->GetNavigationManager()->CanGoBack(); |
| 71 } | 69 } |
| 72 | 70 |
| 73 - (BOOL)canGoForward { | 71 - (BOOL)canGoForward { |
| 74 return _webStateImpl && _webStateImpl->GetNavigationManager()->CanGoForward(); | 72 return _webState && _webState->GetNavigationManager()->CanGoForward(); |
| 75 } | 73 } |
| 76 | 74 |
| 77 - (BOOL)isLoading { | 75 - (BOOL)isLoading { |
| 78 return _webStateImpl->IsLoading(); | 76 return _webState->IsLoading(); |
| 79 } | 77 } |
| 80 | 78 |
| 81 - (NSURL*)visibleURL { | 79 - (NSURL*)visibleURL { |
| 82 return net::NSURLWithGURL(_webStateImpl->GetVisibleURL()); | 80 return net::NSURLWithGURL(_webState->GetVisibleURL()); |
| 83 } | 81 } |
| 84 | 82 |
| 85 - (NSString*)pageTitle { | 83 - (NSString*)pageTitle { |
| 86 return base::SysUTF16ToNSString(_webStateImpl->GetTitle()); | 84 return base::SysUTF16ToNSString(_webState->GetTitle()); |
| 87 } | 85 } |
| 88 | 86 |
| 89 - (void)goBack { | 87 - (void)goBack { |
| 90 if (_webStateImpl->GetNavigationManager()) | 88 if (_webState->GetNavigationManager()) |
| 91 _webStateImpl->GetNavigationManager()->GoBack(); | 89 _webState->GetNavigationManager()->GoBack(); |
| 92 } | 90 } |
| 93 | 91 |
| 94 - (void)goForward { | 92 - (void)goForward { |
| 95 if (_webStateImpl->GetNavigationManager()) | 93 if (_webState->GetNavigationManager()) |
| 96 _webStateImpl->GetNavigationManager()->GoForward(); | 94 _webState->GetNavigationManager()->GoForward(); |
| 97 } | 95 } |
| 98 | 96 |
| 99 - (void)reload { | 97 - (void)reload { |
| 100 [_webController reload]; | 98 _webState->GetNavigationManager()->Reload(true); |
| 101 } | 99 } |
| 102 | 100 |
| 103 - (void)stopLoading { | 101 - (void)stopLoading { |
| 104 [_webController stopLoading]; | 102 _webState->Stop(); |
| 105 } | 103 } |
| 106 | 104 |
| 107 - (void)loadURL:(NSURL*)URL { | 105 - (void)loadURL:(NSURL*)URL { |
| 108 web::NavigationManager::WebLoadParams params(net::GURLWithNSURL(URL)); | 106 web::NavigationManager::WebLoadParams params(net::GURLWithNSURL(URL)); |
| 109 params.transition_type = ui::PAGE_TRANSITION_TYPED; | 107 params.transition_type = ui::PAGE_TRANSITION_TYPED; |
| 110 [_webController loadWithParams:params]; | 108 _webState->GetNavigationManager()->LoadURLWithParams(params); |
| 111 } | 109 } |
| 112 | 110 |
| 113 - (void)evaluateJavaScript:(NSString*)javaScriptString | 111 - (void)evaluateJavaScript:(NSString*)javaScriptString |
| 114 completionHandler:(void (^)(id, NSError*))completionHandler { | 112 completionHandler:(void (^)(id, NSError*))completionHandler { |
| 115 [_webStateImpl->GetJSInjectionReceiver() executeJavaScript:javaScriptString | 113 [_webState->GetJSInjectionReceiver() executeJavaScript:javaScriptString |
| 116 completionHandler:completionHandler]; | 114 completionHandler:completionHandler]; |
| 117 } | 115 } |
| 118 | 116 |
| 119 - (void)setDelegate:(id<CRIWVWebViewDelegate>)delegate { | 117 - (void)setDelegate:(id<CRIWVWebViewDelegate>)delegate { |
| 120 _delegate = delegate; | 118 _delegate = delegate; |
| 121 | 119 |
| 122 // Set up the translate delegate. | 120 // Set up the translate delegate. |
| 123 ios_web_view::CRIWVTranslateClient* translateClient = | 121 ios_web_view::CRIWVTranslateClient* translateClient = |
| 124 ios_web_view::CRIWVTranslateClient::FromWebState(_webStateImpl.get()); | 122 ios_web_view::CRIWVTranslateClient::FromWebState(_webState.get()); |
| 125 id<CRIWVTranslateDelegate> translateDelegate = nil; | 123 id<CRIWVTranslateDelegate> translateDelegate = nil; |
| 126 if ([_delegate respondsToSelector:@selector(translateDelegate)]) | 124 if ([_delegate respondsToSelector:@selector(translateDelegate)]) |
| 127 translateDelegate = [_delegate translateDelegate]; | 125 translateDelegate = [_delegate translateDelegate]; |
| 128 translateClient->set_translate_delegate(translateDelegate); | 126 translateClient->set_translate_delegate(translateDelegate); |
| 129 } | 127 } |
| 130 | 128 |
| 131 // ----------------------------------------------------------------------- | |
| 132 // WebDelegate implementation. | |
| 133 | |
| 134 - (void)notifyDidUpdateWithChanges:(CRIWVWebViewUpdateType)changes { | 129 - (void)notifyDidUpdateWithChanges:(CRIWVWebViewUpdateType)changes { |
| 135 SEL selector = @selector(webView:didUpdateWithChanges:); | 130 SEL selector = @selector(webView:didUpdateWithChanges:); |
| 136 if ([_delegate respondsToSelector:selector]) { | 131 if ([_delegate respondsToSelector:selector]) { |
| 137 [_delegate webView:self didUpdateWithChanges:changes]; | 132 [_delegate webView:self didUpdateWithChanges:changes]; |
| 138 } | 133 } |
| 139 } | 134 } |
| 140 | 135 |
| 141 - (void)webController:(CRWWebController*)webController | 136 // ----------------------------------------------------------------------- |
| 142 titleDidChange:(NSString*)title { | 137 // WebStateObserver implementation. |
| 143 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeTitle]; | |
| 144 } | |
| 145 | 138 |
| 146 - (void)webDidUpdateSessionForLoadWithParams: | 139 - (void)didStartProvisionalNavigationForURL:(const GURL&)URL { |
| 147 (const web::NavigationManager::WebLoadParams&)params | |
| 148 wasInitialNavigation:(BOOL)initialNavigation { | |
| 149 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; | 140 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; |
| 150 } | 141 } |
| 151 | 142 |
| 152 - (void)webWillFinishHistoryNavigationFromEntry:(CRWSessionEntry*)fromEntry { | 143 - (void)didCommitNavigationWithDetails: |
| 144 (const web::LoadCommittedDetails&)details { |
| 153 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; | 145 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; |
| 154 } | 146 } |
| 155 | 147 |
| 156 - (void)webDidAddPendingURL { | 148 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success { |
| 157 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; | 149 DCHECK_EQ(_webState.get(), webState); |
| 158 } | |
| 159 | |
| 160 - (void)webDidUpdateHistoryStateWithPageURL:(const GURL&)pageUrl { | |
| 161 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; | |
| 162 } | |
| 163 | |
| 164 - (void)webCancelStartLoadingRequest { | |
| 165 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; | |
| 166 } | |
| 167 | |
| 168 - (void)webDidStartLoadingURL:(const GURL&)currentUrl | |
| 169 shouldUpdateHistory:(BOOL)updateHistory { | |
| 170 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; | |
| 171 } | |
| 172 | |
| 173 - (void)webDidFinishWithURL:(const GURL&)url loadSuccess:(BOOL)loadSuccess { | |
| 174 SEL selector = @selector(webView:didFinishLoadingWithURL:loadSuccess:); | 150 SEL selector = @selector(webView:didFinishLoadingWithURL:loadSuccess:); |
| 175 if ([_delegate respondsToSelector:selector]) { | 151 if ([_delegate respondsToSelector:selector]) { |
| 176 [_delegate webView:self | 152 [_delegate webView:self |
| 177 didFinishLoadingWithURL:net::NSURLWithGURL(url) | 153 didFinishLoadingWithURL:[self visibleURL] |
| 178 loadSuccess:loadSuccess]; | 154 loadSuccess:success]; |
| 179 } | 155 } |
| 180 } | 156 } |
| 181 | 157 |
| 182 - (void)webLoadCancelled:(const GURL&)url { | |
| 183 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; | |
| 184 } | |
| 185 | |
| 186 - (void)webWillAddPendingURL:(const GURL&)url | |
| 187 transition:(ui::PageTransition)transition { | |
| 188 } | |
| 189 - (CRWWebController*)webPageOrderedOpen:(const GURL&)url | |
| 190 referrer:(const web::Referrer&)referrer | |
| 191 windowName:(NSString*)windowName | |
| 192 inBackground:(BOOL)inBackground { | |
| 193 return nil; | |
| 194 } | |
| 195 | |
| 196 - (CRWWebController*)webPageOrderedOpen { | |
| 197 return nil; | |
| 198 } | |
| 199 | |
| 200 - (void)webPageOrderedClose { | |
| 201 } | |
| 202 - (void)openURLWithParams:(const web::WebState::OpenURLParams&)params { | |
| 203 } | |
| 204 - (BOOL)openExternalURL:(const GURL&)url linkClicked:(BOOL)linkClicked { | |
| 205 return NO; | |
| 206 } | |
| 207 - (void)webController:(CRWWebController*)webController | |
| 208 retrievePlaceholderOverlayImage:(void (^)(UIImage*))block { | |
| 209 } | |
| 210 - (void)webController:(CRWWebController*)webController | |
| 211 onFormResubmissionForRequest:(NSURLRequest*)request | |
| 212 continueBlock:(ProceduralBlock)continueBlock | |
| 213 cancelBlock:(ProceduralBlock)cancelBlock { | |
| 214 } | |
| 215 - (void)webWillReload { | |
| 216 } | |
| 217 - (void)webWillInitiateLoadWithParams: | |
| 218 (web::NavigationManager::WebLoadParams&)params { | |
| 219 } | |
| 220 - (BOOL)webController:(CRWWebController*)webController | |
| 221 shouldOpenURL:(const GURL&)url | |
| 222 mainDocumentURL:(const GURL&)mainDocumentURL | |
| 223 linkClicked:(BOOL)linkClicked { | |
| 224 SEL selector = @selector(webView:shouldOpenURL:mainDocumentURL:linkClicked:); | |
| 225 if ([_delegate respondsToSelector:selector]) { | |
| 226 return [_delegate webView:self | |
| 227 shouldOpenURL:net::NSURLWithGURL(url) | |
| 228 mainDocumentURL:net::NSURLWithGURL(mainDocumentURL) | |
| 229 linkClicked:linkClicked]; | |
| 230 } | |
| 231 return YES; | |
| 232 } | |
| 233 | |
| 234 // ----------------------------------------------------------------------- | 158 // ----------------------------------------------------------------------- |
| 235 // CRWWebStateDelegate implementation. | 159 // CRWWebStateDelegate implementation. |
| 236 | 160 |
| 237 - (void)webState:(web::WebState*)webState didChangeProgress:(double)progress { | 161 - (void)webState:(web::WebState*)webState didChangeProgress:(double)progress { |
| 238 _loadProgress = progress; | 162 _loadProgress = progress; |
| 239 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeProgress]; | 163 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeProgress]; |
| 240 } | 164 } |
| 241 | 165 |
| 242 @end | 166 @end |
| OLD | NEW |