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

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

Issue 2604773002: Create distiller files for Reading List. (Closed)
Patch Set: feedback Created 3 years, 11 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"
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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
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_->web_state_ = nullptr;
133 } 134 }
134 135
135 #pragma mark - 136 #pragma mark -
136 137
137 DistillerPageIOS::DistillerPageIOS( 138 DistillerPageIOS::DistillerPageIOS(std::unique_ptr<web::WebState> web_state)
138 FaviconWebStateDispatcher* web_state_dispatcher) 139 : web_state_(std::move(web_state)), weak_ptr_factory_(this) {
139 : web_state_dispatcher_(web_state_dispatcher), weak_ptr_factory_(this) {} 140 if (web_state_) {
140 141 web_state_observer_ =
141 DistillerPageIOS::~DistillerPageIOS() { 142 base::MakeUnique<DistillerWebStateObserver>(web_state_.get(), this);
143 }
142 } 144 }
143 145
144 bool DistillerPageIOS::StringifyOutput() { 146 bool DistillerPageIOS::StringifyOutput() {
145 return false; 147 return false;
146 } 148 }
147 149
150 DistillerPageIOS::~DistillerPageIOS() {}
151
152 std::unique_ptr<web::WebState> DistillerPageIOS::ReplaceWebState(
153 std::unique_ptr<web::WebState> web_state) {
154 std::unique_ptr<web::WebState> old_web_state = std::move(web_state_);
155 web_state_ = std::move(web_state);
156 if (web_state_) {
157 web_state_observer_ =
158 base::MakeUnique<DistillerWebStateObserver>(web_state_.get(), this);
159 } else {
160 web_state_observer_.reset();
161 }
162 return old_web_state;
163 }
164
148 void DistillerPageIOS::DistillPageImpl(const GURL& url, 165 void DistillerPageIOS::DistillPageImpl(const GURL& url,
149 const std::string& script) { 166 const std::string& script) {
150 if (!url.is_valid() || !script.length()) 167 if (!url.is_valid() || !script.length())
151 return; 168 return;
152 url_ = url; 169 url_ = url;
153 script_ = script; 170 script_ = script;
154 171
155 web_state_ = web_state_dispatcher_->RequestWebState();
156
157 if (!web_state_) { 172 if (!web_state_) {
158 OnLoadURLDone(web::PageLoadCompletionStatus::FAILURE); 173 OnLoadURLDone(web::PageLoadCompletionStatus::FAILURE);
159 return; 174 return;
160 } 175 }
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. 176 // Load page using WebState.
171 web::NavigationManager::WebLoadParams params(url_); 177 web::NavigationManager::WebLoadParams params(url_);
172 web_state_->SetWebUsageEnabled(true); 178 web_state_->SetWebUsageEnabled(true);
173 web_state_->GetNavigationManager()->LoadURLWithParams(params); 179 web_state_->GetNavigationManager()->LoadURLWithParams(params);
174 // GetView is needed because the view is not created (but needed) when 180 // GetView is needed because the view is not created (but needed) when
175 // loading the page. 181 // loading the page.
176 web_state_->GetView(); 182 web_state_->GetView();
177 } 183 }
178 184
179 void DistillerPageIOS::OnLoadURLDone( 185 void DistillerPageIOS::OnLoadURLDone(
180 web::PageLoadCompletionStatus load_completion_status) { 186 web::PageLoadCompletionStatus load_completion_status) {
181 // Don't attempt to distill if the page load failed or if there is no 187 // Don't attempt to distill if the page load failed or if there is no
182 // WebState. 188 // WebState.
183 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE || 189 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE ||
184 !web_state_) { 190 !web_state_) {
185 HandleJavaScriptResult(nil); 191 HandleJavaScriptResult(nil);
186 return; 192 return;
187 } 193 }
188
189 // Inject the script.
190 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr(); 194 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr();
191
192 [[web_state_->GetJSInjectionReceiver() 195 [[web_state_->GetJSInjectionReceiver()
193 instanceOfClass:[CRWJSInjectionManager class]] 196 instanceOfClass:[CRWJSInjectionManager class]]
194 executeJavaScript:base::SysUTF8ToNSString(script_) 197 executeJavaScript:base::SysUTF8ToNSString(script_)
195 completionHandler:^(id result, NSError* error) { 198 completionHandler:^(id result, NSError* error) {
196 DistillerPageIOS* distiller_page = weak_this.get(); 199 DistillerPageIOS* distiller_page = weak_this.get();
197 if (distiller_page) 200 if (distiller_page)
198 distiller_page->HandleJavaScriptResult(result); 201 distiller_page->HandleJavaScriptResult(result);
199 }]; 202 }];
200 } 203 }
201 204
202 void DistillerPageIOS::HandleJavaScriptResult(id result) { 205 void DistillerPageIOS::HandleJavaScriptResult(id result) {
203 web_state_dispatcher_->ReturnWebState(web_state_);
204 web_state_ = nullptr;
gambard 2016/12/27 10:53:29 reset WebStateObserver.
205 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue(); 206 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue();
206 if (result) { 207 if (result) {
207 resultValue = ValueResultFromScriptResult(result); 208 resultValue = ValueResultFromScriptResult(result);
208 } 209 }
209 OnDistillationDone(url_, resultValue.get()); 210 OnDistillationDone(url_, resultValue.get());
210 } 211 }
211 212
212 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult( 213 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult(
213 id wk_result) { 214 id wk_result) {
214 return ::ValueResultFromScriptResult(wk_result, 215 return ::ValueResultFromScriptResult(wk_result,
215 kMaximumParsingRecursionDepth); 216 kMaximumParsingRecursionDepth);
216 } 217 }
217 } // namespace dom_distiller 218 } // namespace dom_distiller
OLDNEW
« no previous file with comments | « components/dom_distiller/ios/distiller_page_ios.h ('k') | components/dom_distiller/ios/favicon_web_state_dispatcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698