extension_resource.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #ifndef EXTENSIONS_COMMON_EXTENSION_RESOURCE_H_
  5. #define EXTENSIONS_COMMON_EXTENSION_RESOURCE_H_
  6. #include "base/files/file_path.h"
  7. #include "extensions/common/extension_id.h"
  8. namespace extensions {
  9. // Represents a resource inside an extension. Hence a resource pointing to the
  10. // root extension directory isn't a valid ExtensionResource.
  11. // Examples include an image, or a JavaScript file. This is more complicated
  12. // than just a simple FilePath because extension resources can come from
  13. // multiple physical file locations depending on locale.
  14. class ExtensionResource {
  15. public:
  16. // SymlinkPolicy decides whether we'll allow resources to be a symlink to
  17. // anywhere, or whether they must end up within the extension root.
  18. enum SymlinkPolicy {
  19. SYMLINKS_MUST_RESOLVE_WITHIN_ROOT,
  20. FOLLOW_SYMLINKS_ANYWHERE,
  21. };
  22. ExtensionResource();
  23. ExtensionResource(const ExtensionId& extension_id,
  24. const base::FilePath& extension_root,
  25. const base::FilePath& relative_path);
  26. ExtensionResource(const ExtensionResource& other);
  27. ExtensionResource(ExtensionResource&& other);
  28. ExtensionResource& operator=(ExtensionResource&& other);
  29. ~ExtensionResource();
  30. // set_follow_symlinks_anywhere allows the resource to be a symlink to
  31. // anywhere in the filesystem. By default, resources have to be within
  32. // |extension_root| after resolving symlinks.
  33. void set_follow_symlinks_anywhere();
  34. // Returns actual path to the resource (default or locale specific). In the
  35. // browser process, this will DCHECK if not called on the file thread. To
  36. // easily load extension images on the UI thread, see ImageLoader.
  37. const base::FilePath& GetFilePath() const;
  38. // Gets the physical file path for the extension resource, taking into account
  39. // localization. In the browser process, this will DCHECK if not called on the
  40. // file thread. To easily load extension images on the UI thread, see
  41. // ImageLoader.
  42. //
  43. // The relative path must not resolve to a location outside of
  44. // |extension_root|. Iff |file_can_symlink_outside_root| is true, then the
  45. // file can be a symlink that links outside of |extension_root|.
  46. static base::FilePath GetFilePath(const base::FilePath& extension_root,
  47. const base::FilePath& relative_path,
  48. SymlinkPolicy symlink_policy);
  49. // Getters
  50. const ExtensionId& extension_id() const { return extension_id_; }
  51. // Note that this might be empty for a valid ExtensionResource since dummy
  52. // Extension objects may be created with an empty extension root path in code.
  53. const base::FilePath& extension_root() const { return extension_root_; }
  54. const base::FilePath& relative_path() const { return relative_path_; }
  55. bool empty() const { return relative_path_.empty(); }
  56. private:
  57. // The id of the extension that this resource is associated with.
  58. ExtensionId extension_id_;
  59. // Extension root.
  60. base::FilePath extension_root_;
  61. // Relative path to resource.
  62. base::FilePath relative_path_;
  63. // If |follow_symlinks_anywhere_| is true then the resource itself must be
  64. // within |extension_root|, but it can be a symlink to a file that is not.
  65. bool follow_symlinks_anywhere_;
  66. // Full path to extension resource. Starts empty.
  67. mutable base::FilePath full_resource_path_;
  68. };
  69. } // namespace extensions
  70. #endif // EXTENSIONS_COMMON_EXTENSION_RESOURCE_H_