[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/extensions/extension_action_manager.h" |
| 6 | |
[email protected] | f0eb58a | 2012-12-17 22:10:49 | [diff] [blame] | 7 | #include "chrome/browser/extensions/api/system_indicator/system_indicator_manager.h" |
| 8 | #include "chrome/browser/extensions/api/system_indicator/system_indicator_manager_factory.h" |
[email protected] | 8ddcc92 | 2012-10-13 05:13:03 | [diff] [blame] | 9 | #include "chrome/browser/extensions/extension_action.h" |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 10 | #include "chrome/browser/extensions/extension_system.h" |
[email protected] | 018bf65 | 2013-05-03 23:18:34 | [diff] [blame] | 11 | #include "chrome/browser/profiles/incognito_helpers.h" |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 12 | #include "chrome/browser/profiles/profile.h" |
| 13 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | 23b3c0a | 2013-01-16 23:36:36 | [diff] [blame] | 14 | #include "chrome/common/extensions/api/extension_action/action_info.h" |
[email protected] | f965548 | 2013-02-13 09:22:51 | [diff] [blame] | 15 | #include "chrome/common/extensions/api/extension_action/page_action_handler.h" |
| 16 | #include "chrome/common/extensions/api/extension_action/script_badge_handler.h" |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 17 | #include "chrome/common/extensions/extension.h" |
[email protected] | 544471a | 2012-10-13 05:27:09 | [diff] [blame] | 18 | #include "chrome/common/extensions/feature_switch.h" |
[email protected] | 0dd6f203 | 2013-05-20 23:33:40 | [diff] [blame] | 19 | #include "components/browser_context_keyed_service/browser_context_dependency_manager.h" |
| 20 | #include "components/browser_context_keyed_service/browser_context_keyed_service_factory.h" |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 21 | #include "content/public/browser/notification_service.h" |
| 22 | #include "content/public/browser/notification_source.h" |
| 23 | |
| 24 | namespace extensions { |
| 25 | |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 26 | namespace { |
| 27 | |
[email protected] | f1484c5 | 2013-05-22 23:25:44 | [diff] [blame^] | 28 | // BrowserContextKeyedServiceFactory for ExtensionActionManager. |
| 29 | class ExtensionActionManagerFactory : public BrowserContextKeyedServiceFactory { |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 30 | public: |
[email protected] | f1484c5 | 2013-05-22 23:25:44 | [diff] [blame^] | 31 | // BrowserContextKeyedServiceFactory implementation: |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 32 | static ExtensionActionManager* GetForProfile(Profile* profile) { |
| 33 | return static_cast<ExtensionActionManager*>( |
[email protected] | f1484c5 | 2013-05-22 23:25:44 | [diff] [blame^] | 34 | GetInstance()->GetServiceForBrowserContext(profile, true)); |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static ExtensionActionManagerFactory* GetInstance(); |
| 38 | |
| 39 | private: |
| 40 | friend struct DefaultSingletonTraits<ExtensionActionManagerFactory>; |
| 41 | |
| 42 | ExtensionActionManagerFactory() |
[email protected] | f1484c5 | 2013-05-22 23:25:44 | [diff] [blame^] | 43 | : BrowserContextKeyedServiceFactory( |
| 44 | "ExtensionActionManager", |
| 45 | BrowserContextDependencyManager::GetInstance()) { |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 46 | } |
| 47 | |
[email protected] | f1484c5 | 2013-05-22 23:25:44 | [diff] [blame^] | 48 | virtual BrowserContextKeyedService* BuildServiceInstanceFor( |
[email protected] | c7fa436 | 2013-04-26 18:09:02 | [diff] [blame] | 49 | content::BrowserContext* profile) const OVERRIDE { |
| 50 | return new ExtensionActionManager(static_cast<Profile*>(profile)); |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | 018bf65 | 2013-05-03 23:18:34 | [diff] [blame] | 53 | virtual content::BrowserContext* GetBrowserContextToUse( |
| 54 | content::BrowserContext* context) const OVERRIDE { |
| 55 | return chrome::GetBrowserContextRedirectedInIncognito(context); |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 56 | } |
| 57 | }; |
| 58 | |
| 59 | ExtensionActionManagerFactory* |
| 60 | ExtensionActionManagerFactory::GetInstance() { |
| 61 | return Singleton<ExtensionActionManagerFactory>::get(); |
| 62 | } |
| 63 | |
| 64 | } // namespace |
| 65 | |
[email protected] | f0eb58a | 2012-12-17 22:10:49 | [diff] [blame] | 66 | ExtensionActionManager::ExtensionActionManager(Profile* profile) |
| 67 | : profile_(profile) { |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 68 | CHECK_EQ(profile, profile->GetOriginalProfile()) |
| 69 | << "Don't instantiate this with an incognito profile."; |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 70 | registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 71 | content::Source<Profile>(profile)); |
| 72 | } |
| 73 | |
| 74 | ExtensionActionManager::~ExtensionActionManager() { |
| 75 | // Don't assert that the ExtensionAction maps are empty because Extensions are |
| 76 | // sometimes (only in tests?) not unloaded before the Profile is destroyed. |
| 77 | } |
| 78 | |
[email protected] | 5ed5ec5 | 2012-10-12 21:28:30 | [diff] [blame] | 79 | ExtensionActionManager* ExtensionActionManager::Get(Profile* profile) { |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 80 | return ExtensionActionManagerFactory::GetForProfile(profile); |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void ExtensionActionManager::Observe( |
| 84 | int type, |
| 85 | const content::NotificationSource& source, |
| 86 | const content::NotificationDetails& details) { |
| 87 | switch (type) { |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 88 | case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
| 89 | const Extension* extension = |
| 90 | content::Details<UnloadedExtensionInfo>(details)->extension; |
[email protected] | 5ed5ec5 | 2012-10-12 21:28:30 | [diff] [blame] | 91 | page_actions_.erase(extension->id()); |
| 92 | browser_actions_.erase(extension->id()); |
| 93 | script_badges_.erase(extension->id()); |
[email protected] | f0eb58a | 2012-12-17 22:10:49 | [diff] [blame] | 94 | system_indicators_.erase(extension->id()); |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 95 | break; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
[email protected] | 5ed5ec5 | 2012-10-12 21:28:30 | [diff] [blame] | 100 | namespace { |
| 101 | |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 102 | // Returns map[extension_id] if that entry exists. Otherwise, if |
| 103 | // action_info!=NULL, creates an ExtensionAction from it, fills in the map, and |
| 104 | // returns that. Otherwise (action_info==NULL), returns NULL. |
| 105 | ExtensionAction* GetOrCreateOrNull( |
| 106 | std::map<std::string, linked_ptr<ExtensionAction> >* map, |
| 107 | const std::string& extension_id, |
[email protected] | 23b3c0a | 2013-01-16 23:36:36 | [diff] [blame] | 108 | ActionInfo::Type action_type, |
| 109 | const ActionInfo* action_info) { |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 110 | std::map<std::string, linked_ptr<ExtensionAction> >::const_iterator it = |
| 111 | map->find(extension_id); |
| 112 | if (it != map->end()) |
| 113 | return it->second.get(); |
| 114 | if (!action_info) |
| 115 | return NULL; |
| 116 | linked_ptr<ExtensionAction> action(new ExtensionAction( |
| 117 | extension_id, action_type, *action_info)); |
| 118 | (*map)[extension_id] = action; |
| 119 | return action.get(); |
[email protected] | 5ed5ec5 | 2012-10-12 21:28:30 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | ad12b6b | 2012-11-28 23:21:15 | [diff] [blame] | 122 | } // namespace |
[email protected] | 5ed5ec5 | 2012-10-12 21:28:30 | [diff] [blame] | 123 | |
| 124 | ExtensionAction* ExtensionActionManager::GetPageAction( |
| 125 | const extensions::Extension& extension) const { |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 126 | // The action box changes the meaning of the page action area, so we |
| 127 | // need to convert page actions into browser actions. |
| 128 | if (FeatureSwitch::script_badges()->IsEnabled()) |
| 129 | return NULL; |
| 130 | return GetOrCreateOrNull(&page_actions_, extension.id(), |
[email protected] | 23b3c0a | 2013-01-16 23:36:36 | [diff] [blame] | 131 | ActionInfo::TYPE_PAGE, |
[email protected] | f965548 | 2013-02-13 09:22:51 | [diff] [blame] | 132 | ActionInfo::GetPageActionInfo(&extension)); |
[email protected] | 5ed5ec5 | 2012-10-12 21:28:30 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | ExtensionAction* ExtensionActionManager::GetBrowserAction( |
| 136 | const extensions::Extension& extension) const { |
[email protected] | 972b02f | 2013-01-28 20:38:14 | [diff] [blame] | 137 | const ActionInfo* action_info = ActionInfo::GetBrowserActionInfo(&extension); |
[email protected] | 23b3c0a | 2013-01-16 23:36:36 | [diff] [blame] | 138 | ActionInfo::Type action_type = ActionInfo::TYPE_BROWSER; |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 139 | if (FeatureSwitch::script_badges()->IsEnabled() && |
[email protected] | f965548 | 2013-02-13 09:22:51 | [diff] [blame] | 140 | ActionInfo::GetPageActionInfo(&extension)) { |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 141 | // The action box changes the meaning of the page action area, so we |
| 142 | // need to convert page actions into browser actions. |
[email protected] | f965548 | 2013-02-13 09:22:51 | [diff] [blame] | 143 | action_info = ActionInfo::GetPageActionInfo(&extension); |
[email protected] | 23b3c0a | 2013-01-16 23:36:36 | [diff] [blame] | 144 | action_type = ActionInfo::TYPE_PAGE; |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 145 | } |
| 146 | return GetOrCreateOrNull(&browser_actions_, extension.id(), |
| 147 | action_type, action_info); |
[email protected] | 5ed5ec5 | 2012-10-12 21:28:30 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | ad12b6b | 2012-11-28 23:21:15 | [diff] [blame] | 150 | ExtensionAction* ExtensionActionManager::GetSystemIndicator( |
| 151 | const extensions::Extension& extension) const { |
[email protected] | f0eb58a | 2012-12-17 22:10:49 | [diff] [blame] | 152 | // If it does not already exist, create the SystemIndicatorManager for the |
| 153 | // given profile. This could return NULL if the system indicator area is |
| 154 | // unavailable on the current system. If so, return NULL to signal that |
| 155 | // the system indicator area is unusable. |
| 156 | if (!extensions::SystemIndicatorManagerFactory::GetForProfile(profile_)) |
| 157 | return NULL; |
| 158 | |
[email protected] | ad12b6b | 2012-11-28 23:21:15 | [diff] [blame] | 159 | return GetOrCreateOrNull(&system_indicators_, extension.id(), |
[email protected] | 23b3c0a | 2013-01-16 23:36:36 | [diff] [blame] | 160 | ActionInfo::TYPE_SYSTEM_INDICATOR, |
[email protected] | 4e78693 | 2013-03-25 19:21:04 | [diff] [blame] | 161 | ActionInfo::GetSystemIndicatorInfo(&extension)); |
[email protected] | ad12b6b | 2012-11-28 23:21:15 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | 5ed5ec5 | 2012-10-12 21:28:30 | [diff] [blame] | 164 | ExtensionAction* ExtensionActionManager::GetScriptBadge( |
| 165 | const extensions::Extension& extension) const { |
[email protected] | 61e3cbe | 2012-10-14 22:06:50 | [diff] [blame] | 166 | return GetOrCreateOrNull(&script_badges_, extension.id(), |
[email protected] | 23b3c0a | 2013-01-16 23:36:36 | [diff] [blame] | 167 | ActionInfo::TYPE_SCRIPT_BADGE, |
| 168 | ActionInfo::GetScriptBadgeInfo(&extension)); |
[email protected] | 5ed5ec5 | 2012-10-12 21:28:30 | [diff] [blame] | 169 | } |
| 170 | |
[email protected] | ad12b6b | 2012-11-28 23:21:15 | [diff] [blame] | 171 | } // namespace extensions |