| 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 #include "components/dom_distiller/ios/distiller_page_ios.h" | 5 #include "components/dom_distiller/ios/distiller_page_ios.h" |
| 6 | 6 |
| 7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" |
| 11 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/mac/foundation_util.h" | 14 #include "base/mac/foundation_util.h" |
| 14 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 15 #include "base/strings/sys_string_conversions.h" | 16 #include "base/strings/sys_string_conversions.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/values.h" | 18 #include "base/values.h" |
| 18 #include "components/favicon/ios/web_favicon_driver.h" | |
| 19 #include "ios/web/public/browser_state.h" | 19 #include "ios/web/public/browser_state.h" |
| 20 #import "ios/web/public/navigation_manager.h" | 20 #import "ios/web/public/navigation_manager.h" |
| 21 #import "ios/web/public/web_state/js/crw_js_injection_manager.h" | 21 #import "ios/web/public/web_state/js/crw_js_injection_manager.h" |
| 22 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | 22 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
| 23 #import "ios/web/public/web_state/web_state.h" | 23 #import "ios/web/public/web_state/web_state.h" |
| 24 #include "ios/web/public/web_state/web_state_observer.h" |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 // This is duplicated here from ios/web/web_state/ui/web_view_js_utils.mm in | 28 // This is duplicated here from ios/web/web_state/ui/web_view_js_utils.mm in |
| 28 // order to handle numbers. The dom distiller proto expects integers and the | 29 // order to handle numbers. The dom distiller proto expects integers and the |
| 29 // generated JSON deserializer does not accept doubles in the place of ints. | 30 // generated JSON deserializer does not accept doubles in the place of ints. |
| 30 // However WKWebView only returns "numbers." However, here the proto expects | 31 // However WKWebView only returns "numbers." However, here the proto expects |
| 31 // integers and doubles, which is done by checking if the number has a fraction | 32 // integers and doubles, which is done by checking if the number has a fraction |
| 32 // or not; since this is a hacky method it's isolated to this file so as to | 33 // or not; since this is a hacky method it's isolated to this file so as to |
| 33 // limit the risk of broken JS calls. | 34 // limit the risk of broken JS calls. |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 DCHECK(web_state); | 123 DCHECK(web_state); |
| 123 DCHECK(distiller_page_); | 124 DCHECK(distiller_page_); |
| 124 } | 125 } |
| 125 | 126 |
| 126 void DistillerWebStateObserver::PageLoaded( | 127 void DistillerWebStateObserver::PageLoaded( |
| 127 web::PageLoadCompletionStatus load_completion_status) { | 128 web::PageLoadCompletionStatus load_completion_status) { |
| 128 distiller_page_->OnLoadURLDone(load_completion_status); | 129 distiller_page_->OnLoadURLDone(load_completion_status); |
| 129 } | 130 } |
| 130 | 131 |
| 131 void DistillerWebStateObserver::WebStateDestroyed() { | 132 void DistillerWebStateObserver::WebStateDestroyed() { |
| 132 distiller_page_->web_state_ = nullptr; | 133 distiller_page_->DetachWebState(); |
| 133 } | 134 } |
| 134 | 135 |
| 135 #pragma mark - | 136 #pragma mark - |
| 136 | 137 |
| 137 DistillerPageIOS::DistillerPageIOS( | 138 DistillerPageIOS::DistillerPageIOS(web::BrowserState* browser_state) |
| 138 FaviconWebStateDispatcher* web_state_dispatcher) | 139 : browser_state_(browser_state), weak_ptr_factory_(this) {} |
| 139 : web_state_dispatcher_(web_state_dispatcher), weak_ptr_factory_(this) {} | |
| 140 | |
| 141 DistillerPageIOS::~DistillerPageIOS() { | |
| 142 } | |
| 143 | 140 |
| 144 bool DistillerPageIOS::StringifyOutput() { | 141 bool DistillerPageIOS::StringifyOutput() { |
| 145 return false; | 142 return false; |
| 146 } | 143 } |
| 147 | 144 |
| 145 DistillerPageIOS::~DistillerPageIOS() {} |
| 146 |
| 147 void DistillerPageIOS::AttachWebState( |
| 148 std::unique_ptr<web::WebState> web_state) { |
| 149 if (web_state_) { |
| 150 DetachWebState(); |
| 151 } |
| 152 web_state_ = std::move(web_state); |
| 153 if (web_state_) { |
| 154 web_state_observer_ = |
| 155 base::MakeUnique<DistillerWebStateObserver>(web_state_.get(), this); |
| 156 } |
| 157 } |
| 158 |
| 159 std::unique_ptr<web::WebState> DistillerPageIOS::DetachWebState() { |
| 160 std::unique_ptr<web::WebState> old_web_state = std::move(web_state_); |
| 161 web_state_observer_.reset(); |
| 162 web_state_.reset(); |
| 163 return old_web_state; |
| 164 } |
| 165 |
| 148 void DistillerPageIOS::DistillPageImpl(const GURL& url, | 166 void DistillerPageIOS::DistillPageImpl(const GURL& url, |
| 149 const std::string& script) { | 167 const std::string& script) { |
| 150 if (!url.is_valid() || !script.length()) | 168 if (!url.is_valid() || !script.length()) |
| 151 return; | 169 return; |
| 152 url_ = url; | 170 url_ = url; |
| 153 script_ = script; | 171 script_ = script; |
| 154 | 172 |
| 155 web_state_ = web_state_dispatcher_->RequestWebState(); | |
| 156 | |
| 157 if (!web_state_) { | 173 if (!web_state_) { |
| 158 OnLoadURLDone(web::PageLoadCompletionStatus::FAILURE); | 174 const web::WebState::CreateParams web_state_create_params(browser_state_); |
| 159 return; | 175 std::unique_ptr<web::WebState> web_state_unique = |
| 176 web::WebState::Create(web_state_create_params); |
| 177 AttachWebState(std::move(web_state_unique)); |
| 160 } | 178 } |
| 161 | |
| 162 web_state_observer_ = | |
| 163 base::MakeUnique<DistillerWebStateObserver>(web_state_, this); | |
| 164 | |
| 165 // The favicon driver needs to know which URL is currently fetched. | |
| 166 favicon::WebFaviconDriver* favicon_driver = | |
| 167 favicon::WebFaviconDriver::FromWebState(web_state_); | |
| 168 favicon_driver->FetchFavicon(url_); | |
| 169 | |
| 170 // Load page using WebState. | 179 // Load page using WebState. |
| 171 web::NavigationManager::WebLoadParams params(url_); | 180 web::NavigationManager::WebLoadParams params(url_); |
| 172 web_state_->SetWebUsageEnabled(true); | 181 web_state_->SetWebUsageEnabled(true); |
| 173 web_state_->GetNavigationManager()->LoadURLWithParams(params); | 182 web_state_->GetNavigationManager()->LoadURLWithParams(params); |
| 174 // GetView is needed because the view is not created (but needed) when | 183 // GetView is needed because the view is not created (but needed) when |
| 175 // loading the page. | 184 // loading the page. |
| 176 web_state_->GetView(); | 185 web_state_->GetView(); |
| 177 } | 186 } |
| 178 | 187 |
| 179 void DistillerPageIOS::OnLoadURLDone( | 188 void DistillerPageIOS::OnLoadURLDone( |
| 180 web::PageLoadCompletionStatus load_completion_status) { | 189 web::PageLoadCompletionStatus load_completion_status) { |
| 181 // Don't attempt to distill if the page load failed or if there is no | 190 // Don't attempt to distill if the page load failed or if there is no |
| 182 // WebState. | 191 // WebState. |
| 183 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE || | 192 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE || |
| 184 !web_state_) { | 193 !web_state_) { |
| 185 HandleJavaScriptResult(nil); | 194 HandleJavaScriptResult(nil); |
| 186 return; | 195 return; |
| 187 } | 196 } |
| 188 | |
| 189 // Inject the script. | 197 // Inject the script. |
| 190 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr(); | 198 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr(); |
| 191 | |
| 192 [[web_state_->GetJSInjectionReceiver() | 199 [[web_state_->GetJSInjectionReceiver() |
| 193 instanceOfClass:[CRWJSInjectionManager class]] | 200 instanceOfClass:[CRWJSInjectionManager class]] |
| 194 executeJavaScript:base::SysUTF8ToNSString(script_) | 201 executeJavaScript:base::SysUTF8ToNSString(script_) |
| 195 completionHandler:^(id result, NSError* error) { | 202 completionHandler:^(id result, NSError* error) { |
| 196 DistillerPageIOS* distiller_page = weak_this.get(); | 203 DistillerPageIOS* distiller_page = weak_this.get(); |
| 197 if (distiller_page) | 204 if (distiller_page) |
| 198 distiller_page->HandleJavaScriptResult(result); | 205 distiller_page->HandleJavaScriptResult(result); |
| 199 }]; | 206 }]; |
| 200 } | 207 } |
| 201 | 208 |
| 202 void DistillerPageIOS::HandleJavaScriptResult(id result) { | 209 void DistillerPageIOS::HandleJavaScriptResult(id result) { |
| 203 web_state_dispatcher_->ReturnWebState(web_state_); | |
| 204 web_state_ = nullptr; | |
| 205 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue(); | 210 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue(); |
| 206 if (result) { | 211 if (result) { |
| 207 resultValue = ValueResultFromScriptResult(result); | 212 resultValue = ValueResultFromScriptResult(result); |
| 208 } | 213 } |
| 209 OnDistillationDone(url_, resultValue.get()); | 214 OnDistillationDone(url_, resultValue.get()); |
| 210 } | 215 } |
| 211 | 216 |
| 212 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult( | 217 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult( |
| 213 id wk_result) { | 218 id wk_result) { |
| 214 return ::ValueResultFromScriptResult(wk_result, | 219 return ::ValueResultFromScriptResult(wk_result, |
| 215 kMaximumParsingRecursionDepth); | 220 kMaximumParsingRecursionDepth); |
| 216 } | 221 } |
| 217 } // namespace dom_distiller | 222 } // namespace dom_distiller |
| OLD | NEW |