1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/ntp/favicon_webui_handler.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/string_split.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_icon_manager.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/extension_resource.h"
#include "chrome/common/url_constants.h"
#include "grit/ui_resources.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/color_analysis.h"
// Thin inheritance-dependent trampoline to forward notification of app
// icon loads to the FaviconWebUIHandler. Base class does caching of icons.
class ExtensionIconColorManager : public ExtensionIconManager {
public:
explicit ExtensionIconColorManager(FaviconWebUIHandler* handler)
: ExtensionIconManager(),
handler_(handler) {}
virtual ~ExtensionIconColorManager() {}
virtual void OnImageLoaded(SkBitmap* image,
const ExtensionResource& resource,
int index) OVERRIDE {
ExtensionIconManager::OnImageLoaded(image, resource, index);
handler_->NotifyAppIconReady(resource.extension_id());
}
private:
FaviconWebUIHandler* handler_;
};
FaviconWebUIHandler::FaviconWebUIHandler()
: id_(0),
app_icon_color_manager_(new ExtensionIconColorManager(this)) {
}
FaviconWebUIHandler::~FaviconWebUIHandler() {
}
void FaviconWebUIHandler::RegisterMessages() {
web_ui_->RegisterMessageCallback("getFaviconDominantColor",
base::Bind(&FaviconWebUIHandler::HandleGetFaviconDominantColor,
base::Unretained(this)));
web_ui_->RegisterMessageCallback("getAppIconDominantColor",
base::Bind(&FaviconWebUIHandler::HandleGetAppIconDominantColor,
base::Unretained(this)));
}
void FaviconWebUIHandler::HandleGetFaviconDominantColor(const ListValue* args) {
std::string path;
CHECK(args->GetString(0, &path));
DCHECK(StartsWithASCII(path, "chrome://favicon/size/16/", false)) <<
"path is " << path;
path = path.substr(arraysize("chrome://favicon/size/16/") - 1);
std::string dom_id;
CHECK(args->GetString(1, &dom_id));
dom_id_map_[id_] = dom_id;
FaviconService* favicon_service =
Profile::FromWebUI(web_ui_)->GetFaviconService(Profile::EXPLICIT_ACCESS);
if (!favicon_service || path.empty())
return;
FaviconService::Handle handle = favicon_service->GetFaviconForURL(
GURL(path),
history::FAVICON,
&consumer_,
base::Bind(&FaviconWebUIHandler::OnFaviconDataAvailable,
base::Unretained(this)));
consumer_.SetClientData(favicon_service, handle, id_++);
}
void FaviconWebUIHandler::OnFaviconDataAvailable(
FaviconService::Handle request_handle,
history::FaviconData favicon) {
FaviconService* favicon_service =
Profile::FromWebUI(web_ui_)->GetFaviconService(Profile::EXPLICIT_ACCESS);
int id = consumer_.GetClientData(favicon_service, request_handle);
scoped_ptr<StringValue> color_value;
if (favicon.is_valid())
color_value.reset(GetDominantColorCssString(favicon.image_data));
else
color_value.reset(new StringValue("#919191"));
StringValue dom_id(dom_id_map_[id]);
web_ui_->CallJavascriptFunction("ntp4.setStripeColor", dom_id, *color_value);
dom_id_map_.erase(id);
}
StringValue* FaviconWebUIHandler::GetDominantColorCssString(
scoped_refptr<RefCountedMemory> png) {
color_utils::GridSampler sampler;
SkColor color = color_utils::CalculateKMeanColorOfPNG(png, 100, 665, sampler);
std::string css_color = base::StringPrintf("rgb(%d, %d, %d)",
SkColorGetR(color),
SkColorGetG(color),
SkColorGetB(color));
return new StringValue(css_color);
}
void FaviconWebUIHandler::HandleGetAppIconDominantColor(
const ListValue* args) {
std::string extension_id;
CHECK(args->GetString(0, &extension_id));
ExtensionService* extension_service =
Profile::FromWebUI(web_ui_)->GetExtensionService();
const Extension* extension = extension_service->GetExtensionById(
extension_id, false);
if (!extension)
return;
app_icon_color_manager_->LoadIcon(extension);
}
void FaviconWebUIHandler::NotifyAppIconReady(const std::string& extension_id) {
const SkBitmap& bitmap = app_icon_color_manager_->GetIcon(extension_id);
// TODO(estade): would be nice to avoid a round trip through png encoding.
std::vector<unsigned char> bits;
if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &bits))
return;
scoped_refptr<RefCountedStaticMemory> bits_mem(
new RefCountedStaticMemory(&bits.front(), bits.size()));
scoped_ptr<StringValue> color_value(GetDominantColorCssString(bits_mem));
StringValue id(extension_id);
web_ui_->CallJavascriptFunction(
"ntp4.setStripeColor", id, *color_value);
}
|