Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "chrome/browser/ui/toolbar/recent_tabs_sub_menu_model.h" | 5 #include "chrome/browser/ui/toolbar/recent_tabs_sub_menu_model.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/prefs/scoped_user_pref_update.h" | 9 #include "base/prefs/scoped_user_pref_update.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 62 const int kFirstLocalTabCommandId = WrenchMenuModel::kMinRecentTabsCommandId; | 62 const int kFirstLocalTabCommandId = WrenchMenuModel::kMinRecentTabsCommandId; |
| 63 const int kFirstLocalWindowCommandId = 1031; | 63 const int kFirstLocalWindowCommandId = 1031; |
| 64 const int kFirstOtherDevicesTabCommandId = 1051; | 64 const int kFirstOtherDevicesTabCommandId = 1051; |
| 65 const int kMinDeviceNameCommandId = 1100; | 65 const int kMinDeviceNameCommandId = 1100; |
| 66 const int kMaxDeviceNameCommandId = 1110; | 66 const int kMaxDeviceNameCommandId = 1110; |
| 67 | 67 |
| 68 // The maximum number of local recently closed entries (tab or window) to be | 68 // The maximum number of local recently closed entries (tab or window) to be |
| 69 // shown in the menu. | 69 // shown in the menu. |
| 70 const int kMaxLocalEntries = 8; | 70 const int kMaxLocalEntries = 8; |
| 71 | 71 |
| 72 // Index of the separator that follows the history menu item. Used as a | |
| 73 // reference position for inserting local entries. | |
| 74 const int kHistorySeparatorIndex = 1; | |
| 75 | |
| 72 // Comparator function for use with std::sort that will sort sessions by | 76 // Comparator function for use with std::sort that will sort sessions by |
| 73 // descending modified_time (i.e., most recent first). | 77 // descending modified_time (i.e., most recent first). |
| 74 bool SortSessionsByRecency(const browser_sync::SyncedSession* s1, | 78 bool SortSessionsByRecency(const browser_sync::SyncedSession* s1, |
| 75 const browser_sync::SyncedSession* s2) { | 79 const browser_sync::SyncedSession* s2) { |
| 76 return s1->modified_time > s2->modified_time; | 80 return s1->modified_time > s2->modified_time; |
| 77 } | 81 } |
| 78 | 82 |
| 79 // Comparator function for use with std::sort that will sort tabs by | 83 // Comparator function for use with std::sort that will sort tabs by |
| 80 // descending timestamp (i.e., most recent first). | 84 // descending timestamp (i.e., most recent first). |
| 81 bool SortTabsByRecency(const sessions::SessionTab* t1, | 85 bool SortTabsByRecency(const sessions::SessionTab* t1, |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 const int RecentTabsSubMenuModel::kRecentlyClosedHeaderCommandId = 1120; | 168 const int RecentTabsSubMenuModel::kRecentlyClosedHeaderCommandId = 1120; |
| 165 const int RecentTabsSubMenuModel::kDisabledRecentlyClosedHeaderCommandId = 1121; | 169 const int RecentTabsSubMenuModel::kDisabledRecentlyClosedHeaderCommandId = 1121; |
| 166 | 170 |
| 167 RecentTabsSubMenuModel::RecentTabsSubMenuModel( | 171 RecentTabsSubMenuModel::RecentTabsSubMenuModel( |
| 168 ui::AcceleratorProvider* accelerator_provider, | 172 ui::AcceleratorProvider* accelerator_provider, |
| 169 Browser* browser, | 173 Browser* browser, |
| 170 browser_sync::OpenTabsUIDelegate* open_tabs_delegate) | 174 browser_sync::OpenTabsUIDelegate* open_tabs_delegate) |
| 171 : ui::SimpleMenuModel(this), | 175 : ui::SimpleMenuModel(this), |
| 172 browser_(browser), | 176 browser_(browser), |
| 173 open_tabs_delegate_(open_tabs_delegate), | 177 open_tabs_delegate_(open_tabs_delegate), |
| 174 last_local_model_index_(-1), | 178 last_local_model_index_(kHistorySeparatorIndex), |
| 175 default_favicon_(ui::ResourceBundle::GetSharedInstance(). | 179 default_favicon_(ui::ResourceBundle::GetSharedInstance(). |
| 176 GetNativeImageNamed(IDR_DEFAULT_FAVICON)), | 180 GetNativeImageNamed(IDR_DEFAULT_FAVICON)), |
| 177 weak_ptr_factory_(this) { | 181 weak_ptr_factory_(this) { |
| 178 // Invoke asynchronous call to load tabs from local last session, which does | 182 // Invoke asynchronous call to load tabs from local last session, which does |
| 179 // nothing if the tabs have already been loaded or they shouldn't be loaded. | 183 // nothing if the tabs have already been loaded or they shouldn't be loaded. |
| 180 // TabRestoreServiceChanged() will be called after the tabs are loaded. | 184 // TabRestoreServiceChanged() will be called after the tabs are loaded. |
| 181 TabRestoreService* service = | 185 TabRestoreService* service = |
| 182 TabRestoreServiceFactory::GetForProfile(browser_->profile()); | 186 TabRestoreServiceFactory::GetForProfile(browser_->profile()); |
| 183 if (service) { | 187 if (service) { |
| 184 service->LoadTabsFromLastSession(); | 188 service->LoadTabsFromLastSession(); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 203 accel_data.modifiers); | 207 accel_data.modifiers); |
| 204 break; | 208 break; |
| 205 } | 209 } |
| 206 } | 210 } |
| 207 #else | 211 #else |
| 208 if (accelerator_provider) { | 212 if (accelerator_provider) { |
| 209 accelerator_provider->GetAcceleratorForCommandId( | 213 accelerator_provider->GetAcceleratorForCommandId( |
| 210 IDC_RESTORE_TAB, &reopen_closed_tab_accelerator_); | 214 IDC_RESTORE_TAB, &reopen_closed_tab_accelerator_); |
| 211 } | 215 } |
| 212 #endif // defined(USE_ASH) | 216 #endif // defined(USE_ASH) |
| 217 | |
| 218 if (accelerator_provider) { | |
| 219 accelerator_provider->GetAcceleratorForCommandId( | |
| 220 IDC_SHOW_HISTORY, &show_history_accelerator_); | |
| 221 } | |
| 213 } | 222 } |
| 214 | 223 |
| 215 RecentTabsSubMenuModel::~RecentTabsSubMenuModel() { | 224 RecentTabsSubMenuModel::~RecentTabsSubMenuModel() { |
| 216 TabRestoreService* service = | 225 TabRestoreService* service = |
| 217 TabRestoreServiceFactory::GetForProfile(browser_->profile()); | 226 TabRestoreServiceFactory::GetForProfile(browser_->profile()); |
| 218 if (service) | 227 if (service) |
| 219 service->RemoveObserver(this); | 228 service->RemoveObserver(this); |
| 220 } | 229 } |
| 221 | 230 |
| 222 bool RecentTabsSubMenuModel::IsCommandIdChecked(int command_id) const { | 231 bool RecentTabsSubMenuModel::IsCommandIdChecked(int command_id) const { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 238 // If there are no recently closed items, we show the accelerator beside | 247 // If there are no recently closed items, we show the accelerator beside |
| 239 // the header, otherwise, we show it beside the first item underneath it. | 248 // the header, otherwise, we show it beside the first item underneath it. |
| 240 int index_in_menu = GetIndexOfCommandId(command_id); | 249 int index_in_menu = GetIndexOfCommandId(command_id); |
| 241 int header_index = GetIndexOfCommandId(kRecentlyClosedHeaderCommandId); | 250 int header_index = GetIndexOfCommandId(kRecentlyClosedHeaderCommandId); |
| 242 if ((command_id == kDisabledRecentlyClosedHeaderCommandId || | 251 if ((command_id == kDisabledRecentlyClosedHeaderCommandId || |
| 243 (header_index != -1 && index_in_menu == header_index + 1)) && | 252 (header_index != -1 && index_in_menu == header_index + 1)) && |
| 244 reopen_closed_tab_accelerator_.key_code() != ui::VKEY_UNKNOWN) { | 253 reopen_closed_tab_accelerator_.key_code() != ui::VKEY_UNKNOWN) { |
| 245 *accelerator = reopen_closed_tab_accelerator_; | 254 *accelerator = reopen_closed_tab_accelerator_; |
| 246 return true; | 255 return true; |
| 247 } | 256 } |
| 257 | |
| 258 if (command_id == IDC_SHOW_HISTORY) { | |
| 259 *accelerator = show_history_accelerator_; | |
| 260 return true; | |
| 261 } | |
| 262 | |
| 248 return false; | 263 return false; |
| 249 } | 264 } |
| 250 | 265 |
| 251 void RecentTabsSubMenuModel::ExecuteCommand(int command_id, int event_flags) { | 266 void RecentTabsSubMenuModel::ExecuteCommand(int command_id, int event_flags) { |
| 252 if (command_id == IDC_SHOW_HISTORY) { | 267 if (command_id == IDC_SHOW_HISTORY) { |
| 253 UMA_HISTOGRAM_ENUMERATION("WrenchMenu.RecentTabsSubMenu", SHOW_MORE, | 268 UMA_HISTOGRAM_ENUMERATION("WrenchMenu.RecentTabsSubMenu", SHOW_MORE, |
| 254 LIMIT_RECENT_TAB_ACTION); | 269 LIMIT_RECENT_TAB_ACTION); |
| 255 // We show all "other devices" on the history page. | 270 // We show all "other devices" on the history page. |
| 256 chrome::ExecuteCommandWithDisposition(browser_, IDC_SHOW_HISTORY, | 271 chrome::ExecuteCommandWithDisposition(browser_, IDC_SHOW_HISTORY, |
| 257 ui::DispositionFromEventFlags(event_flags)); | 272 ui::DispositionFromEventFlags(event_flags)); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 359 const TabNavigationItem& item = (*tab_items)[tab_items_idx]; | 374 const TabNavigationItem& item = (*tab_items)[tab_items_idx]; |
| 360 *url = item.url.possibly_invalid_spec(); | 375 *url = item.url.possibly_invalid_spec(); |
| 361 *title = item.title; | 376 *title = item.title; |
| 362 return true; | 377 return true; |
| 363 } | 378 } |
| 364 return false; | 379 return false; |
| 365 } | 380 } |
| 366 | 381 |
| 367 void RecentTabsSubMenuModel::Build() { | 382 void RecentTabsSubMenuModel::Build() { |
| 368 // The menu contains: | 383 // The menu contains: |
| 384 // - History to open the full history tab. | |
| 385 // - Separator | |
| 369 // - Recently closed header, then list of local recently closed tabs/windows, | 386 // - Recently closed header, then list of local recently closed tabs/windows, |
| 370 // then separator | 387 // then separator |
| 371 // - device 1 section header, then list of tabs from device, then separator | 388 // - device 1 section header, then list of tabs from device, then separator |
| 372 // - device 2 section header, then list of tabs from device, then separator | 389 // - device 2 section header, then list of tabs from device, then separator |
| 373 // - device 3 section header, then list of tabs from device, then separator | 390 // - device 3 section header, then list of tabs from device, then separator |
| 374 // - More... to open the history tab to get more other devices. | |
| 375 // |local_tab_navigation_items_| and |other_devices_tab_navigation_items_| | 391 // |local_tab_navigation_items_| and |other_devices_tab_navigation_items_| |
| 376 // only contain navigatable (and hence executable) tab items for local | 392 // only contain navigatable (and hence executable) tab items for local |
| 377 // recently closed tabs and tabs from other devices respectively. | 393 // recently closed tabs and tabs from other devices respectively. |
| 378 // |local_window_items_| contains the local recently closed windows. | 394 // |local_window_items_| contains the local recently closed windows. |
| 395 InsertItemWithStringIdAt(0, IDC_SHOW_HISTORY, IDS_SHOW_HISTORY); | |
| 396 InsertSeparatorAt(1, ui::NORMAL_SEPARATOR); | |
|
Peter Kasting
2015/06/22 23:14:24
Can we ever have no items from both the below call
edwardjung
2015/06/24 12:41:59
No items in both cases would result in the followi
| |
| 379 BuildLocalEntries(); | 397 BuildLocalEntries(); |
| 380 BuildTabsFromOtherDevices(); | 398 BuildTabsFromOtherDevices(); |
| 381 } | 399 } |
| 382 | 400 |
| 383 void RecentTabsSubMenuModel::BuildLocalEntries() { | 401 void RecentTabsSubMenuModel::BuildLocalEntries() { |
| 402 last_local_model_index_ = kHistorySeparatorIndex; | |
| 403 | |
| 384 // All local items use InsertItem*At() to append or insert a menu item. | 404 // All local items use InsertItem*At() to append or insert a menu item. |
| 385 // We're appending if building the entries for the first time i.e. invoked | 405 // We're appending if building the entries for the first time i.e. invoked |
| 386 // from Constructor(), inserting when local entries change subsequently i.e. | 406 // from Constructor(), inserting when local entries change subsequently i.e. |
| 387 // invoked from TabRestoreServiceChanged(). | 407 // invoked from TabRestoreServiceChanged(). |
| 388 | |
| 389 DCHECK_EQ(last_local_model_index_, -1); | |
| 390 | |
| 391 TabRestoreService* service = | 408 TabRestoreService* service = |
| 392 TabRestoreServiceFactory::GetForProfile(browser_->profile()); | 409 TabRestoreServiceFactory::GetForProfile(browser_->profile()); |
| 410 | |
| 393 if (!service || service->entries().size() == 0) { | 411 if (!service || service->entries().size() == 0) { |
| 394 // This is to show a disabled restore tab entry with the accelerator to | 412 // This is to show a disabled restore tab entry with the accelerator to |
| 395 // teach users about this command. | 413 // teach users about this command. |
| 396 InsertItemWithStringIdAt(++last_local_model_index_, | 414 InsertItemWithStringIdAt(++last_local_model_index_, |
| 397 kDisabledRecentlyClosedHeaderCommandId, | 415 kDisabledRecentlyClosedHeaderCommandId, |
| 398 IDS_RECENTLY_CLOSED); | 416 IDS_RECENTLY_CLOSED); |
| 399 } else { | 417 } else { |
| 400 InsertItemWithStringIdAt(++last_local_model_index_, | 418 InsertItemWithStringIdAt(++last_local_model_index_, |
| 401 kRecentlyClosedHeaderCommandId, | 419 kRecentlyClosedHeaderCommandId, |
| 402 IDS_RECENTLY_CLOSED); | 420 IDS_RECENTLY_CLOSED); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 422 } else { | 440 } else { |
| 423 DCHECK_EQ(entry->type, TabRestoreService::WINDOW); | 441 DCHECK_EQ(entry->type, TabRestoreService::WINDOW); |
| 424 BuildLocalWindowItem( | 442 BuildLocalWindowItem( |
| 425 entry->id, | 443 entry->id, |
| 426 static_cast<TabRestoreService::Window*>(entry)->tabs.size(), | 444 static_cast<TabRestoreService::Window*>(entry)->tabs.size(), |
| 427 ++last_local_model_index_); | 445 ++last_local_model_index_); |
| 428 } | 446 } |
| 429 ++added_count; | 447 ++added_count; |
| 430 } | 448 } |
| 431 } | 449 } |
| 432 | |
| 433 DCHECK_GE(last_local_model_index_, 0); | 450 DCHECK_GE(last_local_model_index_, 0); |
| 434 } | 451 } |
| 435 | 452 |
| 436 void RecentTabsSubMenuModel::BuildTabsFromOtherDevices() { | 453 void RecentTabsSubMenuModel::BuildTabsFromOtherDevices() { |
| 437 // All other devices' items (device headers or tabs) use AddItem*() to append | 454 // All other devices' items (device headers or tabs) use AddItem*() to append |
| 438 // a menu item, because they are always only built once (i.e. invoked from | 455 // a menu item, because they are always only built once (i.e. invoked from |
| 439 // Constructor()) and don't change after that. | 456 // Constructor()) and don't change after that. |
| 440 | 457 |
| 441 browser_sync::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(); | 458 browser_sync::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(); |
| 442 std::vector<const browser_sync::SyncedSession*> sessions; | 459 std::vector<const browser_sync::SyncedSession*> sessions; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 502 k < std::min(tabs_in_session.size(), kMaxTabsPerSessionToShow); | 519 k < std::min(tabs_in_session.size(), kMaxTabsPerSessionToShow); |
| 503 ++k) { | 520 ++k) { |
| 504 BuildOtherDevicesTabItem(session_tag, *tabs_in_session[k]); | 521 BuildOtherDevicesTabItem(session_tag, *tabs_in_session[k]); |
| 505 } // for all tabs in one session | 522 } // for all tabs in one session |
| 506 | 523 |
| 507 ++num_sessions_added; | 524 ++num_sessions_added; |
| 508 } // for all sessions | 525 } // for all sessions |
| 509 | 526 |
| 510 // We are not supposed to get here unless at least some items were added. | 527 // We are not supposed to get here unless at least some items were added. |
| 511 DCHECK_GT(GetItemCount(), 0); | 528 DCHECK_GT(GetItemCount(), 0); |
| 512 AddSeparator(ui::NORMAL_SEPARATOR); | |
| 513 AddItemWithStringId(IDC_SHOW_HISTORY, IDS_RECENT_TABS_MORE); | |
| 514 } | 529 } |
| 515 | 530 |
| 516 void RecentTabsSubMenuModel::BuildLocalTabItem(int session_id, | 531 void RecentTabsSubMenuModel::BuildLocalTabItem(int session_id, |
| 517 const base::string16& title, | 532 const base::string16& title, |
| 518 const GURL& url, | 533 const GURL& url, |
| 519 int curr_model_index) { | 534 int curr_model_index) { |
| 520 TabNavigationItem item(std::string(), session_id, title, url); | 535 TabNavigationItem item(std::string(), session_id, title, url); |
| 521 int command_id = TabVectorIndexToCommandId( | 536 int command_id = TabVectorIndexToCommandId( |
| 522 local_tab_navigation_items_.size(), kFirstLocalTabCommandId); | 537 local_tab_navigation_items_.size(), kFirstLocalTabCommandId); |
| 523 // See comments in BuildLocalEntries() about usage of InsertItem*At(). | 538 // See comments in BuildLocalEntries() about usage of InsertItem*At(). |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 647 if (command_id >= kFirstOtherDevicesTabCommandId) { | 662 if (command_id >= kFirstOtherDevicesTabCommandId) { |
| 648 *tab_items = &other_devices_tab_navigation_items_; | 663 *tab_items = &other_devices_tab_navigation_items_; |
| 649 return command_id - kFirstOtherDevicesTabCommandId; | 664 return command_id - kFirstOtherDevicesTabCommandId; |
| 650 } | 665 } |
| 651 *tab_items = &local_tab_navigation_items_; | 666 *tab_items = &local_tab_navigation_items_; |
| 652 return command_id - kFirstLocalTabCommandId; | 667 return command_id - kFirstLocalTabCommandId; |
| 653 } | 668 } |
| 654 | 669 |
| 655 void RecentTabsSubMenuModel::ClearLocalEntries() { | 670 void RecentTabsSubMenuModel::ClearLocalEntries() { |
| 656 // Remove local items (recently closed tabs and windows) from menumodel. | 671 // Remove local items (recently closed tabs and windows) from menumodel. |
| 657 while (last_local_model_index_ >= 0) | 672 while (last_local_model_index_ > kHistorySeparatorIndex) |
| 658 RemoveItemAt(last_local_model_index_--); | 673 RemoveItemAt(last_local_model_index_--); |
| 659 | 674 |
| 660 // Cancel asynchronous FaviconService::GetFaviconImageForPageURL() tasks of | 675 // Cancel asynchronous FaviconService::GetFaviconImageForPageURL() tasks of |
| 661 // all | 676 // all local tabs. |
| 662 // local tabs. | |
| 663 local_tab_cancelable_task_tracker_.TryCancelAll(); | 677 local_tab_cancelable_task_tracker_.TryCancelAll(); |
| 664 | 678 |
| 665 // Remove all local tab navigation items. | 679 // Remove all local tab navigation items. |
| 666 local_tab_navigation_items_.clear(); | 680 local_tab_navigation_items_.clear(); |
| 667 | 681 |
| 668 // Remove all local window items. | 682 // Remove all local window items. |
| 669 local_window_items_.clear(); | 683 local_window_items_.clear(); |
| 670 } | 684 } |
| 671 | 685 |
| 672 browser_sync::OpenTabsUIDelegate* | 686 browser_sync::OpenTabsUIDelegate* |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 689 | 703 |
| 690 ui::MenuModelDelegate* menu_model_delegate = GetMenuModelDelegate(); | 704 ui::MenuModelDelegate* menu_model_delegate = GetMenuModelDelegate(); |
| 691 if (menu_model_delegate) | 705 if (menu_model_delegate) |
| 692 menu_model_delegate->OnMenuStructureChanged(); | 706 menu_model_delegate->OnMenuStructureChanged(); |
| 693 } | 707 } |
| 694 | 708 |
| 695 void RecentTabsSubMenuModel::TabRestoreServiceDestroyed( | 709 void RecentTabsSubMenuModel::TabRestoreServiceDestroyed( |
| 696 TabRestoreService* service) { | 710 TabRestoreService* service) { |
| 697 TabRestoreServiceChanged(service); | 711 TabRestoreServiceChanged(service); |
| 698 } | 712 } |
| OLD | NEW |