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

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

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

Powered by Google App Engine
This is Rietveld 408576698