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