manifest_handler_helpers.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #include "extensions/common/manifest_handler_helpers.h"
  5. #include <stddef.h>
  6. #include "base/check.h"
  7. #include "base/strings/string_number_conversions.h"
  8. #include "base/strings/string_split.h"
  9. #include "base/strings/utf_string_conversions.h"
  10. #include "base/values.h"
  11. #include "extensions/common/constants.h"
  12. #include "extensions/common/error_utils.h"
  13. #include "extensions/common/extension.h"
  14. #include "extensions/common/extension_icon_set.h"
  15. #include "extensions/common/manifest_constants.h"
  16. namespace extensions {
  17. namespace errors = manifest_errors;
  18. namespace manifest_handler_helpers {
  19. std::vector<base::StringPiece> TokenizeDictionaryPath(base::StringPiece path) {
  20. return base::SplitStringPiece(path, ".", base::TRIM_WHITESPACE,
  21. base::SPLIT_WANT_ALL);
  22. }
  23. bool NormalizeAndValidatePath(std::string* path) {
  24. return NormalizeAndValidatePath(*path, path);
  25. }
  26. bool NormalizeAndValidatePath(const std::string& path,
  27. std::string* normalized_path) {
  28. size_t first_non_slash = path.find_first_not_of('/');
  29. if (first_non_slash == std::string::npos) {
  30. *normalized_path = "";
  31. return false;
  32. }
  33. *normalized_path = path.substr(first_non_slash);
  34. return true;
  35. }
  36. bool LoadIconsFromDictionary(const base::Value* icons_value,
  37. ExtensionIconSet* icons,
  38. std::u16string* error) {
  39. DCHECK(icons);
  40. DCHECK(error);
  41. for (auto entry : icons_value->DictItems()) {
  42. int size = 0;
  43. if (!base::StringToInt(entry.first, &size) || size <= 0 ||
  44. size > extension_misc::EXTENSION_ICON_GIGANTOR * 4) {
  45. *error = ErrorUtils::FormatErrorMessageUTF16(errors::kInvalidIconKey,
  46. entry.first);
  47. return false;
  48. }
  49. std::string icon_path;
  50. if (!entry.second.is_string() ||
  51. !NormalizeAndValidatePath(entry.second.GetString(), &icon_path)) {
  52. *error = ErrorUtils::FormatErrorMessageUTF16(errors::kInvalidIconPath,
  53. entry.first);
  54. return false;
  55. }
  56. icons->Add(size, icon_path);
  57. }
  58. return true;
  59. }
  60. } // namespace manifest_handler_helpers
  61. } // namespace extensions