| 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 "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/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 115 |
| 116 // Since the map owns the HistoryItems, delete anything that still exists. | 116 // Since the map owns the HistoryItems, delete anything that still exists. |
| 117 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.begin(); | 117 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.begin(); |
| 118 while (it != menu_item_map_.end()) { | 118 while (it != menu_item_map_.end()) { |
| 119 HistoryItem* item = it->second; | 119 HistoryItem* item = it->second; |
| 120 menu_item_map_.erase(it++); | 120 menu_item_map_.erase(it++); |
| 121 delete item; | 121 delete item; |
| 122 } | 122 } |
| 123 } | 123 } |
| 124 | 124 |
| 125 void HistoryMenuBridge::TabRestoreServiceChanged(TabRestoreService* service) { | 125 void HistoryMenuBridge::TabRestoreServiceChanged( |
| 126 const TabRestoreService::Entries& entries = service->entries(); | 126 sessions::TabRestoreService* service) { |
| 127 const sessions::TabRestoreService::Entries& entries = service->entries(); |
| 127 | 128 |
| 128 // Clear the history menu before rebuilding. | 129 // Clear the history menu before rebuilding. |
| 129 NSMenu* menu = HistoryMenu(); | 130 NSMenu* menu = HistoryMenu(); |
| 130 ClearMenuSection(menu, kRecentlyClosed); | 131 ClearMenuSection(menu, kRecentlyClosed); |
| 131 | 132 |
| 132 // Index for the next menu item. | 133 // Index for the next menu item. |
| 133 NSInteger index = [menu indexOfItemWithTag:kRecentlyClosedTitle] + 1; | 134 NSInteger index = [menu indexOfItemWithTag:kRecentlyClosedTitle] + 1; |
| 134 NSUInteger added_count = 0; | 135 NSUInteger added_count = 0; |
| 135 | 136 |
| 136 for (TabRestoreService::Entries::const_iterator it = entries.begin(); | 137 for (sessions::TabRestoreService::Entries::const_iterator it = |
| 138 entries.begin(); |
| 137 it != entries.end() && added_count < kRecentlyClosedCount; ++it) { | 139 it != entries.end() && added_count < kRecentlyClosedCount; ++it) { |
| 138 TabRestoreService::Entry* entry = *it; | 140 sessions::TabRestoreService::Entry* entry = *it; |
| 139 | 141 |
| 140 // If this is a window, create a submenu for all of its tabs. | 142 // If this is a window, create a submenu for all of its tabs. |
| 141 if (entry->type == TabRestoreService::WINDOW) { | 143 if (entry->type == sessions::TabRestoreService::WINDOW) { |
| 142 TabRestoreService::Window* entry_win = (TabRestoreService::Window*)entry; | 144 sessions::TabRestoreService::Window* entry_win = |
| 143 std::vector<TabRestoreService::Tab>& tabs = entry_win->tabs; | 145 (sessions::TabRestoreService::Window*)entry; |
| 146 std::vector<sessions::TabRestoreService::Tab>& tabs = entry_win->tabs; |
| 144 if (!tabs.size()) | 147 if (!tabs.size()) |
| 145 continue; | 148 continue; |
| 146 | 149 |
| 147 // Create the item for the parent/window. Do not set the title yet because | 150 // Create the item for the parent/window. Do not set the title yet because |
| 148 // the actual number of items that are in the menu will not be known until | 151 // the actual number of items that are in the menu will not be known until |
| 149 // things like the NTP are filtered out, which is done when the tab items | 152 // things like the NTP are filtered out, which is done when the tab items |
| 150 // are actually created. | 153 // are actually created. |
| 151 HistoryItem* item = new HistoryItem(); | 154 HistoryItem* item = new HistoryItem(); |
| 152 item->session_id = entry_win->id; | 155 item->session_id = entry_win->id; |
| 153 | 156 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 165 // Duplicate the HistoryItem otherwise the different NSMenuItems will | 168 // Duplicate the HistoryItem otherwise the different NSMenuItems will |
| 166 // point to the same HistoryItem, which would then be double-freed when | 169 // point to the same HistoryItem, which would then be double-freed when |
| 167 // removing the items from the map or in the dtor. | 170 // removing the items from the map or in the dtor. |
| 168 HistoryItem* dup_item = new HistoryItem(*item); | 171 HistoryItem* dup_item = new HistoryItem(*item); |
| 169 menu_item_map_.insert(std::make_pair(restore_item.get(), dup_item)); | 172 menu_item_map_.insert(std::make_pair(restore_item.get(), dup_item)); |
| 170 [submenu addItem:restore_item.get()]; | 173 [submenu addItem:restore_item.get()]; |
| 171 [submenu addItem:[NSMenuItem separatorItem]]; | 174 [submenu addItem:[NSMenuItem separatorItem]]; |
| 172 | 175 |
| 173 // Loop over the window's tabs and add them to the submenu. | 176 // Loop over the window's tabs and add them to the submenu. |
| 174 NSInteger subindex = [[submenu itemArray] count]; | 177 NSInteger subindex = [[submenu itemArray] count]; |
| 175 std::vector<TabRestoreService::Tab>::const_iterator it; | 178 std::vector<sessions::TabRestoreService::Tab>::const_iterator it; |
| 176 for (it = tabs.begin(); it != tabs.end(); ++it) { | 179 for (it = tabs.begin(); it != tabs.end(); ++it) { |
| 177 TabRestoreService::Tab tab = *it; | 180 sessions::TabRestoreService::Tab tab = *it; |
| 178 HistoryItem* tab_item = HistoryItemForTab(tab); | 181 HistoryItem* tab_item = HistoryItemForTab(tab); |
| 179 if (tab_item) { | 182 if (tab_item) { |
| 180 item->tabs.push_back(tab_item); | 183 item->tabs.push_back(tab_item); |
| 181 AddItemToMenu(tab_item, submenu.get(), kRecentlyClosed + 1, | 184 AddItemToMenu(tab_item, submenu.get(), kRecentlyClosed + 1, |
| 182 subindex++); | 185 subindex++); |
| 183 } | 186 } |
| 184 } | 187 } |
| 185 | 188 |
| 186 // 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 |
| 187 // of the parent menu item. | 190 // of the parent menu item. |
| 188 item->title = l10n_util::GetPluralStringFUTF16( | 191 item->title = l10n_util::GetPluralStringFUTF16( |
| 189 IDS_RECENTLY_CLOSED_WINDOW, item->tabs.size()); | 192 IDS_RECENTLY_CLOSED_WINDOW, item->tabs.size()); |
| 190 | 193 |
| 191 // 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 |
| 192 // 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. |
| 193 if ([[submenu itemArray] count] > 2) { | 196 if ([[submenu itemArray] count] > 2) { |
| 194 // Create the menu item parent. | 197 // Create the menu item parent. |
| 195 NSMenuItem* parent_item = | 198 NSMenuItem* parent_item = |
| 196 AddItemToMenu(item, menu, kRecentlyClosed, index++); | 199 AddItemToMenu(item, menu, kRecentlyClosed, index++); |
| 197 [parent_item setSubmenu:submenu.get()]; | 200 [parent_item setSubmenu:submenu.get()]; |
| 198 ++added_count; | 201 ++added_count; |
| 199 } | 202 } |
| 200 } else if (entry->type == TabRestoreService::TAB) { | 203 } else if (entry->type == sessions::TabRestoreService::TAB) { |
| 201 TabRestoreService::Tab* tab = | 204 sessions::TabRestoreService::Tab* tab = |
| 202 static_cast<TabRestoreService::Tab*>(entry); | 205 static_cast<sessions::TabRestoreService::Tab*>(entry); |
| 203 HistoryItem* item = HistoryItemForTab(*tab); | 206 HistoryItem* item = HistoryItemForTab(*tab); |
| 204 if (item) { | 207 if (item) { |
| 205 AddItemToMenu(item, menu, kRecentlyClosed, index++); | 208 AddItemToMenu(item, menu, kRecentlyClosed, index++); |
| 206 ++added_count; | 209 ++added_count; |
| 207 } | 210 } |
| 208 } | 211 } |
| 209 } | 212 } |
| 210 } | 213 } |
| 211 | 214 |
| 212 void HistoryMenuBridge::TabRestoreServiceDestroyed( | 215 void HistoryMenuBridge::TabRestoreServiceDestroyed( |
| 213 TabRestoreService* service) { | 216 sessions::TabRestoreService* service) { |
| 214 // Intentionally left blank. We hold a weak reference to the service. | 217 // Intentionally left blank. We hold a weak reference to the service. |
| 215 } | 218 } |
| 216 | 219 |
| 217 void HistoryMenuBridge::ResetMenu() { | 220 void HistoryMenuBridge::ResetMenu() { |
| 218 NSMenu* menu = HistoryMenu(); | 221 NSMenu* menu = HistoryMenu(); |
| 219 ClearMenuSection(menu, kVisited); | 222 ClearMenuSection(menu, kVisited); |
| 220 ClearMenuSection(menu, kRecentlyClosed); | 223 ClearMenuSection(menu, kRecentlyClosed); |
| 221 } | 224 } |
| 222 | 225 |
| 223 void HistoryMenuBridge::BuildMenu() { | 226 void HistoryMenuBridge::BuildMenu() { |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 } | 368 } |
| 366 | 369 |
| 367 // We are already invalid by the time we finished, darn. | 370 // We are already invalid by the time we finished, darn. |
| 368 if (need_recreate_) | 371 if (need_recreate_) |
| 369 CreateMenu(); | 372 CreateMenu(); |
| 370 | 373 |
| 371 create_in_progress_ = false; | 374 create_in_progress_ = false; |
| 372 } | 375 } |
| 373 | 376 |
| 374 HistoryMenuBridge::HistoryItem* HistoryMenuBridge::HistoryItemForTab( | 377 HistoryMenuBridge::HistoryItem* HistoryMenuBridge::HistoryItemForTab( |
| 375 const TabRestoreService::Tab& entry) { | 378 const sessions::TabRestoreService::Tab& entry) { |
| 376 DCHECK(!entry.navigations.empty()); | 379 DCHECK(!entry.navigations.empty()); |
| 377 | 380 |
| 378 const sessions::SerializedNavigationEntry& current_navigation = | 381 const sessions::SerializedNavigationEntry& current_navigation = |
| 379 entry.navigations.at(entry.current_navigation_index); | 382 entry.navigations.at(entry.current_navigation_index); |
| 380 HistoryItem* item = new HistoryItem(); | 383 HistoryItem* item = new HistoryItem(); |
| 381 item->title = current_navigation.title(); | 384 item->title = current_navigation.title(); |
| 382 item->url = current_navigation.virtual_url(); | 385 item->url = current_navigation.virtual_url(); |
| 383 item->session_id = entry.id; | 386 item->session_id = entry.id; |
| 384 | 387 |
| 385 // Tab navigations don't come with icons, so we always have to request them. | 388 // Tab navigations don't come with icons, so we always have to request them. |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 const history::URLRows& deleted_rows, | 449 const history::URLRows& deleted_rows, |
| 447 const std::set<GURL>& favicon_urls) { | 450 const std::set<GURL>& favicon_urls) { |
| 448 OnHistoryChanged(); | 451 OnHistoryChanged(); |
| 449 } | 452 } |
| 450 | 453 |
| 451 void HistoryMenuBridge::OnHistoryServiceLoaded( | 454 void HistoryMenuBridge::OnHistoryServiceLoaded( |
| 452 history::HistoryService* history_service) { | 455 history::HistoryService* history_service) { |
| 453 history_service_ = history_service; | 456 history_service_ = history_service; |
| 454 Init(); | 457 Init(); |
| 455 } | 458 } |
| OLD | NEW |