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

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

Issue 2768633003: Dynamic updating recent menu for tabs from other devices. (Closed)
Patch Set: Fixes after review. Created 3 years, 9 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
OLDNEW
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
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), 185 open_tabs_delegate_(nullptr),
187 last_local_model_index_(kHistorySeparatorIndex), 186 last_local_model_index_(kHistorySeparatorIndex),
188 default_favicon_( 187 default_favicon_(
189 ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed( 188 ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(
190 IDR_DEFAULT_FAVICON)), 189 IDR_DEFAULT_FAVICON)),
190 #if !defined(OS_MACOSX)
191 tab_restore_service_observer_(this),
192 sync_observer_(this),
193 #endif // !defined(OS_MACOSX)
191 weak_ptr_factory_(this) { 194 weak_ptr_factory_(this) {
192 // Invoke asynchronous call to load tabs from local last session, which does 195 // 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. 196 // nothing if the tabs have already been loaded or they shouldn't be loaded.
194 // TabRestoreServiceChanged() will be called after the tabs are loaded. 197 // TabRestoreServiceChanged() will be called after the tabs are loaded.
195 sessions::TabRestoreService* service = 198 sessions::TabRestoreService* service =
196 TabRestoreServiceFactory::GetForProfile(browser_->profile()); 199 TabRestoreServiceFactory::GetForProfile(browser_->profile());
197 if (service) { 200 if (service) {
198 service->LoadTabsFromLastSession(); 201 service->LoadTabsFromLastSession();
199 202
200 // TODO(sail): enable this when mac implements the dynamic menu, together with 203 // Mac doesn't support the dynamic menu.
201 // MenuModelDelegate::MenuStructureChanged().
202 #if !defined(OS_MACOSX) 204 #if !defined(OS_MACOSX)
203 service->AddObserver(this); 205 tab_restore_service_observer_.Add(service);
204 #endif 206 #endif
205 } 207 }
206 208
209 // Mac doesn't support the dynamic menu.
210 #if !defined(OS_MACOSX)
211 browser_sync::ProfileSyncService* sync_service =
212 ProfileSyncServiceFactory::GetInstance()->GetForProfile(
213 browser_->profile());
214 if (sync_service && sync_service->IsSyncActive())
pavely 2017/03/24 01:20:05 Is this line executed once per process or every ti
Peter Kasting 2017/03/24 01:35:21 Looks like it's called each time the menu is opene
pavely 2017/03/24 04:13:22 Yeah, we should remove call to IsSyncActive(). If
215 sync_observer_.Add(sync_service);
216 #endif // !defined(OS_MACOSX)
217
207 Build(); 218 Build();
208 219
209 // Retrieve accelerator key for IDC_RESTORE_TAB now, because on ASH, it's not 220 // 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 221 // defined in |accelerator_provider|, but in shell, so simply retrieve it now
211 // for all ASH and non-ASH for use in |GetAcceleratorForCommandId|. 222 // for all ASH and non-ASH for use in |GetAcceleratorForCommandId|.
212 #if defined(USE_ASH) 223 #if defined(USE_ASH)
213 for (size_t i = 0; i < ash::kAcceleratorDataLength; ++i) { 224 for (size_t i = 0; i < ash::kAcceleratorDataLength; ++i) {
214 const ash::AcceleratorData& accel_data = ash::kAcceleratorData[i]; 225 const ash::AcceleratorData& accel_data = ash::kAcceleratorData[i];
215 if (accel_data.action == ash::RESTORE_TAB) { 226 if (accel_data.action == ash::RESTORE_TAB) {
216 reopen_closed_tab_accelerator_ = ui::Accelerator(accel_data.keycode, 227 reopen_closed_tab_accelerator_ = ui::Accelerator(accel_data.keycode,
217 accel_data.modifiers); 228 accel_data.modifiers);
218 break; 229 break;
219 } 230 }
220 } 231 }
221 #else 232 #else
222 if (accelerator_provider) { 233 if (accelerator_provider) {
223 accelerator_provider->GetAcceleratorForCommandId( 234 accelerator_provider->GetAcceleratorForCommandId(
224 IDC_RESTORE_TAB, &reopen_closed_tab_accelerator_); 235 IDC_RESTORE_TAB, &reopen_closed_tab_accelerator_);
225 } 236 }
226 #endif // defined(USE_ASH) 237 #endif // defined(USE_ASH)
227 238
228 if (accelerator_provider) { 239 if (accelerator_provider) {
229 accelerator_provider->GetAcceleratorForCommandId( 240 accelerator_provider->GetAcceleratorForCommandId(
230 IDC_SHOW_HISTORY, &show_history_accelerator_); 241 IDC_SHOW_HISTORY, &show_history_accelerator_);
231 } 242 }
232 } 243 }
233 244
234 RecentTabsSubMenuModel::~RecentTabsSubMenuModel() { 245 RecentTabsSubMenuModel::~RecentTabsSubMenuModel() {}
235 sessions::TabRestoreService* service =
236 TabRestoreServiceFactory::GetForProfile(browser_->profile());
237 if (service)
238 service->RemoveObserver(this);
239 }
240 246
241 bool RecentTabsSubMenuModel::IsCommandIdChecked(int command_id) const { 247 bool RecentTabsSubMenuModel::IsCommandIdChecked(int command_id) const {
242 return false; 248 return false;
243 } 249 }
244 250
245 bool RecentTabsSubMenuModel::IsCommandIdEnabled(int command_id) const { 251 bool RecentTabsSubMenuModel::IsCommandIdEnabled(int command_id) const {
246 if (command_id == kRecentlyClosedHeaderCommandId || 252 if (command_id == kRecentlyClosedHeaderCommandId ||
247 command_id == kDisabledRecentlyClosedHeaderCommandId || 253 command_id == kDisabledRecentlyClosedHeaderCommandId ||
248 command_id == IDC_RECENT_TABS_NO_DEVICE_TABS || 254 command_id == IDC_RECENT_TABS_NO_DEVICE_TABS ||
249 IsDeviceNameCommandId(command_id)) { 255 IsDeviceNameCommandId(command_id)) {
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 } 473 }
468 } 474 }
469 ++added_count; 475 ++added_count;
470 } 476 }
471 } 477 }
472 DCHECK_GE(last_local_model_index_, 0); 478 DCHECK_GE(last_local_model_index_, 0);
473 } 479 }
474 480
475 void RecentTabsSubMenuModel::BuildTabsFromOtherDevices() { 481 void RecentTabsSubMenuModel::BuildTabsFromOtherDevices() {
476 // All other devices' items (device headers or tabs) use AddItem*() to append 482 // 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 483 // a menu item, because they take always place in the end of menu.
478 // Constructor()) and don't change after that.
479 484
480 sync_sessions::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(); 485 sync_sessions::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate();
481 std::vector<const sync_sessions::SyncedSession*> sessions; 486 std::vector<const sync_sessions::SyncedSession*> sessions;
482 if (!open_tabs || !open_tabs->GetAllForeignSessions(&sessions)) { 487 if (!open_tabs || !open_tabs->GetAllForeignSessions(&sessions)) {
483 AddSeparator(ui::NORMAL_SEPARATOR); 488 AddSeparator(ui::NORMAL_SEPARATOR);
484 AddItemWithStringId(IDC_RECENT_TABS_NO_DEVICE_TABS, 489 AddItemWithStringId(IDC_RECENT_TABS_NO_DEVICE_TABS,
485 IDS_RECENT_TABS_NO_DEVICE_TABS); 490 IDS_RECENT_TABS_NO_DEVICE_TABS);
486 return; 491 return;
487 } 492 }
488 493
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
701 // all local tabs. 706 // all local tabs.
702 local_tab_cancelable_task_tracker_.TryCancelAll(); 707 local_tab_cancelable_task_tracker_.TryCancelAll();
703 708
704 // Remove all local tab navigation items. 709 // Remove all local tab navigation items.
705 local_tab_navigation_items_.clear(); 710 local_tab_navigation_items_.clear();
706 711
707 // Remove all local window items. 712 // Remove all local window items.
708 local_window_items_.clear(); 713 local_window_items_.clear();
709 } 714 }
710 715
716 void RecentTabsSubMenuModel::ClearTabsFromOtherDevices() {
717 DCHECK_GE(last_local_model_index_, 0);
718 int count = GetItemCount();
719 for (int index = count - 1; index > last_local_model_index_; --index)
720 RemoveItemAt(index);
721
722 other_devices_tab_cancelable_task_tracker_.TryCancelAll();
723
724 other_devices_tab_navigation_items_.clear();
725 }
726
711 sync_sessions::OpenTabsUIDelegate* 727 sync_sessions::OpenTabsUIDelegate*
712 RecentTabsSubMenuModel::GetOpenTabsUIDelegate() { 728 RecentTabsSubMenuModel::GetOpenTabsUIDelegate() {
713 if (!open_tabs_delegate_) { 729 if (!open_tabs_delegate_) {
714 browser_sync::ProfileSyncService* service = 730 browser_sync::ProfileSyncService* service =
715 ProfileSyncServiceFactory::GetInstance()->GetForProfile( 731 ProfileSyncServiceFactory::GetInstance()->GetForProfile(
716 browser_->profile()); 732 browser_->profile());
717 // Only return the delegate if it exists and it is done syncing sessions. 733 // Only return the delegate if it exists and it is done syncing sessions.
718 if (service && service->IsSyncActive()) 734 if (service && service->IsSyncActive())
719 open_tabs_delegate_ = service->GetOpenTabsUIDelegate(); 735 open_tabs_delegate_ = service->GetOpenTabsUIDelegate();
720 } 736 }
721 return open_tabs_delegate_; 737 return open_tabs_delegate_;
722 } 738 }
723 739
724 void RecentTabsSubMenuModel::TabRestoreServiceChanged( 740 void RecentTabsSubMenuModel::TabRestoreServiceChanged(
725 sessions::TabRestoreService* service) { 741 sessions::TabRestoreService* service) {
726 ClearLocalEntries(); 742 ClearLocalEntries();
727 743
728 BuildLocalEntries(); 744 BuildLocalEntries();
729 745
730 ui::MenuModelDelegate* menu_model_delegate = GetMenuModelDelegate(); 746 ui::MenuModelDelegate* menu_model_delegate = GetMenuModelDelegate();
731 if (menu_model_delegate) 747 if (menu_model_delegate)
732 menu_model_delegate->OnMenuStructureChanged(); 748 menu_model_delegate->OnMenuStructureChanged();
733 } 749 }
734 750
735 void RecentTabsSubMenuModel::TabRestoreServiceDestroyed( 751 void RecentTabsSubMenuModel::TabRestoreServiceDestroyed(
736 sessions::TabRestoreService* service) { 752 sessions::TabRestoreService* service) {
737 TabRestoreServiceChanged(service); 753 TabRestoreServiceChanged(service);
738 } 754 }
755
756 void RecentTabsSubMenuModel::OnSyncCycleCompleted(syncer::SyncService* sync) {}
757
758 void RecentTabsSubMenuModel::OnForeignSessionUpdated(
759 syncer::SyncService* sync) {
760 ClearTabsFromOtherDevices();
761
762 BuildTabsFromOtherDevices();
763
764 ui::MenuModelDelegate* menu_model_delegate = GetMenuModelDelegate();
765 if (menu_model_delegate)
766 menu_model_delegate->OnMenuStructureChanged();
767 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698