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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
110 | 110 |
111 // Note that all requests sent to either the history service or the favicon | 111 // Note that all requests sent to either the history service or the favicon |
112 // service will be automatically cancelled by their respective Consumers, so | 112 // service will be automatically cancelled by their respective Consumers, so |
113 // task cancellation is not done manually here in the dtor. | 113 // task cancellation is not done manually here in the dtor. |
114 HistoryMenuBridge::~HistoryMenuBridge() { | 114 HistoryMenuBridge::~HistoryMenuBridge() { |
115 // Unregister ourselves as observers and notifications. | 115 // Unregister ourselves as observers and notifications. |
116 DCHECK(profile_); | 116 DCHECK(profile_); |
117 if (history_service_) { | 117 if (history_service_) { |
118 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED, | 118 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED, |
119 content::Source<Profile>(profile_)); | 119 content::Source<Profile>(profile_)); |
120 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_URL_VISITED, | |
121 content::Source<Profile>(profile_)); | |
122 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, | 120 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, |
123 content::Source<Profile>(profile_)); | 121 content::Source<Profile>(profile_)); |
122 history_service_->RemoveObserver(this); | |
124 } else { | 123 } else { |
125 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_LOADED, | 124 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_LOADED, |
126 content::Source<Profile>(profile_)); | 125 content::Source<Profile>(profile_)); |
127 } | 126 } |
128 | 127 |
129 if (tab_restore_service_) | 128 if (tab_restore_service_) |
130 tab_restore_service_->RemoveObserver(this); | 129 tab_restore_service_->RemoveObserver(this); |
131 | 130 |
132 // Since the map owns the HistoryItems, delete anything that still exists. | 131 // Since the map owns the HistoryItems, delete anything that still exists. |
133 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.begin(); | 132 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.begin(); |
(...skipping 22 matching lines...) Expand all Loading... | |
156 content::Source<Profile>(profile_)); | 155 content::Source<Profile>(profile_)); |
157 } | 156 } |
158 } | 157 } |
159 | 158 |
160 // All other notification types that we observe indicate that the history has | 159 // All other notification types that we observe indicate that the history has |
161 // changed and we need to rebuild. | 160 // changed and we need to rebuild. |
162 need_recreate_ = true; | 161 need_recreate_ = true; |
163 CreateMenu(); | 162 CreateMenu(); |
164 } | 163 } |
165 | 164 |
165 void HistoryMenuBridge::OnURLVisited(HistoryService* history_service, | |
166 ui::PageTransition transition, | |
167 const history::URLRow& row, | |
168 const history::RedirectList& redirects, | |
169 base::Time visit_time) { | |
170 // History has changed, rebuild menu. | |
171 need_recreate_ = true; | |
droger
2014/10/14 15:15:24
Should we share the code with Observe() for this?
sdefresne
2014/10/14 16:02:05
Done.
| |
172 CreateMenu(); | |
173 } | |
174 | |
166 void HistoryMenuBridge::TabRestoreServiceChanged(TabRestoreService* service) { | 175 void HistoryMenuBridge::TabRestoreServiceChanged(TabRestoreService* service) { |
167 const TabRestoreService::Entries& entries = service->entries(); | 176 const TabRestoreService::Entries& entries = service->entries(); |
168 | 177 |
169 // Clear the history menu before rebuilding. | 178 // Clear the history menu before rebuilding. |
170 NSMenu* menu = HistoryMenu(); | 179 NSMenu* menu = HistoryMenu(); |
171 ClearMenuSection(menu, kRecentlyClosed); | 180 ClearMenuSection(menu, kRecentlyClosed); |
172 | 181 |
173 // Index for the next menu item. | 182 // Index for the next menu item. |
174 NSInteger index = [menu indexOfItemWithTag:kRecentlyClosedTitle] + 1; | 183 NSInteger index = [menu indexOfItemWithTag:kRecentlyClosedTitle] + 1; |
175 NSUInteger added_count = 0; | 184 NSUInteger added_count = 0; |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
353 base::SysUTF16ToNSString(full_title), base::SysUTF8ToNSString(url)]; | 362 base::SysUTF16ToNSString(full_title), base::SysUTF8ToNSString(url)]; |
354 [item->menu_item setToolTip:tooltip]; | 363 [item->menu_item setToolTip:tooltip]; |
355 | 364 |
356 [menu insertItem:item->menu_item.get() atIndex:index]; | 365 [menu insertItem:item->menu_item.get() atIndex:index]; |
357 menu_item_map_.insert(std::make_pair(item->menu_item.get(), item)); | 366 menu_item_map_.insert(std::make_pair(item->menu_item.get(), item)); |
358 | 367 |
359 return item->menu_item.get(); | 368 return item->menu_item.get(); |
360 } | 369 } |
361 | 370 |
362 void HistoryMenuBridge::Init() { | 371 void HistoryMenuBridge::Init() { |
372 DCHECK(history_service_); | |
363 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED, | 373 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED, |
364 content::Source<Profile>(profile_)); | 374 content::Source<Profile>(profile_)); |
365 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED, | |
366 content::Source<Profile>(profile_)); | |
367 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, | 375 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, |
368 content::Source<Profile>(profile_)); | 376 content::Source<Profile>(profile_)); |
377 history_service_->AddObserver(this); | |
369 } | 378 } |
370 | 379 |
371 void HistoryMenuBridge::CreateMenu() { | 380 void HistoryMenuBridge::CreateMenu() { |
372 // If we're currently running CreateMenu(), wait until it finishes. | 381 // If we're currently running CreateMenu(), wait until it finishes. |
373 if (create_in_progress_) | 382 if (create_in_progress_) |
374 return; | 383 return; |
375 create_in_progress_ = true; | 384 create_in_progress_ = true; |
376 need_recreate_ = false; | 385 need_recreate_ = false; |
377 | 386 |
378 DCHECK(history_service_); | 387 DCHECK(history_service_); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
465 } | 474 } |
466 | 475 |
467 void HistoryMenuBridge::CancelFaviconRequest(HistoryItem* item) { | 476 void HistoryMenuBridge::CancelFaviconRequest(HistoryItem* item) { |
468 DCHECK(item); | 477 DCHECK(item); |
469 if (item->icon_requested) { | 478 if (item->icon_requested) { |
470 cancelable_task_tracker_.TryCancel(item->icon_task_id); | 479 cancelable_task_tracker_.TryCancel(item->icon_task_id); |
471 item->icon_requested = false; | 480 item->icon_requested = false; |
472 item->icon_task_id = base::CancelableTaskTracker::kBadTaskId; | 481 item->icon_task_id = base::CancelableTaskTracker::kBadTaskId; |
473 } | 482 } |
474 } | 483 } |
OLD | NEW |