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/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/values.h" | 16 #include "base/values.h" |
17 #include "components/dom_distiller/ios/distiller_favicon_ios.h" | |
17 #include "ios/public/provider/web/web_controller_provider.h" | 18 #include "ios/public/provider/web/web_controller_provider.h" |
18 #include "ios/web/public/browser_state.h" | 19 #include "ios/web/public/browser_state.h" |
20 #include "ios/web/public/favicon_url.h" | |
19 | 21 |
20 namespace { | 22 namespace { |
21 | 23 |
24 std::string favicon_script_ = | |
gambard
2016/11/28 10:20:04
This script is the same as in ios/web/web_state/js
kkhorimoto
2016/11/28 23:48:25
WebStateObserver already exposes FaviconUrlUpdated
| |
25 "(function() {" | |
26 " var favicons = [];" | |
27 " var hasFavicon = false;" | |
28 " delete favicons.toJSON; " | |
29 " var links = document.getElementsByTagName('link');" | |
30 " var linkCount = links.length;" | |
31 " for (var i = 0; i < linkCount; ++i) {" | |
32 " if (links[i].rel) {" | |
33 " var rel = links[i].rel.toLowerCase();" | |
34 " if (rel == 'shortcut icon' ||" | |
35 " rel == 'icon' ||" | |
36 " rel == 'apple-touch-icon' ||" | |
37 " rel == 'apple-touch-icon-precomposed') {" | |
38 " var favicon = {" | |
39 " rel: links[i].rel.toLowerCase()," | |
40 " href: links[i].href" | |
41 " };" | |
42 " favicons.push(favicon);" | |
43 " if (rel == 'icon' || rel == 'shortcut icon') {" | |
44 " hasFavicon = true;" | |
45 " }" | |
46 " }" | |
47 " }" | |
48 " }" | |
49 " if (!hasFavicon) {" | |
50 " var location = document.location;" | |
51 " if (location.protocol == 'http:' || location.protocol == 'https:') {" | |
52 " var favicon = {" | |
53 " rel: 'icon'," | |
54 " href: location.origin + '/favicon.ico'" | |
55 " };" | |
56 " favicons.push(favicon);" | |
57 " }" | |
58 " }" | |
59 " return favicons;" | |
60 " })();"; | |
61 | |
22 // This is duplicated here from ios/web/web_state/ui/web_view_js_utils.mm in | 62 // This is duplicated here from ios/web/web_state/ui/web_view_js_utils.mm in |
23 // order to handle numbers. The dom distiller proto expects integers and the | 63 // order to handle numbers. The dom distiller proto expects integers and the |
24 // generated JSON deserializer does not accept doubles in the place of ints. | 64 // generated JSON deserializer does not accept doubles in the place of ints. |
25 // However WKWebView only returns "numbers." However, here the proto expects | 65 // However WKWebView only returns "numbers." However, here the proto expects |
26 // integers and doubles, which is done by checking if the number has a fraction | 66 // integers and doubles, which is done by checking if the number has a fraction |
27 // or not; since this is a hacky method it's isolated to this file so as to | 67 // or not; since this is a hacky method it's isolated to this file so as to |
28 // limit the risk of broken JS calls. | 68 // limit the risk of broken JS calls. |
29 | 69 |
30 int const kMaximumParsingRecursionDepth = 6; | 70 int const kMaximumParsingRecursionDepth = 6; |
31 // Converts result of WKWebView script evaluation to base::Value, parsing | 71 // Converts result of WKWebView script evaluation to base::Value, parsing |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 DCHECK(distiller_page_); | 157 DCHECK(distiller_page_); |
118 } | 158 } |
119 | 159 |
120 void DistillerWebStateObserver::PageLoaded( | 160 void DistillerWebStateObserver::PageLoaded( |
121 web::PageLoadCompletionStatus load_completion_status) { | 161 web::PageLoadCompletionStatus load_completion_status) { |
122 distiller_page_->OnLoadURLDone(load_completion_status); | 162 distiller_page_->OnLoadURLDone(load_completion_status); |
123 } | 163 } |
124 | 164 |
125 #pragma mark - | 165 #pragma mark - |
126 | 166 |
127 DistillerPageIOS::DistillerPageIOS(web::BrowserState* browser_state) | 167 DistillerPageIOS::DistillerPageIOS(web::BrowserState* browser_state, |
128 : browser_state_(browser_state), weak_ptr_factory_(this) { | 168 DistillerFaviconIOS* distiller_favicon) |
129 } | 169 : browser_state_(browser_state), |
170 distiller_favicon_(distiller_favicon), | |
171 weak_ptr_factory_(this) {} | |
130 | 172 |
131 DistillerPageIOS::~DistillerPageIOS() { | 173 DistillerPageIOS::~DistillerPageIOS() { |
132 } | 174 } |
133 | 175 |
134 bool DistillerPageIOS::StringifyOutput() { | 176 bool DistillerPageIOS::StringifyOutput() { |
135 return false; | 177 return false; |
136 } | 178 } |
137 | 179 |
138 void DistillerPageIOS::DistillPageImpl(const GURL& url, | 180 void DistillerPageIOS::DistillPageImpl(const GURL& url, |
139 const std::string& script) { | 181 const std::string& script) { |
(...skipping 27 matching lines...) Expand all Loading... | |
167 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE || | 209 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE || |
168 !provider_) { | 210 !provider_) { |
169 HandleJavaScriptResult(nil); | 211 HandleJavaScriptResult(nil); |
170 return; | 212 return; |
171 } | 213 } |
172 | 214 |
173 // Inject the script. | 215 // Inject the script. |
174 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr(); | 216 base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr(); |
175 provider_->InjectScript(script_, ^(id result, NSError* error) { | 217 provider_->InjectScript(script_, ^(id result, NSError* error) { |
176 DistillerPageIOS* distiller_page = weak_this.get(); | 218 DistillerPageIOS* distiller_page = weak_this.get(); |
177 if (distiller_page) | 219 |
220 if (distiller_page) { | |
178 distiller_page->HandleJavaScriptResult(result); | 221 distiller_page->HandleJavaScriptResult(result); |
222 | |
223 // Get the favicons. | |
224 provider_->InjectScript(favicon_script_, ^(id result, NSError* error) { | |
225 DistillerPageIOS* distiller_page_favicon = weak_this.get(); | |
226 if (distiller_page_favicon) | |
227 distiller_page_favicon->HandleFaviconResult(result); | |
228 }); | |
229 } | |
179 }); | 230 }); |
180 } | 231 } |
181 | 232 |
233 void DistillerPageIOS::HandleFaviconResult(id result) { | |
234 std::unique_ptr<base::Value> resultValue = | |
235 ValueResultFromScriptResult(result); | |
236 base::ListValue* favicons = nullptr; | |
237 if (!resultValue || !resultValue->GetAsList(&favicons)) | |
238 return; | |
239 | |
240 std::vector<web::FaviconURL> urls; | |
241 web::ExtractFaviconURL(favicons, urls); | |
242 | |
243 if (!urls.empty()) | |
244 distiller_favicon_->HandleFaviconURL(urls, url_); | |
245 } | |
246 | |
182 void DistillerPageIOS::HandleJavaScriptResult(id result) { | 247 void DistillerPageIOS::HandleJavaScriptResult(id result) { |
183 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue(); | 248 std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue(); |
184 if (result) { | 249 if (result) { |
185 resultValue = ValueResultFromScriptResult(result); | 250 resultValue = ValueResultFromScriptResult(result); |
186 } | 251 } |
187 OnDistillationDone(url_, resultValue.get()); | 252 OnDistillationDone(url_, resultValue.get()); |
188 } | 253 } |
189 | 254 |
190 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult( | 255 std::unique_ptr<base::Value> DistillerPageIOS::ValueResultFromScriptResult( |
191 id wk_result) { | 256 id wk_result) { |
192 return ::ValueResultFromScriptResult(wk_result, | 257 return ::ValueResultFromScriptResult(wk_result, |
193 kMaximumParsingRecursionDepth); | 258 kMaximumParsingRecursionDepth); |
194 } | 259 } |
195 } // namespace dom_distiller | 260 } // namespace dom_distiller |
OLD | NEW |