| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/ui/webui/ntp/favicon_webui_handler.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/strings/string_split.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 #include "base/strings/stringprintf.h" | |
| 14 #include "base/values.h" | |
| 15 #include "chrome/browser/extensions/extension_icon_manager.h" | |
| 16 #include "chrome/browser/favicon/favicon_service_factory.h" | |
| 17 #include "chrome/browser/history/top_sites_factory.h" | |
| 18 #include "chrome/browser/profiles/profile.h" | |
| 19 #include "chrome/common/url_constants.h" | |
| 20 #include "components/favicon/core/favicon_service.h" | |
| 21 #include "components/history/core/browser/top_sites.h" | |
| 22 #include "content/public/browser/web_ui.h" | |
| 23 #include "extensions/browser/extension_registry.h" | |
| 24 #include "third_party/skia/include/core/SkBitmap.h" | |
| 25 #include "ui/gfx/codec/png_codec.h" | |
| 26 #include "ui/gfx/color_analysis.h" | |
| 27 #include "ui/gfx/favicon_size.h" | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 base::StringValue* SkColorToCss(SkColor color) { | |
| 32 return new base::StringValue(base::StringPrintf("rgb(%d, %d, %d)", | |
| 33 SkColorGetR(color), | |
| 34 SkColorGetG(color), | |
| 35 SkColorGetB(color))); | |
| 36 } | |
| 37 | |
| 38 base::StringValue* GetDominantColorCssString( | |
| 39 scoped_refptr<base::RefCountedMemory> png) { | |
| 40 color_utils::GridSampler sampler; | |
| 41 SkColor color = color_utils::CalculateKMeanColorOfPNG(png); | |
| 42 return SkColorToCss(color); | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 // Thin inheritance-dependent trampoline to forward notification of app | |
| 48 // icon loads to the FaviconWebUIHandler. Base class does caching of icons. | |
| 49 class ExtensionIconColorManager : public ExtensionIconManager { | |
| 50 public: | |
| 51 explicit ExtensionIconColorManager(FaviconWebUIHandler* handler) | |
| 52 : ExtensionIconManager(), | |
| 53 handler_(handler) {} | |
| 54 ~ExtensionIconColorManager() override {} | |
| 55 | |
| 56 void OnImageLoaded(const std::string& extension_id, | |
| 57 const gfx::Image& image) override { | |
| 58 ExtensionIconManager::OnImageLoaded(extension_id, image); | |
| 59 handler_->NotifyAppIconReady(extension_id); | |
| 60 } | |
| 61 | |
| 62 private: | |
| 63 FaviconWebUIHandler* handler_; | |
| 64 }; | |
| 65 | |
| 66 FaviconWebUIHandler::FaviconWebUIHandler() | |
| 67 : id_(0), | |
| 68 app_icon_color_manager_(new ExtensionIconColorManager(this)) { | |
| 69 } | |
| 70 | |
| 71 FaviconWebUIHandler::~FaviconWebUIHandler() { | |
| 72 } | |
| 73 | |
| 74 void FaviconWebUIHandler::RegisterMessages() { | |
| 75 web_ui()->RegisterMessageCallback("getFaviconDominantColor", | |
| 76 base::Bind(&FaviconWebUIHandler::HandleGetFaviconDominantColor, | |
| 77 base::Unretained(this))); | |
| 78 web_ui()->RegisterMessageCallback("getAppIconDominantColor", | |
| 79 base::Bind(&FaviconWebUIHandler::HandleGetAppIconDominantColor, | |
| 80 base::Unretained(this))); | |
| 81 } | |
| 82 | |
| 83 void FaviconWebUIHandler::HandleGetFaviconDominantColor( | |
| 84 const base::ListValue* args) { | |
| 85 std::string path; | |
| 86 CHECK(args->GetString(0, &path)); | |
| 87 std::string prefix = "chrome://favicon/size/"; | |
| 88 DCHECK(base::StartsWith(path, prefix, base::CompareCase::INSENSITIVE_ASCII)) | |
| 89 << "path is " << path; | |
| 90 size_t slash = path.find("/", prefix.length()); | |
| 91 path = path.substr(slash + 1); | |
| 92 | |
| 93 std::string dom_id; | |
| 94 CHECK(args->GetString(1, &dom_id)); | |
| 95 | |
| 96 favicon::FaviconService* favicon_service = | |
| 97 FaviconServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()), | |
| 98 ServiceAccessType::EXPLICIT_ACCESS); | |
| 99 if (!favicon_service || path.empty()) | |
| 100 return; | |
| 101 | |
| 102 GURL url(path); | |
| 103 // Intercept requests for prepopulated pages if TopSites exists. | |
| 104 scoped_refptr<history::TopSites> top_sites = | |
| 105 TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui())); | |
| 106 if (top_sites) { | |
| 107 for (const auto& prepopulated_page : top_sites->GetPrepopulatedPages()) { | |
| 108 if (url == prepopulated_page.most_visited.url) { | |
| 109 base::StringValue dom_id_value(dom_id); | |
| 110 std::unique_ptr<base::StringValue> color( | |
| 111 SkColorToCss(prepopulated_page.color)); | |
| 112 web_ui()->CallJavascriptFunctionUnsafe("ntp.setFaviconDominantColor", | |
| 113 dom_id_value, *color); | |
| 114 return; | |
| 115 } | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 dom_id_map_[id_] = dom_id; | |
| 120 favicon_service->GetRawFaviconForPageURL( | |
| 121 url, | |
| 122 favicon_base::FAVICON, | |
| 123 gfx::kFaviconSize, | |
| 124 base::Bind(&FaviconWebUIHandler::OnFaviconDataAvailable, | |
| 125 base::Unretained(this), | |
| 126 id_++), | |
| 127 &cancelable_task_tracker_); | |
| 128 } | |
| 129 | |
| 130 void FaviconWebUIHandler::OnFaviconDataAvailable( | |
| 131 int id, | |
| 132 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | |
| 133 std::unique_ptr<base::StringValue> color_value; | |
| 134 | |
| 135 if (bitmap_result.is_valid()) | |
| 136 color_value.reset(GetDominantColorCssString(bitmap_result.bitmap_data)); | |
| 137 else | |
| 138 color_value.reset(new base::StringValue("#919191")); | |
| 139 | |
| 140 base::StringValue dom_id(dom_id_map_[id]); | |
| 141 web_ui()->CallJavascriptFunctionUnsafe("ntp.setFaviconDominantColor", dom_id, | |
| 142 *color_value); | |
| 143 dom_id_map_.erase(id); | |
| 144 } | |
| 145 | |
| 146 void FaviconWebUIHandler::HandleGetAppIconDominantColor( | |
| 147 const base::ListValue* args) { | |
| 148 std::string extension_id; | |
| 149 CHECK(args->GetString(0, &extension_id)); | |
| 150 | |
| 151 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 152 extensions::ExtensionRegistry* extension_registry = | |
| 153 extensions::ExtensionRegistry::Get(profile); | |
| 154 const extensions::Extension* extension = | |
| 155 extension_registry->enabled_extensions().GetByID(extension_id); | |
| 156 if (!extension) | |
| 157 return; | |
| 158 app_icon_color_manager_->LoadIcon(profile, extension); | |
| 159 } | |
| 160 | |
| 161 void FaviconWebUIHandler::NotifyAppIconReady(const std::string& extension_id) { | |
| 162 const SkBitmap& bitmap = app_icon_color_manager_->GetIcon(extension_id); | |
| 163 // TODO(estade): would be nice to avoid a round trip through png encoding. | |
| 164 std::vector<unsigned char> bits; | |
| 165 if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &bits)) | |
| 166 return; | |
| 167 scoped_refptr<base::RefCountedStaticMemory> bits_mem( | |
| 168 new base::RefCountedStaticMemory(&bits.front(), bits.size())); | |
| 169 std::unique_ptr<base::StringValue> color_value( | |
| 170 GetDominantColorCssString(bits_mem)); | |
| 171 base::StringValue id(extension_id); | |
| 172 web_ui()->CallJavascriptFunctionUnsafe("ntp.setFaviconDominantColor", id, | |
| 173 *color_value); | |
| 174 } | |
| OLD | NEW |