Home | History | Annotate | Download | only in manifest_handlers
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "extensions/common/manifest_handlers/icons_handler.h"
      6 
      7 #include "base/file_util.h"
      8 #include "base/lazy_instance.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/strings/string_number_conversions.h"
     11 #include "base/strings/utf_string_conversions.h"
     12 #include "base/values.h"
     13 #include "extensions/common/constants.h"
     14 #include "extensions/common/extension.h"
     15 #include "extensions/common/file_util.h"
     16 #include "extensions/common/manifest_constants.h"
     17 #include "extensions/common/manifest_handler_helpers.h"
     18 #include "grit/extensions_strings.h"
     19 #include "ui/gfx/size.h"
     20 
     21 namespace extensions {
     22 
     23 namespace keys = manifest_keys;
     24 
     25 static base::LazyInstance<ExtensionIconSet> g_empty_icon_set =
     26     LAZY_INSTANCE_INITIALIZER;
     27 
     28 // static
     29 const ExtensionIconSet& IconsInfo::GetIcons(const Extension* extension) {
     30   IconsInfo* info = static_cast<IconsInfo*>(
     31       extension->GetManifestData(keys::kIcons));
     32   return info ? info->icons : g_empty_icon_set.Get();
     33 }
     34 
     35 // static
     36 ExtensionResource IconsInfo::GetIconResource(
     37     const Extension* extension,
     38     int size,
     39     ExtensionIconSet::MatchType match_type) {
     40   std::string path = GetIcons(extension).Get(size, match_type);
     41   return path.empty() ? ExtensionResource() : extension->GetResource(path);
     42 }
     43 
     44 // static
     45 GURL IconsInfo::GetIconURL(const Extension* extension,
     46                            int size,
     47                            ExtensionIconSet::MatchType match_type) {
     48   std::string path = GetIcons(extension).Get(size, match_type);
     49   return path.empty() ? GURL() : extension->GetResourceURL(path);
     50 }
     51 
     52 IconsHandler::IconsHandler() {
     53 }
     54 
     55 IconsHandler::~IconsHandler() {
     56 }
     57 
     58 bool IconsHandler::Parse(Extension* extension, base::string16* error) {
     59   scoped_ptr<IconsInfo> icons_info(new IconsInfo);
     60   const base::DictionaryValue* icons_dict = NULL;
     61   if (!extension->manifest()->GetDictionary(keys::kIcons, &icons_dict)) {
     62     *error = base::ASCIIToUTF16(manifest_errors::kInvalidIcons);
     63     return false;
     64   }
     65 
     66   if (!manifest_handler_helpers::LoadIconsFromDictionary(
     67           icons_dict,
     68           extension_misc::kExtensionIconSizes,
     69           extension_misc::kNumExtensionIconSizes,
     70           &icons_info->icons,
     71           error)) {
     72     return false;
     73   }
     74 
     75   extension->SetManifestData(keys::kIcons, icons_info.release());
     76   return true;
     77 }
     78 
     79 bool IconsHandler::Validate(const Extension* extension,
     80                             std::string* error,
     81                             std::vector<InstallWarning>* warnings) const {
     82   return file_util::ValidateExtensionIconSet(IconsInfo::GetIcons(extension),
     83                                              extension,
     84                                              IDS_EXTENSION_LOAD_ICON_FAILED,
     85                                              error);
     86 }
     87 
     88 const std::vector<std::string> IconsHandler::Keys() const {
     89   return SingleKey(keys::kIcons);
     90 }
     91 
     92 }  // namespace extensions
     93