OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ios/chrome/browser/web/web_controller_provider_impl.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/sys_string_conversions.h" |
| 9 #import "ios/web/public/navigation_manager.h" |
| 10 #import "ios/web/web_state/ui/crw_web_controller.h" |
| 11 #import "ios/web/web_state/web_state_impl.h" |
| 12 #include "url/gurl.h" |
| 13 |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 15 #error "This file requires ARC support." |
| 16 #endif |
| 17 |
| 18 WebControllerProviderImpl::WebControllerProviderImpl( |
| 19 web::BrowserState* browser_state) |
| 20 : ios::WebControllerProvider(browser_state), |
| 21 web_state_impl_(base::MakeUnique<web::WebStateImpl>(browser_state)), |
| 22 suppresses_dialogs_(false) { |
| 23 DCHECK(browser_state); |
| 24 web_state_impl_->GetNavigationManagerImpl().InitializeSession(nil, nil, NO, |
| 25 -1); |
| 26 [web_state_impl_->GetWebController() setWebUsageEnabled:YES]; |
| 27 } |
| 28 |
| 29 WebControllerProviderImpl::~WebControllerProviderImpl() {} |
| 30 |
| 31 bool WebControllerProviderImpl::SuppressesDialogs() const { |
| 32 return suppresses_dialogs_; |
| 33 } |
| 34 |
| 35 void WebControllerProviderImpl::SetSuppressesDialogs( |
| 36 bool should_suppress_dialogs) { |
| 37 if (suppresses_dialogs_ != should_suppress_dialogs) { |
| 38 suppresses_dialogs_ = should_suppress_dialogs; |
| 39 [web_state_impl_->GetWebController() |
| 40 setShouldSuppressDialogs:suppresses_dialogs_]; |
| 41 } |
| 42 } |
| 43 |
| 44 void WebControllerProviderImpl::LoadURL(const GURL& url) { |
| 45 if (!url.is_valid()) |
| 46 return; |
| 47 |
| 48 [web_state_impl_->GetWebController() |
| 49 loadWithParams:web::NavigationManager::WebLoadParams(url)]; |
| 50 [web_state_impl_->GetWebController() triggerPendingLoad]; |
| 51 last_requested_url_ = url; |
| 52 } |
| 53 |
| 54 web::WebState* WebControllerProviderImpl::GetWebState() const { |
| 55 return web_state_impl_.get(); |
| 56 } |
| 57 |
| 58 void WebControllerProviderImpl::InjectScript( |
| 59 const std::string& script, |
| 60 web::JavaScriptResultBlock completion) { |
| 61 if (web_state_impl_ && |
| 62 web_state_impl_->GetVisibleURL() == last_requested_url_) { |
| 63 // Only inject the script if the web controller has finished loading the |
| 64 // last requested url. |
| 65 NSString* scriptString = base::SysUTF8ToNSString(script); |
| 66 [web_state_impl_->GetWebController() executeJavaScript:scriptString |
| 67 completionHandler:completion]; |
| 68 } else if (completion) { |
| 69 // If the web controller isn't ready, call the |completion| immediately. |
| 70 completion(nil, nil); |
| 71 } |
| 72 } |
OLD | NEW |