| 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 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 : ui::SimpleMenuModel(this), | 184 : ui::SimpleMenuModel(this), |
| 185 browser_(browser), | 185 browser_(browser), |
| 186 open_tabs_delegate_(open_tabs_delegate), | 186 open_tabs_delegate_(open_tabs_delegate), |
| 187 last_local_model_index_(kHistorySeparatorIndex), | 187 last_local_model_index_(kHistorySeparatorIndex), |
| 188 default_favicon_(ui::ResourceBundle::GetSharedInstance(). | 188 default_favicon_(ui::ResourceBundle::GetSharedInstance(). |
| 189 GetNativeImageNamed(IDR_DEFAULT_FAVICON)), | 189 GetNativeImageNamed(IDR_DEFAULT_FAVICON)), |
| 190 weak_ptr_factory_(this) { | 190 weak_ptr_factory_(this) { |
| 191 // Invoke asynchronous call to load tabs from local last session, which does | 191 // Invoke asynchronous call to load tabs from local last session, which does |
| 192 // nothing if the tabs have already been loaded or they shouldn't be loaded. | 192 // nothing if the tabs have already been loaded or they shouldn't be loaded. |
| 193 // TabRestoreServiceChanged() will be called after the tabs are loaded. | 193 // TabRestoreServiceChanged() will be called after the tabs are loaded. |
| 194 TabRestoreService* service = | 194 sessions::TabRestoreService* service = |
| 195 TabRestoreServiceFactory::GetForProfile(browser_->profile()); | 195 TabRestoreServiceFactory::GetForProfile(browser_->profile()); |
| 196 if (service) { | 196 if (service) { |
| 197 service->LoadTabsFromLastSession(); | 197 service->LoadTabsFromLastSession(); |
| 198 | 198 |
| 199 // TODO(sail): enable this when mac implements the dynamic menu, together with | 199 // TODO(sail): enable this when mac implements the dynamic menu, together with |
| 200 // MenuModelDelegate::MenuStructureChanged(). | 200 // MenuModelDelegate::MenuStructureChanged(). |
| 201 #if !defined(OS_MACOSX) | 201 #if !defined(OS_MACOSX) |
| 202 service->AddObserver(this); | 202 service->AddObserver(this); |
| 203 #endif | 203 #endif |
| 204 } | 204 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 224 } | 224 } |
| 225 #endif // defined(USE_ASH) | 225 #endif // defined(USE_ASH) |
| 226 | 226 |
| 227 if (accelerator_provider) { | 227 if (accelerator_provider) { |
| 228 accelerator_provider->GetAcceleratorForCommandId( | 228 accelerator_provider->GetAcceleratorForCommandId( |
| 229 IDC_SHOW_HISTORY, &show_history_accelerator_); | 229 IDC_SHOW_HISTORY, &show_history_accelerator_); |
| 230 } | 230 } |
| 231 } | 231 } |
| 232 | 232 |
| 233 RecentTabsSubMenuModel::~RecentTabsSubMenuModel() { | 233 RecentTabsSubMenuModel::~RecentTabsSubMenuModel() { |
| 234 TabRestoreService* service = | 234 sessions::TabRestoreService* service = |
| 235 TabRestoreServiceFactory::GetForProfile(browser_->profile()); | 235 TabRestoreServiceFactory::GetForProfile(browser_->profile()); |
| 236 if (service) | 236 if (service) |
| 237 service->RemoveObserver(this); | 237 service->RemoveObserver(this); |
| 238 } | 238 } |
| 239 | 239 |
| 240 bool RecentTabsSubMenuModel::IsCommandIdChecked(int command_id) const { | 240 bool RecentTabsSubMenuModel::IsCommandIdChecked(int command_id) const { |
| 241 return false; | 241 return false; |
| 242 } | 242 } |
| 243 | 243 |
| 244 bool RecentTabsSubMenuModel::IsCommandIdEnabled(int command_id) const { | 244 bool RecentTabsSubMenuModel::IsCommandIdEnabled(int command_id) const { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 } | 283 } |
| 284 | 284 |
| 285 DCHECK_NE(IDC_RECENT_TABS_NO_DEVICE_TABS, command_id); | 285 DCHECK_NE(IDC_RECENT_TABS_NO_DEVICE_TABS, command_id); |
| 286 DCHECK(!IsDeviceNameCommandId(command_id)); | 286 DCHECK(!IsDeviceNameCommandId(command_id)); |
| 287 | 287 |
| 288 WindowOpenDisposition disposition = | 288 WindowOpenDisposition disposition = |
| 289 ui::DispositionFromEventFlags(event_flags); | 289 ui::DispositionFromEventFlags(event_flags); |
| 290 if (disposition == CURRENT_TAB) // Force to open a new foreground tab. | 290 if (disposition == CURRENT_TAB) // Force to open a new foreground tab. |
| 291 disposition = NEW_FOREGROUND_TAB; | 291 disposition = NEW_FOREGROUND_TAB; |
| 292 | 292 |
| 293 TabRestoreService* service = | 293 sessions::TabRestoreService* service = |
| 294 TabRestoreServiceFactory::GetForProfile(browser_->profile()); | 294 TabRestoreServiceFactory::GetForProfile(browser_->profile()); |
| 295 TabRestoreServiceDelegate* delegate = | 295 sessions::TabRestoreServiceDelegate* delegate = |
| 296 BrowserTabRestoreServiceDelegate::FindDelegateForWebContents( | 296 BrowserTabRestoreServiceDelegate::FindDelegateForWebContents( |
| 297 browser_->tab_strip_model()->GetActiveWebContents()); | 297 browser_->tab_strip_model()->GetActiveWebContents()); |
| 298 if (IsTabModelCommandId(command_id)) { | 298 if (IsTabModelCommandId(command_id)) { |
| 299 TabNavigationItems* tab_items = NULL; | 299 TabNavigationItems* tab_items = NULL; |
| 300 int tab_items_idx = CommandIdToTabVectorIndex(command_id, &tab_items); | 300 int tab_items_idx = CommandIdToTabVectorIndex(command_id, &tab_items); |
| 301 const TabNavigationItem& item = (*tab_items)[tab_items_idx]; | 301 const TabNavigationItem& item = (*tab_items)[tab_items_idx]; |
| 302 DCHECK(item.tab_id > -1 && item.url.is_valid()); | 302 DCHECK(item.tab_id > -1 && item.url.is_valid()); |
| 303 | 303 |
| 304 if (item.session_tag.empty()) { // Restore tab of local session. | 304 if (item.session_tag.empty()) { // Restore tab of local session. |
| 305 if (service && delegate) { | 305 if (service && delegate) { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 BuildTabsFromOtherDevices(); | 407 BuildTabsFromOtherDevices(); |
| 408 } | 408 } |
| 409 | 409 |
| 410 void RecentTabsSubMenuModel::BuildLocalEntries() { | 410 void RecentTabsSubMenuModel::BuildLocalEntries() { |
| 411 last_local_model_index_ = kHistorySeparatorIndex; | 411 last_local_model_index_ = kHistorySeparatorIndex; |
| 412 | 412 |
| 413 // All local items use InsertItem*At() to append or insert a menu item. | 413 // All local items use InsertItem*At() to append or insert a menu item. |
| 414 // We're appending if building the entries for the first time i.e. invoked | 414 // We're appending if building the entries for the first time i.e. invoked |
| 415 // from Constructor(), inserting when local entries change subsequently i.e. | 415 // from Constructor(), inserting when local entries change subsequently i.e. |
| 416 // invoked from TabRestoreServiceChanged(). | 416 // invoked from TabRestoreServiceChanged(). |
| 417 TabRestoreService* service = | 417 sessions::TabRestoreService* service = |
| 418 TabRestoreServiceFactory::GetForProfile(browser_->profile()); | 418 TabRestoreServiceFactory::GetForProfile(browser_->profile()); |
| 419 | 419 |
| 420 if (!service || service->entries().size() == 0) { | 420 if (!service || service->entries().size() == 0) { |
| 421 // This is to show a disabled restore tab entry with the accelerator to | 421 // This is to show a disabled restore tab entry with the accelerator to |
| 422 // teach users about this command. | 422 // teach users about this command. |
| 423 InsertItemWithStringIdAt(++last_local_model_index_, | 423 InsertItemWithStringIdAt(++last_local_model_index_, |
| 424 kDisabledRecentlyClosedHeaderCommandId, | 424 kDisabledRecentlyClosedHeaderCommandId, |
| 425 IDS_RECENTLY_CLOSED); | 425 IDS_RECENTLY_CLOSED); |
| 426 } else { | 426 } else { |
| 427 InsertItemWithStringIdAt(++last_local_model_index_, | 427 InsertItemWithStringIdAt(++last_local_model_index_, |
| 428 kRecentlyClosedHeaderCommandId, | 428 kRecentlyClosedHeaderCommandId, |
| 429 IDS_RECENTLY_CLOSED); | 429 IDS_RECENTLY_CLOSED); |
| 430 #if defined(OS_MACOSX) | 430 #if defined(OS_MACOSX) |
| 431 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 431 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 432 SetIcon(last_local_model_index_, | 432 SetIcon(last_local_model_index_, |
| 433 rb.GetNativeImageNamed(IDR_RECENTLY_CLOSED_WINDOW)); | 433 rb.GetNativeImageNamed(IDR_RECENTLY_CLOSED_WINDOW)); |
| 434 #else | 434 #else |
| 435 SetIcon(last_local_model_index_, CreateFavicon(gfx::VectorIconId::TAB)); | 435 SetIcon(last_local_model_index_, CreateFavicon(gfx::VectorIconId::TAB)); |
| 436 #endif | 436 #endif |
| 437 | 437 |
| 438 int added_count = 0; | 438 int added_count = 0; |
| 439 TabRestoreService::Entries entries = service->entries(); | 439 sessions::TabRestoreService::Entries entries = service->entries(); |
| 440 for (TabRestoreService::Entries::const_iterator it = entries.begin(); | 440 for (sessions::TabRestoreService::Entries::const_iterator it = |
| 441 entries.begin(); |
| 441 it != entries.end() && added_count < kMaxLocalEntries; ++it) { | 442 it != entries.end() && added_count < kMaxLocalEntries; ++it) { |
| 442 TabRestoreService::Entry* entry = *it; | 443 sessions::TabRestoreService::Entry* entry = *it; |
| 443 if (entry->type == TabRestoreService::TAB) { | 444 if (entry->type == sessions::TabRestoreService::TAB) { |
| 444 TabRestoreService::Tab* tab = | 445 sessions::TabRestoreService::Tab* tab = |
| 445 static_cast<TabRestoreService::Tab*>(entry); | 446 static_cast<sessions::TabRestoreService::Tab*>(entry); |
| 446 const sessions::SerializedNavigationEntry& current_navigation = | 447 const sessions::SerializedNavigationEntry& current_navigation = |
| 447 tab->navigations.at(tab->current_navigation_index); | 448 tab->navigations.at(tab->current_navigation_index); |
| 448 BuildLocalTabItem( | 449 BuildLocalTabItem( |
| 449 entry->id, | 450 entry->id, |
| 450 current_navigation.title(), | 451 current_navigation.title(), |
| 451 current_navigation.virtual_url(), | 452 current_navigation.virtual_url(), |
| 452 ++last_local_model_index_); | 453 ++last_local_model_index_); |
| 453 } else { | 454 } else { |
| 454 DCHECK_EQ(entry->type, TabRestoreService::WINDOW); | 455 DCHECK_EQ(entry->type, sessions::TabRestoreService::WINDOW); |
| 455 BuildLocalWindowItem( | 456 BuildLocalWindowItem( |
| 456 entry->id, | 457 entry->id, static_cast<sessions::TabRestoreService::Window*>(entry) |
| 457 static_cast<TabRestoreService::Window*>(entry)->tabs.size(), | 458 ->tabs.size(), |
| 458 ++last_local_model_index_); | 459 ++last_local_model_index_); |
| 459 } | 460 } |
| 460 ++added_count; | 461 ++added_count; |
| 461 } | 462 } |
| 462 } | 463 } |
| 463 DCHECK_GE(last_local_model_index_, 0); | 464 DCHECK_GE(last_local_model_index_, 0); |
| 464 } | 465 } |
| 465 | 466 |
| 466 void RecentTabsSubMenuModel::BuildTabsFromOtherDevices() { | 467 void RecentTabsSubMenuModel::BuildTabsFromOtherDevices() { |
| 467 // All other devices' items (device headers or tabs) use AddItem*() to append | 468 // All other devices' items (device headers or tabs) use AddItem*() to append |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 705 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> | 706 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
| 706 GetForProfile(browser_->profile()); | 707 GetForProfile(browser_->profile()); |
| 707 // Only return the delegate if it exists and it is done syncing sessions. | 708 // Only return the delegate if it exists and it is done syncing sessions. |
| 708 if (service && service->IsSyncActive()) | 709 if (service && service->IsSyncActive()) |
| 709 open_tabs_delegate_ = service->GetOpenTabsUIDelegate(); | 710 open_tabs_delegate_ = service->GetOpenTabsUIDelegate(); |
| 710 } | 711 } |
| 711 return open_tabs_delegate_; | 712 return open_tabs_delegate_; |
| 712 } | 713 } |
| 713 | 714 |
| 714 void RecentTabsSubMenuModel::TabRestoreServiceChanged( | 715 void RecentTabsSubMenuModel::TabRestoreServiceChanged( |
| 715 TabRestoreService* service) { | 716 sessions::TabRestoreService* service) { |
| 716 ClearLocalEntries(); | 717 ClearLocalEntries(); |
| 717 | 718 |
| 718 BuildLocalEntries(); | 719 BuildLocalEntries(); |
| 719 | 720 |
| 720 ui::MenuModelDelegate* menu_model_delegate = GetMenuModelDelegate(); | 721 ui::MenuModelDelegate* menu_model_delegate = GetMenuModelDelegate(); |
| 721 if (menu_model_delegate) | 722 if (menu_model_delegate) |
| 722 menu_model_delegate->OnMenuStructureChanged(); | 723 menu_model_delegate->OnMenuStructureChanged(); |
| 723 } | 724 } |
| 724 | 725 |
| 725 void RecentTabsSubMenuModel::TabRestoreServiceDestroyed( | 726 void RecentTabsSubMenuModel::TabRestoreServiceDestroyed( |
| 726 TabRestoreService* service) { | 727 sessions::TabRestoreService* service) { |
| 727 TabRestoreServiceChanged(service); | 728 TabRestoreServiceChanged(service); |
| 728 } | 729 } |
| OLD | NEW |