| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/string_split.h" | 9 #include "base/string_split.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/history/top_sites.h" | 13 #include "chrome/browser/history/top_sites.h" |
| 14 #include "chrome/browser/extensions/extension_icon_manager.h" | 14 #include "chrome/browser/extensions/extension_icon_manager.h" |
| 15 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/common/extensions/extension_resource.h" | 18 #include "chrome/common/extensions/extension_resource.h" |
| 18 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
| 19 #include "content/public/browser/web_ui.h" | 20 #include "content/public/browser/web_ui.h" |
| 20 #include "grit/ui_resources.h" | 21 #include "grit/ui_resources.h" |
| 21 #include "third_party/skia/include/core/SkBitmap.h" | 22 #include "third_party/skia/include/core/SkBitmap.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "ui/gfx/codec/png_codec.h" | 24 #include "ui/gfx/codec/png_codec.h" |
| 24 #include "ui/gfx/color_analysis.h" | 25 #include "ui/gfx/color_analysis.h" |
| 25 | 26 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 void FaviconWebUIHandler::HandleGetFaviconDominantColor(const ListValue* args) { | 82 void FaviconWebUIHandler::HandleGetFaviconDominantColor(const ListValue* args) { |
| 82 std::string path; | 83 std::string path; |
| 83 CHECK(args->GetString(0, &path)); | 84 CHECK(args->GetString(0, &path)); |
| 84 DCHECK(StartsWithASCII(path, "chrome://favicon/size/16/", false)) << | 85 DCHECK(StartsWithASCII(path, "chrome://favicon/size/16/", false)) << |
| 85 "path is " << path; | 86 "path is " << path; |
| 86 path = path.substr(arraysize("chrome://favicon/size/16/") - 1); | 87 path = path.substr(arraysize("chrome://favicon/size/16/") - 1); |
| 87 | 88 |
| 88 std::string dom_id; | 89 std::string dom_id; |
| 89 CHECK(args->GetString(1, &dom_id)); | 90 CHECK(args->GetString(1, &dom_id)); |
| 90 | 91 |
| 91 FaviconService* favicon_service = | 92 FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( |
| 92 Profile::FromWebUI(web_ui())->GetFaviconService(Profile::EXPLICIT_ACCESS); | 93 Profile::FromWebUI(web_ui()), Profile::EXPLICIT_ACCESS); |
| 93 if (!favicon_service || path.empty()) | 94 if (!favicon_service || path.empty()) |
| 94 return; | 95 return; |
| 95 | 96 |
| 96 GURL url(path); | 97 GURL url(path); |
| 97 // Intercept requests for prepopulated pages. | 98 // Intercept requests for prepopulated pages. |
| 98 for (size_t i = 0; i < arraysize(history::kPrepopulatedPages); i++) { | 99 for (size_t i = 0; i < arraysize(history::kPrepopulatedPages); i++) { |
| 99 if (url.spec() == | 100 if (url.spec() == |
| 100 l10n_util::GetStringUTF8(history::kPrepopulatedPages[i].url_id)) { | 101 l10n_util::GetStringUTF8(history::kPrepopulatedPages[i].url_id)) { |
| 101 StringValue dom_id_value(dom_id); | 102 StringValue dom_id_value(dom_id); |
| 102 scoped_ptr<StringValue> color( | 103 scoped_ptr<StringValue> color( |
| 103 SkColorToCss(history::kPrepopulatedPages[i].color)); | 104 SkColorToCss(history::kPrepopulatedPages[i].color)); |
| 104 web_ui()->CallJavascriptFunction("ntp.setStripeColor", | 105 web_ui()->CallJavascriptFunction("ntp.setStripeColor", |
| 105 dom_id_value, *color); | 106 dom_id_value, *color); |
| 106 return; | 107 return; |
| 107 } | 108 } |
| 108 } | 109 } |
| 109 | 110 |
| 110 dom_id_map_[id_] = dom_id; | 111 dom_id_map_[id_] = dom_id; |
| 111 FaviconService::Handle handle = favicon_service->GetFaviconForURL( | 112 FaviconService::Handle handle = favicon_service->GetFaviconForURL( |
| 113 Profile::FromWebUI(web_ui()), |
| 112 url, | 114 url, |
| 113 history::FAVICON, | 115 history::FAVICON, |
| 114 &consumer_, | 116 &consumer_, |
| 115 base::Bind(&FaviconWebUIHandler::OnFaviconDataAvailable, | 117 base::Bind(&FaviconWebUIHandler::OnFaviconDataAvailable, |
| 116 base::Unretained(this))); | 118 base::Unretained(this))); |
| 117 consumer_.SetClientData(favicon_service, handle, id_++); | 119 consumer_.SetClientData(favicon_service, handle, id_++); |
| 118 } | 120 } |
| 119 | 121 |
| 120 void FaviconWebUIHandler::OnFaviconDataAvailable( | 122 void FaviconWebUIHandler::OnFaviconDataAvailable( |
| 121 FaviconService::Handle request_handle, | 123 FaviconService::Handle request_handle, |
| 122 history::FaviconData favicon) { | 124 history::FaviconData favicon) { |
| 123 FaviconService* favicon_service = | 125 FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( |
| 124 Profile::FromWebUI(web_ui())->GetFaviconService(Profile::EXPLICIT_ACCESS); | 126 Profile::FromWebUI(web_ui()), Profile::EXPLICIT_ACCESS); |
| 125 int id = consumer_.GetClientData(favicon_service, request_handle); | 127 int id = consumer_.GetClientData(favicon_service, request_handle); |
| 126 scoped_ptr<StringValue> color_value; | 128 scoped_ptr<StringValue> color_value; |
| 127 | 129 |
| 128 if (favicon.is_valid()) | 130 if (favicon.is_valid()) |
| 129 color_value.reset(GetDominantColorCssString(favicon.image_data)); | 131 color_value.reset(GetDominantColorCssString(favicon.image_data)); |
| 130 else | 132 else |
| 131 color_value.reset(new StringValue("#919191")); | 133 color_value.reset(new StringValue("#919191")); |
| 132 | 134 |
| 133 StringValue dom_id(dom_id_map_[id]); | 135 StringValue dom_id(dom_id_map_[id]); |
| 134 web_ui()->CallJavascriptFunction("ntp.setStripeColor", dom_id, *color_value); | 136 web_ui()->CallJavascriptFunction("ntp.setStripeColor", dom_id, *color_value); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 155 std::vector<unsigned char> bits; | 157 std::vector<unsigned char> bits; |
| 156 if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &bits)) | 158 if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &bits)) |
| 157 return; | 159 return; |
| 158 scoped_refptr<base::RefCountedStaticMemory> bits_mem( | 160 scoped_refptr<base::RefCountedStaticMemory> bits_mem( |
| 159 new base::RefCountedStaticMemory(&bits.front(), bits.size())); | 161 new base::RefCountedStaticMemory(&bits.front(), bits.size())); |
| 160 scoped_ptr<StringValue> color_value(GetDominantColorCssString(bits_mem)); | 162 scoped_ptr<StringValue> color_value(GetDominantColorCssString(bits_mem)); |
| 161 StringValue id(extension_id); | 163 StringValue id(extension_id); |
| 162 web_ui()->CallJavascriptFunction( | 164 web_ui()->CallJavascriptFunction( |
| 163 "ntp.setStripeColor", id, *color_value); | 165 "ntp.setStripeColor", id, *color_value); |
| 164 } | 166 } |
| OLD | NEW |