Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(359)

Side by Side Diff: components/dom_distiller/ios/distiller_page_ios.mm

Issue 2604773002: Create distiller files for Reading List. (Closed)
Patch Set: Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
17 #include "base/values.h" 19 #include "base/values.h"
18 #include "components/favicon/ios/web_favicon_driver.h" 20 #include "components/favicon/ios/web_favicon_driver.h"
19 #include "ios/web/public/browser_state.h" 21 #include "ios/web/public/browser_state.h"
20 #import "ios/web/public/navigation_manager.h" 22 #import "ios/web/public/navigation_manager.h"
21 #import "ios/web/public/web_state/js/crw_js_injection_manager.h" 23 #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" 24 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h"
23 #import "ios/web/public/web_state/web_state.h" 25 #import "ios/web/public/web_state/web_state.h"
24 26
25 namespace { 27 namespace {
26 28
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 web::PageLoadCompletionStatus load_completion_status) { 129 web::PageLoadCompletionStatus load_completion_status) {
128 distiller_page_->OnLoadURLDone(load_completion_status); 130 distiller_page_->OnLoadURLDone(load_completion_status);
129 } 131 }
130 132
131 void DistillerWebStateObserver::WebStateDestroyed() { 133 void DistillerWebStateObserver::WebStateDestroyed() {
132 distiller_page_->web_state_ = nullptr; 134 distiller_page_->web_state_ = nullptr;
133 } 135 }
134 136
135 #pragma mark - 137 #pragma mark -
136 138
137 DistillerPageIOS::DistillerPageIOS( 139 DistillerPageIOS::DistillerPageIOS(std::unique_ptr<web::WebState> web_state)
138 FaviconWebStateDispatcher* web_state_dispatcher) 140 : web_state_(std::move(web_state)), weak_ptr_factory_(this) {
139 : web_state_dispatcher_(web_state_dispatcher), weak_ptr_factory_(this) {} 141 if (web_state_) {
140 142 web_state_observer_ =
141 DistillerPageIOS::~DistillerPageIOS() { 143 base::MakeUnique<DistillerWebStateObserver>(web_state_.get(), this);
144 }
142 } 145 }
143 146
144 bool DistillerPageIOS::StringifyOutput() { 147 bool DistillerPageIOS::StringifyOutput() {
145 return false; 148 return false;
146 } 149 }
147 150
151 DistillerPageIOS::~DistillerPageIOS() {}
152
153 std::unique_ptr<web::WebState> DistillerPageIOS::ReplaceWebState(
gambard 2016/12/27 09:35:58 I am not fond of this logic of switching web state
Olivier 2016/12/27 10:08:36 We need to update the observer. I am not sure how
154 std::unique_ptr<web::WebState> web_state) {
155 std::unique_ptr<web::WebState> old_web_state = std::move(web_state_);
156 web_state_ = std::move(web_state);
157 if (web_state_) {
158 web_state_observer_ =
159 base::MakeUnique<DistillerWebStateObserver>(web_state_.get(), this);
160 } else {
161 web_state_observer_.reset();
162 }
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 OnLoadURLDone(web::PageLoadCompletionStatus::FAILURE);
159 return; 175 return;
160 } 176 }
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_);
gambard 2016/12/27 09:35:58 I don't see this code in the new version.
Olivier 2016/12/27 10:08:36 Done.
169
170 // Load page using WebState. 177 // Load page using WebState.
171 web::NavigationManager::WebLoadParams params(url_); 178 web::NavigationManager::WebLoadParams params(url_);
172 web_state_->SetWebUsageEnabled(true); 179 web_state_->SetWebUsageEnabled(true);
173 web_state_->GetNavigationManager()->LoadURLWithParams(params); 180 web_state_->GetNavigationManager()->LoadURLWithParams(params);
174 // GetView is needed because the view is not created (but needed) when 181 // GetView is needed because the view is not created (but needed) when
175 // loading the page. 182 // loading the page.
176 web_state_->GetView(); 183 web_state_->GetView();
177 } 184 }
178 185
179 void DistillerPageIOS::OnLoadURLDone( 186 void DistillerPageIOS::OnLoadURLDone(
180 web::PageLoadCompletionStatus load_completion_status) { 187 web::PageLoadCompletionStatus load_completion_status) {
181 // Don't attempt to distill if the page load failed or if there is no 188 // Don't attempt to distill if the page load failed or if there is no
182 // WebState. 189 // WebState.
183 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE || 190 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE ||
184 !web_state_) { 191 !web_state_) {
185 HandleJavaScriptResult(nil); 192 HandleJavaScriptResult(nil);
186 return; 193 return;
187 } 194 }
188
189 // Inject the script.
190 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr(); 195 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr();
191 196 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
197 FROM_HERE, base::Bind(&DistillerPageIOS::DistillPage, weak_this),
198 base::TimeDelta::FromSeconds(2));
gambard 2016/12/27 09:35:58 Use a constant for the "2".
Olivier 2016/12/27 10:08:36 Code removed.
199 }
200 void DistillerPageIOS::DistillPage() {
201 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr();
192 [[web_state_->GetJSInjectionReceiver() 202 [[web_state_->GetJSInjectionReceiver()
193 instanceOfClass:[CRWJSInjectionManager class]] 203 instanceOfClass:[CRWJSInjectionManager class]]
194 executeJavaScript:base::SysUTF8ToNSString(script_) 204 executeJavaScript:base::SysUTF8ToNSString(script_)
195 completionHandler:^(id result, NSError* error) { 205 completionHandler:^(id result, NSError* error) {
196 DistillerPageIOS* distiller_page = weak_this.get(); 206 DistillerPageIOS* distiller_page = weak_this.get();
197 if (distiller_page) 207 if (distiller_page)
198 distiller_page->HandleJavaScriptResult(result); 208 distiller_page->HandleJavaScriptResult(result);
199 }]; 209 }];
200 } 210 }
201 211
202 void DistillerPageIOS::HandleJavaScriptResult(id result) { 212 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(); 213 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue();
206 if (result) { 214 if (result) {
207 resultValue = ValueResultFromScriptResult(result); 215 resultValue = ValueResultFromScriptResult(result);
208 } 216 }
209 OnDistillationDone(url_, resultValue.get()); 217 OnDistillationDone(url_, resultValue.get());
210 } 218 }
211 219
212 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult( 220 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult(
213 id wk_result) { 221 id wk_result) {
214 return ::ValueResultFromScriptResult(wk_result, 222 return ::ValueResultFromScriptResult(wk_result,
215 kMaximumParsingRecursionDepth); 223 kMaximumParsingRecursionDepth);
216 } 224 }
217 } // namespace dom_distiller 225 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698