Chromium Code Reviews| 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" |
| 18 #include "base/threading/thread_task_runner_handle.h" | |
|
gambard
2016/12/27 13:56:57
Not needed.
Olivier
2016/12/27 14:52:14
Done.
| |
| 17 #include "base/values.h" | 19 #include "base/values.h" |
| 18 #include "components/favicon/ios/web_favicon_driver.h" | |
| 19 #include "ios/web/public/browser_state.h" | 20 #include "ios/web/public/browser_state.h" |
| 20 #import "ios/web/public/navigation_manager.h" | 21 #import "ios/web/public/navigation_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_manager.h" |
| 22 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | 23 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
| 23 #import "ios/web/public/web_state/web_state.h" | 24 #import "ios/web/public/web_state/web_state.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 |
| (...skipping 93 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)); | |
|
gambard
2016/12/27 13:56:57
Nit: you can probably create it directly inside th
Olivier
2016/12/27 14:52:14
What do you mean?
| |
| 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. | |
|
gambard
2016/12/27 13:56:57
Why removing the comment?
Olivier
2016/12/27 14:52:14
Done.
| |
| 190 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr(); | 197 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr(); |
| 191 | |
| 192 [[web_state_->GetJSInjectionReceiver() | 198 [[web_state_->GetJSInjectionReceiver() |
| 193 instanceOfClass:[CRWJSInjectionManager class]] | 199 instanceOfClass:[CRWJSInjectionManager class]] |
| 194 executeJavaScript:base::SysUTF8ToNSString(script_) | 200 executeJavaScript:base::SysUTF8ToNSString(script_) |
| 195 completionHandler:^(id result, NSError* error) { | 201 completionHandler:^(id result, NSError* error) { |
| 196 DistillerPageIOS* distiller_page = weak_this.get(); | 202 DistillerPageIOS* distiller_page = weak_this.get(); |
| 197 if (distiller_page) | 203 if (distiller_page) |
| 198 distiller_page->HandleJavaScriptResult(result); | 204 distiller_page->HandleJavaScriptResult(result); |
| 199 }]; | 205 }]; |
| 200 } | 206 } |
| 201 | 207 |
| 202 void DistillerPageIOS::HandleJavaScriptResult(id result) { | 208 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(); | 209 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue(); |
| 206 if (result) { | 210 if (result) { |
| 207 resultValue = ValueResultFromScriptResult(result); | 211 resultValue = ValueResultFromScriptResult(result); |
| 208 } | 212 } |
| 209 OnDistillationDone(url_, resultValue.get()); | 213 OnDistillationDone(url_, resultValue.get()); |
| 210 } | 214 } |
| 211 | 215 |
| 212 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult( | 216 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult( |
| 213 id wk_result) { | 217 id wk_result) { |
| 214 return ::ValueResultFromScriptResult(wk_result, | 218 return ::ValueResultFromScriptResult(wk_result, |
| 215 kMaximumParsingRecursionDepth); | 219 kMaximumParsingRecursionDepth); |
| 216 } | 220 } |
| 217 } // namespace dom_distiller | 221 } // namespace dom_distiller |
| OLD | NEW |