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

Unified Diff: components/dom_distiller/ios/distiller_page_ios.mm

Issue 2529283002: Save favicon during reading list distillation (Closed)
Patch Set: Add a comment Created 4 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: components/dom_distiller/ios/distiller_page_ios.mm
diff --git a/components/dom_distiller/ios/distiller_page_ios.mm b/components/dom_distiller/ios/distiller_page_ios.mm
index 213e82fd671d09cfb1f851921b5844a75f129d8c..6b7d290213a82b0f99ec194541854720cab39358 100644
--- a/components/dom_distiller/ios/distiller_page_ios.mm
+++ b/components/dom_distiller/ios/distiller_page_ios.mm
@@ -14,11 +14,51 @@
#include "base/memory/ptr_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/values.h"
+#include "components/dom_distiller/ios/distiller_favicon_ios.h"
#include "ios/public/provider/web/web_controller_provider.h"
#include "ios/web/public/browser_state.h"
+#include "ios/web/public/favicon_url.h"
namespace {
+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
+ "(function() {"
+ " var favicons = [];"
+ " var hasFavicon = false;"
+ " delete favicons.toJSON; "
+ " var links = document.getElementsByTagName('link');"
+ " var linkCount = links.length;"
+ " for (var i = 0; i < linkCount; ++i) {"
+ " if (links[i].rel) {"
+ " var rel = links[i].rel.toLowerCase();"
+ " if (rel == 'shortcut icon' ||"
+ " rel == 'icon' ||"
+ " rel == 'apple-touch-icon' ||"
+ " rel == 'apple-touch-icon-precomposed') {"
+ " var favicon = {"
+ " rel: links[i].rel.toLowerCase(),"
+ " href: links[i].href"
+ " };"
+ " favicons.push(favicon);"
+ " if (rel == 'icon' || rel == 'shortcut icon') {"
+ " hasFavicon = true;"
+ " }"
+ " }"
+ " }"
+ " }"
+ " if (!hasFavicon) {"
+ " var location = document.location;"
+ " if (location.protocol == 'http:' || location.protocol == 'https:') {"
+ " var favicon = {"
+ " rel: 'icon',"
+ " href: location.origin + '/favicon.ico'"
+ " };"
+ " favicons.push(favicon);"
+ " }"
+ " }"
+ " return favicons;"
+ " })();";
+
// This is duplicated here from ios/web/web_state/ui/web_view_js_utils.mm in
// order to handle numbers. The dom distiller proto expects integers and the
// generated JSON deserializer does not accept doubles in the place of ints.
@@ -124,9 +164,11 @@ void DistillerWebStateObserver::PageLoaded(
#pragma mark -
-DistillerPageIOS::DistillerPageIOS(web::BrowserState* browser_state)
- : browser_state_(browser_state), weak_ptr_factory_(this) {
-}
+DistillerPageIOS::DistillerPageIOS(web::BrowserState* browser_state,
+ DistillerFaviconIOS* distiller_favicon)
+ : browser_state_(browser_state),
+ distiller_favicon_(distiller_favicon),
+ weak_ptr_factory_(this) {}
DistillerPageIOS::~DistillerPageIOS() {
}
@@ -174,11 +216,34 @@ void DistillerPageIOS::OnLoadURLDone(
base::WeakPtr<DistillerPageIOS> weak_this = weak_ptr_factory_.GetWeakPtr();
provider_->InjectScript(script_, ^(id result, NSError* error) {
DistillerPageIOS* distiller_page = weak_this.get();
- if (distiller_page)
+
+ if (distiller_page) {
distiller_page->HandleJavaScriptResult(result);
+
+ // Get the favicons.
+ provider_->InjectScript(favicon_script_, ^(id result, NSError* error) {
+ DistillerPageIOS* distiller_page_favicon = weak_this.get();
+ if (distiller_page_favicon)
+ distiller_page_favicon->HandleFaviconResult(result);
+ });
+ }
});
}
+void DistillerPageIOS::HandleFaviconResult(id result) {
+ std::unique_ptr<base::Value> resultValue =
+ ValueResultFromScriptResult(result);
+ base::ListValue* favicons = nullptr;
+ if (!resultValue || !resultValue->GetAsList(&favicons))
+ return;
+
+ std::vector<web::FaviconURL> urls;
+ web::ExtractFaviconURL(favicons, urls);
+
+ if (!urls.empty())
+ distiller_favicon_->HandleFaviconURL(urls, url_);
+}
+
void DistillerPageIOS::HandleJavaScriptResult(id result) {
std::unique_ptr<base::Value> resultValue = base::Value::CreateNullValue();
if (result) {

Powered by Google App Engine
This is Rietveld 408576698