Chromium Code Reviews| 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_action_manager.h" | 5 #include "chrome/browser/extensions/extension_action_manager.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/system_indicator/system_indicator_manage r_factory.h" | 7 #include "chrome/browser/extensions/api/system_indicator/system_indicator_manage r_factory.h" |
| 8 #include "chrome/browser/extensions/extension_action.h" | 8 #include "chrome/browser/extensions/extension_action.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "components/keyed_service/content/browser_context_dependency_manager.h" | 11 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 12 #include "extensions/browser/extension_registry.h" | 12 #include "extensions/browser/extension_registry.h" |
| 13 #include "extensions/browser/extension_system.h" | 13 #include "extensions/browser/extension_system.h" |
| 14 #include "extensions/browser/extensions_browser_client.h" | 14 #include "extensions/browser/extensions_browser_client.h" |
| 15 #include "extensions/common/constants.h" | |
| 16 #include "extensions/common/manifest_handlers/icons_handler.h" | |
| 15 | 17 |
| 16 namespace extensions { | 18 namespace extensions { |
| 17 | 19 |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| 20 // BrowserContextKeyedServiceFactory for ExtensionActionManager. | 22 // BrowserContextKeyedServiceFactory for ExtensionActionManager. |
| 21 class ExtensionActionManagerFactory : public BrowserContextKeyedServiceFactory { | 23 class ExtensionActionManagerFactory : public BrowserContextKeyedServiceFactory { |
| 22 public: | 24 public: |
| 23 // BrowserContextKeyedServiceFactory implementation: | 25 // BrowserContextKeyedServiceFactory implementation: |
| 24 static ExtensionActionManager* GetForProfile(Profile* profile) { | 26 static ExtensionActionManager* GetForProfile(Profile* profile) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 content::BrowserContext* browser_context, | 77 content::BrowserContext* browser_context, |
| 76 const Extension* extension, | 78 const Extension* extension, |
| 77 UnloadedExtensionInfo::Reason reason) { | 79 UnloadedExtensionInfo::Reason reason) { |
| 78 page_actions_.erase(extension->id()); | 80 page_actions_.erase(extension->id()); |
| 79 browser_actions_.erase(extension->id()); | 81 browser_actions_.erase(extension->id()); |
| 80 system_indicators_.erase(extension->id()); | 82 system_indicators_.erase(extension->id()); |
| 81 } | 83 } |
| 82 | 84 |
| 83 namespace { | 85 namespace { |
| 84 | 86 |
| 87 // Loads resources missing from |action| (ie title, icons) from "icons" key of | |
|
Devlin
2014/07/25 16:46:55
"i.e."
"the "icons" key"
gpdavis
2014/07/28 21:40:36
Done.
| |
| 88 // |extension|'s manifest. | |
| 89 void PopulateMissingValues(const Extension& extension, | |
| 90 ExtensionAction* action) { | |
| 91 if (!action) | |
|
Devlin
2014/07/25 16:46:55
Why are we passing in NULL actions in the first pl
gpdavis
2014/07/28 21:40:36
You're right; we shouldn't need to check for this.
| |
| 92 return; | |
| 93 | |
| 94 // If the title is missing from |action|, set it to |extension|'s name. | |
| 95 if (!action->HasTitle(ExtensionAction::kDefaultTabId)) | |
| 96 action->SetTitle(ExtensionAction::kDefaultTabId, extension.name()); | |
| 97 | |
| 98 // Get largest available icon for |extension|. If no icon is found, there | |
| 99 // is nothing available to replace missing action icons with, so we return. | |
| 100 std::string icon_path = extensions::IconsInfo::GetIcons(&extension).Get( | |
| 101 extension_misc::EXTENSION_ICON_GIGANTOR, | |
| 102 ExtensionIconSet::MATCH_SMALLER); | |
| 103 if (icon_path.empty()) | |
| 104 return; | |
| 105 | |
| 106 scoped_ptr<ExtensionIconSet> default_icon(new ExtensionIconSet()); | |
| 107 if (action->default_icon()) | |
| 108 *default_icon = *action->default_icon(); | |
| 109 | |
| 110 // Replace any missing extension action icons with the largest icon | |
| 111 // retrieved from |extension|'s manifest. | |
| 112 for (size_t i = 0; i < extension_misc::kNumExtensionActionIconSizes; ++i) { | |
| 113 int size = extension_misc::kExtensionActionIconSizes[i]; | |
| 114 if (default_icon->Get(size, ExtensionIconSet::MATCH_EXACTLY).empty()) | |
| 115 default_icon->Add(size, icon_path); | |
| 116 } | |
| 117 | |
| 118 action->set_default_icon(default_icon.Pass()); | |
| 119 } | |
| 120 | |
| 85 // Returns map[extension_id] if that entry exists. Otherwise, if | 121 // Returns map[extension_id] if that entry exists. Otherwise, if |
| 86 // action_info!=NULL, creates an ExtensionAction from it, fills in the map, and | 122 // action_info!=NULL, creates an ExtensionAction from it, fills in the map, and |
| 87 // returns that. Otherwise (action_info==NULL), returns NULL. | 123 // returns that. Otherwise (action_info==NULL), returns NULL. |
| 88 ExtensionAction* GetOrCreateOrNull( | 124 ExtensionAction* GetOrCreateOrNull( |
| 89 std::map<std::string, linked_ptr<ExtensionAction> >* map, | 125 std::map<std::string, linked_ptr<ExtensionAction> >* map, |
| 90 const std::string& extension_id, | 126 const std::string& extension_id, |
| 91 ActionInfo::Type action_type, | 127 ActionInfo::Type action_type, |
| 92 const ActionInfo* action_info, | 128 const ActionInfo* action_info, |
| 93 Profile* profile) { | 129 Profile* profile) { |
| 94 std::map<std::string, linked_ptr<ExtensionAction> >::const_iterator it = | 130 std::map<std::string, linked_ptr<ExtensionAction> >::const_iterator it = |
| 95 map->find(extension_id); | 131 map->find(extension_id); |
| 96 if (it != map->end()) | 132 if (it != map->end()) |
| 97 return it->second.get(); | 133 return it->second.get(); |
| 98 if (!action_info) | 134 if (!action_info) |
| 99 return NULL; | 135 return NULL; |
| 100 | 136 |
| 101 // Only create action info for enabled extensions. | 137 // Only create action info for enabled extensions. |
| 102 // This avoids bugs where actions are recreated just after being removed | 138 // This avoids bugs where actions are recreated just after being removed |
| 103 // in response to OnExtensionUnloaded(). | 139 // in response to OnExtensionUnloaded(). |
| 104 ExtensionService* service = | 140 ExtensionService* service = |
| 105 ExtensionSystem::Get(profile)->extension_service(); | 141 ExtensionSystem::Get(profile)->extension_service(); |
| 106 if (!service->GetExtensionById(extension_id, false)) | 142 if (!service->GetExtensionById(extension_id, false)) |
| 107 return NULL; | 143 return NULL; |
| 108 | 144 |
| 109 linked_ptr<ExtensionAction> action(new ExtensionAction( | 145 linked_ptr<ExtensionAction> action(new ExtensionAction( |
| 110 extension_id, action_type, *action_info)); | 146 extension_id, action_type, *action_info)); |
| 111 (*map)[extension_id] = action; | 147 (*map)[extension_id] = action; |
| 148 PopulateMissingValues(*service->GetExtensionById(extension_id, false), | |
|
Devlin
2014/07/25 16:46:55
this is deprecated. Use ExtensionRegistry::enabled
gpdavis
2014/07/28 21:40:36
Any idea why this deprecation isn't documented? D
not at google - send to devlin
2014/07/28 21:55:36
good point: https://codereview.chromium.org/426553
| |
| 149 action.get()); | |
| 112 return action.get(); | 150 return action.get(); |
| 113 } | 151 } |
| 114 | 152 |
| 115 } // namespace | 153 } // namespace |
| 116 | 154 |
| 117 ExtensionAction* ExtensionActionManager::GetPageAction( | 155 ExtensionAction* ExtensionActionManager::GetPageAction( |
| 118 const extensions::Extension& extension) const { | 156 const Extension& extension) const { |
| 119 return GetOrCreateOrNull(&page_actions_, extension.id(), | 157 return GetOrCreateOrNull(&page_actions_, extension.id(), |
| 120 ActionInfo::TYPE_PAGE, | 158 ActionInfo::TYPE_PAGE, |
| 121 ActionInfo::GetPageActionInfo(&extension), | 159 ActionInfo::GetPageActionInfo(&extension), |
| 122 profile_); | 160 profile_); |
| 123 } | 161 } |
| 124 | 162 |
| 125 ExtensionAction* ExtensionActionManager::GetBrowserAction( | 163 ExtensionAction* ExtensionActionManager::GetBrowserAction( |
| 126 const extensions::Extension& extension) const { | 164 const Extension& extension) const { |
| 127 return GetOrCreateOrNull(&browser_actions_, extension.id(), | 165 return GetOrCreateOrNull(&browser_actions_, extension.id(), |
| 128 ActionInfo::TYPE_BROWSER, | 166 ActionInfo::TYPE_BROWSER, |
| 129 ActionInfo::GetBrowserActionInfo(&extension), | 167 ActionInfo::GetBrowserActionInfo(&extension), |
| 130 profile_); | 168 profile_); |
| 131 } | 169 } |
| 132 | 170 |
| 171 ExtensionAction* ExtensionActionManager::GetBestFitAction( | |
|
Devlin
2014/07/25 16:46:55
I'm a bit confused. I thought part of the purpose
gpdavis
2014/07/28 21:40:36
I'm actually a bit confused about the purpose of t
not at google - send to devlin
2014/07/28 21:55:36
Yes exactly.
one could imagine super-cool functio
gpdavis
2014/07/29 00:18:01
Can do. A couple of design questions:
Script inj
| |
| 172 const Extension& extension) const { | |
| 173 if (ActionInfo::GetBrowserActionInfo(&extension)) | |
| 174 return GetBrowserAction(extension); | |
| 175 return GetPageAction(extension); | |
| 176 } | |
| 177 | |
| 133 ExtensionAction* ExtensionActionManager::GetSystemIndicator( | 178 ExtensionAction* ExtensionActionManager::GetSystemIndicator( |
| 134 const extensions::Extension& extension) const { | 179 const Extension& extension) const { |
| 135 // If it does not already exist, create the SystemIndicatorManager for the | 180 // If it does not already exist, create the SystemIndicatorManager for the |
| 136 // given profile. This could return NULL if the system indicator area is | 181 // given profile. This could return NULL if the system indicator area is |
| 137 // unavailable on the current system. If so, return NULL to signal that | 182 // unavailable on the current system. If so, return NULL to signal that |
| 138 // the system indicator area is unusable. | 183 // the system indicator area is unusable. |
| 139 if (!extensions::SystemIndicatorManagerFactory::GetForProfile(profile_)) | 184 if (!extensions::SystemIndicatorManagerFactory::GetForProfile(profile_)) |
| 140 return NULL; | 185 return NULL; |
| 141 | 186 |
| 142 return GetOrCreateOrNull(&system_indicators_, extension.id(), | 187 return GetOrCreateOrNull(&system_indicators_, extension.id(), |
| 143 ActionInfo::TYPE_SYSTEM_INDICATOR, | 188 ActionInfo::TYPE_SYSTEM_INDICATOR, |
| 144 ActionInfo::GetSystemIndicatorInfo(&extension), | 189 ActionInfo::GetSystemIndicatorInfo(&extension), |
| 145 profile_); | 190 profile_); |
| 146 } | 191 } |
| 147 | 192 |
| 148 } // namespace extensions | 193 } // namespace extensions |
| OLD | NEW |