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

Unified 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: Enhanced tests, updated icon logic Created 6 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/extension_action_manager.cc
diff --git a/chrome/browser/extensions/extension_action_manager.cc b/chrome/browser/extensions/extension_action_manager.cc
index b4799741373788db69ca8511c2a4e6cd1c9bd681..eeefe0310daabdd903e781c891d3dcff506aac60 100644
--- a/chrome/browser/extensions/extension_action_manager.cc
+++ b/chrome/browser/extensions/extension_action_manager.cc
@@ -6,12 +6,13 @@
#include "chrome/browser/extensions/api/system_indicator/system_indicator_manager_factory.h"
#include "chrome/browser/extensions/extension_action.h"
-#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/extensions_browser_client.h"
+#include "extensions/common/constants.h"
+#include "extensions/common/manifest_handlers/icons_handler.h"
namespace extensions {
@@ -82,17 +83,65 @@ void ExtensionActionManager::OnExtensionUnloaded(
namespace {
+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
+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.
+
+// Loads resources missing from |action| (i.e. title, icons) from the "icons"
+// key of |extension|'s manifest.
+void PopulateMissingValues(const Extension& extension,
+ ExtensionAction* action) {
+ // If the title is missing from |action|, set it to |extension|'s name.
+ if (action->GetTitle(ExtensionAction::kDefaultTabId).empty())
+ action->SetTitle(ExtensionAction::kDefaultTabId, extension.name());
+
+ scoped_ptr<ExtensionIconSet> default_icon(new ExtensionIconSet());
+ if (action->default_icon())
+ *default_icon = *action->default_icon();
+
+ // If the action has a 38px icon and no 19px icon, add the 38px icon path
+ // 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
+ if (default_icon->Get(kIconSizes[0],
+ ExtensionIconSet::MATCH_EXACTLY).empty() &&
+ !default_icon->Get(kIconSizes[1],
+ ExtensionIconSet::MATCH_EXACTLY).empty()) {
+ default_icon->Add(kIconSizes[0], default_icon->Get(kIconSizes[1],
+ ExtensionIconSet::MATCH_EXACTLY));
+ } else {
+ // Get largest available icon for |extension|.
+ const ExtensionIconSet& extension_icons =
+ extensions::IconsInfo::GetIcons(&extension);
+ std::string largest_icon = extension_icons.Get(
+ extension_misc::EXTENSION_ICON_GIGANTOR,
+ ExtensionIconSet::MATCH_SMALLER);
+
+ if (!largest_icon.empty()) {
+ // Replace any missing extension action icons with the largest icon
+ // retrieved from |extension|'s manifest so long as the largest icon is
+ // larger than the current key.
+ for (size_t i = 0; i < kNumIcons; ++i) {
+ int size = kIconSizes[i];
+ if (default_icon->Get(size, ExtensionIconSet::MATCH_EXACTLY).empty()
+ && extension_icons.GetIconSizeFromPath(largest_icon) > size)
+ 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.
+ }
+
+ }
+ }
+
+ action->set_default_icon(default_icon.Pass());
+}
+
// Returns map[extension_id] if that entry exists. Otherwise, if
// action_info!=NULL, creates an ExtensionAction from it, fills in the map, and
// returns that. Otherwise (action_info==NULL), returns NULL.
ExtensionAction* GetOrCreateOrNull(
std::map<std::string, linked_ptr<ExtensionAction> >* map,
- const std::string& extension_id,
+ const Extension* extension,
ActionInfo::Type action_type,
const ActionInfo* action_info,
Profile* profile) {
std::map<std::string, linked_ptr<ExtensionAction> >::const_iterator it =
- map->find(extension_id);
+ map->find(extension->id());
if (it != map->end())
return it->second.get();
if (!action_info)
@@ -101,37 +150,53 @@ ExtensionAction* GetOrCreateOrNull(
// Only create action info for enabled extensions.
// This avoids bugs where actions are recreated just after being removed
// in response to OnExtensionUnloaded().
- ExtensionService* service =
- ExtensionSystem::Get(profile)->extension_service();
- if (!service->GetExtensionById(extension_id, false))
+ if (!extension)
return NULL;
linked_ptr<ExtensionAction> action(new ExtensionAction(
- extension_id, action_type, *action_info));
- (*map)[extension_id] = action;
+ extension->id(), action_type, *action_info));
+ (*map)[extension->id()] = action;
+ PopulateMissingValues(*extension, action.get());
return action.get();
}
} // namespace
ExtensionAction* ExtensionActionManager::GetPageAction(
- const extensions::Extension& extension) const {
- return GetOrCreateOrNull(&page_actions_, extension.id(),
+ const Extension& extension) const {
+ return GetOrCreateOrNull(&page_actions_, &extension,
ActionInfo::TYPE_PAGE,
ActionInfo::GetPageActionInfo(&extension),
profile_);
}
ExtensionAction* ExtensionActionManager::GetBrowserAction(
- const extensions::Extension& extension) const {
- return GetOrCreateOrNull(&browser_actions_, extension.id(),
+ const Extension& extension) const {
+ return GetOrCreateOrNull(&browser_actions_, &extension,
ActionInfo::TYPE_BROWSER,
ActionInfo::GetBrowserActionInfo(&extension),
profile_);
}
+scoped_ptr<ExtensionAction> ExtensionActionManager::GetBestFitAction(
+ const Extension& extension,
+ ActionInfo::Type type) const {
+ const ActionInfo* info = ActionInfo::GetBrowserActionInfo(&extension);
+ if (!info)
+ info = ActionInfo::GetPageActionInfo(&extension);
+
+ // Create a new ExtensionAction of |type| with |extension|'s ActionInfo.
+ // If no ActionInfo exists for |extension|, create and return a new action
+ // with a blank ActionInfo.
+ // Populate any missing values from |extension|'s manifest.
+ scoped_ptr<ExtensionAction> new_action(new ExtensionAction(
+ extension.id(), type, info ? *info : ActionInfo()));
+ PopulateMissingValues(extension, new_action.get());
+ return new_action.Pass();
+}
+
ExtensionAction* ExtensionActionManager::GetSystemIndicator(
- const extensions::Extension& extension) const {
+ const Extension& extension) const {
// If it does not already exist, create the SystemIndicatorManager for the
// given profile. This could return NULL if the system indicator area is
// unavailable on the current system. If so, return NULL to signal that
@@ -139,7 +204,7 @@ ExtensionAction* ExtensionActionManager::GetSystemIndicator(
if (!extensions::SystemIndicatorManagerFactory::GetForProfile(profile_))
return NULL;
- return GetOrCreateOrNull(&system_indicators_, extension.id(),
+ return GetOrCreateOrNull(&system_indicators_, &extension,
ActionInfo::TYPE_SYSTEM_INDICATOR,
ActionInfo::GetSystemIndicatorInfo(&extension),
profile_);

Powered by Google App Engine
This is Rietveld 408576698