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/extensions/extension_toolbar_model.h" | 5 #include "chrome/browser/extensions/extension_toolbar_model.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/metrics/histogram_base.h" | 10 #include "base/metrics/histogram_base.h" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
13 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 13 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
14 #include "chrome/browser/extensions/extension_action.h" | 14 #include "chrome/browser/extensions/extension_action.h" |
15 #include "chrome/browser/extensions/extension_action_manager.h" | 15 #include "chrome/browser/extensions/extension_action_manager.h" |
16 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
17 #include "chrome/browser/extensions/extension_tab_util.h" | 17 #include "chrome/browser/extensions/extension_tab_util.h" |
18 #include "chrome/browser/extensions/extension_toolbar_model_factory.h" | 18 #include "chrome/browser/extensions/extension_toolbar_model_factory.h" |
19 #include "chrome/browser/extensions/extension_util.h" | 19 #include "chrome/browser/extensions/extension_util.h" |
20 #include "chrome/browser/extensions/tab_helper.h" | 20 #include "chrome/browser/extensions/tab_helper.h" |
21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
23 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
24 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
25 #include "content/public/browser/notification_details.h" | 25 #include "content/public/browser/notification_details.h" |
26 #include "content/public/browser/notification_source.h" | 26 #include "content/public/browser/notification_source.h" |
27 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
28 #include "extensions/browser/extension_prefs.h" | 28 #include "extensions/browser/extension_prefs.h" |
| 29 #include "extensions/browser/extension_registry.h" |
29 #include "extensions/browser/extension_system.h" | 30 #include "extensions/browser/extension_system.h" |
30 #include "extensions/browser/pref_names.h" | 31 #include "extensions/browser/pref_names.h" |
31 #include "extensions/common/extension.h" | 32 #include "extensions/common/extension.h" |
32 #include "extensions/common/extension_set.h" | 33 #include "extensions/common/extension_set.h" |
33 #include "extensions/common/feature_switch.h" | 34 #include "extensions/common/feature_switch.h" |
34 | 35 |
35 namespace extensions { | 36 namespace extensions { |
36 | 37 |
37 bool ExtensionToolbarModel::Observer::BrowserActionShowPopup( | 38 bool ExtensionToolbarModel::Observer::BrowserActionShowPopup( |
38 const extensions::Extension* extension) { | 39 const extensions::Extension* extension) { |
39 return false; | 40 return false; |
40 } | 41 } |
41 | 42 |
42 ExtensionToolbarModel::ExtensionToolbarModel( | 43 ExtensionToolbarModel::ExtensionToolbarModel( |
43 Profile* profile, | 44 Profile* profile, |
44 extensions::ExtensionPrefs* extension_prefs) | 45 extensions::ExtensionPrefs* extension_prefs) |
45 : profile_(profile), | 46 : profile_(profile), |
46 extension_prefs_(extension_prefs), | 47 extension_prefs_(extension_prefs), |
47 prefs_(profile_->GetPrefs()), | 48 prefs_(profile_->GetPrefs()), |
48 extensions_initialized_(false), | 49 extensions_initialized_(false), |
49 is_highlighting_(false), | 50 is_highlighting_(false), |
| 51 extension_registry_observer_(this), |
50 weak_ptr_factory_(this) { | 52 weak_ptr_factory_(this) { |
51 registrar_.Add(this, | 53 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
52 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 54 |
53 content::Source<Profile>(profile_)); | |
54 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | |
55 content::Source<Profile>(profile_)); | |
56 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, | 55 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, |
57 content::Source<Profile>(profile_)); | 56 content::Source<Profile>(profile_)); |
58 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | 57 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
59 content::Source<Profile>(profile_)); | 58 content::Source<Profile>(profile_)); |
60 registrar_.Add( | 59 registrar_.Add( |
61 this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED, | 60 this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED, |
62 content::Source<extensions::ExtensionPrefs>(extension_prefs_)); | 61 content::Source<extensions::ExtensionPrefs>(extension_prefs_)); |
63 | 62 |
64 visible_icon_count_ = prefs_->GetInteger( | 63 visible_icon_count_ = prefs_->GetInteger( |
65 extensions::pref_names::kToolbarSize); | 64 extensions::pref_names::kToolbarSize); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 count == static_cast<int>(toolbar_items_.size()) ? -1 : count; | 172 count == static_cast<int>(toolbar_items_.size()) ? -1 : count; |
174 // Only set the prefs if we're not in highlight mode. Highlight mode is | 173 // Only set the prefs if we're not in highlight mode. Highlight mode is |
175 // designed to be a transitory state, and should not persist across browser | 174 // designed to be a transitory state, and should not persist across browser |
176 // restarts (though it may be re-entered). | 175 // restarts (though it may be re-entered). |
177 if (!is_highlighting_) { | 176 if (!is_highlighting_) { |
178 prefs_->SetInteger(extensions::pref_names::kToolbarSize, | 177 prefs_->SetInteger(extensions::pref_names::kToolbarSize, |
179 visible_icon_count_); | 178 visible_icon_count_); |
180 } | 179 } |
181 } | 180 } |
182 | 181 |
| 182 void ExtensionToolbarModel::OnExtensionLoaded( |
| 183 content::BrowserContext* browser_context, |
| 184 const Extension* extension) { |
| 185 // We don't want to add the same extension twice. It may have already been |
| 186 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user |
| 187 // hides the browser action and then disables and enables the extension. |
| 188 for (size_t i = 0; i < toolbar_items_.size(); i++) { |
| 189 if (toolbar_items_[i].get() == extension) |
| 190 return; |
| 191 } |
| 192 if (ExtensionActionAPI::GetBrowserActionVisibility(extension_prefs_, |
| 193 extension->id())) { |
| 194 AddExtension(extension); |
| 195 } |
| 196 } |
| 197 |
| 198 void ExtensionToolbarModel::OnExtensionUnloaded( |
| 199 content::BrowserContext* browser_context, |
| 200 const Extension* extension, |
| 201 UnloadedExtensionInfo::Reason reason) { |
| 202 RemoveExtension(extension); |
| 203 } |
| 204 |
183 void ExtensionToolbarModel::Observe( | 205 void ExtensionToolbarModel::Observe( |
184 int type, | 206 int type, |
185 const content::NotificationSource& source, | 207 const content::NotificationSource& source, |
186 const content::NotificationDetails& details) { | 208 const content::NotificationDetails& details) { |
187 ExtensionService* extension_service = | 209 ExtensionService* extension_service = |
188 ExtensionSystem::Get(profile_)->extension_service(); | 210 ExtensionSystem::Get(profile_)->extension_service(); |
189 DCHECK(extension_service); | 211 DCHECK(extension_service); |
190 if (!extension_service->is_ready()) | 212 if (!extension_service->is_ready()) |
191 return; | 213 return; |
192 | 214 |
193 switch (type) { | 215 switch (type) { |
194 case chrome::NOTIFICATION_EXTENSIONS_READY: | 216 case chrome::NOTIFICATION_EXTENSIONS_READY: |
195 InitializeExtensionList(extension_service); | 217 InitializeExtensionList(extension_service); |
196 break; | 218 break; |
197 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: { | |
198 const Extension* extension = | |
199 content::Details<const Extension>(details).ptr(); | |
200 // We don't want to add the same extension twice. It may have already been | |
201 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user | |
202 // hides the browser action and then disables and enables the extension. | |
203 for (size_t i = 0; i < toolbar_items_.size(); i++) { | |
204 if (toolbar_items_[i].get() == extension) | |
205 return; // Already exists. | |
206 } | |
207 if (ExtensionActionAPI::GetBrowserActionVisibility(extension_prefs_, | |
208 extension->id())) { | |
209 AddExtension(extension); | |
210 } | |
211 break; | |
212 } | |
213 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { | |
214 const Extension* extension = | |
215 content::Details<extensions::UnloadedExtensionInfo>(details) | |
216 ->extension; | |
217 RemoveExtension(extension); | |
218 break; | |
219 } | |
220 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { | 219 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { |
221 const Extension* extension = | 220 const Extension* extension = |
222 content::Details<const Extension>(details).ptr(); | 221 content::Details<const Extension>(details).ptr(); |
223 UninstalledExtension(extension); | 222 UninstalledExtension(extension); |
224 break; | 223 break; |
225 } | 224 } |
226 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED: { | 225 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED: { |
227 const Extension* extension = extension_service->GetExtensionById( | 226 const Extension* extension = extension_service->GetExtensionById( |
228 *content::Details<const std::string>(details).ptr(), true); | 227 *content::Details<const std::string>(details).ptr(), true); |
229 if (ExtensionActionAPI::GetBrowserActionVisibility(extension_prefs_, | 228 if (ExtensionActionAPI::GetBrowserActionVisibility(extension_prefs_, |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 is_highlighting_ = false; | 589 is_highlighting_ = false; |
591 if (old_visible_icon_count_ != visible_icon_count_) { | 590 if (old_visible_icon_count_ != visible_icon_count_) { |
592 SetVisibleIconCount(old_visible_icon_count_); | 591 SetVisibleIconCount(old_visible_icon_count_); |
593 FOR_EACH_OBSERVER(Observer, observers_, VisibleCountChanged()); | 592 FOR_EACH_OBSERVER(Observer, observers_, VisibleCountChanged()); |
594 } | 593 } |
595 FOR_EACH_OBSERVER(Observer, observers_, HighlightModeChanged(false)); | 594 FOR_EACH_OBSERVER(Observer, observers_, HighlightModeChanged(false)); |
596 } | 595 } |
597 }; | 596 }; |
598 | 597 |
599 } // namespace extensions | 598 } // namespace extensions |
OLD | NEW |