OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/ntp/favicon_webui_handler.h" | 5 #include "chrome/browser/ui/webui/ntp/favicon_webui_handler.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
| 12 #include "content/browser/tab_contents/tab_contents.h" |
12 #include "grit/ui_resources.h" | 13 #include "grit/ui_resources.h" |
13 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
14 #include "ui/gfx/codec/png_codec.h" | 15 #include "ui/gfx/codec/png_codec.h" |
15 #include "ui/gfx/color_analysis.h" | 16 #include "ui/gfx/color_analysis.h" |
16 | 17 |
17 FaviconWebUIHandler::FaviconWebUIHandler() { | 18 FaviconWebUIHandler::FaviconWebUIHandler() { |
18 } | 19 } |
19 | 20 |
20 FaviconWebUIHandler::~FaviconWebUIHandler() { | 21 FaviconWebUIHandler::~FaviconWebUIHandler() { |
21 } | 22 } |
22 | 23 |
23 void FaviconWebUIHandler::RegisterMessages() { | 24 void FaviconWebUIHandler::RegisterMessages() { |
24 web_ui_->RegisterMessageCallback("getFaviconDominantColor", | 25 web_ui_->RegisterMessageCallback("getFaviconDominantColor", |
25 NewCallback(this, &FaviconWebUIHandler::HandleGetFaviconDominantColor)); | 26 NewCallback(this, &FaviconWebUIHandler::HandleGetFaviconDominantColor)); |
26 } | 27 } |
27 | 28 |
28 void FaviconWebUIHandler::HandleGetFaviconDominantColor(const ListValue* args) { | 29 void FaviconWebUIHandler::HandleGetFaviconDominantColor(const ListValue* args) { |
29 std::string path; | 30 std::string path; |
30 CHECK(args->GetString(0, &path)); | 31 CHECK(args->GetString(0, &path)); |
31 DCHECK(StartsWithASCII(path, "chrome://favicon/size/32/", false)) << | 32 DCHECK(StartsWithASCII(path, "chrome://favicon/size/32/", false)) << |
32 "path is " << path; | 33 "path is " << path; |
33 path = path.substr(arraysize("chrome://favicon/size/32/") - 1); | 34 path = path.substr(arraysize("chrome://favicon/size/32/") - 1); |
34 | 35 |
35 double id; | 36 double id; |
36 CHECK(args->GetDouble(1, &id)); | 37 CHECK(args->GetDouble(1, &id)); |
37 | 38 |
| 39 Profile* profile = |
| 40 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
38 FaviconService* favicon_service = | 41 FaviconService* favicon_service = |
39 web_ui_->GetProfile()->GetFaviconService(Profile::EXPLICIT_ACCESS); | 42 profile->GetFaviconService(Profile::EXPLICIT_ACCESS); |
40 if (!favicon_service || path.empty()) | 43 if (!favicon_service || path.empty()) |
41 return; | 44 return; |
42 | 45 |
43 FaviconService::Handle handle = favicon_service->GetFaviconForURL( | 46 FaviconService::Handle handle = favicon_service->GetFaviconForURL( |
44 GURL(path), | 47 GURL(path), |
45 history::FAVICON, | 48 history::FAVICON, |
46 &consumer_, | 49 &consumer_, |
47 NewCallback(this, &FaviconWebUIHandler::OnFaviconDataAvailable)); | 50 NewCallback(this, &FaviconWebUIHandler::OnFaviconDataAvailable)); |
48 consumer_.SetClientData(favicon_service, handle, static_cast<int>(id)); | 51 consumer_.SetClientData(favicon_service, handle, static_cast<int>(id)); |
49 } | 52 } |
50 | 53 |
51 void FaviconWebUIHandler::OnFaviconDataAvailable( | 54 void FaviconWebUIHandler::OnFaviconDataAvailable( |
52 FaviconService::Handle request_handle, | 55 FaviconService::Handle request_handle, |
53 history::FaviconData favicon) { | 56 history::FaviconData favicon) { |
| 57 Profile* profile = |
| 58 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
54 FaviconService* favicon_service = | 59 FaviconService* favicon_service = |
55 web_ui_->GetProfile()->GetFaviconService(Profile::EXPLICIT_ACCESS); | 60 profile->GetFaviconService(Profile::EXPLICIT_ACCESS); |
56 int id = consumer_.GetClientData(favicon_service, request_handle); | 61 int id = consumer_.GetClientData(favicon_service, request_handle); |
57 FundamentalValue id_value(id); | 62 FundamentalValue id_value(id); |
58 scoped_ptr<StringValue> color_value; | 63 scoped_ptr<StringValue> color_value; |
59 | 64 |
60 if (favicon.is_valid()) { | 65 if (favicon.is_valid()) { |
61 // TODO(estade): cache the response | 66 // TODO(estade): cache the response |
62 color_utils::GridSampler sampler; | 67 color_utils::GridSampler sampler; |
63 SkColor color = | 68 SkColor color = |
64 color_utils::CalculateKMeanColorOfPNG(favicon.image_data, 100, 665, | 69 color_utils::CalculateKMeanColorOfPNG(favicon.image_data, 100, 665, |
65 sampler); | 70 sampler); |
66 std::string css_color = base::StringPrintf("rgb(%d, %d, %d)", | 71 std::string css_color = base::StringPrintf("rgb(%d, %d, %d)", |
67 SkColorGetR(color), | 72 SkColorGetR(color), |
68 SkColorGetG(color), | 73 SkColorGetG(color), |
69 SkColorGetB(color)); | 74 SkColorGetB(color)); |
70 color_value.reset(new StringValue(css_color)); | 75 color_value.reset(new StringValue(css_color)); |
71 } else { | 76 } else { |
72 color_value.reset(new StringValue("#919191")); | 77 color_value.reset(new StringValue("#919191")); |
73 } | 78 } |
74 | 79 |
75 web_ui_->CallJavascriptFunction("ntp4.setFaviconDominantColor", | 80 web_ui_->CallJavascriptFunction("ntp4.setFaviconDominantColor", |
76 id_value, *color_value); | 81 id_value, *color_value); |
77 } | 82 } |
OLD | NEW |