Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Side by Side Diff: chrome/browser/extensions/extension_action_manager.cc

Issue 415813003: Improve extension icon prediction (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/extensions/extension_action_manager.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
88 // |extension|'s manifest.
89 void PopulateMissingValues(const extensions::Extension& extension,
not at google - send to devlin 2014/07/24 00:41:26 extensions:: unnecessary (you might as well do fil
gpdavis 2014/07/24 17:57:46 Done.
90 ExtensionAction* action) {
91 if (action) {
not at google - send to devlin 2014/07/24 00:41:26 if anything, early-return here if it's null. but I
gpdavis 2014/07/24 17:57:45 Done.
92 // If the title is missing from |action|, set it to |extension|'s name.
93 if (!action->HasTitle(ExtensionAction::kDefaultTabId))
94 action->SetTitle(ExtensionAction::kDefaultTabId, extension.name());
95
96 // Get largest available icon for |extension|. If no icon is found, there
97 // is nothing available to replace missing action icons with, so we return.
98 std::string icon_path = extensions::IconsInfo::GetIcons(&extension).Get(
99 extension_misc::EXTENSION_ICON_GIGANTOR,
100 ExtensionIconSet::MATCH_SMALLER);
101 if (icon_path.empty())
102 return;
103
104 const ExtensionIconSet* default_icon = action->default_icon();
105 ExtensionIconSet new_default_icon;
106 if (default_icon)
107 new_default_icon = *default_icon;
108
109 // Replace any missing extension action icons with the largest icon
110 // retrieved from |extension|'s manifest.
111 for (size_t i = 0; i < extension_misc::kNumExtensionActionIconSizes; ++i) {
112 int size = extension_misc::kExtensionActionIconSizes[i];
113 if (new_default_icon.Get(size, ExtensionIconSet::MATCH_EXACTLY).empty())
114 new_default_icon.Add(size, icon_path);
115 }
116
117 action->set_default_icon(
118 make_scoped_ptr(new ExtensionIconSet(new_default_icon)));
not at google - send to devlin 2014/07/24 00:41:26 to avoid all this copying you could assign new_def
gpdavis 2014/07/24 17:57:46 Done.
119 }
120 }
121
85 // Returns map[extension_id] if that entry exists. Otherwise, if 122 // Returns map[extension_id] if that entry exists. Otherwise, if
86 // action_info!=NULL, creates an ExtensionAction from it, fills in the map, and 123 // action_info!=NULL, creates an ExtensionAction from it, fills in the map, and
87 // returns that. Otherwise (action_info==NULL), returns NULL. 124 // returns that. Otherwise (action_info==NULL), returns NULL.
88 ExtensionAction* GetOrCreateOrNull( 125 ExtensionAction* GetOrCreateOrNull(
89 std::map<std::string, linked_ptr<ExtensionAction> >* map, 126 std::map<std::string, linked_ptr<ExtensionAction> >* map,
90 const std::string& extension_id, 127 const std::string& extension_id,
91 ActionInfo::Type action_type, 128 ActionInfo::Type action_type,
92 const ActionInfo* action_info, 129 const ActionInfo* action_info,
93 Profile* profile) { 130 Profile* profile) {
94 std::map<std::string, linked_ptr<ExtensionAction> >::const_iterator it = 131 std::map<std::string, linked_ptr<ExtensionAction> >::const_iterator it =
95 map->find(extension_id); 132 map->find(extension_id);
96 if (it != map->end()) 133 if (it != map->end())
97 return it->second.get(); 134 return it->second.get();
98 if (!action_info) 135 if (!action_info)
99 return NULL; 136 return NULL;
100 137
101 // Only create action info for enabled extensions. 138 // Only create action info for enabled extensions.
102 // This avoids bugs where actions are recreated just after being removed 139 // This avoids bugs where actions are recreated just after being removed
103 // in response to OnExtensionUnloaded(). 140 // in response to OnExtensionUnloaded().
104 ExtensionService* service = 141 ExtensionService* service =
105 ExtensionSystem::Get(profile)->extension_service(); 142 ExtensionSystem::Get(profile)->extension_service();
106 if (!service->GetExtensionById(extension_id, false)) 143 if (!service->GetExtensionById(extension_id, false))
107 return NULL; 144 return NULL;
108 145
109 linked_ptr<ExtensionAction> action(new ExtensionAction( 146 linked_ptr<ExtensionAction> action(new ExtensionAction(
110 extension_id, action_type, *action_info)); 147 extension_id, action_type, *action_info));
111 (*map)[extension_id] = action; 148 (*map)[extension_id] = action;
149 PopulateMissingValues(*service->GetExtensionById(extension_id, false),
150 action.get());
not at google - send to devlin 2014/07/24 00:41:26 nice! but let's be conservative for now and just d
gpdavis 2014/07/24 17:57:45 Unfortunately, this needs to be done upon creation
not at google - send to devlin 2014/07/24 18:01:46 I think that's fine.
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 extensions::Extension& extension) const {
119 return GetOrCreateOrNull(&page_actions_, extension.id(), 158 return GetOrCreateOrNull(&page_actions_, extension.id(),
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 extensions::Extension& extension) const {
127 return GetOrCreateOrNull(&browser_actions_, extension.id(), 166 return GetOrCreateOrNull(&browser_actions_, extension.id(),
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 ExtensionAction* ExtensionActionManager::GetBestFitAction(
173 const extensions::Extension& extension) const {
174 if (ActionInfo::GetBrowserActionInfo(&extension))
175 return GetBrowserAction(extension);
176 else
not at google - send to devlin 2014/07/24 00:41:26 no else after return.
gpdavis 2014/07/24 17:57:46 Done.
177 return GetPageAction(extension);
178 }
179
133 ExtensionAction* ExtensionActionManager::GetSystemIndicator( 180 ExtensionAction* ExtensionActionManager::GetSystemIndicator(
134 const extensions::Extension& extension) const { 181 const extensions::Extension& extension) const {
135 // If it does not already exist, create the SystemIndicatorManager for the 182 // If it does not already exist, create the SystemIndicatorManager for the
136 // given profile. This could return NULL if the system indicator area is 183 // 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 184 // unavailable on the current system. If so, return NULL to signal that
138 // the system indicator area is unusable. 185 // the system indicator area is unusable.
139 if (!extensions::SystemIndicatorManagerFactory::GetForProfile(profile_)) 186 if (!extensions::SystemIndicatorManagerFactory::GetForProfile(profile_))
140 return NULL; 187 return NULL;
141 188
142 return GetOrCreateOrNull(&system_indicators_, extension.id(), 189 return GetOrCreateOrNull(&system_indicators_, extension.id(),
143 ActionInfo::TYPE_SYSTEM_INDICATOR, 190 ActionInfo::TYPE_SYSTEM_INDICATOR,
144 ActionInfo::GetSystemIndicatorInfo(&extension), 191 ActionInfo::GetSystemIndicatorInfo(&extension),
145 profile_); 192 profile_);
146 } 193 }
147 194
148 } // namespace extensions 195 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_action_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698