Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / webui / ntp / favicon_webui_handler.cc
blob27e71a40fd3b150093ceae476cd00801a986c551
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"
7 #include "base/bind.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"
28 namespace {
30 base::StringValue* SkColorToCss(SkColor color) {
31 return new base::StringValue(base::StringPrintf("rgb(%d, %d, %d)",
32 SkColorGetR(color),
33 SkColorGetG(color),
34 SkColorGetB(color)));
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);
44 } // namespace
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 {
49 public:
50 explicit ExtensionIconColorManager(FaviconWebUIHandler* handler)
51 : ExtensionIconManager(),
52 handler_(handler) {}
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);
61 private:
62 FaviconWebUIHandler* handler_;
65 FaviconWebUIHandler::FaviconWebUIHandler()
66 : id_(0),
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) {
84 std::string path;
85 CHECK(args->GetString(0, &path));
86 std::string prefix = "chrome://favicon/size/";
87 DCHECK(base::StartsWith(path, prefix, base::CompareCase::INSENSITIVE_ASCII))
88 << "path is " << path;
89 size_t slash = path.find("/", prefix.length());
90 path = path.substr(slash + 1);
92 std::string dom_id;
93 CHECK(args->GetString(1, &dom_id));
95 favicon::FaviconService* favicon_service =
96 FaviconServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()),
97 ServiceAccessType::EXPLICIT_ACCESS);
98 if (!favicon_service || path.empty())
99 return;
101 GURL url(path);
102 // Intercept requests for prepopulated pages if TopSites exists.
103 scoped_refptr<history::TopSites> top_sites =
104 TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui()));
105 if (top_sites) {
106 for (const auto& prepopulated_page : top_sites->GetPrepopulatedPages()) {
107 if (url == prepopulated_page.most_visited.url) {
108 base::StringValue dom_id_value(dom_id);
109 scoped_ptr<base::StringValue> color(
110 SkColorToCss(prepopulated_page.color));
111 web_ui()->CallJavascriptFunction("ntp.setFaviconDominantColor",
112 dom_id_value, *color);
113 return;
118 dom_id_map_[id_] = dom_id;
119 favicon_service->GetRawFaviconForPageURL(
120 url,
121 favicon_base::FAVICON,
122 gfx::kFaviconSize,
123 base::Bind(&FaviconWebUIHandler::OnFaviconDataAvailable,
124 base::Unretained(this),
125 id_++),
126 &cancelable_task_tracker_);
129 void FaviconWebUIHandler::OnFaviconDataAvailable(
130 int id,
131 const favicon_base::FaviconRawBitmapResult& bitmap_result) {
132 scoped_ptr<base::StringValue> color_value;
134 if (bitmap_result.is_valid())
135 color_value.reset(GetDominantColorCssString(bitmap_result.bitmap_data));
136 else
137 color_value.reset(new base::StringValue("#919191"));
139 base::StringValue dom_id(dom_id_map_[id]);
140 web_ui()->CallJavascriptFunction("ntp.setFaviconDominantColor",
141 dom_id, *color_value);
142 dom_id_map_.erase(id);
145 void FaviconWebUIHandler::HandleGetAppIconDominantColor(
146 const base::ListValue* args) {
147 std::string extension_id;
148 CHECK(args->GetString(0, &extension_id));
150 Profile* profile = Profile::FromWebUI(web_ui());
151 extensions::ExtensionRegistry* extension_registry =
152 extensions::ExtensionRegistry::Get(profile);
153 const extensions::Extension* extension =
154 extension_registry->enabled_extensions().GetByID(extension_id);
155 if (!extension)
156 return;
157 app_icon_color_manager_->LoadIcon(profile, extension);
160 void FaviconWebUIHandler::NotifyAppIconReady(const std::string& extension_id) {
161 const SkBitmap& bitmap = app_icon_color_manager_->GetIcon(extension_id);
162 // TODO(estade): would be nice to avoid a round trip through png encoding.
163 std::vector<unsigned char> bits;
164 if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &bits))
165 return;
166 scoped_refptr<base::RefCountedStaticMemory> bits_mem(
167 new base::RefCountedStaticMemory(&bits.front(), bits.size()));
168 scoped_ptr<base::StringValue> color_value(
169 GetDominantColorCssString(bits_mem));
170 base::StringValue id(extension_id);
171 web_ui()->CallJavascriptFunction(
172 "ntp.setFaviconDominantColor", id, *color_value);