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 const int* kIconSizes = extension_misc::kExtensionActionIconSizes; | |
91 const size_t kNumIconSizes = extension_misc::kNumExtensionActionIconSizes; | |
92 | |
93 // If the title is missing from |action|, set it to |extension|'s name. | |
94 if (action->GetTitle(ExtensionAction::kDefaultTabId).empty()) | |
95 action->SetTitle(ExtensionAction::kDefaultTabId, extension.name()); | |
96 | |
97 scoped_ptr<ExtensionIconSet> default_icon(new ExtensionIconSet()); | |
98 if (action->default_icon()) | |
99 *default_icon = *action->default_icon(); | |
100 | |
101 const ExtensionIconSet& extension_icons = | |
102 extensions::IconsInfo::GetIcons(&extension); | |
103 std::string largest_icon = extension_icons.Get( | |
104 extension_misc::EXTENSION_ICON_GIGANTOR, | |
105 ExtensionIconSet::MATCH_SMALLER); | |
106 int largest_icon_size; | |
107 | |
108 if (!largest_icon.empty() && | |
109 (largest_icon_size = extension_icons.GetIconSizeFromPath(largest_icon))) { | |
not at google - send to devlin
2014/08/08 00:21:07
this looks kind of awkward, can't you declare that
gpdavis
2014/08/08 01:03:58
Oh, I misinterpreted what you said. I thought you
| |
110 // Replace any missing extension action icons with the largest icon | |
111 // retrieved from |extension|'s manifest so long as the largest icon is | |
112 // larger than the current key. | |
113 for (int i = kNumIconSizes - 1; i >= 0; --i) { | |
114 int size = kIconSizes[i]; | |
115 if (default_icon->Get(size, ExtensionIconSet::MATCH_BIGGER).empty() | |
116 && largest_icon_size > size) { | |
not at google - send to devlin
2014/08/08 00:21:07
this largest_icon_size>size check is funny - you'r
gpdavis
2014/08/08 01:03:58
What if the action has no icons, but the extension
not at google - send to devlin
2014/08/08 14:15:02
ah right, of course. now that it's a new day I thi
| |
117 default_icon->Add(size, largest_icon); | |
118 break; | |
119 } | |
120 } | |
121 action->set_default_icon(default_icon.Pass()); | |
122 } | |
123 } | |
124 | |
85 // Returns map[extension_id] if that entry exists. Otherwise, if | 125 // Returns map[extension_id] if that entry exists. Otherwise, if |
86 // action_info!=NULL, creates an ExtensionAction from it, fills in the map, and | 126 // action_info!=NULL, creates an ExtensionAction from it, fills in the map, and |
87 // returns that. Otherwise (action_info==NULL), returns NULL. | 127 // returns that. Otherwise (action_info==NULL), returns NULL. |
88 ExtensionAction* GetOrCreateOrNull( | 128 ExtensionAction* GetOrCreateOrNull( |
89 std::map<std::string, linked_ptr<ExtensionAction> >* map, | 129 std::map<std::string, linked_ptr<ExtensionAction> >* map, |
90 const std::string& extension_id, | 130 const Extension* extension, |
91 ActionInfo::Type action_type, | 131 ActionInfo::Type action_type, |
92 const ActionInfo* action_info, | 132 const ActionInfo* action_info, |
93 Profile* profile) { | 133 Profile* profile) { |
94 std::map<std::string, linked_ptr<ExtensionAction> >::const_iterator it = | 134 std::map<std::string, linked_ptr<ExtensionAction> >::const_iterator it = |
95 map->find(extension_id); | 135 map->find(extension->id()); |
96 if (it != map->end()) | 136 if (it != map->end()) |
97 return it->second.get(); | 137 return it->second.get(); |
98 if (!action_info) | 138 if (!action_info) |
99 return NULL; | 139 return NULL; |
100 | 140 |
101 // Only create action info for enabled extensions. | 141 // Only create action info for enabled extensions. |
102 // This avoids bugs where actions are recreated just after being removed | 142 // This avoids bugs where actions are recreated just after being removed |
103 // in response to OnExtensionUnloaded(). | 143 // in response to OnExtensionUnloaded(). |
104 ExtensionService* service = | 144 if (!extension) |
105 ExtensionSystem::Get(profile)->extension_service(); | |
106 if (!service->GetExtensionById(extension_id, false)) | |
107 return NULL; | 145 return NULL; |
108 | 146 |
109 linked_ptr<ExtensionAction> action(new ExtensionAction( | 147 linked_ptr<ExtensionAction> action(new ExtensionAction( |
110 extension_id, action_type, *action_info)); | 148 extension->id(), action_type, *action_info)); |
111 (*map)[extension_id] = action; | 149 (*map)[extension->id()] = action; |
150 PopulateMissingValues(*extension, action.get()); | |
112 return action.get(); | 151 return action.get(); |
113 } | 152 } |
114 | 153 |
115 } // namespace | 154 } // namespace |
116 | 155 |
117 ExtensionAction* ExtensionActionManager::GetPageAction( | 156 ExtensionAction* ExtensionActionManager::GetPageAction( |
118 const extensions::Extension& extension) const { | 157 const Extension& extension) const { |
119 return GetOrCreateOrNull(&page_actions_, extension.id(), | 158 return GetOrCreateOrNull(&page_actions_, &extension, |
120 ActionInfo::TYPE_PAGE, | 159 ActionInfo::TYPE_PAGE, |
121 ActionInfo::GetPageActionInfo(&extension), | 160 ActionInfo::GetPageActionInfo(&extension), |
122 profile_); | 161 profile_); |
123 } | 162 } |
124 | 163 |
125 ExtensionAction* ExtensionActionManager::GetBrowserAction( | 164 ExtensionAction* ExtensionActionManager::GetBrowserAction( |
126 const extensions::Extension& extension) const { | 165 const Extension& extension) const { |
127 return GetOrCreateOrNull(&browser_actions_, extension.id(), | 166 return GetOrCreateOrNull(&browser_actions_, &extension, |
128 ActionInfo::TYPE_BROWSER, | 167 ActionInfo::TYPE_BROWSER, |
129 ActionInfo::GetBrowserActionInfo(&extension), | 168 ActionInfo::GetBrowserActionInfo(&extension), |
130 profile_); | 169 profile_); |
131 } | 170 } |
132 | 171 |
172 scoped_ptr<ExtensionAction> ExtensionActionManager::GetBestFitAction( | |
173 const Extension& extension, | |
174 ActionInfo::Type type) const { | |
175 const ActionInfo* info = ActionInfo::GetBrowserActionInfo(&extension); | |
176 if (!info) | |
177 info = ActionInfo::GetPageActionInfo(&extension); | |
178 | |
179 // Create a new ExtensionAction of |type| with |extension|'s ActionInfo. | |
180 // If no ActionInfo exists for |extension|, create and return a new action | |
181 // with a blank ActionInfo. | |
182 // Populate any missing values from |extension|'s manifest. | |
183 scoped_ptr<ExtensionAction> new_action(new ExtensionAction( | |
184 extension.id(), type, info ? *info : ActionInfo())); | |
185 PopulateMissingValues(extension, new_action.get()); | |
186 return new_action.Pass(); | |
187 } | |
188 | |
133 ExtensionAction* ExtensionActionManager::GetSystemIndicator( | 189 ExtensionAction* ExtensionActionManager::GetSystemIndicator( |
134 const extensions::Extension& extension) const { | 190 const Extension& extension) const { |
135 // If it does not already exist, create the SystemIndicatorManager for the | 191 // If it does not already exist, create the SystemIndicatorManager for the |
136 // given profile. This could return NULL if the system indicator area is | 192 // 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 | 193 // unavailable on the current system. If so, return NULL to signal that |
138 // the system indicator area is unusable. | 194 // the system indicator area is unusable. |
139 if (!extensions::SystemIndicatorManagerFactory::GetForProfile(profile_)) | 195 if (!extensions::SystemIndicatorManagerFactory::GetForProfile(profile_)) |
140 return NULL; | 196 return NULL; |
141 | 197 |
142 return GetOrCreateOrNull(&system_indicators_, extension.id(), | 198 return GetOrCreateOrNull(&system_indicators_, &extension, |
143 ActionInfo::TYPE_SYSTEM_INDICATOR, | 199 ActionInfo::TYPE_SYSTEM_INDICATOR, |
144 ActionInfo::GetSystemIndicatorInfo(&extension), | 200 ActionInfo::GetSystemIndicatorInfo(&extension), |
145 profile_); | 201 profile_); |
146 } | 202 } |
147 | 203 |
148 } // namespace extensions | 204 } // namespace extensions |
OLD | NEW |