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 const int* kIconSizes = extension_misc::kExtensionActionIconSizes; | |
not at google - send to devlin
2014/08/07 16:03:37
should this be const int kIconSizes[] = ...? or do
gpdavis
2014/08/07 18:30:59
Well, I was trying to avoid copying the array by j
not at google - send to devlin
2014/08/07 19:03:28
I don't think using [] copies the array.
gpdavis
2014/08/07 21:28:18
You were right about the compiler not liking it:
c
| |
87 const int kNumIcons = extension_misc::kNumExtensionActionIconSizes; | |
not at google - send to devlin
2014/08/07 16:03:37
not much point making these scoped outside of the
gpdavis
2014/08/07 18:30:59
Done.
| |
88 | |
89 // Loads resources missing from |action| (i.e. title, icons) from the "icons" | |
90 // key of |extension|'s manifest. | |
91 void PopulateMissingValues(const Extension& extension, | |
92 ExtensionAction* action) { | |
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 // If the action has a 38px icon and no 19px icon, add the 38px icon path | |
102 // for the 19px key and return; | |
not at google - send to devlin
2014/08/07 16:03:37
why this? seems very specific to 19 vs 38 px icons
gpdavis
2014/08/07 18:30:59
It certainly is specific to 19 and 38px icons.
Es
not at google - send to devlin
2014/08/07 19:03:28
Yes the modulo thing was so that we get cleaner ic
gpdavis
2014/08/07 21:28:18
This is essentially what I have, except I'm passin
not at google - send to devlin
2014/08/07 21:34:41
Yeah that's what I'm saying.
Specifics of 19 and
| |
103 if (default_icon->Get(kIconSizes[0], | |
104 ExtensionIconSet::MATCH_EXACTLY).empty() && | |
105 !default_icon->Get(kIconSizes[1], | |
106 ExtensionIconSet::MATCH_EXACTLY).empty()) { | |
107 default_icon->Add(kIconSizes[0], default_icon->Get(kIconSizes[1], | |
108 ExtensionIconSet::MATCH_EXACTLY)); | |
109 } else { | |
110 // Get largest available icon for |extension|. | |
111 const ExtensionIconSet& extension_icons = | |
112 extensions::IconsInfo::GetIcons(&extension); | |
113 std::string largest_icon = extension_icons.Get( | |
114 extension_misc::EXTENSION_ICON_GIGANTOR, | |
115 ExtensionIconSet::MATCH_SMALLER); | |
116 | |
117 if (!largest_icon.empty()) { | |
118 // Replace any missing extension action icons with the largest icon | |
119 // retrieved from |extension|'s manifest so long as the largest icon is | |
120 // larger than the current key. | |
121 for (size_t i = 0; i < kNumIcons; ++i) { | |
122 int size = kIconSizes[i]; | |
123 if (default_icon->Get(size, ExtensionIconSet::MATCH_EXACTLY).empty() | |
124 && extension_icons.GetIconSizeFromPath(largest_icon) > size) | |
125 default_icon->Add(size, largest_icon); | |
not at google - send to devlin
2014/08/07 16:03:37
{} around this.
gpdavis
2014/08/07 18:30:59
Done.
| |
126 } | |
127 | |
128 } | |
129 } | |
130 | |
131 action->set_default_icon(default_icon.Pass()); | |
132 } | |
133 | |
85 // Returns map[extension_id] if that entry exists. Otherwise, if | 134 // Returns map[extension_id] if that entry exists. Otherwise, if |
86 // action_info!=NULL, creates an ExtensionAction from it, fills in the map, and | 135 // action_info!=NULL, creates an ExtensionAction from it, fills in the map, and |
87 // returns that. Otherwise (action_info==NULL), returns NULL. | 136 // returns that. Otherwise (action_info==NULL), returns NULL. |
88 ExtensionAction* GetOrCreateOrNull( | 137 ExtensionAction* GetOrCreateOrNull( |
89 std::map<std::string, linked_ptr<ExtensionAction> >* map, | 138 std::map<std::string, linked_ptr<ExtensionAction> >* map, |
90 const std::string& extension_id, | 139 const Extension* extension, |
91 ActionInfo::Type action_type, | 140 ActionInfo::Type action_type, |
92 const ActionInfo* action_info, | 141 const ActionInfo* action_info, |
93 Profile* profile) { | 142 Profile* profile) { |
94 std::map<std::string, linked_ptr<ExtensionAction> >::const_iterator it = | 143 std::map<std::string, linked_ptr<ExtensionAction> >::const_iterator it = |
95 map->find(extension_id); | 144 map->find(extension->id()); |
96 if (it != map->end()) | 145 if (it != map->end()) |
97 return it->second.get(); | 146 return it->second.get(); |
98 if (!action_info) | 147 if (!action_info) |
99 return NULL; | 148 return NULL; |
100 | 149 |
101 // Only create action info for enabled extensions. | 150 // Only create action info for enabled extensions. |
102 // This avoids bugs where actions are recreated just after being removed | 151 // This avoids bugs where actions are recreated just after being removed |
103 // in response to OnExtensionUnloaded(). | 152 // in response to OnExtensionUnloaded(). |
104 ExtensionService* service = | 153 if (!extension) |
105 ExtensionSystem::Get(profile)->extension_service(); | |
106 if (!service->GetExtensionById(extension_id, false)) | |
107 return NULL; | 154 return NULL; |
108 | 155 |
109 linked_ptr<ExtensionAction> action(new ExtensionAction( | 156 linked_ptr<ExtensionAction> action(new ExtensionAction( |
110 extension_id, action_type, *action_info)); | 157 extension->id(), action_type, *action_info)); |
111 (*map)[extension_id] = action; | 158 (*map)[extension->id()] = action; |
159 PopulateMissingValues(*extension, action.get()); | |
112 return action.get(); | 160 return action.get(); |
113 } | 161 } |
114 | 162 |
115 } // namespace | 163 } // namespace |
116 | 164 |
117 ExtensionAction* ExtensionActionManager::GetPageAction( | 165 ExtensionAction* ExtensionActionManager::GetPageAction( |
118 const extensions::Extension& extension) const { | 166 const Extension& extension) const { |
119 return GetOrCreateOrNull(&page_actions_, extension.id(), | 167 return GetOrCreateOrNull(&page_actions_, &extension, |
120 ActionInfo::TYPE_PAGE, | 168 ActionInfo::TYPE_PAGE, |
121 ActionInfo::GetPageActionInfo(&extension), | 169 ActionInfo::GetPageActionInfo(&extension), |
122 profile_); | 170 profile_); |
123 } | 171 } |
124 | 172 |
125 ExtensionAction* ExtensionActionManager::GetBrowserAction( | 173 ExtensionAction* ExtensionActionManager::GetBrowserAction( |
126 const extensions::Extension& extension) const { | 174 const Extension& extension) const { |
127 return GetOrCreateOrNull(&browser_actions_, extension.id(), | 175 return GetOrCreateOrNull(&browser_actions_, &extension, |
128 ActionInfo::TYPE_BROWSER, | 176 ActionInfo::TYPE_BROWSER, |
129 ActionInfo::GetBrowserActionInfo(&extension), | 177 ActionInfo::GetBrowserActionInfo(&extension), |
130 profile_); | 178 profile_); |
131 } | 179 } |
132 | 180 |
181 scoped_ptr<ExtensionAction> ExtensionActionManager::GetBestFitAction( | |
182 const Extension& extension, | |
183 ActionInfo::Type type) const { | |
184 const ActionInfo* info = ActionInfo::GetBrowserActionInfo(&extension); | |
185 if (!info) | |
186 info = ActionInfo::GetPageActionInfo(&extension); | |
187 | |
188 // Create a new ExtensionAction of |type| with |extension|'s ActionInfo. | |
189 // If no ActionInfo exists for |extension|, create and return a new action | |
190 // with a blank ActionInfo. | |
191 // Populate any missing values from |extension|'s manifest. | |
192 scoped_ptr<ExtensionAction> new_action(new ExtensionAction( | |
193 extension.id(), type, info ? *info : ActionInfo())); | |
194 PopulateMissingValues(extension, new_action.get()); | |
195 return new_action.Pass(); | |
196 } | |
197 | |
133 ExtensionAction* ExtensionActionManager::GetSystemIndicator( | 198 ExtensionAction* ExtensionActionManager::GetSystemIndicator( |
134 const extensions::Extension& extension) const { | 199 const Extension& extension) const { |
135 // If it does not already exist, create the SystemIndicatorManager for the | 200 // If it does not already exist, create the SystemIndicatorManager for the |
136 // given profile. This could return NULL if the system indicator area is | 201 // 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 | 202 // unavailable on the current system. If so, return NULL to signal that |
138 // the system indicator area is unusable. | 203 // the system indicator area is unusable. |
139 if (!extensions::SystemIndicatorManagerFactory::GetForProfile(profile_)) | 204 if (!extensions::SystemIndicatorManagerFactory::GetForProfile(profile_)) |
140 return NULL; | 205 return NULL; |
141 | 206 |
142 return GetOrCreateOrNull(&system_indicators_, extension.id(), | 207 return GetOrCreateOrNull(&system_indicators_, &extension, |
143 ActionInfo::TYPE_SYSTEM_INDICATOR, | 208 ActionInfo::TYPE_SYSTEM_INDICATOR, |
144 ActionInfo::GetSystemIndicatorInfo(&extension), | 209 ActionInfo::GetSystemIndicatorInfo(&extension), |
145 profile_); | 210 profile_); |
146 } | 211 } |
147 | 212 |
148 } // namespace extensions | 213 } // namespace extensions |
OLD | NEW |