| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 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/gtk/list_store_favicon_loader.h" |
| 6 |
| 7 #include "app/resource_bundle.h" |
| 8 #include "base/gfx/gtk_util.h" |
| 9 #include "base/gfx/png_decoder.h" |
| 10 #include "chrome/browser/profile.h" |
| 11 #include "grit/app_resources.h" |
| 12 #include "third_party/skia/include/core/SkBitmap.h" |
| 13 |
| 14 ListStoreFavIconLoader::ListStoreFavIconLoader( |
| 15 GtkListStore* list_store, gint favicon_col, gint favicon_handle_col, |
| 16 Profile* profile, CancelableRequestConsumer* consumer) |
| 17 : list_store_(list_store), favicon_col_(favicon_col), |
| 18 favicon_handle_col_(favicon_handle_col), profile_(profile), |
| 19 consumer_(consumer) { |
| 20 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 21 default_favicon_ = rb.GetPixbufNamed(IDR_DEFAULT_FAVICON); |
| 22 } |
| 23 |
| 24 void ListStoreFavIconLoader::LoadFaviconForRow(const GURL& url, |
| 25 GtkTreeIter* iter) { |
| 26 HistoryService* history = |
| 27 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 28 if (history) { |
| 29 HistoryService::Handle handle = history->GetFavIconForURL( |
| 30 url, consumer_, |
| 31 NewCallback(this, &ListStoreFavIconLoader::OnGotFavIcon)); |
| 32 gtk_list_store_set(list_store_, iter, |
| 33 favicon_handle_col_, handle, |
| 34 favicon_col_, default_favicon_, |
| 35 -1); |
| 36 } |
| 37 } |
| 38 |
| 39 bool ListStoreFavIconLoader::GetRowByFavIconHandle( |
| 40 HistoryService::Handle handle, GtkTreeIter* result_iter) { |
| 41 GtkTreeIter iter; |
| 42 gboolean valid = gtk_tree_model_get_iter_first( |
| 43 GTK_TREE_MODEL(list_store_), &iter); |
| 44 while (valid) { |
| 45 gint row_handle; |
| 46 gtk_tree_model_get(GTK_TREE_MODEL(list_store_), &iter, |
| 47 favicon_handle_col_, &row_handle, |
| 48 -1); |
| 49 if (row_handle == handle) { |
| 50 *result_iter = iter; |
| 51 return true; |
| 52 } |
| 53 valid = gtk_tree_model_iter_next( |
| 54 GTK_TREE_MODEL(list_store_), &iter); |
| 55 } |
| 56 return false; |
| 57 } |
| 58 |
| 59 void ListStoreFavIconLoader::OnGotFavIcon( |
| 60 HistoryService::Handle handle, bool know_fav_icon, |
| 61 scoped_refptr<RefCountedBytes> image_data, bool is_expired, GURL icon_url) { |
| 62 GtkTreeIter iter; |
| 63 if (!GetRowByFavIconHandle(handle, &iter)) |
| 64 return; |
| 65 gtk_list_store_set(list_store_, &iter, |
| 66 favicon_handle_col_, 0, |
| 67 -1); |
| 68 if (know_fav_icon && image_data.get() && !image_data->data.empty()) { |
| 69 int width, height; |
| 70 std::vector<unsigned char> decoded_data; |
| 71 if (PNGDecoder::Decode(&image_data->data.front(), image_data->data.size(), |
| 72 PNGDecoder::FORMAT_BGRA, &decoded_data, &width, |
| 73 &height)) { |
| 74 SkBitmap icon; |
| 75 icon.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 76 icon.allocPixels(); |
| 77 memcpy(icon.getPixels(), &decoded_data.front(), |
| 78 width * height * 4); |
| 79 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&icon); |
| 80 gtk_list_store_set(list_store_, &iter, |
| 81 favicon_col_, pixbuf, |
| 82 -1); |
| 83 g_object_unref(pixbuf); |
| 84 } |
| 85 } |
| 86 } |
| OLD | NEW |