Chromium Code Reviews| 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 "app/resource_bundle.h" |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 const int BackForwardMenuModel::kMaxHistoryItems = 12; | 21 const int BackForwardMenuModel::kMaxHistoryItems = 12; |
| 22 const int BackForwardMenuModel::kMaxChapterStops = 5; | 22 const int BackForwardMenuModel::kMaxChapterStops = 5; |
| 23 | 23 |
| 24 BackForwardMenuModel::BackForwardMenuModel(Browser* browser, | 24 BackForwardMenuModel::BackForwardMenuModel(Browser* browser, |
| 25 ModelType model_type) | 25 ModelType model_type) |
| 26 : browser_(browser), | 26 : browser_(browser), |
| 27 test_tab_contents_(NULL), | 27 test_tab_contents_(NULL), |
| 28 model_type_(model_type) { | 28 model_type_(model_type) { |
| 29 } | 29 } |
| 30 | 30 |
| 31 bool BackForwardMenuModel::HasIcons() const { | |
| 32 return true; | |
| 33 } | |
| 34 | |
| 35 int BackForwardMenuModel::GetItemCount() const { | |
| 36 return GetTotalItemCount(); | |
| 37 } | |
| 38 | |
| 39 menus::MenuModel::ItemType BackForwardMenuModel::GetTypeAt(int index) const { | |
| 40 return IsSeparator(index) ? TYPE_SEPARATOR : TYPE_COMMAND; | |
| 41 } | |
| 42 | |
| 43 int BackForwardMenuModel::GetCommandIdAt(int index) const { | |
| 44 return index; | |
| 45 } | |
| 46 | |
| 47 string16 BackForwardMenuModel::GetLabelAt(int index) const { | |
| 48 // Return label "Show Full History" for the last item of the menu. | |
| 49 if (index == GetTotalItemCount() - 1) | |
| 50 return l10n_util::GetStringUTF16(IDS_SHOWFULLHISTORY_LINK); | |
| 51 | |
| 52 // Return an empty string for a separator. | |
| 53 if (IsSeparator(index)) | |
| 54 return string16(); | |
| 55 | |
| 56 NavigationEntry* entry = GetNavigationEntry(index); | |
| 57 return entry->GetTitleForDisplay(&GetTabContents()->controller()); | |
| 58 } | |
| 59 | |
| 60 bool BackForwardMenuModel::IsLabelDynamicAt(int index) const { | |
| 61 // This object is only used for a single showing of a menu. | |
| 62 return false; | |
| 63 } | |
| 64 | |
| 65 bool BackForwardMenuModel::GetAcceleratorAt( | |
| 66 int index, | |
| 67 menus::Accelerator* accelerator) const { | |
| 68 return false; | |
| 69 } | |
| 70 | |
| 71 bool BackForwardMenuModel::IsItemCheckedAt(int index) const { | |
| 72 NOTREACHED(); | |
| 73 return false; | |
| 74 } | |
| 75 | |
| 76 int BackForwardMenuModel::GetGroupIdAt(int index) const { | |
| 77 NOTREACHED(); | |
| 78 return false; | |
| 79 } | |
| 80 | |
| 81 bool BackForwardMenuModel::GetIconAt(int index, SkBitmap* icon) const { | |
| 82 if (!ItemHasIcon(index)) | |
| 83 return false; | |
| 84 | |
| 85 if (index == GetTotalItemCount() - 1) { | |
| 86 *icon = *ResourceBundle::GetSharedInstance().GetBitmapNamed( | |
| 87 IDR_HISTORY_FAVICON); | |
| 88 } else { | |
| 89 NavigationEntry* entry = GetNavigationEntry(index); | |
| 90 *icon = entry->favicon().bitmap(); | |
| 91 } | |
| 92 | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 bool BackForwardMenuModel::IsEnabledAt(int index) const { | |
| 97 return index < GetTotalItemCount() && !IsSeparator(index); | |
|
viettrungluu
2009/12/30 00:54:26
It seems to me that |index < GetTotalItemCount()|
Evan Stade
2009/12/30 01:12:10
just copying the existing code.
| |
| 98 } | |
| 99 | |
| 100 menus::MenuModel* BackForwardMenuModel::GetSubmenuModelAt(int index) const { | |
| 101 return NULL; | |
|
viettrungluu
2009/12/30 00:54:26
NOTREACHED()?
Evan Stade
2009/12/30 01:12:10
this is not new code; it is taken from BackForward
viettrungluu
2009/12/30 01:20:34
To be clear, I'm not blaming you for the code. I j
| |
| 102 } | |
| 103 | |
| 104 void BackForwardMenuModel::HighlightChangedTo(int index) { | |
| 105 } | |
| 106 | |
| 107 void BackForwardMenuModel::ActivatedAt(int index) { | |
| 108 NavigationController& controller = GetTabContents()->controller(); | |
| 109 | |
| 110 DCHECK(!IsSeparator(index)); | |
| 111 | |
| 112 // Execute the command for the last item: "Show Full History". | |
| 113 if (index == GetTotalItemCount() - 1) { | |
| 114 UserMetrics::RecordComputedAction(BuildActionName("ShowFullHistory", -1), | |
| 115 controller.profile()); | |
| 116 browser_->ShowSingleDOMUITab(GURL(chrome::kChromeUIHistoryURL)); | |
| 117 return; | |
| 118 } | |
| 119 | |
| 120 // Log whether it was a history or chapter click. | |
| 121 if (index < GetHistoryItemCount()) { | |
| 122 UserMetrics::RecordComputedAction( | |
| 123 BuildActionName("HistoryClick", index), controller.profile()); | |
| 124 } else { | |
| 125 UserMetrics::RecordComputedAction( | |
| 126 BuildActionName("ChapterClick", index - GetHistoryItemCount() - 1), | |
| 127 controller.profile()); | |
| 128 } | |
| 129 | |
| 130 int controller_index = MenuIdToNavEntryIndex(index); | |
| 131 if (controller_index >= 0 && controller_index < controller.entry_count()) | |
| 132 controller.GoToIndex(controller_index); | |
| 133 else | |
| 134 NOTREACHED(); | |
| 135 } | |
| 136 | |
| 137 void BackForwardMenuModel::MenuWillShow() { | |
| 138 UserMetrics::RecordComputedAction(BuildActionName("Popup", -1), | |
|
viettrungluu
2009/12/30 00:54:26
Really? (I mean this in the sense that "Popup" doe
Evan Stade
2009/12/30 01:12:10
this is not new code; it is taken from BackForward
| |
| 139 browser_->profile()); | |
| 140 } | |
| 141 | |
| 142 bool BackForwardMenuModel::IsSeparator(int index) const { | |
| 143 int history_items = GetHistoryItemCount(); | |
| 144 // If the index is past the number of history items + separator, | |
| 145 // we then consider if it is a chapter-stop entry. | |
| 146 if (index > history_items) { | |
| 147 // We either are in ChapterStop area, or at the end of the list (the "Show | |
| 148 // Full History" link). | |
| 149 int chapter_stops = GetChapterStopCount(history_items); | |
| 150 if (chapter_stops == 0) | |
| 151 return false; // We must have reached the "Show Full History" link. | |
| 152 // Otherwise, look to see if we have reached the separator for the | |
| 153 // chapter-stops. If not, this is a chapter stop. | |
| 154 return (index == history_items + 1 + chapter_stops); | |
| 155 } | |
| 156 | |
| 157 // Look to see if we have reached the separator for the history items. | |
| 158 return index == history_items; | |
| 159 } | |
| 160 | |
| 31 int BackForwardMenuModel::GetHistoryItemCount() const { | 161 int BackForwardMenuModel::GetHistoryItemCount() const { |
| 32 TabContents* contents = GetTabContents(); | 162 TabContents* contents = GetTabContents(); |
| 33 int items = 0; | 163 int items = 0; |
| 34 | 164 |
| 35 if (model_type_ == FORWARD_MENU) { | 165 if (model_type_ == FORWARD_MENU) { |
| 36 // Only count items from n+1 to end (if n is current entry) | 166 // Only count items from n+1 to end (if n is current entry) |
| 37 items = contents->controller().entry_count() - | 167 items = contents->controller().entry_count() - |
| 38 contents->controller().GetCurrentEntryIndex() - 1; | 168 contents->controller().GetCurrentEntryIndex() - 1; |
| 39 } else { | 169 } else { |
| 40 items = contents->controller().GetCurrentEntryIndex(); | 170 items = contents->controller().GetCurrentEntryIndex(); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 offset *= -1; | 279 offset *= -1; |
| 150 | 280 |
| 151 TabContents* contents = GetTabContents(); | 281 TabContents* contents = GetTabContents(); |
| 152 int entry = contents->controller().GetCurrentEntryIndex() + offset; | 282 int entry = contents->controller().GetCurrentEntryIndex() + offset; |
| 153 for (int i = 0; i < skip + 1; i++) | 283 for (int i = 0; i < skip + 1; i++) |
| 154 entry = GetIndexOfNextChapterStop(entry, forward); | 284 entry = GetIndexOfNextChapterStop(entry, forward); |
| 155 | 285 |
| 156 return entry; | 286 return entry; |
| 157 } | 287 } |
| 158 | 288 |
| 159 void BackForwardMenuModel::ExecuteCommandById(int menu_id) { | 289 bool BackForwardMenuModel::ItemHasCommand(int index) const { |
| 160 TabContents* contents = GetTabContents(); | 290 return !IsSeparator(index) && index < GetTotalItemCount(); |
|
viettrungluu
2009/12/30 00:54:26
What I previously said about |index < GetTotalItem
Evan Stade
2009/12/30 01:12:10
ditto
| |
| 161 NavigationController& controller = contents->controller(); | |
| 162 | |
| 163 DCHECK(!IsSeparator(menu_id)); | |
| 164 | |
| 165 // Execute the command for the last item: "Show Full History". | |
| 166 if (menu_id == GetTotalItemCount()) { | |
| 167 UserMetrics::RecordComputedAction(BuildActionName("ShowFullHistory", -1), | |
| 168 controller.profile()); | |
| 169 browser_->ShowSingleDOMUITab(GURL(chrome::kChromeUIHistoryURL)); | |
| 170 return; | |
| 171 } | |
| 172 | |
| 173 // Log whether it was a history or chapter click. | |
| 174 if (menu_id <= GetHistoryItemCount()) { | |
| 175 UserMetrics::RecordComputedAction( | |
| 176 BuildActionName("HistoryClick", menu_id), controller.profile()); | |
| 177 } else { | |
| 178 UserMetrics::RecordComputedAction( | |
| 179 BuildActionName("ChapterClick", menu_id - GetHistoryItemCount() - 1), | |
| 180 controller.profile()); | |
| 181 } | |
| 182 | |
| 183 int index = MenuIdToNavEntryIndex(menu_id); | |
| 184 if (index >= 0 && index < controller.entry_count()) | |
| 185 controller.GoToIndex(index); | |
| 186 } | 291 } |
| 187 | 292 |
| 188 bool BackForwardMenuModel::IsSeparator(int menu_id) const { | 293 bool BackForwardMenuModel::ItemHasIcon(int index) const { |
| 189 int history_items = GetHistoryItemCount(); | 294 return index < GetTotalItemCount() && !IsSeparator(index); |
|
viettrungluu
2009/12/30 00:54:26
Ditto (and if you keep it this way, could you make
Evan Stade
2009/12/30 01:12:10
Done.
| |
| 190 // If the menu_id is higher than the number of history items + separator, | |
| 191 // we then consider if it is a chapter-stop entry. | |
| 192 if (menu_id > history_items + 1) { | |
| 193 // We either are in ChapterStop area, or at the end of the list (the "Show | |
| 194 // Full History" link). | |
| 195 int chapter_stops = GetChapterStopCount(history_items); | |
| 196 if (chapter_stops == 0) | |
| 197 return false; // We must have reached the "Show Full History" link. | |
| 198 // Otherwise, look to see if we have reached the separator for the | |
| 199 // chapter-stops. If not, this is a chapter stop. | |
| 200 return (menu_id == history_items + 1 + | |
| 201 chapter_stops + 1); | |
| 202 } | |
| 203 | |
| 204 // Look to see if we have reached the separator for the history items. | |
| 205 return menu_id == history_items + 1; | |
| 206 } | |
| 207 | |
| 208 string16 BackForwardMenuModel::GetItemLabel(int menu_id) const { | |
| 209 // Return label "Show Full History" for the last item of the menu. | |
| 210 if (menu_id == GetTotalItemCount()) | |
| 211 return l10n_util::GetStringUTF16(IDS_SHOWFULLHISTORY_LINK); | |
| 212 | |
| 213 // Return an empty string for a separator. | |
| 214 if (IsSeparator(menu_id)) | |
| 215 return string16(); | |
| 216 | |
| 217 NavigationEntry* entry = GetNavigationEntry(menu_id); | |
| 218 return entry->GetTitleForDisplay(&GetTabContents()->controller()); | |
| 219 } | |
| 220 | |
| 221 const SkBitmap& BackForwardMenuModel::GetItemIcon(int menu_id) const { | |
| 222 DCHECK(ItemHasIcon(menu_id)); | |
| 223 | |
| 224 if (menu_id == GetTotalItemCount()) { | |
| 225 return *ResourceBundle::GetSharedInstance().GetBitmapNamed( | |
| 226 IDR_HISTORY_FAVICON); | |
| 227 } | |
| 228 | |
| 229 NavigationEntry* entry = GetNavigationEntry(menu_id); | |
| 230 return entry->favicon().bitmap(); | |
| 231 } | |
| 232 | |
| 233 bool BackForwardMenuModel::ItemHasIcon(int menu_id) const { | |
| 234 return menu_id - 1 < GetTotalItemCount() && !IsSeparator(menu_id); | |
| 235 } | |
| 236 | |
| 237 bool BackForwardMenuModel::ItemHasCommand(int menu_id) const { | |
| 238 return menu_id - 1 < GetTotalItemCount() && !IsSeparator(menu_id); | |
| 239 } | 295 } |
| 240 | 296 |
| 241 string16 BackForwardMenuModel::GetShowFullHistoryLabel() const { | 297 string16 BackForwardMenuModel::GetShowFullHistoryLabel() const { |
| 242 return l10n_util::GetStringUTF16(IDS_SHOWFULLHISTORY_LINK); | 298 return l10n_util::GetStringUTF16(IDS_SHOWFULLHISTORY_LINK); |
| 243 } | 299 } |
| 244 | 300 |
| 245 TabContents* BackForwardMenuModel::GetTabContents() const { | 301 TabContents* BackForwardMenuModel::GetTabContents() const { |
| 246 // We use the test tab contents if the unit test has specified it. | 302 // We use the test tab contents if the unit test has specified it. |
| 247 return test_tab_contents_ ? test_tab_contents_ : | 303 return test_tab_contents_ ? test_tab_contents_ : |
| 248 browser_->GetSelectedTabContents(); | 304 browser_->GetSelectedTabContents(); |
| 249 } | 305 } |
| 250 | 306 |
| 251 int BackForwardMenuModel::MenuIdToNavEntryIndex(int menu_id) const { | 307 int BackForwardMenuModel::MenuIdToNavEntryIndex(int index) const { |
|
Evan Martin
2009/12/30 19:29:30
This is a bit weird, that the function name says i
Evan Stade
2009/12/30 20:32:30
will rename the function
| |
| 252 TabContents* contents = GetTabContents(); | 308 TabContents* contents = GetTabContents(); |
| 253 int history_items = GetHistoryItemCount(); | 309 int history_items = GetHistoryItemCount(); |
| 254 | 310 |
| 255 DCHECK(menu_id > 0); | 311 DCHECK_GE(index, 0); |
| 256 | 312 |
| 257 // Convert anything above the History items separator. | 313 // Convert anything above the History items separator. |
| 258 if (menu_id <= history_items) { | 314 if (index < history_items) { |
| 259 if (model_type_ == FORWARD_MENU) { | 315 if (model_type_ == FORWARD_MENU) { |
| 260 // The |menu_id| is relative to our current position, so we need to add. | 316 index += contents->controller().GetCurrentEntryIndex() + 1; |
| 261 menu_id += contents->controller().GetCurrentEntryIndex(); | |
| 262 } else { | 317 } else { |
| 263 // Back menu is reverse. | 318 // Back menu is reverse. |
| 264 menu_id = contents->controller().GetCurrentEntryIndex() - menu_id; | 319 index = contents->controller().GetCurrentEntryIndex() - (index + 1); |
| 265 } | 320 } |
| 266 return menu_id; | 321 return index; |
| 267 } | 322 } |
| 268 if (menu_id == history_items + 1) | 323 if (index == history_items) |
| 269 return -1; // Don't translate the separator for history items. | 324 return -1; // Don't translate the separator for history items. |
| 270 | 325 |
| 271 if (menu_id >= history_items + 1 + GetChapterStopCount(history_items) + 1) | 326 if (index >= history_items + 1 + GetChapterStopCount(history_items)) |
| 272 return -1; // This is beyond the last chapter stop so we abort. | 327 return -1; // This is beyond the last chapter stop so we abort. |
| 273 | 328 |
| 274 // This menu item is a chapter stop located between the two separators. | 329 // This menu item is a chapter stop located between the two separators. |
| 275 menu_id = FindChapterStop(history_items, | 330 index = FindChapterStop(history_items, |
| 276 model_type_ == FORWARD_MENU, | 331 model_type_ == FORWARD_MENU, |
| 277 menu_id - history_items - 1 - 1); | 332 index - history_items - 1); |
| 278 | 333 |
| 279 return menu_id; | 334 return index; |
| 280 } | 335 } |
| 281 | 336 |
| 282 NavigationEntry* BackForwardMenuModel::GetNavigationEntry(int menu_id) const { | 337 NavigationEntry* BackForwardMenuModel::GetNavigationEntry(int index) const { |
| 283 int index = MenuIdToNavEntryIndex(menu_id); | 338 int controller_index = MenuIdToNavEntryIndex(index); |
| 284 return GetTabContents()->controller().GetEntryAtIndex(index); | 339 NavigationController& controller = GetTabContents()->controller(); |
| 340 if (controller_index >= 0 && controller_index < controller.entry_count()) | |
| 341 return controller.GetEntryAtIndex(controller_index); | |
| 342 | |
| 343 NOTREACHED(); | |
| 344 return NULL; | |
| 285 } | 345 } |
| 286 | 346 |
| 287 std::string BackForwardMenuModel::BuildActionName( | 347 std::string BackForwardMenuModel::BuildActionName( |
| 288 const std::string& action, int index) const { | 348 const std::string& action, int index) const { |
| 289 DCHECK(!action.empty()); | 349 DCHECK(!action.empty()); |
| 290 DCHECK(index >= -1); | 350 DCHECK(index >= -1); |
| 291 std::string metric_string; | 351 std::string metric_string; |
| 292 if (model_type_ == FORWARD_MENU) | 352 if (model_type_ == FORWARD_MENU) |
| 293 metric_string += "ForwardMenu_"; | 353 metric_string += "ForwardMenu_"; |
| 294 else | 354 else |
| 295 metric_string += "BackMenu_"; | 355 metric_string += "BackMenu_"; |
| 296 metric_string += action; | 356 metric_string += action; |
| 297 if (index != -1) | 357 if (index != -1) { |
| 298 metric_string += IntToString(index); | 358 // +1 is for historical reasons (indices used to start at 1). |
| 359 metric_string += IntToString(index + 1); | |
| 360 } | |
| 299 return metric_string; | 361 return metric_string; |
| 300 } | 362 } |
| OLD | NEW |