Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / extension_action_manager.cc
blob7e0f40106cf35b75b054e382c3ad1e6080065a09
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.
5 #include "chrome/browser/extensions/extension_action_manager.h"
7 #include "chrome/browser/extensions/api/system_indicator/system_indicator_manager_factory.h"
8 #include "chrome/browser/extensions/extension_action.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "components/keyed_service/content/browser_context_dependency_manager.h"
11 #include "extensions/browser/extension_registry.h"
12 #include "extensions/browser/extension_system.h"
13 #include "extensions/browser/extensions_browser_client.h"
14 #include "extensions/common/constants.h"
16 namespace extensions {
18 namespace {
20 // BrowserContextKeyedServiceFactory for ExtensionActionManager.
21 class ExtensionActionManagerFactory : public BrowserContextKeyedServiceFactory {
22 public:
23 // BrowserContextKeyedServiceFactory implementation:
24 static ExtensionActionManager* GetForBrowserContext(
25 content::BrowserContext* context) {
26 return static_cast<ExtensionActionManager*>(
27 GetInstance()->GetServiceForBrowserContext(context, true));
30 static ExtensionActionManagerFactory* GetInstance();
32 private:
33 friend struct DefaultSingletonTraits<ExtensionActionManagerFactory>;
35 ExtensionActionManagerFactory()
36 : BrowserContextKeyedServiceFactory(
37 "ExtensionActionManager",
38 BrowserContextDependencyManager::GetInstance()) {
41 KeyedService* BuildServiceInstanceFor(
42 content::BrowserContext* profile) const override {
43 return new ExtensionActionManager(static_cast<Profile*>(profile));
46 content::BrowserContext* GetBrowserContextToUse(
47 content::BrowserContext* context) const override {
48 return ExtensionsBrowserClient::Get()->GetOriginalContext(context);
52 ExtensionActionManagerFactory*
53 ExtensionActionManagerFactory::GetInstance() {
54 return Singleton<ExtensionActionManagerFactory>::get();
57 } // namespace
59 ExtensionActionManager::ExtensionActionManager(Profile* profile)
60 : profile_(profile), extension_registry_observer_(this) {
61 CHECK_EQ(profile, profile->GetOriginalProfile())
62 << "Don't instantiate this with an incognito profile.";
63 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_));
66 ExtensionActionManager::~ExtensionActionManager() {
67 // Don't assert that the ExtensionAction maps are empty because Extensions are
68 // sometimes (only in tests?) not unloaded before the Profile is destroyed.
71 ExtensionActionManager* ExtensionActionManager::Get(
72 content::BrowserContext* context) {
73 return ExtensionActionManagerFactory::GetForBrowserContext(context);
76 void ExtensionActionManager::OnExtensionUnloaded(
77 content::BrowserContext* browser_context,
78 const Extension* extension,
79 UnloadedExtensionInfo::Reason reason) {
80 page_actions_.erase(extension->id());
81 browser_actions_.erase(extension->id());
82 system_indicators_.erase(extension->id());
85 namespace {
87 // Returns map[extension_id] if that entry exists. Otherwise, if
88 // action_info!=NULL, creates an ExtensionAction from it, fills in the map, and
89 // returns that. Otherwise (action_info==NULL), returns NULL.
90 ExtensionAction* GetOrCreateOrNull(
91 std::map<std::string, linked_ptr<ExtensionAction> >* map,
92 const Extension& extension,
93 ActionInfo::Type action_type,
94 const ActionInfo* action_info,
95 Profile* profile) {
96 std::map<std::string, linked_ptr<ExtensionAction> >::const_iterator it =
97 map->find(extension.id());
98 if (it != map->end())
99 return it->second.get();
100 if (!action_info)
101 return NULL;
103 // Only create action info for enabled extensions.
104 // This avoids bugs where actions are recreated just after being removed
105 // in response to OnExtensionUnloaded().
106 if (!ExtensionRegistry::Get(profile)
107 ->enabled_extensions().Contains(extension.id())) {
108 return NULL;
111 linked_ptr<ExtensionAction> action(new ExtensionAction(
112 extension, action_type, *action_info));
113 (*map)[extension.id()] = action;
114 return action.get();
117 } // namespace
119 ExtensionAction* ExtensionActionManager::GetPageAction(
120 const Extension& extension) const {
121 return GetOrCreateOrNull(&page_actions_, extension,
122 ActionInfo::TYPE_PAGE,
123 ActionInfo::GetPageActionInfo(&extension),
124 profile_);
127 ExtensionAction* ExtensionActionManager::GetBrowserAction(
128 const Extension& extension) const {
129 return GetOrCreateOrNull(&browser_actions_, extension,
130 ActionInfo::TYPE_BROWSER,
131 ActionInfo::GetBrowserActionInfo(&extension),
132 profile_);
135 scoped_ptr<ExtensionAction> ExtensionActionManager::GetBestFitAction(
136 const Extension& extension,
137 ActionInfo::Type type) const {
138 const ActionInfo* info = ActionInfo::GetBrowserActionInfo(&extension);
139 if (!info)
140 info = ActionInfo::GetPageActionInfo(&extension);
142 // Create a new ExtensionAction of |type| with |extension|'s ActionInfo.
143 // If no ActionInfo exists for |extension|, create and return a new action
144 // with a blank ActionInfo.
145 // Populate any missing values from |extension|'s manifest.
146 scoped_ptr<ExtensionAction> new_action(new ExtensionAction(
147 extension, type, info ? *info : ActionInfo()));
148 return new_action.Pass();
151 ExtensionAction* ExtensionActionManager::GetSystemIndicator(
152 const Extension& extension) const {
153 // If it does not already exist, create the SystemIndicatorManager for the
154 // given profile. This could return NULL if the system indicator area is
155 // unavailable on the current system. If so, return NULL to signal that
156 // the system indicator area is unusable.
157 if (!SystemIndicatorManagerFactory::GetForProfile(profile_))
158 return NULL;
160 return GetOrCreateOrNull(&system_indicators_, extension,
161 ActionInfo::TYPE_SYSTEM_INDICATOR,
162 ActionInfo::GetSystemIndicatorInfo(&extension),
163 profile_);
166 ExtensionAction* ExtensionActionManager::GetExtensionAction(
167 const Extension& extension) const {
168 ExtensionAction* action = GetBrowserAction(extension);
169 return action ? action : GetPageAction(extension);
172 } // namespace extensions