Index: chrome/browser/ui/toolbar/back_forward_menu_model.cc |
diff --git a/chrome/browser/ui/toolbar/back_forward_menu_model.cc b/chrome/browser/ui/toolbar/back_forward_menu_model.cc |
index c77c89407c73653999fa432cc0406b66fc78a5a9..322e2e206abbbae01828f170d508a07c34090c09 100644 |
--- a/chrome/browser/ui/toolbar/back_forward_menu_model.cc |
+++ b/chrome/browser/ui/toolbar/back_forward_menu_model.cc |
@@ -22,6 +22,7 @@ |
#include "ui/base/l10n/l10n_util.h" |
#include "ui/base/resource/resource_bundle.h" |
#include "ui/base/text/text_elider.h" |
+#include "ui/gfx/codec/png_codec.h" |
const int BackForwardMenuModel::kMaxHistoryItems = 12; |
const int BackForwardMenuModel::kMaxChapterStops = 5; |
@@ -113,7 +114,7 @@ int BackForwardMenuModel::GetGroupIdAt(int index) const { |
return false; |
} |
-bool BackForwardMenuModel::GetIconAt(int index, SkBitmap* icon) const { |
+bool BackForwardMenuModel::GetIconAt(int index, SkBitmap* icon) { |
if (!ItemHasIcon(index)) |
return false; |
@@ -123,6 +124,9 @@ bool BackForwardMenuModel::GetIconAt(int index, SkBitmap* icon) const { |
} else { |
NavigationEntry* entry = GetNavigationEntry(index); |
*icon = entry->favicon().bitmap(); |
+ if (!entry->favicon().is_valid() && menu_model_delegate()) { |
+ FetchFavicon(entry); |
+ } |
} |
return true; |
@@ -182,6 +186,8 @@ void BackForwardMenuModel::ActivatedAtWithDisposition( |
void BackForwardMenuModel::MenuWillShow() { |
UserMetrics::RecordComputedAction(BuildActionName("Popup", -1), |
browser_->profile()); |
+ requested_favicons_.clear(); |
+ load_consumer_.CancelAllRequests(); |
} |
bool BackForwardMenuModel::IsSeparator(int index) const { |
@@ -203,6 +209,72 @@ bool BackForwardMenuModel::IsSeparator(int index) const { |
return index == history_items; |
} |
+void BackForwardMenuModel::SetMenuModelDelegate( |
+ ui::MenuModelDelegate* menu_model_delegate) { |
+ menu_model_delegate_ = menu_model_delegate; |
+} |
+ |
+void BackForwardMenuModel::FetchFavicon(NavigationEntry* entry) { |
+ // If the favicon has already been requested for this menu, don't do |
+ // anything. |
+ if (requested_favicons_.find(entry->unique_id()) != |
+ requested_favicons_.end()) { |
+ return; |
+ } |
+ requested_favicons_.insert(entry->unique_id()); |
+ FaviconService* favicon_service = |
+ browser_->profile()->GetFaviconService(Profile::EXPLICIT_ACCESS); |
+ if (!favicon_service) |
+ return; |
+ FaviconService::Handle handle = favicon_service->GetFaviconForURL( |
+ entry->url(), history::FAVICON, &load_consumer_, |
+ NewCallback(this, &BackForwardMenuModel::OnFavIconDataAvailable)); |
+ load_consumer_.SetClientData(favicon_service, handle, entry->unique_id()); |
+} |
+ |
+void BackForwardMenuModel::OnFavIconDataAvailable( |
+ FaviconService::Handle handle, |
+ history::FaviconData favicon) { |
+ if (favicon.known_icon && favicon.image_data.get() && |
sky
2011/03/18 17:54:11
This should be is_valid()
|
+ favicon.image_data->size()) { |
+ int unique_id = load_consumer_.GetClientDataForCurrentRequest(); |
+ // Find the current model_index for the unique_id. |
+ NavigationEntry* entry = NULL; |
+ int model_index = -1; |
+ for (int i = 0; i < GetItemCount() - 1; i++) { |
+ if (IsSeparator(i)) |
+ continue; |
+ if (GetNavigationEntry(i)->unique_id() == unique_id) { |
+ model_index = i; |
+ entry = GetNavigationEntry(i); |
+ break; |
+ } |
+ } |
+ |
+ if (!entry) |
+ // The NavigationEntry wasn't found, this can happen if the user |
+ // navigates to another page and a NavigatationEntry falls out of the |
+ // range of kMaxHistoryItems. |
+ return; |
+ |
+ // Now that we have a valid NavigationEntry, decode the favicon and assign |
+ // it to the NavigationEntry. |
+ SkBitmap fav_icon; |
+ if (gfx::PNGCodec::Decode(favicon.image_data->front(), |
+ favicon.image_data->size(), |
+ &fav_icon)) { |
+ entry->favicon().set_is_valid(true); |
+ entry->favicon().set_url(favicon.icon_url); |
+ if (fav_icon.empty()) |
+ return; |
+ entry->favicon().set_bitmap(fav_icon); |
+ if (menu_model_delegate()) { |
+ menu_model_delegate()->OnIconChanged(model_index); |
+ } |
+ } |
+ } |
+} |
+ |
int BackForwardMenuModel::GetHistoryItemCount() const { |
TabContents* contents = GetTabContents(); |
int items = 0; |