| 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/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 RecentTabsSubMenuModel::~RecentTabsSubMenuModel() { | 140 RecentTabsSubMenuModel::~RecentTabsSubMenuModel() { |
| 141 } | 141 } |
| 142 | 142 |
| 143 bool RecentTabsSubMenuModel::IsCommandIdChecked(int command_id) const { | 143 bool RecentTabsSubMenuModel::IsCommandIdChecked(int command_id) const { |
| 144 return false; | 144 return false; |
| 145 } | 145 } |
| 146 | 146 |
| 147 bool RecentTabsSubMenuModel::IsCommandIdEnabled(int command_id) const { | 147 bool RecentTabsSubMenuModel::IsCommandIdEnabled(int command_id) const { |
| 148 if (command_id == IDC_RESTORE_TAB) | 148 if (command_id == IDC_RESTORE_TAB) |
| 149 return chrome::IsCommandEnabled(browser_, command_id); | 149 return chrome::IsCommandEnabled(browser_, command_id); |
| 150 if (command_id == kDisabledCommandId) | 150 if (command_id == kDisabledCommandId || |
| 151 command_id == IDC_RECENT_TABS_NO_DEVICE_TABS) { |
| 151 return false; | 152 return false; |
| 153 } |
| 152 int model_index = CommandIdToModelIndex(command_id); | 154 int model_index = CommandIdToModelIndex(command_id); |
| 153 return model_index >= 0 && model_index < static_cast<int>(model_.size()); | 155 return model_index >= 0 && model_index < static_cast<int>(model_.size()); |
| 154 } | 156 } |
| 155 | 157 |
| 156 bool RecentTabsSubMenuModel::GetAcceleratorForCommandId( | 158 bool RecentTabsSubMenuModel::GetAcceleratorForCommandId( |
| 157 int command_id, ui::Accelerator* accelerator) { | 159 int command_id, ui::Accelerator* accelerator) { |
| 158 if (command_id == IDC_RESTORE_TAB && | 160 if (command_id == IDC_RESTORE_TAB && |
| 159 reopen_closed_tab_accelerator_.key_code() != ui::VKEY_UNKNOWN) { | 161 reopen_closed_tab_accelerator_.key_code() != ui::VKEY_UNKNOWN) { |
| 160 *accelerator = reopen_closed_tab_accelerator_; | 162 *accelerator = reopen_closed_tab_accelerator_; |
| 161 return true; | 163 return true; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 186 ExecuteCommand(command_id, 0); | 188 ExecuteCommand(command_id, 0); |
| 187 } | 189 } |
| 188 | 190 |
| 189 void RecentTabsSubMenuModel::ExecuteCommand(int command_id, int event_flags) { | 191 void RecentTabsSubMenuModel::ExecuteCommand(int command_id, int event_flags) { |
| 190 if (command_id == IDC_RESTORE_TAB) { | 192 if (command_id == IDC_RESTORE_TAB) { |
| 191 chrome::ExecuteCommandWithDisposition(browser_, command_id, | 193 chrome::ExecuteCommandWithDisposition(browser_, command_id, |
| 192 chrome::DispositionFromEventFlags(event_flags)); | 194 chrome::DispositionFromEventFlags(event_flags)); |
| 193 return; | 195 return; |
| 194 } | 196 } |
| 195 | 197 |
| 196 DCHECK(command_id != kDisabledCommandId); | 198 DCHECK_NE(kDisabledCommandId, command_id); |
| 199 DCHECK_NE(IDC_RECENT_TABS_NO_DEVICE_TABS, command_id); |
| 200 |
| 197 int model_idx = CommandIdToModelIndex(command_id); | 201 int model_idx = CommandIdToModelIndex(command_id); |
| 198 DCHECK(model_idx >= 0 && model_idx < static_cast<int>(model_.size())); | 202 DCHECK(model_idx >= 0 && model_idx < static_cast<int>(model_.size())); |
| 199 const NavigationItem& item = model_[model_idx]; | 203 const NavigationItem& item = model_[model_idx]; |
| 200 DCHECK(item.tab_id > -1 && item.url.is_valid()); | 204 DCHECK(item.tab_id > -1 && item.url.is_valid()); |
| 201 | 205 |
| 202 WindowOpenDisposition disposition = | 206 WindowOpenDisposition disposition = |
| 203 chrome::DispositionFromEventFlags(event_flags); | 207 chrome::DispositionFromEventFlags(event_flags); |
| 204 if (disposition == CURRENT_TAB) // Force to open a new foreground tab. | 208 if (disposition == CURRENT_TAB) // Force to open a new foreground tab. |
| 205 disposition = NEW_FOREGROUND_TAB; | 209 disposition = NEW_FOREGROUND_TAB; |
| 206 | 210 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 226 return; | 230 return; |
| 227 int prev_num_tabs = browser_->tab_strip_model()->count(); | 231 int prev_num_tabs = browser_->tab_strip_model()->count(); |
| 228 SessionRestore::RestoreForeignSessionTab( | 232 SessionRestore::RestoreForeignSessionTab( |
| 229 chrome::GetActiveWebContents(browser_), *tab, disposition); | 233 chrome::GetActiveWebContents(browser_), *tab, disposition); |
| 230 // If tab is successfully added (i.e. there's 1 more tab), select it. | 234 // If tab is successfully added (i.e. there's 1 more tab), select it. |
| 231 if (browser_->tab_strip_model()->count() == prev_num_tabs + 1) | 235 if (browser_->tab_strip_model()->count() == prev_num_tabs + 1) |
| 232 chrome::SelectNextTab(browser_); | 236 chrome::SelectNextTab(browser_); |
| 233 } | 237 } |
| 234 } | 238 } |
| 235 | 239 |
| 240 int RecentTabsSubMenuModel::GetMaxWidthForItemAtIndex(int item_index) const { |
| 241 int command_id = GetCommandIdAt(item_index); |
| 242 if (command_id == IDC_RECENT_TABS_NO_DEVICE_TABS || |
| 243 command_id == IDC_RESTORE_TAB) { |
| 244 return -1; |
| 245 } |
| 246 return 320; |
| 247 } |
| 248 |
| 236 void RecentTabsSubMenuModel::Build() { | 249 void RecentTabsSubMenuModel::Build() { |
| 237 // The menu contains: | 250 // The menu contains: |
| 238 // - Reopen closed tab, then separator | 251 // - Reopen closed tab, then separator |
| 239 // - device 1 section header, then list of tabs from device, then separator | 252 // - device 1 section header, then list of tabs from device, then separator |
| 240 // - device 2 section header, then list of tabs from device, then separator | 253 // - device 2 section header, then list of tabs from device, then separator |
| 241 // ... | 254 // ... |
| 242 // |model_| only contains navigatable (and hence executable) tab items for | 255 // |model_| only contains navigatable (and hence executable) tab items for |
| 243 // other devices. | 256 // other devices. |
| 244 BuildLastClosed(); | 257 BuildLastClosed(); |
| 245 BuildDevices(); | 258 BuildDevices(); |
| 246 if (model_.empty()) | 259 if (model_.empty()) { |
| 247 AddItemWithStringId(kDisabledCommandId, IDS_RECENT_TABS_NO_DEVICE_TABS); | 260 AddItemWithStringId(IDC_RECENT_TABS_NO_DEVICE_TABS, |
| 261 IDS_RECENT_TABS_NO_DEVICE_TABS); |
| 262 } |
| 248 } | 263 } |
| 249 | 264 |
| 250 void RecentTabsSubMenuModel::BuildLastClosed() { | 265 void RecentTabsSubMenuModel::BuildLastClosed() { |
| 251 AddItem(IDC_RESTORE_TAB, GetLabelForCommandId(IDC_RESTORE_TAB)); | 266 AddItem(IDC_RESTORE_TAB, GetLabelForCommandId(IDC_RESTORE_TAB)); |
| 252 AddSeparator(ui::NORMAL_SEPARATOR); | 267 AddSeparator(ui::NORMAL_SEPARATOR); |
| 253 } | 268 } |
| 254 | 269 |
| 255 void RecentTabsSubMenuModel::BuildDevices() { | 270 void RecentTabsSubMenuModel::BuildDevices() { |
| 256 browser_sync::SessionModelAssociator* associator = GetModelAssociator(); | 271 browser_sync::SessionModelAssociator* associator = GetModelAssociator(); |
| 257 if (!associator) | 272 if (!associator) |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 RecentTabsSubMenuModel::GetModelAssociator() { | 442 RecentTabsSubMenuModel::GetModelAssociator() { |
| 428 if (!associator_) { | 443 if (!associator_) { |
| 429 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> | 444 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
| 430 GetForProfile(browser_->profile()); | 445 GetForProfile(browser_->profile()); |
| 431 // Only return the associator if it exists and it is done syncing sessions. | 446 // Only return the associator if it exists and it is done syncing sessions. |
| 432 if (service && service->ShouldPushChanges()) | 447 if (service && service->ShouldPushChanges()) |
| 433 associator_ = service->GetSessionModelAssociator(); | 448 associator_ = service->GetSessionModelAssociator(); |
| 434 } | 449 } |
| 435 return associator_; | 450 return associator_; |
| 436 } | 451 } |
| OLD | NEW |