Index: chrome/browser/ui/toolbar/back_forward_menu_model.cc |
=================================================================== |
--- chrome/browser/ui/toolbar/back_forward_menu_model.cc (revision 70486) |
+++ chrome/browser/ui/toolbar/back_forward_menu_model.cc (working copy) |
@@ -11,11 +11,13 @@ |
#include "app/resource_bundle.h" |
#include "base/string_number_conversions.h" |
#include "chrome/browser/metrics/user_metrics.h" |
+#include "chrome/browser/profiles/profile.h" |
#include "chrome/browser/tab_contents/navigation_controller.h" |
#include "chrome/browser/tab_contents/navigation_entry.h" |
#include "chrome/browser/tab_contents/tab_contents.h" |
#include "chrome/browser/ui/browser.h" |
#include "chrome/common/url_constants.h" |
+#include "gfx/codec/png_codec.h" |
#include "grit/generated_resources.h" |
#include "grit/theme_resources.h" |
#include "net/base/registry_controlled_domain.h" |
@@ -28,7 +30,8 @@ |
ModelType model_type) |
: browser_(browser), |
test_tab_contents_(NULL), |
- model_type_(model_type) { |
+ model_type_(model_type), |
+ delegate_(NULL) { |
} |
bool BackForwardMenuModel::HasIcons() const { |
@@ -106,7 +109,7 @@ |
return false; |
} |
-bool BackForwardMenuModel::GetIconAt(int index, SkBitmap* icon) const { |
+bool BackForwardMenuModel::GetIconAt(int index, SkBitmap* icon) { |
if (!ItemHasIcon(index)) |
return false; |
@@ -116,6 +119,9 @@ |
} else { |
NavigationEntry* entry = GetNavigationEntry(index); |
*icon = entry->favicon().bitmap(); |
+ if (!entry->favicon().is_valid()) { |
+ FetchFavicon(entry, index); |
+ } |
} |
return true; |
@@ -175,6 +181,8 @@ |
void BackForwardMenuModel::MenuWillShow() { |
UserMetrics::RecordComputedAction(BuildActionName("Popup", -1), |
browser_->profile()); |
+ requested_favicons_.clear(); |
+ load_consumer_.CancelAllRequests(); |
} |
bool BackForwardMenuModel::IsSeparator(int index) const { |
@@ -196,6 +204,67 @@ |
return index == history_items; |
} |
+void BackForwardMenuModel::SetDelegate(Delegate* delegate) { |
+ delegate_ = delegate; |
+} |
+ |
+void BackForwardMenuModel::FetchFavicon(NavigationEntry* entry, int index) { |
+ // 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(), &load_consumer_, |
+ NewCallback(this, &BackForwardMenuModel::OnFavIconDataAvailable)); |
+ load_consumer_.SetClientData(favicon_service, handle, entry->unique_id()); |
+} |
+ |
+void BackForwardMenuModel::OnFavIconDataAvailable( |
+ FaviconService::Handle handle, |
+ bool know_favicon, |
+ scoped_refptr<RefCountedMemory> data, |
+ bool expired, |
+ GURL icon_url) { |
+ if (know_favicon && data.get() && data->size() && data->size() > 0) { |
+ int unique_id = load_consumer_.GetClientDataForCurrentRequest(); |
+ // We need both the NavigationEntry and the model_index so we search for |
+ // the unique_index by model_index. |
+ 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) |
+ return; |
+ // Now that we have a valid NavigationEntry, decode the favicon and assign |
+ // it to the NavigationEntry. |
+ SkBitmap fav_icon; |
+ if (gfx::PNGCodec::Decode(data->front(), data->size(), &fav_icon)) { |
+ entry->favicon().set_is_valid(true); |
+ entry->favicon().set_url(icon_url); |
+ if (fav_icon.empty()) |
+ return; |
+ entry->favicon().set_bitmap(fav_icon); |
+ if (delegate_) { |
+ delegate_->OnIconChanged(model_index); |
+ } |
+ } |
+ } |
+} |
+ |
int BackForwardMenuModel::GetHistoryItemCount() const { |
TabContents* contents = GetTabContents(); |
int items = 0; |