summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/ntp/favicon_webui_handler.cc
blob: 72466c4e3b6da7625aef726d45d2d473dfcc9c26 (plain)
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
// 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/callback.h"
#include "base/string_util.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.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"

FaviconWebUIHandler::FaviconWebUIHandler() {
}

FaviconWebUIHandler::~FaviconWebUIHandler() {
}

void FaviconWebUIHandler::RegisterMessages() {
  web_ui_->RegisterMessageCallback("getFaviconDominantColor",
      NewCallback(this, &FaviconWebUIHandler::HandleGetFaviconDominantColor));
}

void FaviconWebUIHandler::HandleGetFaviconDominantColor(const ListValue* args) {
  std::string path;
  CHECK(args->GetString(0, &path));
  DCHECK(StartsWithASCII(path, "chrome://favicon/size/32/", false)) <<
      "path is " << path;
  path = path.substr(arraysize("chrome://favicon/size/32/") - 1);

  double id;
  CHECK(args->GetDouble(1, &id));

  FaviconService* favicon_service =
      web_ui_->GetProfile()->GetFaviconService(Profile::EXPLICIT_ACCESS);
  if (!favicon_service || path.empty())
    return;

  FaviconService::Handle handle = favicon_service->GetFaviconForURL(
      GURL(path),
      history::FAVICON,
      &consumer_,
      NewCallback(this, &FaviconWebUIHandler::OnFaviconDataAvailable));
  consumer_.SetClientData(favicon_service, handle, static_cast<int>(id));
}

void FaviconWebUIHandler::OnFaviconDataAvailable(
    FaviconService::Handle request_handle,
    history::FaviconData favicon) {
  FaviconService* favicon_service =
      web_ui_->GetProfile()->GetFaviconService(Profile::EXPLICIT_ACCESS);
  int id = consumer_.GetClientData(favicon_service, request_handle);
  FundamentalValue id_value(id);
  scoped_ptr<StringValue> color_value;

  if (favicon.is_valid()) {
    // TODO(estade): cache the response
    color_utils::GridSampler sampler;
    SkColor color =
        color_utils::CalculateKMeanColorOfPNG(favicon.image_data, 100, 665,
                                              sampler);
    std::string css_color = base::StringPrintf("rgb(%d, %d, %d)",
                                               SkColorGetR(color),
                                               SkColorGetG(color),
                                               SkColorGetB(color));
    color_value.reset(new StringValue(css_color));
  } else {
    color_value.reset(new StringValue("#919191"));
  }

  web_ui_->CallJavascriptFunction("ntp4.setFaviconDominantColor",
                                  id_value, *color_value);
}