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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 // This shouldn't happen except in unit tests. | 70 // This shouldn't happen except in unit tests. |
71 if (profile_) { | 71 if (profile_) { |
72 // Check to see if the history service is ready. Because it loads async, it | 72 // 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 | 73 // may not be ready when the Bridge is created. If this happens, register |
74 // for a notification that tells us the HistoryService is ready. | 74 // for a notification that tells us the HistoryService is ready. |
75 HistoryService* hs = HistoryServiceFactory::GetForProfile( | 75 HistoryService* hs = HistoryServiceFactory::GetForProfile( |
76 profile_, Profile::EXPLICIT_ACCESS); | 76 profile_, Profile::EXPLICIT_ACCESS); |
77 if (hs != NULL && hs->BackendLoaded()) { | 77 if (hs != NULL && hs->BackendLoaded()) { |
78 history_service_ = hs; | 78 history_service_ = hs; |
79 Init(); | 79 Init(); |
80 } else if (hs != NULL) { | |
81 // The service is not ready for use yet, so become notified when it does. | |
82 hs->AddHistoryServiceObserver(this); | |
sdefresne
2014/10/20 13:15:42
Use ScopedObserver<>
| |
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 | |
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 } | 105 } |
110 | 106 |
111 // Note that all requests sent to either the history service or the favicon | 107 // Note that all requests sent to either the history service or the favicon |
112 // service will be automatically cancelled by their respective Consumers, so | 108 // service will be automatically cancelled by their respective Consumers, so |
113 // task cancellation is not done manually here in the dtor. | 109 // task cancellation is not done manually here in the dtor. |
114 HistoryMenuBridge::~HistoryMenuBridge() { | 110 HistoryMenuBridge::~HistoryMenuBridge() { |
115 // Unregister ourselves as observers and notifications. | 111 // Unregister ourselves as observers and notifications. |
116 DCHECK(profile_); | 112 DCHECK(profile_); |
117 if (history_service_) { | 113 if (history_service_) { |
118 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED, | 114 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED, |
119 content::Source<Profile>(profile_)); | 115 content::Source<Profile>(profile_)); |
120 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_URL_VISITED, | 116 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_URL_VISITED, |
121 content::Source<Profile>(profile_)); | 117 content::Source<Profile>(profile_)); |
122 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, | 118 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, |
123 content::Source<Profile>(profile_)); | 119 content::Source<Profile>(profile_)); |
124 } else { | 120 history_service_->RemoveHistoryServiceObserver(this); |
125 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_LOADED, | |
126 content::Source<Profile>(profile_)); | |
127 } | 121 } |
128 | 122 |
129 if (tab_restore_service_) | 123 if (tab_restore_service_) |
130 tab_restore_service_->RemoveObserver(this); | 124 tab_restore_service_->RemoveObserver(this); |
131 | 125 |
132 // Since the map owns the HistoryItems, delete anything that still exists. | 126 // Since the map owns the HistoryItems, delete anything that still exists. |
133 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.begin(); | 127 std::map<NSMenuItem*, HistoryItem*>::iterator it = menu_item_map_.begin(); |
134 while (it != menu_item_map_.end()) { | 128 while (it != menu_item_map_.end()) { |
135 HistoryItem* item = it->second; | 129 HistoryItem* item = it->second; |
136 menu_item_map_.erase(it++); | 130 menu_item_map_.erase(it++); |
137 delete item; | 131 delete item; |
138 } | 132 } |
139 } | 133 } |
140 | 134 |
141 void HistoryMenuBridge::Observe(int type, | 135 void HistoryMenuBridge::Observe(int type, |
142 const content::NotificationSource& source, | 136 const content::NotificationSource& source, |
143 const content::NotificationDetails& details) { | 137 const content::NotificationDetails& details) { |
144 // A history service is now ready. Check to see if it's the one for the main | |
145 // profile. If so, perform final initialization. | |
146 if (type == chrome::NOTIFICATION_HISTORY_LOADED) { | |
147 HistoryService* hs = HistoryServiceFactory::GetForProfile( | |
148 profile_, Profile::EXPLICIT_ACCESS); | |
149 if (hs != NULL && hs->BackendLoaded()) { | |
150 history_service_ = hs; | |
151 Init(); | |
152 | |
153 // Found our HistoryService, so stop listening for this notification. | |
154 registrar_.Remove(this, | |
155 chrome::NOTIFICATION_HISTORY_LOADED, | |
156 content::Source<Profile>(profile_)); | |
157 } | |
158 } | |
159 | |
160 // All other notification types that we observe indicate that the history has | 138 // All other notification types that we observe indicate that the history has |
161 // changed and we need to rebuild. | 139 // changed and we need to rebuild. |
162 need_recreate_ = true; | 140 need_recreate_ = true; |
163 CreateMenu(); | 141 CreateMenu(); |
164 } | 142 } |
165 | 143 |
166 void HistoryMenuBridge::TabRestoreServiceChanged(TabRestoreService* service) { | 144 void HistoryMenuBridge::TabRestoreServiceChanged(TabRestoreService* service) { |
167 const TabRestoreService::Entries& entries = service->entries(); | 145 const TabRestoreService::Entries& entries = service->entries(); |
168 | 146 |
169 // Clear the history menu before rebuilding. | 147 // Clear the history menu before rebuilding. |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
465 } | 443 } |
466 | 444 |
467 void HistoryMenuBridge::CancelFaviconRequest(HistoryItem* item) { | 445 void HistoryMenuBridge::CancelFaviconRequest(HistoryItem* item) { |
468 DCHECK(item); | 446 DCHECK(item); |
469 if (item->icon_requested) { | 447 if (item->icon_requested) { |
470 cancelable_task_tracker_.TryCancel(item->icon_task_id); | 448 cancelable_task_tracker_.TryCancel(item->icon_task_id); |
471 item->icon_requested = false; | 449 item->icon_requested = false; |
472 item->icon_task_id = base::CancelableTaskTracker::kBadTaskId; | 450 item->icon_task_id = base::CancelableTaskTracker::kBadTaskId; |
473 } | 451 } |
474 } | 452 } |
453 | |
454 void HistoryMenuBridge::HistoryServiceLoaded(HistoryService* history_service) { | |
455 HistoryService* hs = | |
456 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | |
sdefresne
2014/10/20 13:15:42
No need to use HistoryServiceFactory to get the Hi
nshaik
2014/10/29 08:43:39
Okay. The only reason why I did this was to ensure
| |
457 if (hs != NULL && hs->BackendLoaded()) { | |
458 history_service_ = hs; | |
459 Init(); | |
460 history_service_->RemoveHistoryServiceObserver(this); | |
461 } | |
462 } | |
OLD | NEW |