OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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/back_forward_menu_model.h" | 7 #include "chrome/browser/back_forward_menu_model.h" |
8 | 8 |
9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
| 10 #include "app/resource_bundle.h" |
10 #include "chrome/browser/browser.h" | 11 #include "chrome/browser/browser.h" |
11 #include "chrome/browser/metrics/user_metrics.h" | 12 #include "chrome/browser/metrics/user_metrics.h" |
12 #include "chrome/browser/tab_contents/navigation_controller.h" | 13 #include "chrome/browser/tab_contents/navigation_controller.h" |
13 #include "chrome/browser/tab_contents/navigation_entry.h" | 14 #include "chrome/browser/tab_contents/navigation_entry.h" |
14 #include "chrome/browser/tab_contents/tab_contents.h" | 15 #include "chrome/browser/tab_contents/tab_contents.h" |
15 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
16 #include "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
| 18 #include "grit/theme_resources.h" |
17 #include "net/base/registry_controlled_domain.h" | 19 #include "net/base/registry_controlled_domain.h" |
18 | 20 |
19 const int BackForwardMenuModel::kMaxHistoryItems = 12; | 21 const int BackForwardMenuModel::kMaxHistoryItems = 12; |
20 const int BackForwardMenuModel::kMaxChapterStops = 5; | 22 const int BackForwardMenuModel::kMaxChapterStops = 5; |
21 | 23 |
| 24 BackForwardMenuModel::BackForwardMenuModel(Browser* browser, |
| 25 ModelType model_type) |
| 26 : browser_(browser), |
| 27 test_tab_contents_(NULL), |
| 28 model_type_(model_type) { |
| 29 } |
| 30 |
22 int BackForwardMenuModel::GetHistoryItemCount() const { | 31 int BackForwardMenuModel::GetHistoryItemCount() const { |
23 TabContents* contents = GetTabContents(); | 32 TabContents* contents = GetTabContents(); |
24 int items = 0; | 33 int items = 0; |
25 | 34 |
26 if (model_type_ == FORWARD_MENU_DELEGATE) { | 35 if (model_type_ == FORWARD_MENU) { |
27 // Only count items from n+1 to end (if n is current entry) | 36 // Only count items from n+1 to end (if n is current entry) |
28 items = contents->controller().entry_count() - | 37 items = contents->controller().entry_count() - |
29 contents->controller().GetCurrentEntryIndex() - 1; | 38 contents->controller().GetCurrentEntryIndex() - 1; |
30 } else { | 39 } else { |
31 items = contents->controller().GetCurrentEntryIndex(); | 40 items = contents->controller().GetCurrentEntryIndex(); |
32 } | 41 } |
33 | 42 |
34 if (items > kMaxHistoryItems) | 43 if (items > kMaxHistoryItems) |
35 items = kMaxHistoryItems; | 44 items = kMaxHistoryItems; |
36 else if (items < 0) | 45 else if (items < 0) |
37 items = 0; | 46 items = 0; |
38 | 47 |
39 return items; | 48 return items; |
40 } | 49 } |
41 | 50 |
42 int BackForwardMenuModel::GetChapterStopCount(int history_items) const { | 51 int BackForwardMenuModel::GetChapterStopCount(int history_items) const { |
43 TabContents* contents = GetTabContents(); | 52 TabContents* contents = GetTabContents(); |
44 | 53 |
45 int chapter_stops = 0; | 54 int chapter_stops = 0; |
46 int current_entry = contents->controller().GetCurrentEntryIndex(); | 55 int current_entry = contents->controller().GetCurrentEntryIndex(); |
47 | 56 |
48 if (history_items == kMaxHistoryItems) { | 57 if (history_items == kMaxHistoryItems) { |
49 int chapter_id = current_entry; | 58 int chapter_id = current_entry; |
50 if (model_type_ == FORWARD_MENU_DELEGATE) { | 59 if (model_type_ == FORWARD_MENU) { |
51 chapter_id += history_items; | 60 chapter_id += history_items; |
52 } else { | 61 } else { |
53 chapter_id -= history_items; | 62 chapter_id -= history_items; |
54 } | 63 } |
55 | 64 |
56 do { | 65 do { |
57 chapter_id = GetIndexOfNextChapterStop(chapter_id, | 66 chapter_id = GetIndexOfNextChapterStop(chapter_id, |
58 model_type_ == FORWARD_MENU_DELEGATE); | 67 model_type_ == FORWARD_MENU); |
59 if (chapter_id != -1) | 68 if (chapter_id != -1) |
60 ++chapter_stops; | 69 ++chapter_stops; |
61 } while (chapter_id != -1 && chapter_stops < kMaxChapterStops); | 70 } while (chapter_id != -1 && chapter_stops < kMaxChapterStops); |
62 } | 71 } |
63 | 72 |
64 return chapter_stops; | 73 return chapter_stops; |
65 } | 74 } |
66 | 75 |
67 int BackForwardMenuModel::GetTotalItemCount() const { | 76 int BackForwardMenuModel::GetTotalItemCount() const { |
68 int items = GetHistoryItemCount(); | 77 int items = GetHistoryItemCount(); |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 return L""; | 215 return L""; |
207 | 216 |
208 NavigationEntry* entry = GetNavigationEntry(menu_id); | 217 NavigationEntry* entry = GetNavigationEntry(menu_id); |
209 return UTF16ToWideHack(entry->GetTitleForDisplay( | 218 return UTF16ToWideHack(entry->GetTitleForDisplay( |
210 &GetTabContents()->controller())); | 219 &GetTabContents()->controller())); |
211 } | 220 } |
212 | 221 |
213 const SkBitmap& BackForwardMenuModel::GetItemIcon(int menu_id) const { | 222 const SkBitmap& BackForwardMenuModel::GetItemIcon(int menu_id) const { |
214 DCHECK(ItemHasIcon(menu_id)); | 223 DCHECK(ItemHasIcon(menu_id)); |
215 | 224 |
| 225 if (menu_id == GetTotalItemCount()) { |
| 226 return *ResourceBundle::GetSharedInstance().GetBitmapNamed( |
| 227 IDR_HISTORY_FAVICON); |
| 228 } |
| 229 |
216 NavigationEntry* entry = GetNavigationEntry(menu_id); | 230 NavigationEntry* entry = GetNavigationEntry(menu_id); |
217 return entry->favicon().bitmap(); | 231 return entry->favicon().bitmap(); |
218 } | 232 } |
219 | 233 |
220 bool BackForwardMenuModel::ItemHasIcon(int menu_id) const { | 234 bool BackForwardMenuModel::ItemHasIcon(int menu_id) const { |
221 // Using "id" not "id - 1" because the last item "Show Full History" | 235 return menu_id - 1 < GetTotalItemCount() && !IsSeparator(menu_id); |
222 // doesn't have an icon. | |
223 return menu_id < GetTotalItemCount() && !IsSeparator(menu_id); | |
224 } | 236 } |
225 | 237 |
226 bool BackForwardMenuModel::ItemHasCommand(int menu_id) const { | 238 bool BackForwardMenuModel::ItemHasCommand(int menu_id) const { |
227 return menu_id - 1 < GetTotalItemCount() && !IsSeparator(menu_id); | 239 return menu_id - 1 < GetTotalItemCount() && !IsSeparator(menu_id); |
228 } | 240 } |
229 | 241 |
230 std::wstring BackForwardMenuModel::GetShowFullHistoryLabel() const { | 242 std::wstring BackForwardMenuModel::GetShowFullHistoryLabel() const { |
231 return l10n_util::GetString(IDS_SHOWFULLHISTORY_LINK); | 243 return l10n_util::GetString(IDS_SHOWFULLHISTORY_LINK); |
232 } | 244 } |
233 | 245 |
234 TabContents* BackForwardMenuModel::GetTabContents() const { | 246 TabContents* BackForwardMenuModel::GetTabContents() const { |
235 // We use the test tab contents if the unit test has specified it. | 247 // We use the test tab contents if the unit test has specified it. |
236 return test_tab_contents_ ? test_tab_contents_ : | 248 return test_tab_contents_ ? test_tab_contents_ : |
237 browser_->GetSelectedTabContents(); | 249 browser_->GetSelectedTabContents(); |
238 } | 250 } |
239 | 251 |
240 int BackForwardMenuModel::MenuIdToNavEntryIndex(int menu_id) const { | 252 int BackForwardMenuModel::MenuIdToNavEntryIndex(int menu_id) const { |
241 TabContents* contents = GetTabContents(); | 253 TabContents* contents = GetTabContents(); |
242 int history_items = GetHistoryItemCount(); | 254 int history_items = GetHistoryItemCount(); |
243 | 255 |
244 DCHECK(menu_id > 0); | 256 DCHECK(menu_id > 0); |
245 | 257 |
246 // Convert anything above the History items separator. | 258 // Convert anything above the History items separator. |
247 if (menu_id <= history_items) { | 259 if (menu_id <= history_items) { |
248 if (model_type_ == FORWARD_MENU_DELEGATE) { | 260 if (model_type_ == FORWARD_MENU) { |
249 // The |menu_id| is relative to our current position, so we need to add. | 261 // The |menu_id| is relative to our current position, so we need to add. |
250 menu_id += contents->controller().GetCurrentEntryIndex(); | 262 menu_id += contents->controller().GetCurrentEntryIndex(); |
251 } else { | 263 } else { |
252 // Back menu is reverse. | 264 // Back menu is reverse. |
253 menu_id = contents->controller().GetCurrentEntryIndex() - menu_id; | 265 menu_id = contents->controller().GetCurrentEntryIndex() - menu_id; |
254 } | 266 } |
255 return menu_id; | 267 return menu_id; |
256 } | 268 } |
257 if (menu_id == history_items + 1) | 269 if (menu_id == history_items + 1) |
258 return -1; // Don't translate the separator for history items. | 270 return -1; // Don't translate the separator for history items. |
259 | 271 |
260 if (menu_id >= history_items + 1 + GetChapterStopCount(history_items) + 1) | 272 if (menu_id >= history_items + 1 + GetChapterStopCount(history_items) + 1) |
261 return -1; // This is beyond the last chapter stop so we abort. | 273 return -1; // This is beyond the last chapter stop so we abort. |
262 | 274 |
263 // This menu item is a chapter stop located between the two separators. | 275 // This menu item is a chapter stop located between the two separators. |
264 menu_id = FindChapterStop(history_items, | 276 menu_id = FindChapterStop(history_items, |
265 model_type_ == FORWARD_MENU_DELEGATE, | 277 model_type_ == FORWARD_MENU, |
266 menu_id - history_items - 1 - 1); | 278 menu_id - history_items - 1 - 1); |
267 | 279 |
268 return menu_id; | 280 return menu_id; |
269 } | 281 } |
270 | 282 |
271 NavigationEntry* BackForwardMenuModel::GetNavigationEntry(int menu_id) const { | 283 NavigationEntry* BackForwardMenuModel::GetNavigationEntry(int menu_id) const { |
272 int index = MenuIdToNavEntryIndex(menu_id); | 284 int index = MenuIdToNavEntryIndex(menu_id); |
273 return GetTabContents()->controller().GetEntryAtIndex(index); | 285 return GetTabContents()->controller().GetEntryAtIndex(index); |
274 } | 286 } |
275 | 287 |
276 std::wstring BackForwardMenuModel::BuildActionName( | 288 std::wstring BackForwardMenuModel::BuildActionName( |
277 const std::wstring& action, int index) const { | 289 const std::wstring& action, int index) const { |
278 DCHECK(!action.empty()); | 290 DCHECK(!action.empty()); |
279 DCHECK(index >= -1); | 291 DCHECK(index >= -1); |
280 std::wstring metric_string; | 292 std::wstring metric_string; |
281 if (model_type_ == FORWARD_MENU_DELEGATE) | 293 if (model_type_ == FORWARD_MENU) |
282 metric_string += L"ForwardMenu_"; | 294 metric_string += L"ForwardMenu_"; |
283 else | 295 else |
284 metric_string += L"BackMenu_"; | 296 metric_string += L"BackMenu_"; |
285 metric_string += action; | 297 metric_string += action; |
286 if (index != -1) | 298 if (index != -1) |
287 metric_string += IntToWString(index); | 299 metric_string += IntToWString(index); |
288 return metric_string; | 300 return metric_string; |
289 } | 301 } |
OLD | NEW |