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 != NULL && hs->BackendLoaded()) { |
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_MODIFIED, | 115 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED, |
119 content::Source<Profile>(profile_)); | 116 content::Source<Profile>(profile_)); |
120 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, | 117 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, |
121 content::Source<Profile>(profile_)); | 118 content::Source<Profile>(profile_)); |
122 history_service_->RemoveObserver(this); | |
123 } else { | |
124 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_LOADED, | |
125 content::Source<Profile>(profile_)); | |
126 } | 119 } |
127 | 120 |
128 if (tab_restore_service_) | 121 if (tab_restore_service_) |
129 tab_restore_service_->RemoveObserver(this); | 122 tab_restore_service_->RemoveObserver(this); |
130 | 123 |
131 // Since the map owns the HistoryItems, delete anything that still exists. | 124 // Since the map owns the HistoryItems, delete anything that still exists. |
132 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.begin(); | 125 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.begin(); |
133 while (it != menu_item_map_.end()) { | 126 while (it != menu_item_map_.end()) { |
134 HistoryItem* item = it->second; | 127 HistoryItem* item = it->second; |
135 menu_item_map_.erase(it++); | 128 menu_item_map_.erase(it++); |
136 delete item; | 129 delete item; |
137 } | 130 } |
138 } | 131 } |
139 | 132 |
140 void HistoryMenuBridge::Observe(int type, | 133 void HistoryMenuBridge::Observe(int type, |
141 const content::NotificationSource& source, | 134 const content::NotificationSource& source, |
142 const content::NotificationDetails& details) { | 135 const content::NotificationDetails& details) { |
143 // A history service is now ready. Check to see if it's the one for the main | |
144 // profile. If so, perform final initialization. | |
145 if (type == chrome::NOTIFICATION_HISTORY_LOADED) { | |
146 HistoryService* hs = HistoryServiceFactory::GetForProfile( | |
147 profile_, Profile::EXPLICIT_ACCESS); | |
148 if (hs != NULL && hs->BackendLoaded()) { | |
149 history_service_ = hs; | |
150 Init(); | |
151 | |
152 // Found our HistoryService, so stop listening for this notification. | |
153 registrar_.Remove(this, | |
154 chrome::NOTIFICATION_HISTORY_LOADED, | |
155 content::Source<Profile>(profile_)); | |
156 } | |
157 } | |
158 | |
159 // All other notification types that we observe indicate that the history has | 136 // All other notification types that we observe indicate that the history has |
160 // changed. | 137 // changed. |
161 OnHistoryChanged(); | 138 OnHistoryChanged(); |
162 } | 139 } |
163 | 140 |
164 void HistoryMenuBridge::TabRestoreServiceChanged(TabRestoreService* service) { | 141 void HistoryMenuBridge::TabRestoreServiceChanged(TabRestoreService* service) { |
165 const TabRestoreService::Entries& entries = service->entries(); | 142 const TabRestoreService::Entries& entries = service->entries(); |
166 | 143 |
167 // Clear the history menu before rebuilding. | 144 // Clear the history menu before rebuilding. |
168 NSMenu* menu = HistoryMenu(); | 145 NSMenu* menu = HistoryMenu(); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 } | 250 } |
274 | 251 |
275 void HistoryMenuBridge::OnURLVisited(HistoryService* history_service, | 252 void HistoryMenuBridge::OnURLVisited(HistoryService* history_service, |
276 ui::PageTransition transition, | 253 ui::PageTransition transition, |
277 const history::URLRow& row, | 254 const history::URLRow& row, |
278 const history::RedirectList& redirects, | 255 const history::RedirectList& redirects, |
279 base::Time visit_time) { | 256 base::Time visit_time) { |
280 OnHistoryChanged(); | 257 OnHistoryChanged(); |
281 } | 258 } |
282 | 259 |
| 260 void HistoryMenuBridge::OnHistoryServiceLoaded( |
| 261 HistoryService* history_service) { |
| 262 DCHECK(history_service); |
| 263 DCHECK(history_service->BackendLoaded()); |
| 264 history_service_ = history_service; |
| 265 Init(); |
| 266 OnHistoryChanged(); |
| 267 } |
| 268 |
283 HistoryMenuBridge::HistoryItem* HistoryMenuBridge::HistoryItemForMenuItem( | 269 HistoryMenuBridge::HistoryItem* HistoryMenuBridge::HistoryItemForMenuItem( |
284 NSMenuItem* item) { | 270 NSMenuItem* item) { |
285 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.find(item); | 271 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.find(item); |
286 if (it != menu_item_map_.end()) { | 272 if (it != menu_item_map_.end()) { |
287 return it->second; | 273 return it->second; |
288 } | 274 } |
289 return NULL; | 275 return NULL; |
290 } | 276 } |
291 | 277 |
292 HistoryService* HistoryMenuBridge::service() { | 278 HistoryService* HistoryMenuBridge::service() { |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 } | 463 } |
478 | 464 |
479 void HistoryMenuBridge::CancelFaviconRequest(HistoryItem* item) { | 465 void HistoryMenuBridge::CancelFaviconRequest(HistoryItem* item) { |
480 DCHECK(item); | 466 DCHECK(item); |
481 if (item->icon_requested) { | 467 if (item->icon_requested) { |
482 cancelable_task_tracker_.TryCancel(item->icon_task_id); | 468 cancelable_task_tracker_.TryCancel(item->icon_task_id); |
483 item->icon_requested = false; | 469 item->icon_requested = false; |
484 item->icon_task_id = base::CancelableTaskTracker::kBadTaskId; | 470 item->icon_task_id = base::CancelableTaskTracker::kBadTaskId; |
485 } | 471 } |
486 } | 472 } |
| 473 |
OLD | NEW |