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" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "chrome/app/chrome_command_ids.h" // IDC_HISTORY_MENU | 12 #include "chrome/app/chrome_command_ids.h" // IDC_HISTORY_MENU |
13 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
14 #include "chrome/browser/favicon/favicon_service_factory.h" | 14 #include "chrome/browser/favicon/favicon_service_factory.h" |
15 #include "chrome/browser/history/history_service_factory.h" | 15 #include "chrome/browser/history/history_service_factory.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/sessions/tab_restore_service_factory.h" | 17 #include "chrome/browser/sessions/tab_restore_service_factory.h" |
18 #import "chrome/browser/ui/cocoa/history_menu_cocoa_controller.h" | 18 #import "chrome/browser/ui/cocoa/history_menu_cocoa_controller.h" |
19 #include "chrome/grit/generated_resources.h" | 19 #include "chrome/grit/generated_resources.h" |
20 #include "content/public/browser/notification_registrar.h" | |
21 #include "content/public/browser/notification_source.h" | |
22 #include "grit/theme_resources.h" | 20 #include "grit/theme_resources.h" |
23 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
24 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
25 #include "ui/gfx/image/image.h" | 23 #include "ui/gfx/image/image.h" |
26 #include "ui/gfx/text_elider.h" | 24 #include "ui/gfx/text_elider.h" |
27 #include "ui/resources/grit/ui_resources.h" | 25 #include "ui/resources/grit/ui_resources.h" |
28 | 26 |
29 namespace { | 27 namespace { |
30 | 28 |
31 // Maximum number of pixels to use for a menu item title. | 29 // Maximum number of pixels to use for a menu item title. |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 [item setImage:rb.GetNativeImageNamed(IDR_HISTORY_FAVICON).ToNSImage()]; | 103 [item setImage:rb.GetNativeImageNamed(IDR_HISTORY_FAVICON).ToNSImage()]; |
106 | 104 |
107 } | 105 } |
108 | 106 |
109 // Note that all requests sent to either the history service or the favicon | 107 // Note that all requests sent to either the history service or the favicon |
110 // service will be automatically cancelled by their respective Consumers, so | 108 // service will be automatically cancelled by their respective Consumers, so |
111 // task cancellation is not done manually here in the dtor. | 109 // task cancellation is not done manually here in the dtor. |
112 HistoryMenuBridge::~HistoryMenuBridge() { | 110 HistoryMenuBridge::~HistoryMenuBridge() { |
113 // Unregister ourselves as observers and notifications. | 111 // Unregister ourselves as observers and notifications. |
114 DCHECK(profile_); | 112 DCHECK(profile_); |
115 if (history_service_) { | |
116 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, | |
117 content::Source<Profile>(profile_)); | |
118 } | |
119 | 113 |
120 if (tab_restore_service_) | 114 if (tab_restore_service_) |
121 tab_restore_service_->RemoveObserver(this); | 115 tab_restore_service_->RemoveObserver(this); |
122 | 116 |
123 // Since the map owns the HistoryItems, delete anything that still exists. | 117 // Since the map owns the HistoryItems, delete anything that still exists. |
124 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.begin(); | 118 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.begin(); |
125 while (it != menu_item_map_.end()) { | 119 while (it != menu_item_map_.end()) { |
126 HistoryItem* item = it->second; | 120 HistoryItem* item = it->second; |
127 menu_item_map_.erase(it++); | 121 menu_item_map_.erase(it++); |
128 delete item; | 122 delete item; |
129 } | 123 } |
130 } | 124 } |
131 | 125 |
132 void HistoryMenuBridge::Observe(int type, | |
133 const content::NotificationSource& source, | |
134 const content::NotificationDetails& details) { | |
135 // chrome::NOTIFICATION_HISTORY_URLS_DELETED is the only notification we are | |
136 // registered for. OnHistoryChanged is the generic function called for any | |
137 // History modifications. | |
138 OnHistoryChanged(); | |
139 } | |
140 | |
141 void HistoryMenuBridge::TabRestoreServiceChanged(TabRestoreService* service) { | 126 void HistoryMenuBridge::TabRestoreServiceChanged(TabRestoreService* service) { |
142 const TabRestoreService::Entries& entries = service->entries(); | 127 const TabRestoreService::Entries& entries = service->entries(); |
143 | 128 |
144 // Clear the history menu before rebuilding. | 129 // Clear the history menu before rebuilding. |
145 NSMenu* menu = HistoryMenu(); | 130 NSMenu* menu = HistoryMenu(); |
146 ClearMenuSection(menu, kRecentlyClosed); | 131 ClearMenuSection(menu, kRecentlyClosed); |
147 | 132 |
148 // Index for the next menu item. | 133 // Index for the next menu item. |
149 NSInteger index = [menu indexOfItemWithTag:kRecentlyClosedTitle] + 1; | 134 NSInteger index = [menu indexOfItemWithTag:kRecentlyClosedTitle] + 1; |
150 NSUInteger added_count = 0; | 135 NSUInteger added_count = 0; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 ClearMenuSection(menu, kRecentlyClosed); | 227 ClearMenuSection(menu, kRecentlyClosed); |
243 } | 228 } |
244 | 229 |
245 void HistoryMenuBridge::BuildMenu() { | 230 void HistoryMenuBridge::BuildMenu() { |
246 // If the history service is ready, use it. Otherwise, a Notification will | 231 // If the history service is ready, use it. Otherwise, a Notification will |
247 // force an update when it's loaded. | 232 // force an update when it's loaded. |
248 if (history_service_) | 233 if (history_service_) |
249 CreateMenu(); | 234 CreateMenu(); |
250 } | 235 } |
251 | 236 |
252 void HistoryMenuBridge::OnURLVisited(HistoryService* history_service, | |
253 ui::PageTransition transition, | |
254 const history::URLRow& row, | |
255 const history::RedirectList& redirects, | |
256 base::Time visit_time) { | |
257 OnHistoryChanged(); | |
258 } | |
259 | |
260 void HistoryMenuBridge::OnURLsModified(HistoryService* history_service, | |
261 const history::URLRows& changed_urls) { | |
262 OnHistoryChanged(); | |
263 } | |
264 | |
265 void HistoryMenuBridge::OnHistoryServiceLoaded( | |
266 HistoryService* history_service) { | |
267 history_service_ = history_service; | |
268 Init(); | |
269 } | |
270 | |
271 HistoryMenuBridge::HistoryItem* HistoryMenuBridge::HistoryItemForMenuItem( | 237 HistoryMenuBridge::HistoryItem* HistoryMenuBridge::HistoryItemForMenuItem( |
272 NSMenuItem* item) { | 238 NSMenuItem* item) { |
273 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.find(item); | 239 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.find(item); |
274 if (it != menu_item_map_.end()) { | 240 if (it != menu_item_map_.end()) { |
275 return it->second; | 241 return it->second; |
276 } | 242 } |
277 return NULL; | 243 return NULL; |
278 } | 244 } |
279 | 245 |
280 HistoryService* HistoryMenuBridge::service() { | 246 HistoryService* HistoryMenuBridge::service() { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 [item->menu_item setToolTip:tooltip]; | 314 [item->menu_item setToolTip:tooltip]; |
349 | 315 |
350 [menu insertItem:item->menu_item.get() atIndex:index]; | 316 [menu insertItem:item->menu_item.get() atIndex:index]; |
351 menu_item_map_.insert(std::make_pair(item->menu_item.get(), item)); | 317 menu_item_map_.insert(std::make_pair(item->menu_item.get(), item)); |
352 | 318 |
353 return item->menu_item.get(); | 319 return item->menu_item.get(); |
354 } | 320 } |
355 | 321 |
356 void HistoryMenuBridge::Init() { | 322 void HistoryMenuBridge::Init() { |
357 DCHECK(history_service_); | 323 DCHECK(history_service_); |
358 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, | |
359 content::Source<Profile>(profile_)); | |
360 } | 324 } |
361 | 325 |
362 void HistoryMenuBridge::CreateMenu() { | 326 void HistoryMenuBridge::CreateMenu() { |
363 // If we're currently running CreateMenu(), wait until it finishes. | 327 // If we're currently running CreateMenu(), wait until it finishes. |
364 if (create_in_progress_) | 328 if (create_in_progress_) |
365 return; | 329 return; |
366 create_in_progress_ = true; | 330 create_in_progress_ = true; |
367 need_recreate_ = false; | 331 need_recreate_ = false; |
368 | 332 |
369 DCHECK(history_service_); | 333 DCHECK(history_service_); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 } | 426 } |
463 | 427 |
464 void HistoryMenuBridge::CancelFaviconRequest(HistoryItem* item) { | 428 void HistoryMenuBridge::CancelFaviconRequest(HistoryItem* item) { |
465 DCHECK(item); | 429 DCHECK(item); |
466 if (item->icon_requested) { | 430 if (item->icon_requested) { |
467 cancelable_task_tracker_.TryCancel(item->icon_task_id); | 431 cancelable_task_tracker_.TryCancel(item->icon_task_id); |
468 item->icon_requested = false; | 432 item->icon_requested = false; |
469 item->icon_task_id = base::CancelableTaskTracker::kBadTaskId; | 433 item->icon_task_id = base::CancelableTaskTracker::kBadTaskId; |
470 } | 434 } |
471 } | 435 } |
| 436 |
| 437 void HistoryMenuBridge::OnURLVisited(HistoryService* history_service, |
| 438 ui::PageTransition transition, |
| 439 const history::URLRow& row, |
| 440 const history::RedirectList& redirects, |
| 441 base::Time visit_time) { |
| 442 OnHistoryChanged(); |
| 443 } |
| 444 |
| 445 void HistoryMenuBridge::OnURLsModified(HistoryService* history_service, |
| 446 const history::URLRows& changed_urls) { |
| 447 OnHistoryChanged(); |
| 448 } |
| 449 |
| 450 void HistoryMenuBridge::OnURLsDeleted( |
| 451 HistoryService* history_service, |
| 452 const history::URLsDeletedDetails& deleted_details) { |
| 453 OnHistoryChanged(); |
| 454 } |
| 455 |
| 456 void HistoryMenuBridge::OnHistoryServiceLoaded( |
| 457 HistoryService* history_service) { |
| 458 history_service_ = history_service; |
| 459 Init(); |
| 460 } |
OLD | NEW |