Home | History | Annotate | Download | only in extensions
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/common/extensions/manifest_handler_helpers.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/strings/string_number_conversions.h"
      9 #include "base/strings/utf_string_conversions.h"
     10 #include "base/values.h"
     11 #include "chrome/common/extensions/extension.h"
     12 #include "chrome/common/extensions/extension_icon_set.h"
     13 #include "chrome/common/extensions/extension_manifest_constants.h"
     14 #include "extensions/common/error_utils.h"
     15 
     16 namespace errors = extension_manifest_errors;
     17 
     18 namespace extensions {
     19 namespace manifest_handler_helpers {
     20 
     21 bool NormalizeAndValidatePath(std::string* path) {
     22   size_t first_non_slash = path->find_first_not_of('/');
     23   if (first_non_slash == std::string::npos) {
     24     *path = "";
     25     return false;
     26   }
     27 
     28   *path = path->substr(first_non_slash);
     29   return true;
     30 }
     31 
     32 bool LoadIconsFromDictionary(const base::DictionaryValue* icons_value,
     33                              const int* icon_sizes,
     34                              size_t num_icon_sizes,
     35                              ExtensionIconSet* icons,
     36                              string16* error) {
     37   DCHECK(icons);
     38   for (size_t i = 0; i < num_icon_sizes; ++i) {
     39     std::string key = base::IntToString(icon_sizes[i]);
     40     if (icons_value->HasKey(key)) {
     41       std::string icon_path;
     42       if (!icons_value->GetString(key, &icon_path)) {
     43         *error = ErrorUtils::FormatErrorMessageUTF16(
     44             errors::kInvalidIconPath, key);
     45         return false;
     46       }
     47 
     48       if (!NormalizeAndValidatePath(&icon_path)) {
     49         *error = ErrorUtils::FormatErrorMessageUTF16(
     50             errors::kInvalidIconPath, key);
     51         return false;
     52       }
     53 
     54       icons->Add(icon_sizes[i], icon_path);
     55     }
     56   }
     57   return true;
     58 }
     59 
     60 }  // namespace extensions
     61 }  // namespace manifest_handler_extensions
     62