Chromium Code Reviews| 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/internal/criwv_web_view_impl.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <utility> | |
| 9 | |
| 10 #import "base/ios/weak_nsobject.h" | |
| 11 #include "base/strings/sys_string_conversions.h" | |
| 12 #import "ios/web/navigation/crw_session_controller.h" | |
| 13 #include "ios/web/public/referrer.h" | |
| 14 #import "ios/web/public/navigation_manager.h" | |
| 15 #import "ios/web/public/web_state/ui/crw_web_delegate.h" | |
| 16 #import "ios/web/public/web_state/web_state.h" | |
| 17 #import "ios/web/public/web_state/web_state_delegate_bridge.h" | |
| 18 #import "ios/web/web_state/ui/crw_web_controller.h" | |
| 19 #import "ios/web/web_state/web_state_impl.h" | |
| 20 #include "ios/web_view/internal/criwv_browser_state.h" | |
| 21 #import "ios/web_view/internal/translate/criwv_translate_client.h" | |
| 22 #import "ios/web_view/public/criwv_web_view_delegate.h" | |
| 23 #import "net/base/mac/url_conversions.h" | |
| 24 #include "ui/base/page_transition_types.h" | |
| 25 #include "url/gurl.h" | |
| 26 | |
| 27 @interface CRIWVWebViewImpl ()<CRWWebDelegate, CRWWebStateDelegate> { | |
| 28 id<CRIWVWebViewDelegate> _delegate; | |
| 29 ios_web_view::CRIWVBrowserState* _browserState; | |
| 30 std::unique_ptr<web::WebStateImpl> _webStateImpl; | |
| 31 base::WeakNSObject<CRWWebController> _webController; | |
| 32 std::unique_ptr<web::WebStateDelegateBridge> _webStateDelegate; | |
| 33 | |
| 34 CGFloat _loadProgress; | |
| 35 } | |
| 36 | |
| 37 // Returns the web state associated with this web view. | |
| 38 - (web::WebState*)webState; | |
| 39 | |
| 40 @end | |
| 41 | |
| 42 @implementation CRIWVWebViewImpl | |
| 43 | |
| 44 @synthesize delegate = _delegate; | |
| 45 @synthesize loadProgress = _loadProgress; | |
| 46 | |
| 47 - (instancetype)initWithBrowserState: | |
| 48 (ios_web_view::CRIWVBrowserState*)browserState { | |
| 49 self = [super init]; | |
| 50 if (self) { | |
| 51 _browserState = browserState; | |
| 52 _webStateImpl.reset(new web::WebStateImpl(_browserState)); | |
|
sdefresne
2017/01/20 10:28:13
nit: _webStateImpl = base::MakeUnique<web::WebStat
michaeldo
2017/01/24 22:35:26
Done in dep CL.
| |
| 53 _webStateImpl->GetNavigationManagerImpl().InitializeSession(nil, nil, NO, | |
| 54 0); | |
| 55 _webStateDelegate.reset(new web::WebStateDelegateBridge(self)); | |
|
sdefresne
2017/01/20 10:28:13
nit: _webStateDelegate = base::MakeUnique<web::Web
michaeldo
2017/01/24 22:35:25
Done in dep CL.
| |
| 56 _webStateImpl->SetDelegate(_webStateDelegate.get()); | |
| 57 _webController.reset(_webStateImpl->GetWebController()); | |
| 58 [_webController setDelegate:self]; | |
| 59 [_webController setWebUsageEnabled:YES]; | |
| 60 | |
| 61 // Initialize Translate. | |
| 62 web::WebState* webState = [_webController webStateImpl]; | |
| 63 ios_web_view::CRIWVTranslateClient::CreateForWebState(webState); | |
| 64 } | |
| 65 return self; | |
| 66 } | |
| 67 | |
| 68 - (web::WebState*)webState { | |
| 69 // TODO(crbug.com/679895): Stop using the private CRWWebController API. | |
|
sdefresne
2017/01/20 10:28:13
I think you can fix this TODO easily by changing t
michaeldo
2017/01/24 22:35:25
Done in dep CL.
| |
| 70 return [_webController webStateImpl]; | |
| 71 } | |
| 72 | |
| 73 - (UIView*)view { | |
| 74 return [_webController view]; | |
| 75 } | |
| 76 | |
| 77 - (BOOL)canGoBack { | |
| 78 return _webStateImpl && _webStateImpl->GetNavigationManager()->CanGoBack(); | |
| 79 } | |
| 80 | |
| 81 - (BOOL)canGoForward { | |
| 82 return _webStateImpl && _webStateImpl->GetNavigationManager()->CanGoForward(); | |
| 83 } | |
| 84 | |
| 85 - (BOOL)isLoading { | |
| 86 return [_webController webStateImpl]->IsLoading(); | |
|
sdefresne
2017/01/20 10:28:13
ditto, here you should be able to just use _webSta
michaeldo
2017/01/24 22:35:26
Done in dep CL.
| |
| 87 } | |
| 88 | |
| 89 - (NSURL*)visibleURL { | |
| 90 return net::NSURLWithGURL([self webState]->GetVisibleURL()); | |
|
sdefresne
2017/01/20 10:28:13
nit: use "self.webState" instead of "[self webStat
michaeldo
2017/01/24 22:35:25
Agreed, done in dep CL.
| |
| 91 } | |
| 92 | |
| 93 - (NSString*)pageTitle { | |
|
sdefresne
2017/01/20 10:28:13
ditto, here you should be able to just use _webSta
michaeldo
2017/01/24 22:35:26
Done in dep CL.
| |
| 94 return base::SysUTF16ToNSString([_webController webStateImpl]->GetTitle()); | |
| 95 } | |
| 96 | |
| 97 - (void)goBack { | |
| 98 if (_webStateImpl) | |
|
sdefresne
2017/01/20 10:28:13
Why check whether _webStateImpl can be null here a
michaeldo
2017/01/24 22:35:26
In dep CL, I updated the check for the navigationM
| |
| 99 _webStateImpl->GetNavigationManager()->GoBack(); | |
| 100 } | |
| 101 | |
| 102 - (void)goForward { | |
| 103 if (_webStateImpl) | |
|
sdefresne
2017/01/20 10:28:13
ditto
michaeldo
2017/01/24 22:35:25
Done in dep CL.
| |
| 104 _webStateImpl->GetNavigationManager()->GoForward(); | |
| 105 } | |
| 106 | |
| 107 - (void)reload { | |
| 108 [_webController reload]; | |
| 109 } | |
| 110 | |
| 111 - (void)stopLoading { | |
| 112 [_webController stopLoading]; | |
| 113 } | |
| 114 | |
| 115 - (void)loadURL:(NSURL*)URL { | |
| 116 web::NavigationManager::WebLoadParams params(net::GURLWithNSURL(URL)); | |
| 117 params.transition_type = ui::PAGE_TRANSITION_TYPED; | |
| 118 [_webController loadWithParams:params]; | |
| 119 } | |
| 120 | |
| 121 - (void)evaluateJavaScript:(NSString*)javaScriptString | |
| 122 completionHandler:(void (^)(id, NSError*))completionHandler { | |
| 123 [[self webState]->GetJSInjectionReceiver() | |
|
sdefresne
2017/01/20 10:28:12
s/[self webState]/self.webState/ or s/[self webSta
michaeldo
2017/01/24 22:35:26
Done in dep CL.
| |
| 124 executeJavaScript:javaScriptString | |
| 125 completionHandler:completionHandler]; | |
| 126 } | |
| 127 | |
| 128 - (void)setDelegate:(id<CRIWVWebViewDelegate>)delegate { | |
| 129 _delegate = delegate; | |
| 130 | |
| 131 // Set up the translate delegate. | |
|
sdefresne
2017/01/20 10:28:13
nit:, I would first check whether _delegate respon
michaeldo
2017/01/24 22:35:26
I think the way it is now may be better since it w
sdefresne
2017/01/25 09:13:26
Acknowdleged. I didn't think of that edge case. Th
| |
| 132 ios_web_view::CRIWVTranslateClient* translateClient = | |
| 133 ios_web_view::CRIWVTranslateClient::FromWebState([self webState]); | |
|
sdefresne
2017/01/20 10:28:13
ditto, self.webState
michaeldo
2017/01/24 22:35:26
Done in dep CL.
| |
| 134 id<CRIWVTranslateDelegate> translateDelegate = nil; | |
| 135 if ([_delegate respondsToSelector:@selector(translateDelegate)]) | |
| 136 translateDelegate = [_delegate translateDelegate]; | |
| 137 translateClient->set_translate_delegate(translateDelegate); | |
| 138 } | |
| 139 | |
| 140 // ----------------------------------------------------------------------- | |
| 141 // WebDelegate implementation. | |
| 142 | |
| 143 - (void)notifyDidUpdateWithChanges:(CRIWVWebViewUpdateType)changes { | |
| 144 SEL selector = @selector(webView:didUpdateWithChanges:); | |
| 145 if ([_delegate respondsToSelector:selector]) { | |
| 146 [_delegate webView:self didUpdateWithChanges:changes]; | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 - (void)webController:(CRWWebController*)webController | |
| 151 titleDidChange:(NSString*)title { | |
| 152 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeTitle]; | |
| 153 } | |
| 154 | |
| 155 - (void)webDidUpdateSessionForLoadWithParams: | |
| 156 (const web::NavigationManager::WebLoadParams&)params | |
| 157 wasInitialNavigation:(BOOL)initialNavigation { | |
| 158 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; | |
| 159 } | |
| 160 | |
| 161 - (void)webWillFinishHistoryNavigationFromEntry:(CRWSessionEntry*)fromEntry { | |
| 162 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; | |
| 163 } | |
| 164 | |
| 165 - (void)webDidAddPendingURL { | |
| 166 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; | |
| 167 } | |
| 168 | |
| 169 - (void)webDidUpdateHistoryStateWithPageURL:(const GURL&)pageUrl { | |
| 170 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; | |
| 171 } | |
| 172 | |
| 173 - (void)webCancelStartLoadingRequest { | |
| 174 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; | |
| 175 } | |
| 176 | |
| 177 - (void)webDidStartLoadingURL:(const GURL&)currentUrl | |
| 178 shouldUpdateHistory:(BOOL)updateHistory { | |
| 179 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; | |
| 180 } | |
| 181 | |
| 182 - (void)webDidFinishWithURL:(const GURL&)url loadSuccess:(BOOL)loadSuccess { | |
| 183 SEL selector = @selector(webView:didFinishLoadingWithURL:loadSuccess:); | |
| 184 if ([_delegate respondsToSelector:selector]) { | |
| 185 [_delegate webView:self | |
| 186 didFinishLoadingWithURL:net::NSURLWithGURL(url) | |
| 187 loadSuccess:loadSuccess]; | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 - (void)webLoadCancelled:(const GURL&)url { | |
| 192 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeURL]; | |
| 193 } | |
| 194 | |
| 195 - (void)webWillAddPendingURL:(const GURL&)url | |
| 196 transition:(ui::PageTransition)transition { | |
| 197 } | |
| 198 - (CRWWebController*)webPageOrderedOpen:(const GURL&)url | |
| 199 referrer:(const web::Referrer&)referrer | |
| 200 windowName:(NSString*)windowName | |
| 201 inBackground:(BOOL)inBackground { | |
| 202 return nil; | |
| 203 } | |
| 204 | |
| 205 - (CRWWebController*)webPageOrderedOpen { | |
| 206 return nil; | |
| 207 } | |
| 208 | |
| 209 - (void)webPageOrderedClose { | |
| 210 } | |
| 211 - (void)openURLWithParams:(const web::WebState::OpenURLParams&)params { | |
| 212 } | |
| 213 - (BOOL)openExternalURL:(const GURL&)url linkClicked:(BOOL)linkClicked { | |
| 214 return NO; | |
| 215 } | |
| 216 - (void)webController:(CRWWebController*)webController | |
| 217 retrievePlaceholderOverlayImage:(void (^)(UIImage*))block { | |
| 218 } | |
| 219 - (void)webController:(CRWWebController*)webController | |
| 220 onFormResubmissionForRequest:(NSURLRequest*)request | |
| 221 continueBlock:(ProceduralBlock)continueBlock | |
| 222 cancelBlock:(ProceduralBlock)cancelBlock { | |
| 223 } | |
| 224 - (void)webWillReload { | |
| 225 } | |
| 226 - (void)webWillInitiateLoadWithParams: | |
| 227 (web::NavigationManager::WebLoadParams&)params { | |
| 228 } | |
| 229 - (BOOL)webController:(CRWWebController*)webController | |
| 230 shouldOpenURL:(const GURL&)url | |
| 231 mainDocumentURL:(const GURL&)mainDocumentURL | |
| 232 linkClicked:(BOOL)linkClicked { | |
| 233 SEL selector = @selector(webView:shouldOpenURL:mainDocumentURL:linkClicked:); | |
| 234 if ([_delegate respondsToSelector:selector]) { | |
| 235 return [_delegate webView:self | |
| 236 shouldOpenURL:net::NSURLWithGURL(url) | |
| 237 mainDocumentURL:net::NSURLWithGURL(mainDocumentURL) | |
| 238 linkClicked:linkClicked]; | |
| 239 } | |
| 240 return YES; | |
| 241 } | |
| 242 | |
| 243 // ----------------------------------------------------------------------- | |
| 244 // CRWWebStateDelegate implementation. | |
| 245 | |
| 246 - (void)webState:(web::WebState*)webState didChangeProgress:(double)progress { | |
| 247 _loadProgress = progress; | |
| 248 [self notifyDidUpdateWithChanges:CRIWVWebViewUpdateTypeProgress]; | |
| 249 } | |
| 250 | |
| 251 @end | |
| OLD | NEW |