OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "base/callback.h" |
| 8 #include "base/string_util.h" |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/common/url_constants.h" |
| 12 #include "grit/app_resources.h" |
| 13 #include "third_party/skia/include/core/SkBitmap.h" |
| 14 #include "ui/gfx/codec/png_codec.h" |
| 15 #include "ui/gfx/color_analysis.h" |
| 16 |
| 17 FaviconWebUIHandler::FaviconWebUIHandler() { |
| 18 } |
| 19 |
| 20 FaviconWebUIHandler::~FaviconWebUIHandler() { |
| 21 } |
| 22 |
| 23 void FaviconWebUIHandler::RegisterMessages() { |
| 24 web_ui_->RegisterMessageCallback("getFaviconDominantColor", |
| 25 NewCallback(this, &FaviconWebUIHandler::HandleGetFaviconDominantColor)); |
| 26 } |
| 27 |
| 28 void FaviconWebUIHandler::HandleGetFaviconDominantColor(const ListValue* args) { |
| 29 std::string path; |
| 30 CHECK(args->GetString(0, &path)); |
| 31 DCHECK(StartsWithASCII(path, "chrome://favicon/", false)) << "path is " |
| 32 << path; |
| 33 path = path.substr(arraysize("chrome://favicon/") - 1); |
| 34 |
| 35 double id; |
| 36 CHECK(args->GetDouble(1, &id)); |
| 37 |
| 38 FaviconService* favicon_service = |
| 39 web_ui_->GetProfile()->GetFaviconService(Profile::EXPLICIT_ACCESS); |
| 40 if (!favicon_service || path.empty()) |
| 41 return; |
| 42 |
| 43 FaviconService::Handle handle = favicon_service->GetFaviconForURL( |
| 44 GURL(path), |
| 45 history::FAVICON, |
| 46 &consumer_, |
| 47 NewCallback(this, &FaviconWebUIHandler::OnFaviconDataAvailable)); |
| 48 consumer_.SetClientData(favicon_service, handle, static_cast<int>(id)); |
| 49 } |
| 50 |
| 51 void FaviconWebUIHandler::OnFaviconDataAvailable( |
| 52 FaviconService::Handle request_handle, |
| 53 history::FaviconData favicon) { |
| 54 FaviconService* favicon_service = |
| 55 web_ui_->GetProfile()->GetFaviconService(Profile::EXPLICIT_ACCESS); |
| 56 int id = consumer_.GetClientData(favicon_service, request_handle); |
| 57 |
| 58 if (favicon.is_valid()) { |
| 59 // TODO(estade): cache the response |
| 60 FundamentalValue id_value(id); |
| 61 |
| 62 color_utils::GridSampler sampler; |
| 63 SkColor color = |
| 64 color_utils::CalculateKMeanColorOfPNG(favicon.image_data, 100, 665, |
| 65 sampler); |
| 66 std::string css_color = base::StringPrintf("rgb(%d, %d, %d)", |
| 67 SkColorGetR(color), |
| 68 SkColorGetG(color), |
| 69 SkColorGetB(color)); |
| 70 StringValue color_value(css_color); |
| 71 web_ui_->CallJavascriptFunction("ntp4.setFaviconDominantColor", |
| 72 id_value, color_value); |
| 73 } |
| 74 } |
OLD | NEW |