Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: chrome/browser/ui/toolbar/back_forward_menu_model.cc

Issue 2928005: Retrieve favicons from history as NavigationEntries are converted from TabNav... Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Updated to compile against trunk. Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #include "chrome/browser/ui/toolbar/back_forward_menu_model.h" 7 #include "chrome/browser/ui/toolbar/back_forward_menu_model.h"
8 8
9 #include "base/string_number_conversions.h" 9 #include "base/string_number_conversions.h"
10 #include "chrome/browser/metrics/user_metrics.h" 10 #include "chrome/browser/metrics/user_metrics.h"
11 #include "chrome/browser/tab_contents/navigation_controller.h" 11 #include "chrome/browser/tab_contents/navigation_controller.h"
12 #include "chrome/browser/tab_contents/navigation_entry.h" 12 #include "chrome/browser/tab_contents/navigation_entry.h"
13 #include "chrome/browser/tab_contents/tab_contents.h" 13 #include "chrome/browser/tab_contents/tab_contents.h"
14 #include "chrome/browser/prefs/pref_service.h" 14 #include "chrome/browser/prefs/pref_service.h"
15 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h" 16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/common/pref_names.h" 17 #include "chrome/common/pref_names.h"
18 #include "chrome/common/url_constants.h" 18 #include "chrome/common/url_constants.h"
19 #include "grit/generated_resources.h" 19 #include "grit/generated_resources.h"
20 #include "grit/theme_resources.h" 20 #include "grit/theme_resources.h"
21 #include "net/base/registry_controlled_domain.h" 21 #include "net/base/registry_controlled_domain.h"
22 #include "ui/base/l10n/l10n_util.h" 22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/resource/resource_bundle.h" 23 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/base/text/text_elider.h" 24 #include "ui/base/text/text_elider.h"
25 #include "ui/gfx/codec/png_codec.h"
25 26
26 const int BackForwardMenuModel::kMaxHistoryItems = 12; 27 const int BackForwardMenuModel::kMaxHistoryItems = 12;
27 const int BackForwardMenuModel::kMaxChapterStops = 5; 28 const int BackForwardMenuModel::kMaxChapterStops = 5;
28 static const int kMaxWidth = 700; 29 static const int kMaxWidth = 700;
29 30
30 BackForwardMenuModel::BackForwardMenuModel(Browser* browser, 31 BackForwardMenuModel::BackForwardMenuModel(Browser* browser,
31 ModelType model_type) 32 ModelType model_type)
32 : browser_(browser), 33 : browser_(browser),
33 test_tab_contents_(NULL), 34 test_tab_contents_(NULL),
34 model_type_(model_type) { 35 model_type_(model_type),
36 delegate_(NULL) {
35 } 37 }
36 38
37 bool BackForwardMenuModel::HasIcons() const { 39 bool BackForwardMenuModel::HasIcons() const {
38 return true; 40 return true;
39 } 41 }
40 42
41 int BackForwardMenuModel::GetItemCount() const { 43 int BackForwardMenuModel::GetItemCount() const {
42 int items = GetHistoryItemCount(); 44 int items = GetHistoryItemCount();
43 45
44 if (items > 0) { 46 if (items > 0) {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 } 105 }
104 106
105 bool BackForwardMenuModel::IsItemCheckedAt(int index) const { 107 bool BackForwardMenuModel::IsItemCheckedAt(int index) const {
106 return false; 108 return false;
107 } 109 }
108 110
109 int BackForwardMenuModel::GetGroupIdAt(int index) const { 111 int BackForwardMenuModel::GetGroupIdAt(int index) const {
110 return false; 112 return false;
111 } 113 }
112 114
113 bool BackForwardMenuModel::GetIconAt(int index, SkBitmap* icon) const { 115 bool BackForwardMenuModel::GetIconAt(int index, SkBitmap* icon) {
114 if (!ItemHasIcon(index)) 116 if (!ItemHasIcon(index))
115 return false; 117 return false;
116 118
117 if (index == GetItemCount() - 1) { 119 if (index == GetItemCount() - 1) {
118 *icon = *ResourceBundle::GetSharedInstance().GetBitmapNamed( 120 *icon = *ResourceBundle::GetSharedInstance().GetBitmapNamed(
119 IDR_HISTORY_FAVICON); 121 IDR_HISTORY_FAVICON);
120 } else { 122 } else {
121 NavigationEntry* entry = GetNavigationEntry(index); 123 NavigationEntry* entry = GetNavigationEntry(index);
122 *icon = entry->favicon().bitmap(); 124 *icon = entry->favicon().bitmap();
125 if (!entry->favicon().is_valid()) {
126 FetchFavicon(entry, index);
127 }
123 } 128 }
124 129
125 return true; 130 return true;
126 } 131 }
127 132
128 ui::ButtonMenuItemModel* BackForwardMenuModel::GetButtonMenuItemAt( 133 ui::ButtonMenuItemModel* BackForwardMenuModel::GetButtonMenuItemAt(
129 int index) const { 134 int index) const {
130 return NULL; 135 return NULL;
131 } 136 }
132 137
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 int controller_index = MenuIndexToNavEntryIndex(index); 177 int controller_index = MenuIndexToNavEntryIndex(index);
173 if (!browser_->NavigateToIndexWithDisposition( 178 if (!browser_->NavigateToIndexWithDisposition(
174 controller_index, static_cast<WindowOpenDisposition>(disposition))) { 179 controller_index, static_cast<WindowOpenDisposition>(disposition))) {
175 NOTREACHED(); 180 NOTREACHED();
176 } 181 }
177 } 182 }
178 183
179 void BackForwardMenuModel::MenuWillShow() { 184 void BackForwardMenuModel::MenuWillShow() {
180 UserMetrics::RecordComputedAction(BuildActionName("Popup", -1), 185 UserMetrics::RecordComputedAction(BuildActionName("Popup", -1),
181 browser_->profile()); 186 browser_->profile());
187 requested_favicons_.clear();
188 load_consumer_.CancelAllRequests();
182 } 189 }
183 190
184 bool BackForwardMenuModel::IsSeparator(int index) const { 191 bool BackForwardMenuModel::IsSeparator(int index) const {
185 int history_items = GetHistoryItemCount(); 192 int history_items = GetHistoryItemCount();
186 // If the index is past the number of history items + separator, 193 // If the index is past the number of history items + separator,
187 // we then consider if it is a chapter-stop entry. 194 // we then consider if it is a chapter-stop entry.
188 if (index > history_items) { 195 if (index > history_items) {
189 // We either are in ChapterStop area, or at the end of the list (the "Show 196 // We either are in ChapterStop area, or at the end of the list (the "Show
190 // Full History" link). 197 // Full History" link).
191 int chapter_stops = GetChapterStopCount(history_items); 198 int chapter_stops = GetChapterStopCount(history_items);
192 if (chapter_stops == 0) 199 if (chapter_stops == 0)
193 return false; // We must have reached the "Show Full History" link. 200 return false; // We must have reached the "Show Full History" link.
194 // Otherwise, look to see if we have reached the separator for the 201 // Otherwise, look to see if we have reached the separator for the
195 // chapter-stops. If not, this is a chapter stop. 202 // chapter-stops. If not, this is a chapter stop.
196 return (index == history_items + 1 + chapter_stops); 203 return (index == history_items + 1 + chapter_stops);
197 } 204 }
198 205
199 // Look to see if we have reached the separator for the history items. 206 // Look to see if we have reached the separator for the history items.
200 return index == history_items; 207 return index == history_items;
201 } 208 }
202 209
210 void BackForwardMenuModel::SetDelegate(Delegate* delegate) {
211 delegate_ = delegate;
212 }
213
214 void BackForwardMenuModel::FetchFavicon(NavigationEntry* entry, int index) {
sky 2011/02/22 18:28:28 It doesn't look like you use index here, can it be
dill 2011/02/24 15:00:28 Yes, that was a leftover from when unique_id wasn'
215 // If the favicon has already been requested for this menu, don't do
216 // anything.
217 if (requested_favicons_.find(entry->unique_id()) !=
218 requested_favicons_.end()) {
219 return;
220 }
221 requested_favicons_.insert(entry->unique_id());
222 FaviconService* favicon_service =
223 browser_->profile()->GetFaviconService(Profile::EXPLICIT_ACCESS);
224 if (!favicon_service)
225 return;
226 FaviconService::Handle handle = favicon_service->GetFaviconForURL(
227 entry->url(), &load_consumer_,
228 NewCallback(this, &BackForwardMenuModel::OnFavIconDataAvailable));
229 load_consumer_.SetClientData(favicon_service, handle, entry->unique_id());
230 }
231
232 void BackForwardMenuModel::OnFavIconDataAvailable(
233 FaviconService::Handle handle,
234 bool know_favicon,
235 scoped_refptr<RefCountedMemory> data,
236 bool expired,
237 GURL icon_url) {
238 if (know_favicon && data.get() && data->size() && data->size() > 0) {
sky 2011/02/22 18:28:28 How come there is data->size() && data->size() > 0
dill 2011/02/24 15:00:28 data->size() > 0 was from an earlier code review,
239 int unique_id = load_consumer_.GetClientDataForCurrentRequest();
240 // We need both the NavigationEntry and the model_index so we search for
sky 2011/02/22 18:28:28 This comment isn't too helpful. The useful comment
dill 2011/02/24 15:00:28 How about here I say "Find the current model_index
sky 2011/02/24 15:12:50 Sounds good.
241 // the unique_index by model_index.
242 NavigationEntry* entry = NULL;
243 int model_index = -1;
244 for (int i = 0; i < GetItemCount() - 1; i++) {
245 if (IsSeparator(i))
246 continue;
247 if (GetNavigationEntry(i)->unique_id() == unique_id) {
248 model_index = i;
249 entry = GetNavigationEntry(i);
250 break;
251 }
252 }
253 if (!entry)
254 return;
255 // Now that we have a valid NavigationEntry, decode the favicon and assign
256 // it to the NavigationEntry.
257 SkBitmap fav_icon;
258 if (gfx::PNGCodec::Decode(data->front(), data->size(), &fav_icon)) {
259 entry->favicon().set_is_valid(true);
260 entry->favicon().set_url(icon_url);
261 if (fav_icon.empty())
262 return;
263 entry->favicon().set_bitmap(fav_icon);
264 if (delegate_) {
265 delegate_->OnIconChanged(model_index);
266 }
267 }
268 }
269 }
270
203 int BackForwardMenuModel::GetHistoryItemCount() const { 271 int BackForwardMenuModel::GetHistoryItemCount() const {
204 TabContents* contents = GetTabContents(); 272 TabContents* contents = GetTabContents();
205 int items = 0; 273 int items = 0;
206 274
207 if (model_type_ == FORWARD_MENU) { 275 if (model_type_ == FORWARD_MENU) {
208 // Only count items from n+1 to end (if n is current entry) 276 // Only count items from n+1 to end (if n is current entry)
209 items = contents->controller().entry_count() - 277 items = contents->controller().entry_count() -
210 contents->controller().GetCurrentEntryIndex() - 1; 278 contents->controller().GetCurrentEntryIndex() - 1;
211 } else { 279 } else {
212 items = contents->controller().GetCurrentEntryIndex(); 280 items = contents->controller().GetCurrentEntryIndex();
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 metric_string += "ForwardMenu_"; 442 metric_string += "ForwardMenu_";
375 else 443 else
376 metric_string += "BackMenu_"; 444 metric_string += "BackMenu_";
377 metric_string += action; 445 metric_string += action;
378 if (index != -1) { 446 if (index != -1) {
379 // +1 is for historical reasons (indices used to start at 1). 447 // +1 is for historical reasons (indices used to start at 1).
380 metric_string += base::IntToString(index + 1); 448 metric_string += base::IntToString(index + 1);
381 } 449 }
382 return metric_string; 450 return metric_string;
383 } 451 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698