file_util.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2013 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_FILE_UTIL_H_
  5. #define EXTENSIONS_COMMON_FILE_UTIL_H_
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #include "base/files/file_path.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "extensions/common/manifest.h"
  12. #include "extensions/common/message_bundle.h"
  13. #include "extensions/common/mojom/manifest.mojom-shared.h"
  14. #include "third_party/skia/include/core/SkColor.h"
  15. class ExtensionIconSet;
  16. class GURL;
  17. namespace extension_l10n_util {
  18. enum class GzippedMessagesPermission;
  19. }
  20. namespace extensions {
  21. class Extension;
  22. struct InstallWarning;
  23. // Utilities for manipulating the on-disk storage of extensions.
  24. namespace file_util {
  25. extern const base::FilePath::CharType kTempDirectoryName[];
  26. // Sets the flag to enable safe installation (i.e. flush all installed files).
  27. void SetUseSafeInstallation(bool use_safe_installation);
  28. // Copies |unpacked_source_dir| into the right location under |extensions_dir|.
  29. // The destination directory is returned on success, or empty path is returned
  30. // on failure.
  31. base::FilePath InstallExtension(const base::FilePath& unpacked_source_dir,
  32. const std::string& id,
  33. const std::string& version,
  34. const base::FilePath& extensions_dir);
  35. // Removes all versions of the extension with |id| from |extensions_dir|.
  36. void UninstallExtension(const base::FilePath& extensions_dir,
  37. const std::string& id);
  38. // Loads and validates an extension from the specified directory. Uses
  39. // the default manifest filename. Returns nullptr on failure, with a
  40. // description of the error in |error|.
  41. scoped_refptr<Extension> LoadExtension(const base::FilePath& extension_root,
  42. mojom::ManifestLocation location,
  43. int flags,
  44. std::string* error);
  45. // The same as LoadExtension except use the provided |extension_id|.
  46. scoped_refptr<Extension> LoadExtension(const base::FilePath& extension_root,
  47. const std::string& extension_id,
  48. mojom::ManifestLocation location,
  49. int flags,
  50. std::string* error);
  51. // The same as LoadExtension except use the provided |manifest_file| and
  52. // |extension_id|. If manifest_file is not specified, uses the default
  53. // manifest filename.
  54. scoped_refptr<Extension> LoadExtension(
  55. const base::FilePath& extension_root,
  56. const base::FilePath::CharType* manifest_file,
  57. const std::string& extension_id,
  58. mojom::ManifestLocation location,
  59. int flags,
  60. std::string* error);
  61. // Loads an extension manifest from the specified directory. Returns NULL
  62. // on failure, with a description of the error in |error|.
  63. std::unique_ptr<base::DictionaryValue> LoadManifest(
  64. const base::FilePath& extension_root,
  65. std::string* error);
  66. // Convenience overload for specifying a manifest filename.
  67. std::unique_ptr<base::DictionaryValue> LoadManifest(
  68. const base::FilePath& extension_root,
  69. const base::FilePath::CharType* manifest_filename,
  70. std::string* error);
  71. // Returns true if the given extension object is valid and consistent.
  72. // May also append a series of warning messages to |warnings|, but they
  73. // should not prevent the extension from running.
  74. //
  75. // Otherwise, returns false, and a description of the error is
  76. // returned in |error|.
  77. bool ValidateExtension(const Extension* extension,
  78. std::string* error,
  79. std::vector<InstallWarning>* warnings);
  80. // Returns a list of files that contain private keys inside |extension_dir|.
  81. std::vector<base::FilePath> FindPrivateKeyFiles(
  82. const base::FilePath& extension_dir);
  83. // We need to reserve the namespace of entries that start with "_" for future
  84. // use by Chrome.
  85. // If any files or directories are found using "_" prefix and are not on
  86. // reserved list we return false, and set error message.
  87. bool CheckForIllegalFilenames(const base::FilePath& extension_path,
  88. std::string* error);
  89. // We need to reserve the names of special Windows filenames, such as
  90. // "com2.zip."
  91. // If any files or directories are found to be using a reserved Windows
  92. // filename, we return false, and set error message.
  93. bool CheckForWindowsReservedFilenames(const base::FilePath& extension_dir,
  94. std::string* error);
  95. // Returns a path to a temporary directory for unpacking an extension that will
  96. // be installed into |extensions_dir|. Creates the directory if necessary.
  97. // The directory will be on the same file system as |extensions_dir| so
  98. // that the extension directory can be efficiently renamed into place. Returns
  99. // an empty file path on failure.
  100. base::FilePath GetInstallTempDir(const base::FilePath& extensions_dir);
  101. // Get a relative file path from a chrome-extension:// URL.
  102. base::FilePath ExtensionURLToRelativeFilePath(const GURL& url);
  103. // If |value| is true, when ValidateExtensionIconSet is called for unpacked
  104. // extensions, an icon which is not sufficiently visible will be reported as
  105. // an error.
  106. void SetReportErrorForInvisibleIconForTesting(bool value);
  107. // Returns true if the icons in |icon_set| exist, and, if enabled, checks that
  108. // they are sufficiently visible compared to |background_color|. On failure,
  109. // populates |error|, which will include the given |manifest_key|.
  110. bool ValidateExtensionIconSet(const ExtensionIconSet& icon_set,
  111. const Extension* extension,
  112. const char* manifest_key,
  113. SkColor background_color,
  114. std::string* error);
  115. // Loads extension message catalogs and returns message bundle. Passes
  116. // |gzip_permission| to extension_l10n_util::LoadMessageCatalogs (see
  117. // extension_l10n_util.h for details).
  118. // Returns null on error or if the extension is not localized.
  119. MessageBundle* LoadMessageBundle(
  120. const base::FilePath& extension_path,
  121. const std::string& default_locale,
  122. extension_l10n_util::GzippedMessagesPermission gzip_permission,
  123. std::string* error);
  124. // Helper functions for getting paths for files used in content verification.
  125. base::FilePath GetVerifiedContentsPath(const base::FilePath& extension_path);
  126. base::FilePath GetComputedHashesPath(const base::FilePath& extension_path);
  127. // Helper function to get the relative path for the directory containing static
  128. // indexed rulesets. Path is relative to the extension path. Used by the
  129. // Declarative Net Request API.
  130. base::FilePath GetIndexedRulesetDirectoryRelativePath();
  131. // Helper function to get the relative path for a given static indexed ruleset.
  132. // Path is relative to the extension path. This is used by the Declarative Net
  133. // Request API.
  134. base::FilePath GetIndexedRulesetRelativePath(int static_ruleset_id);
  135. // Returns the list of file-paths reserved for use by the Extension system in
  136. // the kMetadataFolder.
  137. std::vector<base::FilePath> GetReservedMetadataFilePaths(
  138. const base::FilePath& extension_path);
  139. } // namespace file_util
  140. } // namespace extensions
  141. #endif // EXTENSIONS_COMMON_FILE_UTIL_H_