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

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

Issue 2529283002: Save favicon during reading list distillation (Closed)
Patch Set: Add constructor comments 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 102
99 // Helper class for observing the loading of URLs to distill. 103 // Helper class for observing the loading of URLs to distill.
100 class DistillerWebStateObserver : public web::WebStateObserver { 104 class DistillerWebStateObserver : public web::WebStateObserver {
101 public: 105 public:
102 DistillerWebStateObserver(web::WebState* web_state, 106 DistillerWebStateObserver(web::WebState* web_state,
103 DistillerPageIOS* distiller_page); 107 DistillerPageIOS* distiller_page);
104 108
105 // WebStateObserver implementation: 109 // WebStateObserver implementation:
106 void PageLoaded( 110 void PageLoaded(
107 web::PageLoadCompletionStatus load_completion_status) override; 111 web::PageLoadCompletionStatus load_completion_status) override;
112 void WebStateDestroyed() override;
108 113
109 private: 114 private:
110 DistillerPageIOS* distiller_page_; // weak, owns this object. 115 DistillerPageIOS* distiller_page_; // weak, owns this object.
111 }; 116 };
112 117
113 DistillerWebStateObserver::DistillerWebStateObserver( 118 DistillerWebStateObserver::DistillerWebStateObserver(
114 web::WebState* web_state, 119 web::WebState* web_state,
115 DistillerPageIOS* distiller_page) 120 DistillerPageIOS* distiller_page)
116 : web::WebStateObserver(web_state), distiller_page_(distiller_page) { 121 : web::WebStateObserver(web_state), distiller_page_(distiller_page) {
117 DCHECK(web_state); 122 DCHECK(web_state);
118 DCHECK(distiller_page_); 123 DCHECK(distiller_page_);
119 } 124 }
120 125
121 void DistillerWebStateObserver::PageLoaded( 126 void DistillerWebStateObserver::PageLoaded(
122 web::PageLoadCompletionStatus load_completion_status) { 127 web::PageLoadCompletionStatus load_completion_status) {
123 distiller_page_->OnLoadURLDone(load_completion_status); 128 distiller_page_->OnLoadURLDone(load_completion_status);
124 } 129 }
125 130
131 void DistillerWebStateObserver::WebStateDestroyed() {
132 distiller_page_->web_state_ = nullptr;
133 }
134
126 #pragma mark - 135 #pragma mark -
127 136
128 DistillerPageIOS::DistillerPageIOS(web::BrowserState* browser_state) 137 DistillerPageIOS::DistillerPageIOS(
129 : browser_state_(browser_state), weak_ptr_factory_(this) { 138 FaviconWebStateDispatcher* web_state_dispatcher)
130 } 139 : web_state_dispatcher_(web_state_dispatcher), weak_ptr_factory_(this) {}
131 140
132 DistillerPageIOS::~DistillerPageIOS() { 141 DistillerPageIOS::~DistillerPageIOS() {
133 } 142 }
134 143
135 bool DistillerPageIOS::StringifyOutput() { 144 bool DistillerPageIOS::StringifyOutput() {
136 return false; 145 return false;
137 } 146 }
138 147
139 void DistillerPageIOS::DistillPageImpl(const GURL& url, 148 void DistillerPageIOS::DistillPageImpl(const GURL& url,
140 const std::string& script) { 149 const std::string& script) {
141 if (!url.is_valid() || !script.length()) 150 if (!url.is_valid() || !script.length())
142 return; 151 return;
143 url_ = url; 152 url_ = url;
144 script_ = script; 153 script_ = script;
145 154
146 // Lazily create provider. 155 web_state_ = web_state_dispatcher_->RequestWebState();
147 if (!provider_) { 156
148 if (ios::GetWebControllerProviderFactory()) { 157 if (web_state_) {
149 provider_ = 158 web_state_observer_ =
150 ios::GetWebControllerProviderFactory()->CreateWebControllerProvider( 159 base::MakeUnique<DistillerWebStateObserver>(web_state_, this);
151 browser_state_); 160
152 web_state_observer_.reset( 161 // The favicon driver needs to know which URL is currently fetched.
153 new DistillerWebStateObserver(provider_->GetWebState(), this)); 162 favicon::WebFaviconDriver* favicon_driver =
154 } 163 favicon::WebFaviconDriver::FromWebState(web_state_);
164 favicon_driver->FetchFavicon(url_);
165
166 // Load page using WebState.
167 web::NavigationManager::WebLoadParams params(url_);
168 web_state_->SetWebUsageEnabled(true);
169 web_state_->GetNavigationManager()->LoadURLWithParams(params);
170 // GetView is needed because the view is not created (but needed) when
171 // loading the page.
172 web_state_->GetView();
173 } else {
174 OnLoadURLDone(web::PageLoadCompletionStatus::FAILURE);
155 } 175 }
156
157 // Load page using provider.
158 if (provider_)
159 provider_->LoadURL(url_);
160 else
161 OnLoadURLDone(web::PageLoadCompletionStatus::FAILURE);
162 } 176 }
163 177
164 void DistillerPageIOS::OnLoadURLDone( 178 void DistillerPageIOS::OnLoadURLDone(
165 web::PageLoadCompletionStatus load_completion_status) { 179 web::PageLoadCompletionStatus load_completion_status) {
166 // Don't attempt to distill if the page load failed or if there is no 180 // Don't attempt to distill if the page load failed or if there is no
167 // provider. 181 // WebState.
168 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE || 182 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE ||
169 !provider_) { 183 !web_state_) {
170 HandleJavaScriptResult(nil); 184 HandleJavaScriptResult(nil);
171 return; 185 return;
172 } 186 }
173 187
174 // Inject the script. 188 // Inject the script.
175 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr(); 189 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr();
176 provider_->InjectScript(script_, ^(id result, NSError* error) { 190
177 DistillerPageIOS* distiller_page = weak_this.get(); 191 [[web_state_->GetJSInjectionReceiver()
178 if (distiller_page) 192 instanceOfClass:[CRWJSInjectionManager class]]
179 distiller_page->HandleJavaScriptResult(result); 193 executeJavaScript:base::SysUTF8ToNSString(script_)
180 }); 194 completionHandler:^(id result, NSError* error) {
195 DistillerPageIOS* distiller_page = weak_this.get();
196 if (distiller_page)
197 distiller_page->HandleJavaScriptResult(result);
198 }];
181 } 199 }
182 200
183 void DistillerPageIOS::HandleJavaScriptResult(id result) { 201 void DistillerPageIOS::HandleJavaScriptResult(id result) {
Olivier 2016/12/19 14:05:32 Early return if (!web_state_)
gambard 2016/12/20 08:54:22 No, the web state is not used anymore, it is not a
202 web_state_dispatcher_->ReturnWebState(web_state_);
203 web_state_ = nullptr;
184 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue(); 204 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue();
185 if (result) { 205 if (result) {
186 resultValue = ValueResultFromScriptResult(result); 206 resultValue = ValueResultFromScriptResult(result);
187 } 207 }
188 OnDistillationDone(url_, resultValue.get()); 208 OnDistillationDone(url_, resultValue.get());
189 } 209 }
190 210
191 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult( 211 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult(
192 id wk_result) { 212 id wk_result) {
193 return ::ValueResultFromScriptResult(wk_result, 213 return ::ValueResultFromScriptResult(wk_result,
194 kMaximumParsingRecursionDepth); 214 kMaximumParsingRecursionDepth);
195 } 215 }
196 } // namespace dom_distiller 216 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698