Chromium Code Reviews| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 | 58 |
| 59 HistoryMenuBridge::HistoryItem::~HistoryItem() { | 59 HistoryMenuBridge::HistoryItem::~HistoryItem() { |
| 60 } | 60 } |
| 61 | 61 |
| 62 HistoryMenuBridge::HistoryMenuBridge(Profile* profile) | 62 HistoryMenuBridge::HistoryMenuBridge(Profile* profile) |
| 63 : controller_([[HistoryMenuCocoaController alloc] initWithBridge:this]), | 63 : controller_([[HistoryMenuCocoaController alloc] initWithBridge:this]), |
| 64 profile_(profile), | 64 profile_(profile), |
| 65 history_service_(NULL), | 65 history_service_(NULL), |
| 66 tab_restore_service_(NULL), | 66 tab_restore_service_(NULL), |
| 67 create_in_progress_(false), | 67 create_in_progress_(false), |
| 68 need_recreate_(false) { | 68 need_recreate_(false), |
| 69 history_service_observer_(this) { | |
| 69 // If we don't have a profile, do not bother initializing our data sources. | 70 // If we don't have a profile, do not bother initializing our data sources. |
| 70 // This shouldn't happen except in unit tests. | 71 // This shouldn't happen except in unit tests. |
| 71 if (profile_) { | 72 if (profile_) { |
| 72 // Check to see if the history service is ready. Because it loads async, it | 73 // Check to see if the history service is ready. Because it loads async, it |
| 73 // may not be ready when the Bridge is created. If this happens, register | 74 // may not be ready when the Bridge is created. If this happens, register |
| 74 // for a notification that tells us the HistoryService is ready. | 75 // for a notification that tells us the HistoryService is ready. |
| 75 HistoryService* hs = HistoryServiceFactory::GetForProfile( | 76 HistoryService* hs = HistoryServiceFactory::GetForProfile( |
| 76 profile_, Profile::EXPLICIT_ACCESS); | 77 profile_, Profile::EXPLICIT_ACCESS); |
| 77 if (hs != NULL && hs->BackendLoaded()) { | 78 if (hs && hs->BackendLoaded()) { |
|
Peter Kasting
2014/11/17 18:45:31
Nit: Longer, but seems a little clearer to me:
nshaik
2014/11/17 23:24:29
Done.
| |
| 78 history_service_ = hs; | 79 history_service_ = hs; |
| 79 Init(); | 80 Init(); |
| 81 } else if (hs) { | |
| 82 history_service_observer_.Add(hs); | |
| 80 } | 83 } |
| 81 | 84 |
| 82 tab_restore_service_ = TabRestoreServiceFactory::GetForProfile(profile_); | 85 tab_restore_service_ = TabRestoreServiceFactory::GetForProfile(profile_); |
| 83 if (tab_restore_service_) { | 86 if (tab_restore_service_) { |
| 84 tab_restore_service_->AddObserver(this); | 87 tab_restore_service_->AddObserver(this); |
| 85 // If the tab entries are already loaded, invoke the observer method to | 88 // If the tab entries are already loaded, invoke the observer method to |
| 86 // build the "Recently Closed" section. Otherwise it will be when the | 89 // build the "Recently Closed" section. Otherwise it will be when the |
| 87 // backend loads. | 90 // backend loads. |
| 88 if (!tab_restore_service_->IsLoaded()) | 91 if (!tab_restore_service_->IsLoaded()) |
| 89 tab_restore_service_->LoadTabsFromLastSession(); | 92 tab_restore_service_->LoadTabsFromLastSession(); |
| 90 else | 93 else |
| 91 TabRestoreServiceChanged(tab_restore_service_); | 94 TabRestoreServiceChanged(tab_restore_service_); |
| 92 } | 95 } |
| 93 } | 96 } |
| 94 | 97 |
| 95 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 98 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 96 default_favicon_.reset( | 99 default_favicon_.reset( |
| 97 rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).CopyNSImage()); | 100 rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).CopyNSImage()); |
| 98 | 101 |
| 99 // Set the static icons in the menu. | 102 // Set the static icons in the menu. |
| 100 NSMenuItem* item = [HistoryMenu() itemWithTag:IDC_SHOW_HISTORY]; | 103 NSMenuItem* item = [HistoryMenu() itemWithTag:IDC_SHOW_HISTORY]; |
| 101 [item setImage:rb.GetNativeImageNamed(IDR_HISTORY_FAVICON).ToNSImage()]; | 104 [item setImage:rb.GetNativeImageNamed(IDR_HISTORY_FAVICON).ToNSImage()]; |
| 102 | 105 |
| 103 // The service is not ready for use yet, so become notified when it does. | |
| 104 if (!history_service_) { | |
| 105 registrar_.Add( | |
| 106 this, chrome::NOTIFICATION_HISTORY_LOADED, | |
| 107 content::Source<Profile>(profile_)); | |
| 108 } | |
| 109 } | 106 } |
| 110 | 107 |
| 111 // Note that all requests sent to either the history service or the favicon | 108 // Note that all requests sent to either the history service or the favicon |
| 112 // service will be automatically cancelled by their respective Consumers, so | 109 // service will be automatically cancelled by their respective Consumers, so |
| 113 // task cancellation is not done manually here in the dtor. | 110 // task cancellation is not done manually here in the dtor. |
| 114 HistoryMenuBridge::~HistoryMenuBridge() { | 111 HistoryMenuBridge::~HistoryMenuBridge() { |
| 115 // Unregister ourselves as observers and notifications. | 112 // Unregister ourselves as observers and notifications. |
| 116 DCHECK(profile_); | 113 DCHECK(profile_); |
| 117 if (history_service_) { | 114 if (history_service_) { |
| 118 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, | 115 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, |
| 119 content::Source<Profile>(profile_)); | 116 content::Source<Profile>(profile_)); |
| 120 history_service_->RemoveObserver(this); | |
| 121 } else { | |
| 122 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_LOADED, | |
| 123 content::Source<Profile>(profile_)); | |
| 124 } | 117 } |
| 125 | 118 |
| 126 if (tab_restore_service_) | 119 if (tab_restore_service_) |
| 127 tab_restore_service_->RemoveObserver(this); | 120 tab_restore_service_->RemoveObserver(this); |
| 128 | 121 |
| 129 // Since the map owns the HistoryItems, delete anything that still exists. | 122 // Since the map owns the HistoryItems, delete anything that still exists. |
| 130 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.begin(); | 123 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.begin(); |
| 131 while (it != menu_item_map_.end()) { | 124 while (it != menu_item_map_.end()) { |
| 132 HistoryItem* item = it->second; | 125 HistoryItem* item = it->second; |
| 133 menu_item_map_.erase(it++); | 126 menu_item_map_.erase(it++); |
| 134 delete item; | 127 delete item; |
| 135 } | 128 } |
| 136 } | 129 } |
| 137 | 130 |
| 138 void HistoryMenuBridge::Observe(int type, | 131 void HistoryMenuBridge::Observe(int type, |
| 139 const content::NotificationSource& source, | 132 const content::NotificationSource& source, |
| 140 const content::NotificationDetails& details) { | 133 const content::NotificationDetails& details) { |
| 141 // A history service is now ready. Check to see if it's the one for the main | |
| 142 // profile. If so, perform final initialization. | |
| 143 if (type == chrome::NOTIFICATION_HISTORY_LOADED) { | |
| 144 HistoryService* hs = HistoryServiceFactory::GetForProfile( | |
| 145 profile_, Profile::EXPLICIT_ACCESS); | |
| 146 if (hs != NULL && hs->BackendLoaded()) { | |
| 147 history_service_ = hs; | |
| 148 Init(); | |
| 149 | |
| 150 // Found our HistoryService, so stop listening for this notification. | |
| 151 registrar_.Remove(this, | |
| 152 chrome::NOTIFICATION_HISTORY_LOADED, | |
| 153 content::Source<Profile>(profile_)); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 // All other notification types that we observe indicate that the history has | 134 // All other notification types that we observe indicate that the history has |
| 158 // changed. | 135 // changed. |
|
Peter Kasting
2014/11/17 18:45:32
Nit: Please update the comment ("other" is now con
nshaik
2014/11/17 23:24:29
Done.
| |
| 159 OnHistoryChanged(); | 136 OnHistoryChanged(); |
| 160 } | 137 } |
| 161 | 138 |
| 162 void HistoryMenuBridge::TabRestoreServiceChanged(TabRestoreService* service) { | 139 void HistoryMenuBridge::TabRestoreServiceChanged(TabRestoreService* service) { |
| 163 const TabRestoreService::Entries& entries = service->entries(); | 140 const TabRestoreService::Entries& entries = service->entries(); |
| 164 | 141 |
| 165 // Clear the history menu before rebuilding. | 142 // Clear the history menu before rebuilding. |
| 166 NSMenu* menu = HistoryMenu(); | 143 NSMenu* menu = HistoryMenu(); |
| 167 ClearMenuSection(menu, kRecentlyClosed); | 144 ClearMenuSection(menu, kRecentlyClosed); |
| 168 | 145 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 276 const history::RedirectList& redirects, | 253 const history::RedirectList& redirects, |
| 277 base::Time visit_time) { | 254 base::Time visit_time) { |
| 278 OnHistoryChanged(); | 255 OnHistoryChanged(); |
| 279 } | 256 } |
| 280 | 257 |
| 281 void HistoryMenuBridge::OnURLsModified(HistoryService* history_service, | 258 void HistoryMenuBridge::OnURLsModified(HistoryService* history_service, |
| 282 const history::URLRows& changed_urls) { | 259 const history::URLRows& changed_urls) { |
| 283 OnHistoryChanged(); | 260 OnHistoryChanged(); |
| 284 } | 261 } |
| 285 | 262 |
| 263 void HistoryMenuBridge::OnHistoryServiceLoaded( | |
| 264 HistoryService* history_service) { | |
| 265 DCHECK(history_service); | |
| 266 DCHECK(history_service->BackendLoaded()); | |
|
Peter Kasting
2014/11/17 18:45:31
Nit: I dunno how valuable these DCHECKs are (doesn
nshaik
2014/11/17 23:24:29
Done.
| |
| 267 history_service_ = history_service; | |
| 268 Init(); | |
| 269 } | |
| 270 | |
| 286 HistoryMenuBridge::HistoryItem* HistoryMenuBridge::HistoryItemForMenuItem( | 271 HistoryMenuBridge::HistoryItem* HistoryMenuBridge::HistoryItemForMenuItem( |
| 287 NSMenuItem* item) { | 272 NSMenuItem* item) { |
| 288 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.find(item); | 273 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.find(item); |
| 289 if (it != menu_item_map_.end()) { | 274 if (it != menu_item_map_.end()) { |
| 290 return it->second; | 275 return it->second; |
| 291 } | 276 } |
| 292 return NULL; | 277 return NULL; |
| 293 } | 278 } |
| 294 | 279 |
| 295 HistoryService* HistoryMenuBridge::service() { | 280 HistoryService* HistoryMenuBridge::service() { |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 } | 463 } |
| 479 | 464 |
| 480 void HistoryMenuBridge::CancelFaviconRequest(HistoryItem* item) { | 465 void HistoryMenuBridge::CancelFaviconRequest(HistoryItem* item) { |
| 481 DCHECK(item); | 466 DCHECK(item); |
| 482 if (item->icon_requested) { | 467 if (item->icon_requested) { |
| 483 cancelable_task_tracker_.TryCancel(item->icon_task_id); | 468 cancelable_task_tracker_.TryCancel(item->icon_task_id); |
| 484 item->icon_requested = false; | 469 item->icon_requested = false; |
| 485 item->icon_task_id = base::CancelableTaskTracker::kBadTaskId; | 470 item->icon_task_id = base::CancelableTaskTracker::kBadTaskId; |
| 486 } | 471 } |
| 487 } | 472 } |
| OLD | NEW |