| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/cocoa/history_menu_bridge.h" | 5 #include "chrome/browser/ui/cocoa/history_menu_bridge.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/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 const sessions::TabRestoreService::Entries& entries = service->entries(); | 130 const sessions::TabRestoreService::Entries& entries = service->entries(); |
| 131 | 131 |
| 132 // Clear the history menu before rebuilding. | 132 // Clear the history menu before rebuilding. |
| 133 NSMenu* menu = HistoryMenu(); | 133 NSMenu* menu = HistoryMenu(); |
| 134 ClearMenuSection(menu, kRecentlyClosed); | 134 ClearMenuSection(menu, kRecentlyClosed); |
| 135 | 135 |
| 136 // Index for the next menu item. | 136 // Index for the next menu item. |
| 137 NSInteger index = [menu indexOfItemWithTag:kRecentlyClosedTitle] + 1; | 137 NSInteger index = [menu indexOfItemWithTag:kRecentlyClosedTitle] + 1; |
| 138 NSUInteger added_count = 0; | 138 NSUInteger added_count = 0; |
| 139 | 139 |
| 140 for (sessions::TabRestoreService::Entries::const_iterator it = | 140 for (const auto& entry : entries) { |
| 141 entries.begin(); | 141 if (added_count >= kRecentlyClosedCount) |
| 142 it != entries.end() && added_count < kRecentlyClosedCount; ++it) { | 142 break; |
| 143 sessions::TabRestoreService::Entry* entry = *it; | |
| 144 | |
| 145 // If this is a window, create a submenu for all of its tabs. | 143 // If this is a window, create a submenu for all of its tabs. |
| 146 if (entry->type == sessions::TabRestoreService::WINDOW) { | 144 if (entry->type == sessions::TabRestoreService::WINDOW) { |
| 147 sessions::TabRestoreService::Window* entry_win = | 145 const auto* entry_win = |
| 148 (sessions::TabRestoreService::Window*)entry; | 146 static_cast<sessions::TabRestoreService::Window*>(entry.get()); |
| 149 std::vector<sessions::TabRestoreService::Tab>& tabs = entry_win->tabs; | 147 const std::vector<std::unique_ptr<sessions::TabRestoreService::Tab>>& |
| 148 tabs = entry_win->tabs; |
| 150 if (tabs.empty()) | 149 if (tabs.empty()) |
| 151 continue; | 150 continue; |
| 152 | 151 |
| 153 // Create the item for the parent/window. Do not set the title yet because | 152 // Create the item for the parent/window. Do not set the title yet because |
| 154 // the actual number of items that are in the menu will not be known until | 153 // the actual number of items that are in the menu will not be known until |
| 155 // things like the NTP are filtered out, which is done when the tab items | 154 // things like the NTP are filtered out, which is done when the tab items |
| 156 // are actually created. | 155 // are actually created. |
| 157 HistoryItem* item = new HistoryItem(); | 156 HistoryItem* item = new HistoryItem(); |
| 158 item->session_id = entry_win->id; | 157 item->session_id = entry_win->id; |
| 159 | 158 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 171 // Duplicate the HistoryItem otherwise the different NSMenuItems will | 170 // Duplicate the HistoryItem otherwise the different NSMenuItems will |
| 172 // point to the same HistoryItem, which would then be double-freed when | 171 // point to the same HistoryItem, which would then be double-freed when |
| 173 // removing the items from the map or in the dtor. | 172 // removing the items from the map or in the dtor. |
| 174 HistoryItem* dup_item = new HistoryItem(*item); | 173 HistoryItem* dup_item = new HistoryItem(*item); |
| 175 menu_item_map_.insert(std::make_pair(restore_item.get(), dup_item)); | 174 menu_item_map_.insert(std::make_pair(restore_item.get(), dup_item)); |
| 176 [submenu addItem:restore_item.get()]; | 175 [submenu addItem:restore_item.get()]; |
| 177 [submenu addItem:[NSMenuItem separatorItem]]; | 176 [submenu addItem:[NSMenuItem separatorItem]]; |
| 178 | 177 |
| 179 // Loop over the window's tabs and add them to the submenu. | 178 // Loop over the window's tabs and add them to the submenu. |
| 180 NSInteger subindex = [[submenu itemArray] count]; | 179 NSInteger subindex = [[submenu itemArray] count]; |
| 181 std::vector<sessions::TabRestoreService::Tab>::const_iterator it; | 180 for (const auto& tab : tabs) { |
| 182 for (it = tabs.begin(); it != tabs.end(); ++it) { | 181 HistoryItem* tab_item = HistoryItemForTab(*tab); |
| 183 HistoryItem* tab_item = HistoryItemForTab(*it); | |
| 184 if (tab_item) { | 182 if (tab_item) { |
| 185 item->tabs.push_back(tab_item); | 183 item->tabs.push_back(tab_item); |
| 186 AddItemToMenu(tab_item, submenu.get(), kRecentlyClosed + 1, | 184 AddItemToMenu(tab_item, submenu.get(), kRecentlyClosed + 1, |
| 187 subindex++); | 185 subindex++); |
| 188 } | 186 } |
| 189 } | 187 } |
| 190 | 188 |
| 191 // Now that the number of tabs that has been added is known, set the title | 189 // Now that the number of tabs that has been added is known, set the title |
| 192 // of the parent menu item. | 190 // of the parent menu item. |
| 193 item->title = l10n_util::GetPluralStringFUTF16( | 191 item->title = l10n_util::GetPluralStringFUTF16( |
| 194 IDS_RECENTLY_CLOSED_WINDOW, item->tabs.size()); | 192 IDS_RECENTLY_CLOSED_WINDOW, item->tabs.size()); |
| 195 | 193 |
| 196 // Sometimes it is possible for there to not be any subitems for a given | 194 // Sometimes it is possible for there to not be any subitems for a given |
| 197 // window; if that is the case, do not add the entry to the main menu. | 195 // window; if that is the case, do not add the entry to the main menu. |
| 198 if ([[submenu itemArray] count] > 2) { | 196 if ([[submenu itemArray] count] > 2) { |
| 199 // Create the menu item parent. | 197 // Create the menu item parent. |
| 200 NSMenuItem* parent_item = | 198 NSMenuItem* parent_item = |
| 201 AddItemToMenu(item, menu, kRecentlyClosed, index++); | 199 AddItemToMenu(item, menu, kRecentlyClosed, index++); |
| 202 [parent_item setSubmenu:submenu.get()]; | 200 [parent_item setSubmenu:submenu.get()]; |
| 203 ++added_count; | 201 ++added_count; |
| 204 } | 202 } |
| 205 } else if (entry->type == sessions::TabRestoreService::TAB) { | 203 } else if (entry->type == sessions::TabRestoreService::TAB) { |
| 206 sessions::TabRestoreService::Tab* tab = | 204 const auto& tab = static_cast<sessions::TabRestoreService::Tab&>(*entry); |
| 207 static_cast<sessions::TabRestoreService::Tab*>(entry); | 205 HistoryItem* item = HistoryItemForTab(tab); |
| 208 HistoryItem* item = HistoryItemForTab(*tab); | |
| 209 if (item) { | 206 if (item) { |
| 210 AddItemToMenu(item, menu, kRecentlyClosed, index++); | 207 AddItemToMenu(item, menu, kRecentlyClosed, index++); |
| 211 ++added_count; | 208 ++added_count; |
| 212 } | 209 } |
| 213 } | 210 } |
| 214 } | 211 } |
| 215 } | 212 } |
| 216 | 213 |
| 217 void HistoryMenuBridge::TabRestoreServiceDestroyed( | 214 void HistoryMenuBridge::TabRestoreServiceDestroyed( |
| 218 sessions::TabRestoreService* service) { | 215 sessions::TabRestoreService* service) { |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 const history::URLRows& deleted_rows, | 450 const history::URLRows& deleted_rows, |
| 454 const std::set<GURL>& favicon_urls) { | 451 const std::set<GURL>& favicon_urls) { |
| 455 OnHistoryChanged(); | 452 OnHistoryChanged(); |
| 456 } | 453 } |
| 457 | 454 |
| 458 void HistoryMenuBridge::OnHistoryServiceLoaded( | 455 void HistoryMenuBridge::OnHistoryServiceLoaded( |
| 459 history::HistoryService* history_service) { | 456 history::HistoryService* history_service) { |
| 460 history_service_ = history_service; | 457 history_service_ = history_service; |
| 461 Init(); | 458 Init(); |
| 462 } | 459 } |
| OLD | NEW |