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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 SessionID::id_type tab_id; // -1 for invalid, >= 0 otherwise. | 172 SessionID::id_type tab_id; // -1 for invalid, >= 0 otherwise. |
173 base::string16 title; | 173 base::string16 title; |
174 GURL url; | 174 GURL url; |
175 }; | 175 }; |
176 | 176 |
177 const int RecentTabsSubMenuModel::kRecentlyClosedHeaderCommandId = 1120; | 177 const int RecentTabsSubMenuModel::kRecentlyClosedHeaderCommandId = 1120; |
178 const int RecentTabsSubMenuModel::kDisabledRecentlyClosedHeaderCommandId = 1121; | 178 const int RecentTabsSubMenuModel::kDisabledRecentlyClosedHeaderCommandId = 1121; |
179 | 179 |
180 RecentTabsSubMenuModel::RecentTabsSubMenuModel( | 180 RecentTabsSubMenuModel::RecentTabsSubMenuModel( |
181 ui::AcceleratorProvider* accelerator_provider, | 181 ui::AcceleratorProvider* accelerator_provider, |
182 Browser* browser, | 182 Browser* browser) |
183 sync_sessions::OpenTabsUIDelegate* open_tabs_delegate) | |
184 : ui::SimpleMenuModel(this), | 183 : ui::SimpleMenuModel(this), |
185 browser_(browser), | 184 browser_(browser), |
186 open_tabs_delegate_(open_tabs_delegate), | |
187 last_local_model_index_(kHistorySeparatorIndex), | 185 last_local_model_index_(kHistorySeparatorIndex), |
188 default_favicon_( | 186 default_favicon_( |
189 ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed( | 187 ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed( |
190 IDR_DEFAULT_FAVICON)), | 188 IDR_DEFAULT_FAVICON)), |
191 weak_ptr_factory_(this) { | 189 weak_ptr_factory_(this) { |
192 // Invoke asynchronous call to load tabs from local last session, which does | 190 // Invoke asynchronous call to load tabs from local last session, which does |
193 // nothing if the tabs have already been loaded or they shouldn't be loaded. | 191 // nothing if the tabs have already been loaded or they shouldn't be loaded. |
194 // TabRestoreServiceChanged() will be called after the tabs are loaded. | 192 // TabRestoreServiceChanged() will be called after the tabs are loaded. |
195 sessions::TabRestoreService* service = | 193 sessions::TabRestoreService* service = |
196 TabRestoreServiceFactory::GetForProfile(browser_->profile()); | 194 TabRestoreServiceFactory::GetForProfile(browser_->profile()); |
197 if (service) { | 195 if (service) { |
198 service->LoadTabsFromLastSession(); | 196 service->LoadTabsFromLastSession(); |
199 | 197 |
200 // TODO(sail): enable this when mac implements the dynamic menu, together with | 198 // TODO(sail): enable this when mac implements the dynamic menu, together with |
201 // MenuModelDelegate::MenuStructureChanged(). | 199 // MenuModelDelegate::MenuStructureChanged(). |
202 #if !defined(OS_MACOSX) | 200 #if !defined(OS_MACOSX) |
203 service->AddObserver(this); | 201 service->AddObserver(this); |
204 #endif | 202 #endif |
205 } | 203 } |
206 | 204 |
205 #if !defined(OS_MACOSX) | |
Peter Kasting
2017/03/23 04:48:34
Nit: Add note about why this isn't on for Mac
| |
206 browser_sync::ProfileSyncService* sync_service = | |
207 ProfileSyncServiceFactory::GetInstance()->GetForProfile( | |
208 browser_->profile()); | |
209 if (sync_service && sync_service->IsSyncActive()) { | |
Peter Kasting
2017/03/23 04:48:34
Nit: No {}
| |
210 sync_service->AddObserver(this); | |
211 } | |
212 #endif | |
213 | |
207 Build(); | 214 Build(); |
208 | 215 |
209 // Retrieve accelerator key for IDC_RESTORE_TAB now, because on ASH, it's not | 216 // Retrieve accelerator key for IDC_RESTORE_TAB now, because on ASH, it's not |
210 // defined in |accelerator_provider|, but in shell, so simply retrieve it now | 217 // defined in |accelerator_provider|, but in shell, so simply retrieve it now |
211 // for all ASH and non-ASH for use in |GetAcceleratorForCommandId|. | 218 // for all ASH and non-ASH for use in |GetAcceleratorForCommandId|. |
212 #if defined(USE_ASH) | 219 #if defined(USE_ASH) |
213 for (size_t i = 0; i < ash::kAcceleratorDataLength; ++i) { | 220 for (size_t i = 0; i < ash::kAcceleratorDataLength; ++i) { |
214 const ash::AcceleratorData& accel_data = ash::kAcceleratorData[i]; | 221 const ash::AcceleratorData& accel_data = ash::kAcceleratorData[i]; |
215 if (accel_data.action == ash::RESTORE_TAB) { | 222 if (accel_data.action == ash::RESTORE_TAB) { |
216 reopen_closed_tab_accelerator_ = ui::Accelerator(accel_data.keycode, | 223 reopen_closed_tab_accelerator_ = ui::Accelerator(accel_data.keycode, |
(...skipping 12 matching lines...) Expand all Loading... | |
229 accelerator_provider->GetAcceleratorForCommandId( | 236 accelerator_provider->GetAcceleratorForCommandId( |
230 IDC_SHOW_HISTORY, &show_history_accelerator_); | 237 IDC_SHOW_HISTORY, &show_history_accelerator_); |
231 } | 238 } |
232 } | 239 } |
233 | 240 |
234 RecentTabsSubMenuModel::~RecentTabsSubMenuModel() { | 241 RecentTabsSubMenuModel::~RecentTabsSubMenuModel() { |
235 sessions::TabRestoreService* service = | 242 sessions::TabRestoreService* service = |
236 TabRestoreServiceFactory::GetForProfile(browser_->profile()); | 243 TabRestoreServiceFactory::GetForProfile(browser_->profile()); |
237 if (service) | 244 if (service) |
238 service->RemoveObserver(this); | 245 service->RemoveObserver(this); |
246 | |
247 #if !defined(OS_MACOSX) | |
248 browser_sync::ProfileSyncService* sync_service = | |
249 ProfileSyncServiceFactory::GetInstance()->GetForProfile( | |
250 browser_->profile()); | |
251 if (sync_service) { | |
252 sync_service->RemoveObserver(this); | |
Peter Kasting
2017/03/23 04:48:34
Can this be done with a ScopedObserver class membe
| |
253 } | |
254 #endif | |
239 } | 255 } |
240 | 256 |
241 bool RecentTabsSubMenuModel::IsCommandIdChecked(int command_id) const { | 257 bool RecentTabsSubMenuModel::IsCommandIdChecked(int command_id) const { |
242 return false; | 258 return false; |
243 } | 259 } |
244 | 260 |
245 bool RecentTabsSubMenuModel::IsCommandIdEnabled(int command_id) const { | 261 bool RecentTabsSubMenuModel::IsCommandIdEnabled(int command_id) const { |
246 if (command_id == kRecentlyClosedHeaderCommandId || | 262 if (command_id == kRecentlyClosedHeaderCommandId || |
247 command_id == kDisabledRecentlyClosedHeaderCommandId || | 263 command_id == kDisabledRecentlyClosedHeaderCommandId || |
248 command_id == IDC_RECENT_TABS_NO_DEVICE_TABS || | 264 command_id == IDC_RECENT_TABS_NO_DEVICE_TABS || |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
467 } | 483 } |
468 } | 484 } |
469 ++added_count; | 485 ++added_count; |
470 } | 486 } |
471 } | 487 } |
472 DCHECK_GE(last_local_model_index_, 0); | 488 DCHECK_GE(last_local_model_index_, 0); |
473 } | 489 } |
474 | 490 |
475 void RecentTabsSubMenuModel::BuildTabsFromOtherDevices() { | 491 void RecentTabsSubMenuModel::BuildTabsFromOtherDevices() { |
476 // All other devices' items (device headers or tabs) use AddItem*() to append | 492 // All other devices' items (device headers or tabs) use AddItem*() to append |
477 // a menu item, because they are always only built once (i.e. invoked from | 493 // a menu item, because they take always place in the end of menu. |
478 // Constructor()) and don't change after that. | |
479 | 494 |
480 sync_sessions::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(); | 495 sync_sessions::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(); |
481 std::vector<const sync_sessions::SyncedSession*> sessions; | 496 std::vector<const sync_sessions::SyncedSession*> sessions; |
482 if (!open_tabs || !open_tabs->GetAllForeignSessions(&sessions)) { | 497 if (!open_tabs || !open_tabs->GetAllForeignSessions(&sessions)) { |
483 AddSeparator(ui::NORMAL_SEPARATOR); | 498 AddSeparator(ui::NORMAL_SEPARATOR); |
484 AddItemWithStringId(IDC_RECENT_TABS_NO_DEVICE_TABS, | 499 AddItemWithStringId(IDC_RECENT_TABS_NO_DEVICE_TABS, |
485 IDS_RECENT_TABS_NO_DEVICE_TABS); | 500 IDS_RECENT_TABS_NO_DEVICE_TABS); |
486 return; | 501 return; |
487 } | 502 } |
488 | 503 |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
701 // all local tabs. | 716 // all local tabs. |
702 local_tab_cancelable_task_tracker_.TryCancelAll(); | 717 local_tab_cancelable_task_tracker_.TryCancelAll(); |
703 | 718 |
704 // Remove all local tab navigation items. | 719 // Remove all local tab navigation items. |
705 local_tab_navigation_items_.clear(); | 720 local_tab_navigation_items_.clear(); |
706 | 721 |
707 // Remove all local window items. | 722 // Remove all local window items. |
708 local_window_items_.clear(); | 723 local_window_items_.clear(); |
709 } | 724 } |
710 | 725 |
726 void RecentTabsSubMenuModel::ClearTabsFromOtherDevices() { | |
727 DCHECK_GE(last_local_model_index_, 0); | |
728 int count = GetItemCount(); | |
729 for (int index = count - 1; index > last_local_model_index_; --index) | |
730 RemoveItemAt(index); | |
731 | |
732 // Cancel asynchronous FaviconService::GetFaviconImageForPageURL() tasks of | |
Peter Kasting
2017/03/23 04:48:34
Nit: These comments (here and below) add nothing t
| |
733 // all tabs from the other devices. | |
734 other_devices_tab_cancelable_task_tracker_.TryCancelAll(); | |
735 | |
736 // Remove all tab from the other devices navigation items. | |
737 other_devices_tab_navigation_items_.clear(); | |
738 } | |
739 | |
711 sync_sessions::OpenTabsUIDelegate* | 740 sync_sessions::OpenTabsUIDelegate* |
712 RecentTabsSubMenuModel::GetOpenTabsUIDelegate() { | 741 RecentTabsSubMenuModel::GetOpenTabsUIDelegate() { |
713 if (!open_tabs_delegate_) { | 742 if (!open_tabs_delegate_) { |
714 browser_sync::ProfileSyncService* service = | 743 browser_sync::ProfileSyncService* service = |
715 ProfileSyncServiceFactory::GetInstance()->GetForProfile( | 744 ProfileSyncServiceFactory::GetInstance()->GetForProfile( |
716 browser_->profile()); | 745 browser_->profile()); |
717 // Only return the delegate if it exists and it is done syncing sessions. | 746 // Only return the delegate if it exists and it is done syncing sessions. |
718 if (service && service->IsSyncActive()) | 747 if (service && service->IsSyncActive()) |
719 open_tabs_delegate_ = service->GetOpenTabsUIDelegate(); | 748 open_tabs_delegate_ = service->GetOpenTabsUIDelegate(); |
720 } | 749 } |
721 return open_tabs_delegate_; | 750 return open_tabs_delegate_; |
722 } | 751 } |
723 | 752 |
724 void RecentTabsSubMenuModel::TabRestoreServiceChanged( | 753 void RecentTabsSubMenuModel::TabRestoreServiceChanged( |
725 sessions::TabRestoreService* service) { | 754 sessions::TabRestoreService* service) { |
726 ClearLocalEntries(); | 755 ClearLocalEntries(); |
727 | 756 |
728 BuildLocalEntries(); | 757 BuildLocalEntries(); |
729 | 758 |
730 ui::MenuModelDelegate* menu_model_delegate = GetMenuModelDelegate(); | 759 ui::MenuModelDelegate* menu_model_delegate = GetMenuModelDelegate(); |
731 if (menu_model_delegate) | 760 if (menu_model_delegate) |
732 menu_model_delegate->OnMenuStructureChanged(); | 761 menu_model_delegate->OnMenuStructureChanged(); |
733 } | 762 } |
734 | 763 |
735 void RecentTabsSubMenuModel::TabRestoreServiceDestroyed( | 764 void RecentTabsSubMenuModel::TabRestoreServiceDestroyed( |
736 sessions::TabRestoreService* service) { | 765 sessions::TabRestoreService* service) { |
737 TabRestoreServiceChanged(service); | 766 TabRestoreServiceChanged(service); |
738 } | 767 } |
768 | |
769 void RecentTabsSubMenuModel::OnForeignSessionUpdated( | |
770 syncer::SyncService* sync) { | |
771 ClearTabsFromOtherDevices(); | |
772 | |
773 BuildTabsFromOtherDevices(); | |
774 | |
775 ui::MenuModelDelegate* menu_model_delegate = GetMenuModelDelegate(); | |
776 if (menu_model_delegate) | |
777 menu_model_delegate->OnMenuStructureChanged(); | |
778 } | |
OLD | NEW |