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.
5 #ifndef EXTENSIONS_COMMON_FILE_UTIL_H_
6 #define EXTENSIONS_COMMON_FILE_UTIL_H_
12 #include "base/files/file_path.h"
13 #include "base/memory/ref_counted.h"
14 #include "extensions/common/manifest.h"
15 #include "extensions/common/message_bundle.h"
17 class ExtensionIconSet
;
24 namespace extensions
{
27 struct InstallWarning
;
29 // Utilities for manipulating the on-disk storage of extensions.
32 extern const base::FilePath::CharType kTempDirectoryName
[];
34 // Copies |unpacked_source_dir| into the right location under |extensions_dir|.
35 // The destination directory is returned on success, or empty path is returned
37 base::FilePath
InstallExtension(const base::FilePath
& unpacked_source_dir
,
38 const std::string
& id
,
39 const std::string
& version
,
40 const base::FilePath
& extensions_dir
);
42 // Removes all versions of the extension with |id| from |extensions_dir|.
43 void UninstallExtension(const base::FilePath
& extensions_dir
,
44 const std::string
& id
);
46 // Loads and validates an extension from the specified directory. Returns NULL
47 // on failure, with a description of the error in |error|.
48 scoped_refptr
<Extension
> LoadExtension(const base::FilePath
& extension_root
,
49 Manifest::Location location
,
53 // The same as LoadExtension except use the provided |extension_id|.
54 scoped_refptr
<Extension
> LoadExtension(const base::FilePath
& extension_root
,
55 const std::string
& extension_id
,
56 Manifest::Location location
,
60 // Loads an extension manifest from the specified directory. Returns NULL
61 // on failure, with a description of the error in |error|.
62 scoped_ptr
<base::DictionaryValue
> LoadManifest(
63 const base::FilePath
& extension_root
,
66 // Convenience overload for specifying a manifest filename.
67 scoped_ptr
<base::DictionaryValue
> LoadManifest(
68 const base::FilePath
& extension_root
,
69 const base::FilePath::CharType
* manifest_filename
,
72 // Returns true if the given extension object is valid and consistent.
73 // May also append a series of warning messages to |warnings|, but they
74 // should not prevent the extension from running.
76 // Otherwise, returns false, and a description of the error is
77 // returned in |error|.
78 bool ValidateExtension(const Extension
* extension
,
80 std::vector
<InstallWarning
>* warnings
);
82 // Returns a list of files that contain private keys inside |extension_dir|.
83 std::vector
<base::FilePath
> FindPrivateKeyFiles(
84 const base::FilePath
& extension_dir
);
86 // We need to reserve the namespace of entries that start with "_" for future
88 // If any files or directories are found using "_" prefix and are not on
89 // reserved list we return false, and set error message.
90 bool CheckForIllegalFilenames(const base::FilePath
& extension_path
,
93 // We need to reserve the names of special Windows filenames, such as
95 // If any files or directories are found to be using a reserved Windows
96 // filename, we return false, and set error message.
97 bool CheckForWindowsReservedFilenames(const base::FilePath
& extension_dir
,
100 // Returns a path to a temporary directory for unpacking an extension that will
101 // be installed into |extensions_dir|. Creates the directory if necessary.
102 // The directory will be on the same file system as |extensions_dir| so
103 // that the extension directory can be efficiently renamed into place. Returns
104 // an empty file path on failure.
105 base::FilePath
GetInstallTempDir(const base::FilePath
& extensions_dir
);
107 // Helper function to delete files. This is used to avoid ugly casts which
108 // would be necessary with PostMessage since base::Delete is overloaded.
109 // TODO(skerner): Make a version of Delete that is not overloaded in file_util.
110 void DeleteFile(const base::FilePath
& path
, bool recursive
);
112 // Get a relative file path from a chrome-extension:// URL.
113 base::FilePath
ExtensionURLToRelativeFilePath(const GURL
& url
);
115 // Get a full file path from a chrome-extension-resource:// URL, If the URL
116 // points a file outside of root, this function will return empty FilePath.
117 base::FilePath
ExtensionResourceURLToFilePath(const GURL
& url
,
118 const base::FilePath
& root
);
120 // Returns true if the icons in the icon set exist. Oherwise, populates
121 // |error| with the |error_message_id| for an invalid file.
122 bool ValidateExtensionIconSet(const ExtensionIconSet
& icon_set
,
123 const Extension
* extension
,
124 int error_message_id
,
127 // Loads extension message catalogs and returns message bundle.
128 // Returns NULL on error or if the extension is not localized.
129 MessageBundle
* LoadMessageBundle(const base::FilePath
& extension_path
,
130 const std::string
& default_locale
,
133 // Loads the extension message bundle substitution map. Contains at least
134 // the extension_id item.
135 MessageBundle::SubstitutionMap
* LoadMessageBundleSubstitutionMap(
136 const base::FilePath
& extension_path
,
137 const std::string
& extension_id
,
138 const std::string
& default_locale
);
140 // Loads the extension message bundle substitution map, including messages from
141 // Shared Modules that the given extension imports. Contains at least the
142 // extension_id item.
143 MessageBundle::SubstitutionMap
* LoadMessageBundleSubstitutionMapWithImports(
144 const std::string
& extension_id
,
145 const ExtensionSet
& extension_set
);
147 // Helper functions for getting paths for files used in content verification.
148 base::FilePath
GetVerifiedContentsPath(const base::FilePath
& extension_path
);
149 base::FilePath
GetComputedHashesPath(const base::FilePath
& extension_path
);
151 } // namespace file_util
152 } // namespace extensions
154 #endif // EXTENSIONS_COMMON_FILE_UTIL_H_