1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/ntp/favicon_webui_handler.h"
8 #include "base/bind_helpers.h"
9 #include "base/strings/string_split.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/values.h"
13 #include "chrome/browser/extensions/extension_icon_manager.h"
14 #include "chrome/browser/favicon/favicon_service_factory.h"
15 #include "chrome/browser/history/top_sites_factory.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/url_constants.h"
18 #include "components/favicon/core/favicon_service.h"
19 #include "components/history/core/browser/top_sites.h"
20 #include "content/public/browser/web_ui.h"
21 #include "extensions/browser/extension_registry.h"
22 #include "third_party/skia/include/core/SkBitmap.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/gfx/codec/png_codec.h"
25 #include "ui/gfx/color_analysis.h"
26 #include "ui/gfx/favicon_size.h"
30 base::StringValue
* SkColorToCss(SkColor color
) {
31 return new base::StringValue(base::StringPrintf("rgb(%d, %d, %d)",
37 base::StringValue
* GetDominantColorCssString(
38 scoped_refptr
<base::RefCountedMemory
> png
) {
39 color_utils::GridSampler sampler
;
40 SkColor color
= color_utils::CalculateKMeanColorOfPNG(png
);
41 return SkColorToCss(color
);
46 // Thin inheritance-dependent trampoline to forward notification of app
47 // icon loads to the FaviconWebUIHandler. Base class does caching of icons.
48 class ExtensionIconColorManager
: public ExtensionIconManager
{
50 explicit ExtensionIconColorManager(FaviconWebUIHandler
* handler
)
51 : ExtensionIconManager(),
53 ~ExtensionIconColorManager() override
{}
55 void OnImageLoaded(const std::string
& extension_id
,
56 const gfx::Image
& image
) override
{
57 ExtensionIconManager::OnImageLoaded(extension_id
, image
);
58 handler_
->NotifyAppIconReady(extension_id
);
62 FaviconWebUIHandler
* handler_
;
65 FaviconWebUIHandler::FaviconWebUIHandler()
67 app_icon_color_manager_(new ExtensionIconColorManager(this)) {
70 FaviconWebUIHandler::~FaviconWebUIHandler() {
73 void FaviconWebUIHandler::RegisterMessages() {
74 web_ui()->RegisterMessageCallback("getFaviconDominantColor",
75 base::Bind(&FaviconWebUIHandler::HandleGetFaviconDominantColor
,
76 base::Unretained(this)));
77 web_ui()->RegisterMessageCallback("getAppIconDominantColor",
78 base::Bind(&FaviconWebUIHandler::HandleGetAppIconDominantColor
,
79 base::Unretained(this)));
82 void FaviconWebUIHandler::HandleGetFaviconDominantColor(
83 const base::ListValue
* args
) {
85 CHECK(args
->GetString(0, &path
));
86 std::string prefix
= "chrome://favicon/size/";
87 DCHECK(StartsWithASCII(path
, prefix
, false)) << "path is " << path
;
88 size_t slash
= path
.find("/", prefix
.length());
89 path
= path
.substr(slash
+ 1);
92 CHECK(args
->GetString(1, &dom_id
));
94 favicon::FaviconService
* favicon_service
=
95 FaviconServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()),
96 ServiceAccessType::EXPLICIT_ACCESS
);
97 if (!favicon_service
|| path
.empty())
101 // Intercept requests for prepopulated pages if TopSites exists.
102 scoped_refptr
<history::TopSites
> top_sites
=
103 TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui()));
105 for (const auto& prepopulated_page
: top_sites
->GetPrepopulatedPages()) {
106 if (url
== prepopulated_page
.most_visited
.url
) {
107 base::StringValue
dom_id_value(dom_id
);
108 scoped_ptr
<base::StringValue
> color(
109 SkColorToCss(prepopulated_page
.color
));
110 web_ui()->CallJavascriptFunction("ntp.setFaviconDominantColor",
111 dom_id_value
, *color
);
117 dom_id_map_
[id_
] = dom_id
;
118 favicon_service
->GetRawFaviconForPageURL(
120 favicon_base::FAVICON
,
122 base::Bind(&FaviconWebUIHandler::OnFaviconDataAvailable
,
123 base::Unretained(this),
125 &cancelable_task_tracker_
);
128 void FaviconWebUIHandler::OnFaviconDataAvailable(
130 const favicon_base::FaviconRawBitmapResult
& bitmap_result
) {
131 scoped_ptr
<base::StringValue
> color_value
;
133 if (bitmap_result
.is_valid())
134 color_value
.reset(GetDominantColorCssString(bitmap_result
.bitmap_data
));
136 color_value
.reset(new base::StringValue("#919191"));
138 base::StringValue
dom_id(dom_id_map_
[id
]);
139 web_ui()->CallJavascriptFunction("ntp.setFaviconDominantColor",
140 dom_id
, *color_value
);
141 dom_id_map_
.erase(id
);
144 void FaviconWebUIHandler::HandleGetAppIconDominantColor(
145 const base::ListValue
* args
) {
146 std::string extension_id
;
147 CHECK(args
->GetString(0, &extension_id
));
149 Profile
* profile
= Profile::FromWebUI(web_ui());
150 extensions::ExtensionRegistry
* extension_registry
=
151 extensions::ExtensionRegistry::Get(profile
);
152 const extensions::Extension
* extension
=
153 extension_registry
->enabled_extensions().GetByID(extension_id
);
156 app_icon_color_manager_
->LoadIcon(profile
, extension
);
159 void FaviconWebUIHandler::NotifyAppIconReady(const std::string
& extension_id
) {
160 const SkBitmap
& bitmap
= app_icon_color_manager_
->GetIcon(extension_id
);
161 // TODO(estade): would be nice to avoid a round trip through png encoding.
162 std::vector
<unsigned char> bits
;
163 if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap
, true, &bits
))
165 scoped_refptr
<base::RefCountedStaticMemory
> bits_mem(
166 new base::RefCountedStaticMemory(&bits
.front(), bits
.size()));
167 scoped_ptr
<base::StringValue
> color_value(
168 GetDominantColorCssString(bits_mem
));
169 base::StringValue
id(extension_id
);
170 web_ui()->CallJavascriptFunction(
171 "ntp.setFaviconDominantColor", id
, *color_value
);