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 17 matching lines...) Expand all Loading... |
151 Init(); | 150 Init(); |
152 | 151 |
153 // Found our HistoryService, so stop listening for this notification. | 152 // Found our HistoryService, so stop listening for this notification. |
154 registrar_.Remove(this, | 153 registrar_.Remove(this, |
155 chrome::NOTIFICATION_HISTORY_LOADED, | 154 chrome::NOTIFICATION_HISTORY_LOADED, |
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. |
162 need_recreate_ = true; | 161 OnHistoryChanged(); |
163 CreateMenu(); | |
164 } | 162 } |
165 | 163 |
166 void HistoryMenuBridge::TabRestoreServiceChanged(TabRestoreService* service) { | 164 void HistoryMenuBridge::TabRestoreServiceChanged(TabRestoreService* service) { |
167 const TabRestoreService::Entries& entries = service->entries(); | 165 const TabRestoreService::Entries& entries = service->entries(); |
168 | 166 |
169 // Clear the history menu before rebuilding. | 167 // Clear the history menu before rebuilding. |
170 NSMenu* menu = HistoryMenu(); | 168 NSMenu* menu = HistoryMenu(); |
171 ClearMenuSection(menu, kRecentlyClosed); | 169 ClearMenuSection(menu, kRecentlyClosed); |
172 | 170 |
173 // Index for the next menu item. | 171 // Index for the next menu item. |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 ClearMenuSection(menu, kRecentlyClosed); | 265 ClearMenuSection(menu, kRecentlyClosed); |
268 } | 266 } |
269 | 267 |
270 void HistoryMenuBridge::BuildMenu() { | 268 void HistoryMenuBridge::BuildMenu() { |
271 // If the history service is ready, use it. Otherwise, a Notification will | 269 // If the history service is ready, use it. Otherwise, a Notification will |
272 // force an update when it's loaded. | 270 // force an update when it's loaded. |
273 if (history_service_) | 271 if (history_service_) |
274 CreateMenu(); | 272 CreateMenu(); |
275 } | 273 } |
276 | 274 |
| 275 void HistoryMenuBridge::OnURLVisited(HistoryService* history_service, |
| 276 ui::PageTransition transition, |
| 277 const history::URLRow& row, |
| 278 const history::RedirectList& redirects, |
| 279 base::Time visit_time) { |
| 280 OnHistoryChanged(); |
| 281 } |
| 282 |
277 HistoryMenuBridge::HistoryItem* HistoryMenuBridge::HistoryItemForMenuItem( | 283 HistoryMenuBridge::HistoryItem* HistoryMenuBridge::HistoryItemForMenuItem( |
278 NSMenuItem* item) { | 284 NSMenuItem* item) { |
279 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.find(item); | 285 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.find(item); |
280 if (it != menu_item_map_.end()) { | 286 if (it != menu_item_map_.end()) { |
281 return it->second; | 287 return it->second; |
282 } | 288 } |
283 return NULL; | 289 return NULL; |
284 } | 290 } |
285 | 291 |
286 HistoryService* HistoryMenuBridge::service() { | 292 HistoryService* HistoryMenuBridge::service() { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 base::SysUTF16ToNSString(full_title), base::SysUTF8ToNSString(url)]; | 359 base::SysUTF16ToNSString(full_title), base::SysUTF8ToNSString(url)]; |
354 [item->menu_item setToolTip:tooltip]; | 360 [item->menu_item setToolTip:tooltip]; |
355 | 361 |
356 [menu insertItem:item->menu_item.get() atIndex:index]; | 362 [menu insertItem:item->menu_item.get() atIndex:index]; |
357 menu_item_map_.insert(std::make_pair(item->menu_item.get(), item)); | 363 menu_item_map_.insert(std::make_pair(item->menu_item.get(), item)); |
358 | 364 |
359 return item->menu_item.get(); | 365 return item->menu_item.get(); |
360 } | 366 } |
361 | 367 |
362 void HistoryMenuBridge::Init() { | 368 void HistoryMenuBridge::Init() { |
| 369 DCHECK(history_service_); |
363 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED, | 370 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED, |
364 content::Source<Profile>(profile_)); | 371 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, | 372 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, |
368 content::Source<Profile>(profile_)); | 373 content::Source<Profile>(profile_)); |
| 374 history_service_->AddObserver(this); |
369 } | 375 } |
370 | 376 |
371 void HistoryMenuBridge::CreateMenu() { | 377 void HistoryMenuBridge::CreateMenu() { |
372 // If we're currently running CreateMenu(), wait until it finishes. | 378 // If we're currently running CreateMenu(), wait until it finishes. |
373 if (create_in_progress_) | 379 if (create_in_progress_) |
374 return; | 380 return; |
375 create_in_progress_ = true; | 381 create_in_progress_ = true; |
376 need_recreate_ = false; | 382 need_recreate_ = false; |
377 | 383 |
378 DCHECK(history_service_); | 384 DCHECK(history_service_); |
379 | 385 |
380 history::QueryOptions options; | 386 history::QueryOptions options; |
381 options.max_count = kVisitedCount; | 387 options.max_count = kVisitedCount; |
382 options.SetRecentDayRange(kVisitedScope); | 388 options.SetRecentDayRange(kVisitedScope); |
383 | 389 |
384 history_service_->QueryHistory( | 390 history_service_->QueryHistory( |
385 base::string16(), | 391 base::string16(), |
386 options, | 392 options, |
387 base::Bind(&HistoryMenuBridge::OnVisitedHistoryResults, | 393 base::Bind(&HistoryMenuBridge::OnVisitedHistoryResults, |
388 base::Unretained(this)), | 394 base::Unretained(this)), |
389 &cancelable_task_tracker_); | 395 &cancelable_task_tracker_); |
390 } | 396 } |
391 | 397 |
| 398 void HistoryMenuBridge::OnHistoryChanged() { |
| 399 // History has changed, rebuild menu. |
| 400 need_recreate_ = true; |
| 401 CreateMenu(); |
| 402 } |
| 403 |
392 void HistoryMenuBridge::OnVisitedHistoryResults( | 404 void HistoryMenuBridge::OnVisitedHistoryResults( |
393 history::QueryResults* results) { | 405 history::QueryResults* results) { |
394 NSMenu* menu = HistoryMenu(); | 406 NSMenu* menu = HistoryMenu(); |
395 ClearMenuSection(menu, kVisited); | 407 ClearMenuSection(menu, kVisited); |
396 NSInteger top_item = [menu indexOfItemWithTag:kVisitedTitle] + 1; | 408 NSInteger top_item = [menu indexOfItemWithTag:kVisitedTitle] + 1; |
397 | 409 |
398 size_t count = results->size(); | 410 size_t count = results->size(); |
399 for (size_t i = 0; i < count; ++i) { | 411 for (size_t i = 0; i < count; ++i) { |
400 const history::URLResult& result = (*results)[i]; | 412 const history::URLResult& result = (*results)[i]; |
401 | 413 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 } | 477 } |
466 | 478 |
467 void HistoryMenuBridge::CancelFaviconRequest(HistoryItem* item) { | 479 void HistoryMenuBridge::CancelFaviconRequest(HistoryItem* item) { |
468 DCHECK(item); | 480 DCHECK(item); |
469 if (item->icon_requested) { | 481 if (item->icon_requested) { |
470 cancelable_task_tracker_.TryCancel(item->icon_task_id); | 482 cancelable_task_tracker_.TryCancel(item->icon_task_id); |
471 item->icon_requested = false; | 483 item->icon_requested = false; |
472 item->icon_task_id = base::CancelableTaskTracker::kBadTaskId; | 484 item->icon_task_id = base::CancelableTaskTracker::kBadTaskId; |
473 } | 485 } |
474 } | 486 } |
OLD | NEW |