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/extensions/extension_service.h"
15 #include "chrome/browser/favicon/favicon_service_factory.h"
16 #include "chrome/browser/history/top_sites.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/common/url_constants.h"
19 #include "content/public/browser/web_ui.h"
20 #include "grit/ui_resources.h"
21 #include "third_party/skia/include/core/SkBitmap.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/gfx/codec/png_codec.h"
24 #include "ui/gfx/color_analysis.h"
25 #include "ui/gfx/favicon_size.h"
29 base::StringValue
* SkColorToCss(SkColor color
) {
30 return new base::StringValue(base::StringPrintf("rgb(%d, %d, %d)",
36 base::StringValue
* GetDominantColorCssString(
37 scoped_refptr
<base::RefCountedMemory
> png
) {
38 color_utils::GridSampler sampler
;
40 color_utils::CalculateKMeanColorOfPNG(png
, 100, 665, &sampler
);
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 virtual ~ExtensionIconColorManager() {}
55 virtual 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 FaviconService
* favicon_service
= FaviconServiceFactory::GetForProfile(
95 Profile::FromWebUI(web_ui()), Profile::EXPLICIT_ACCESS
);
96 if (!favicon_service
|| path
.empty())
100 // Intercept requests for prepopulated pages.
101 for (size_t i
= 0; i
< arraysize(history::kPrepopulatedPages
); i
++) {
103 l10n_util::GetStringUTF8(history::kPrepopulatedPages
[i
].url_id
)) {
104 base::StringValue
dom_id_value(dom_id
);
105 scoped_ptr
<base::StringValue
> color(
106 SkColorToCss(history::kPrepopulatedPages
[i
].color
));
107 web_ui()->CallJavascriptFunction("ntp.setFaviconDominantColor",
108 dom_id_value
, *color
);
113 dom_id_map_
[id_
] = dom_id
;
114 favicon_service
->GetRawFaviconForURL(
115 FaviconService::FaviconForURLParams(
119 ui::SCALE_FACTOR_100P
,
120 base::Bind(&FaviconWebUIHandler::OnFaviconDataAvailable
,
121 base::Unretained(this),
123 &cancelable_task_tracker_
);
126 void FaviconWebUIHandler::OnFaviconDataAvailable(
128 const chrome::FaviconBitmapResult
& bitmap_result
) {
129 scoped_ptr
<base::StringValue
> color_value
;
131 if (bitmap_result
.is_valid())
132 color_value
.reset(GetDominantColorCssString(bitmap_result
.bitmap_data
));
134 color_value
.reset(new base::StringValue("#919191"));
136 base::StringValue
dom_id(dom_id_map_
[id
]);
137 web_ui()->CallJavascriptFunction("ntp.setFaviconDominantColor",
138 dom_id
, *color_value
);
139 dom_id_map_
.erase(id
);
142 void FaviconWebUIHandler::HandleGetAppIconDominantColor(
143 const base::ListValue
* args
) {
144 std::string extension_id
;
145 CHECK(args
->GetString(0, &extension_id
));
147 ExtensionService
* extension_service
=
148 Profile::FromWebUI(web_ui())->GetExtensionService();
149 const extensions::Extension
* extension
= extension_service
->GetExtensionById(
150 extension_id
, false);
153 app_icon_color_manager_
->LoadIcon(extension_service
->profile(), extension
);
156 void FaviconWebUIHandler::NotifyAppIconReady(const std::string
& extension_id
) {
157 const SkBitmap
& bitmap
= app_icon_color_manager_
->GetIcon(extension_id
);
158 // TODO(estade): would be nice to avoid a round trip through png encoding.
159 std::vector
<unsigned char> bits
;
160 if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap
, true, &bits
))
162 scoped_refptr
<base::RefCountedStaticMemory
> bits_mem(
163 new base::RefCountedStaticMemory(&bits
.front(), bits
.size()));
164 scoped_ptr
<base::StringValue
> color_value(
165 GetDominantColorCssString(bits_mem
));
166 base::StringValue
id(extension_id
);
167 web_ui()->CallJavascriptFunction(
168 "ntp.setFaviconDominantColor", id
, *color_value
);