| 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/strings/string_number_conversions.h" | 8 #include "base/strings/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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 return false; | 143 return false; |
| 144 } | 144 } |
| 145 | 145 |
| 146 bool RecentTabsSubMenuModel::IsCommandIdEnabled(int command_id) const { | 146 bool RecentTabsSubMenuModel::IsCommandIdEnabled(int command_id) const { |
| 147 if (command_id == IDC_RESTORE_TAB) | 147 if (command_id == IDC_RESTORE_TAB) |
| 148 return chrome::IsCommandEnabled(browser_, command_id); | 148 return chrome::IsCommandEnabled(browser_, command_id); |
| 149 if (command_id == kDisabledCommandId || | 149 if (command_id == kDisabledCommandId || |
| 150 command_id == IDC_RECENT_TABS_NO_DEVICE_TABS) { | 150 command_id == IDC_RECENT_TABS_NO_DEVICE_TABS) { |
| 151 return false; | 151 return false; |
| 152 } | 152 } |
| 153 if (command_id == IDC_SHOW_HISTORY) { |
| 154 return true; |
| 155 } |
| 153 int model_index = CommandIdToModelIndex(command_id); | 156 int model_index = CommandIdToModelIndex(command_id); |
| 154 return model_index >= 0 && model_index < static_cast<int>(model_.size()); | 157 return model_index >= 0 && model_index < static_cast<int>(model_.size()); |
| 155 } | 158 } |
| 156 | 159 |
| 157 bool RecentTabsSubMenuModel::GetAcceleratorForCommandId( | 160 bool RecentTabsSubMenuModel::GetAcceleratorForCommandId( |
| 158 int command_id, ui::Accelerator* accelerator) { | 161 int command_id, ui::Accelerator* accelerator) { |
| 159 if (command_id == IDC_RESTORE_TAB && | 162 if (command_id == IDC_RESTORE_TAB && |
| 160 reopen_closed_tab_accelerator_.key_code() != ui::VKEY_UNKNOWN) { | 163 reopen_closed_tab_accelerator_.key_code() != ui::VKEY_UNKNOWN) { |
| 161 *accelerator = reopen_closed_tab_accelerator_; | 164 *accelerator = reopen_closed_tab_accelerator_; |
| 162 return true; | 165 return true; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 182 } | 185 } |
| 183 return l10n_util::GetStringUTF16(string_id); | 186 return l10n_util::GetStringUTF16(string_id); |
| 184 } | 187 } |
| 185 | 188 |
| 186 void RecentTabsSubMenuModel::ExecuteCommand(int command_id, int event_flags) { | 189 void RecentTabsSubMenuModel::ExecuteCommand(int command_id, int event_flags) { |
| 187 if (command_id == IDC_RESTORE_TAB) { | 190 if (command_id == IDC_RESTORE_TAB) { |
| 188 chrome::ExecuteCommandWithDisposition(browser_, command_id, | 191 chrome::ExecuteCommandWithDisposition(browser_, command_id, |
| 189 ui::DispositionFromEventFlags(event_flags)); | 192 ui::DispositionFromEventFlags(event_flags)); |
| 190 return; | 193 return; |
| 191 } | 194 } |
| 195 if (command_id == IDC_SHOW_HISTORY) { |
| 196 // We show all "other devices" on the history page. |
| 197 chrome::ExecuteCommandWithDisposition(browser_, IDC_SHOW_HISTORY, |
| 198 ui::DispositionFromEventFlags(event_flags)); |
| 199 return; |
| 200 } |
| 192 | 201 |
| 193 DCHECK_NE(kDisabledCommandId, command_id); | 202 DCHECK_NE(kDisabledCommandId, command_id); |
| 194 DCHECK_NE(IDC_RECENT_TABS_NO_DEVICE_TABS, command_id); | 203 DCHECK_NE(IDC_RECENT_TABS_NO_DEVICE_TABS, command_id); |
| 195 | 204 |
| 196 int model_idx = CommandIdToModelIndex(command_id); | 205 int model_idx = CommandIdToModelIndex(command_id); |
| 197 DCHECK(model_idx >= 0 && model_idx < static_cast<int>(model_.size())); | 206 DCHECK(model_idx >= 0 && model_idx < static_cast<int>(model_.size())); |
| 198 const NavigationItem& item = model_[model_idx]; | 207 const NavigationItem& item = model_[model_idx]; |
| 199 DCHECK(item.tab_id > -1 && item.url.is_valid()); | 208 DCHECK(item.tab_id > -1 && item.url.is_valid()); |
| 200 | 209 |
| 201 WindowOpenDisposition disposition = | 210 WindowOpenDisposition disposition = |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 std::vector<const browser_sync::SyncedSession*> sessions; | 272 std::vector<const browser_sync::SyncedSession*> sessions; |
| 264 if (!associator->GetAllForeignSessions(&sessions)) | 273 if (!associator->GetAllForeignSessions(&sessions)) |
| 265 return; | 274 return; |
| 266 | 275 |
| 267 // Sort sessions from most recent to least recent. | 276 // Sort sessions from most recent to least recent. |
| 268 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency); | 277 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency); |
| 269 | 278 |
| 270 const size_t kMaxSessionsToShow = 3; | 279 const size_t kMaxSessionsToShow = 3; |
| 271 size_t num_sessions_added = 0; | 280 size_t num_sessions_added = 0; |
| 272 for (size_t i = 0; | 281 for (size_t i = 0; |
| 273 i < sessions.size() && num_sessions_added < kMaxSessionsToShow; | 282 i < sessions.size() && num_sessions_added < kMaxSessionsToShow; ++i) { |
| 274 ++i) { | |
| 275 const browser_sync::SyncedSession* session = sessions[i]; | 283 const browser_sync::SyncedSession* session = sessions[i]; |
| 276 const std::string& session_tag = session->session_tag; | 284 const std::string& session_tag = session->session_tag; |
| 277 | 285 |
| 278 // Get windows of session. | 286 // Get windows of session. |
| 279 std::vector<const SessionWindow*> windows; | 287 std::vector<const SessionWindow*> windows; |
| 280 if (!associator->GetForeignSession(session_tag, &windows) || | 288 if (!associator->GetForeignSession(session_tag, &windows) || |
| 281 windows.empty()) { | 289 windows.empty()) { |
| 282 continue; | 290 continue; |
| 283 } | 291 } |
| 284 | 292 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 315 // Build tab menu items from sorted session tabs. | 323 // Build tab menu items from sorted session tabs. |
| 316 const size_t kMaxTabsPerSessionToShow = 4; | 324 const size_t kMaxTabsPerSessionToShow = 4; |
| 317 for (size_t k = 0; | 325 for (size_t k = 0; |
| 318 k < std::min(tabs_in_session.size(), kMaxTabsPerSessionToShow); | 326 k < std::min(tabs_in_session.size(), kMaxTabsPerSessionToShow); |
| 319 ++k) { | 327 ++k) { |
| 320 BuildForeignTabItem(session_tag, *tabs_in_session[k]); | 328 BuildForeignTabItem(session_tag, *tabs_in_session[k]); |
| 321 } // for all tabs in one session | 329 } // for all tabs in one session |
| 322 | 330 |
| 323 ++num_sessions_added; | 331 ++num_sessions_added; |
| 324 } // for all sessions | 332 } // for all sessions |
| 333 |
| 334 // We are not supposed to get here unless at least some items were added. |
| 335 DCHECK_GT(GetItemCount(), 0); |
| 336 AddSeparator(ui::NORMAL_SEPARATOR); |
| 337 AddItemWithStringId(IDC_SHOW_HISTORY, IDS_RECENT_TABS_MORE); |
| 325 } | 338 } |
| 326 | 339 |
| 327 void RecentTabsSubMenuModel::BuildForeignTabItem( | 340 void RecentTabsSubMenuModel::BuildForeignTabItem( |
| 328 const std::string& session_tag, | 341 const std::string& session_tag, |
| 329 const SessionTab& tab) { | 342 const SessionTab& tab) { |
| 330 const TabNavigation& current_navigation = | 343 const TabNavigation& current_navigation = |
| 331 tab.navigations.at(tab.normalized_navigation_index()); | 344 tab.navigations.at(tab.normalized_navigation_index()); |
| 332 NavigationItem item(session_tag, tab.tab_id.id(), | 345 NavigationItem item(session_tag, tab.tab_id.id(), |
| 333 current_navigation.virtual_url()); | 346 current_navigation.virtual_url()); |
| 334 int command_id = ModelIndexToCommandId(model_.size()); | 347 int command_id = ModelIndexToCommandId(model_.size()); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 RecentTabsSubMenuModel::GetModelAssociator() { | 437 RecentTabsSubMenuModel::GetModelAssociator() { |
| 425 if (!associator_) { | 438 if (!associator_) { |
| 426 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> | 439 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
| 427 GetForProfile(browser_->profile()); | 440 GetForProfile(browser_->profile()); |
| 428 // Only return the associator if it exists and it is done syncing sessions. | 441 // Only return the associator if it exists and it is done syncing sessions. |
| 429 if (service && service->ShouldPushChanges()) | 442 if (service && service->ShouldPushChanges()) |
| 430 associator_ = service->GetSessionModelAssociator(); | 443 associator_ = service->GetSessionModelAssociator(); |
| 431 } | 444 } |
| 432 return associator_; | 445 return associator_; |
| 433 } | 446 } |
| OLD | NEW |