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" | |
| 10 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 11 #include "components/keyed_service/content/browser_context_dependency_manager.h" | 10 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 12 #include "extensions/browser/extension_registry.h" | 11 #include "extensions/browser/extension_registry.h" |
| 13 #include "extensions/browser/extension_system.h" | 12 #include "extensions/browser/extension_system.h" |
| 14 #include "extensions/browser/extensions_browser_client.h" | 13 #include "extensions/browser/extensions_browser_client.h" |
| 14 #include "extensions/common/constants.h" | |
| 15 #include "extensions/common/manifest_handlers/icons_handler.h" | |
| 15 | 16 |
| 16 namespace extensions { | 17 namespace extensions { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 // BrowserContextKeyedServiceFactory for ExtensionActionManager. | 21 // BrowserContextKeyedServiceFactory for ExtensionActionManager. |
| 21 class ExtensionActionManagerFactory : public BrowserContextKeyedServiceFactory { | 22 class ExtensionActionManagerFactory : public BrowserContextKeyedServiceFactory { |
| 22 public: | 23 public: |
| 23 // BrowserContextKeyedServiceFactory implementation: | 24 // BrowserContextKeyedServiceFactory implementation: |
| 24 static ExtensionActionManager* GetForProfile(Profile* profile) { | 25 static ExtensionActionManager* GetForProfile(Profile* profile) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 content::BrowserContext* browser_context, | 76 content::BrowserContext* browser_context, |
| 76 const Extension* extension, | 77 const Extension* extension, |
| 77 UnloadedExtensionInfo::Reason reason) { | 78 UnloadedExtensionInfo::Reason reason) { |
| 78 page_actions_.erase(extension->id()); | 79 page_actions_.erase(extension->id()); |
| 79 browser_actions_.erase(extension->id()); | 80 browser_actions_.erase(extension->id()); |
| 80 system_indicators_.erase(extension->id()); | 81 system_indicators_.erase(extension->id()); |
| 81 } | 82 } |
| 82 | 83 |
| 83 namespace { | 84 namespace { |
| 84 | 85 |
| 86 // Loads resources missing from |action| (i.e. title, icons) from the "icons" | |
| 87 // key of |extension|'s manifest. | |
| 88 void PopulateMissingValues(const Extension& extension, | |
| 89 ExtensionAction* action) { | |
| 90 // If the title is missing from |action|, set it to |extension|'s name. | |
| 91 if (!action->HasTitle(ExtensionAction::kDefaultTabId)) | |
| 92 action->SetTitle(ExtensionAction::kDefaultTabId, extension.name()); | |
| 93 | |
| 94 // Get largest available icon for |extension|. If no icon is found, there | |
| 95 // is nothing available to replace missing action icons with, so we return. | |
| 96 std::string icon_path = extensions::IconsInfo::GetIcons(&extension).Get( | |
| 97 extension_misc::EXTENSION_ICON_GIGANTOR, | |
| 98 ExtensionIconSet::MATCH_SMALLER); | |
| 99 if (icon_path.empty()) | |
| 100 return; | |
|
not at google - send to devlin
2014/08/01 18:43:37
much as it's tempting to early-return here I think
gpdavis
2014/08/01 20:23:10
Hmm. How would you prefer this to be organized?
| |
| 101 | |
| 102 scoped_ptr<ExtensionIconSet> default_icon(new ExtensionIconSet()); | |
| 103 if (action->default_icon()) | |
| 104 *default_icon = *action->default_icon(); | |
| 105 | |
| 106 // Replace any missing extension action icons with the largest icon | |
| 107 // retrieved from |extension|'s manifest. | |
| 108 for (size_t i = 0; i < extension_misc::kNumExtensionActionIconSizes; ++i) { | |
| 109 int size = extension_misc::kExtensionActionIconSizes[i]; | |
| 110 if (default_icon->Get(size, ExtensionIconSet::MATCH_EXACTLY).empty()) | |
| 111 default_icon->Add(size, icon_path); | |
| 112 } | |
| 113 | |
| 114 action->set_default_icon(default_icon.Pass()); | |
| 115 } | |
| 116 | |
| 85 // Returns map[extension_id] if that entry exists. Otherwise, if | 117 // Returns map[extension_id] if that entry exists. Otherwise, if |
| 86 // action_info!=NULL, creates an ExtensionAction from it, fills in the map, and | 118 // action_info!=NULL, creates an ExtensionAction from it, fills in the map, and |
| 87 // returns that. Otherwise (action_info==NULL), returns NULL. | 119 // returns that. Otherwise (action_info==NULL), returns NULL. |
| 88 ExtensionAction* GetOrCreateOrNull( | 120 ExtensionAction* GetOrCreateOrNull( |
| 89 std::map<std::string, linked_ptr<ExtensionAction> >* map, | 121 std::map<std::string, linked_ptr<ExtensionAction> >* map, |
| 90 const std::string& extension_id, | 122 const std::string& extension_id, |
| 91 ActionInfo::Type action_type, | 123 ActionInfo::Type action_type, |
| 92 const ActionInfo* action_info, | 124 const ActionInfo* action_info, |
| 93 Profile* profile) { | 125 Profile* profile) { |
| 94 std::map<std::string, linked_ptr<ExtensionAction> >::const_iterator it = | 126 std::map<std::string, linked_ptr<ExtensionAction> >::const_iterator it = |
| 95 map->find(extension_id); | 127 map->find(extension_id); |
| 96 if (it != map->end()) | 128 if (it != map->end()) |
| 97 return it->second.get(); | 129 return it->second.get(); |
| 98 if (!action_info) | 130 if (!action_info) |
| 99 return NULL; | 131 return NULL; |
| 100 | 132 |
| 101 // Only create action info for enabled extensions. | 133 // Only create action info for enabled extensions. |
| 102 // This avoids bugs where actions are recreated just after being removed | 134 // This avoids bugs where actions are recreated just after being removed |
| 103 // in response to OnExtensionUnloaded(). | 135 // in response to OnExtensionUnloaded(). |
| 104 ExtensionService* service = | 136 const Extension* extension = ExtensionRegistry::Get(profile) |
|
not at google - send to devlin
2014/08/01 18:43:37
all call sites actually have access to an Extensio
gpdavis
2014/08/01 20:23:10
I'll pass in a const Extension* since we have a nu
| |
| 105 ExtensionSystem::Get(profile)->extension_service(); | 137 ->enabled_extensions().GetByID(extension_id); |
| 106 if (!service->GetExtensionById(extension_id, false)) | 138 if (!extension) |
| 107 return NULL; | 139 return NULL; |
| 108 | 140 |
| 109 linked_ptr<ExtensionAction> action(new ExtensionAction( | 141 linked_ptr<ExtensionAction> action(new ExtensionAction( |
| 110 extension_id, action_type, *action_info)); | 142 extension_id, action_type, *action_info)); |
| 111 (*map)[extension_id] = action; | 143 (*map)[extension_id] = action; |
| 144 PopulateMissingValues(*extension, action.get()); | |
| 112 return action.get(); | 145 return action.get(); |
| 113 } | 146 } |
| 114 | 147 |
| 115 } // namespace | 148 } // namespace |
| 116 | 149 |
| 117 ExtensionAction* ExtensionActionManager::GetPageAction( | 150 ExtensionAction* ExtensionActionManager::GetPageAction( |
| 118 const extensions::Extension& extension) const { | 151 const Extension& extension) const { |
| 119 return GetOrCreateOrNull(&page_actions_, extension.id(), | 152 return GetOrCreateOrNull(&page_actions_, extension.id(), |
| 120 ActionInfo::TYPE_PAGE, | 153 ActionInfo::TYPE_PAGE, |
| 121 ActionInfo::GetPageActionInfo(&extension), | 154 ActionInfo::GetPageActionInfo(&extension), |
| 122 profile_); | 155 profile_); |
| 123 } | 156 } |
| 124 | 157 |
| 125 ExtensionAction* ExtensionActionManager::GetBrowserAction( | 158 ExtensionAction* ExtensionActionManager::GetBrowserAction( |
| 126 const extensions::Extension& extension) const { | 159 const Extension& extension) const { |
| 127 return GetOrCreateOrNull(&browser_actions_, extension.id(), | 160 return GetOrCreateOrNull(&browser_actions_, extension.id(), |
| 128 ActionInfo::TYPE_BROWSER, | 161 ActionInfo::TYPE_BROWSER, |
| 129 ActionInfo::GetBrowserActionInfo(&extension), | 162 ActionInfo::GetBrowserActionInfo(&extension), |
| 130 profile_); | 163 profile_); |
| 131 } | 164 } |
| 132 | 165 |
| 166 scoped_ptr<ExtensionAction> ExtensionActionManager::GetBestFitAction( | |
| 167 const Extension& extension) const { | |
| 168 // Attempt to pull an existing action. | |
| 169 scoped_ptr<ExtensionAction> action(GetBrowserAction(extension)); | |
|
not at google - send to devlin
2014/08/01 18:43:37
I don't think this is right? the browser actions a
gpdavis
2014/08/01 20:23:10
Ahh, okay, so this method shouldn't return a refer
| |
| 170 if (action.get()) | |
| 171 return action.Pass(); | |
| 172 action.reset(GetPageAction(extension)); | |
| 173 if (action.get()) | |
| 174 return action.Pass(); | |
| 175 | |
| 176 // If no action exists, create and return a page action. | |
| 177 // Populate any missing values from |extension|'s manifest. | |
| 178 action.reset( | |
| 179 new ExtensionAction(extension.id(), ActionInfo::TYPE_PAGE, ActionInfo())); | |
|
not at google - send to devlin
2014/08/01 18:43:37
TYPE_PAGE isn't necessarily right. you'll need to
gpdavis
2014/08/01 20:23:10
So we should pass in the type of action we'd like
not at google - send to devlin
2014/08/05 00:40:09
yeah exactly.
gpdavis
2014/08/05 18:44:15
Solid. Patch set 6 has this change.
| |
| 180 PopulateMissingValues(extension, action.get()); | |
| 181 return action.Pass(); | |
| 182 } | |
| 183 | |
| 133 ExtensionAction* ExtensionActionManager::GetSystemIndicator( | 184 ExtensionAction* ExtensionActionManager::GetSystemIndicator( |
| 134 const extensions::Extension& extension) const { | 185 const Extension& extension) const { |
| 135 // If it does not already exist, create the SystemIndicatorManager for the | 186 // If it does not already exist, create the SystemIndicatorManager for the |
| 136 // given profile. This could return NULL if the system indicator area is | 187 // 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 | 188 // unavailable on the current system. If so, return NULL to signal that |
| 138 // the system indicator area is unusable. | 189 // the system indicator area is unusable. |
| 139 if (!extensions::SystemIndicatorManagerFactory::GetForProfile(profile_)) | 190 if (!extensions::SystemIndicatorManagerFactory::GetForProfile(profile_)) |
| 140 return NULL; | 191 return NULL; |
| 141 | 192 |
| 142 return GetOrCreateOrNull(&system_indicators_, extension.id(), | 193 return GetOrCreateOrNull(&system_indicators_, extension.id(), |
| 143 ActionInfo::TYPE_SYSTEM_INDICATOR, | 194 ActionInfo::TYPE_SYSTEM_INDICATOR, |
| 144 ActionInfo::GetSystemIndicatorInfo(&extension), | 195 ActionInfo::GetSystemIndicatorInfo(&extension), |
| 145 profile_); | 196 profile_); |
| 146 } | 197 } |
| 147 | 198 |
| 148 } // namespace extensions | 199 } // namespace extensions |
| OLD | NEW |