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.
5 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_FILE_UTIL_H_
6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_FILE_UTIL_H_
11 #include "base/memory/ref_counted.h"
12 #include "chrome/common/extensions/message_bundle.h"
13 #include "extensions/common/manifest.h"
15 class ExtensionIconSet;
19 class DictionaryValue;
23 namespace extensions {
26 struct InstallWarning;
29 // Utilities for manipulating the on-disk storage of extensions.
30 namespace extension_file_util {
32 // Copies |unpacked_source_dir| into the right location under |extensions_dir|.
33 // The destination directory is returned on success, or empty path is returned
35 base::FilePath InstallExtension(const base::FilePath& unpacked_source_dir,
36 const std::string& id,
37 const std::string& version,
38 const base::FilePath& extensions_dir);
40 // Removes all versions of the extension with |id| from |extensions_dir|.
41 void UninstallExtension(const base::FilePath& extensions_dir,
42 const std::string& id);
44 // Loads and validates an extension from the specified directory. Returns NULL
45 // on failure, with a description of the error in |error|.
46 scoped_refptr<extensions::Extension> LoadExtension(
47 const base::FilePath& extension_root,
48 extensions::Manifest::Location location,
52 // The same as LoadExtension except use the provided |extension_id|.
53 scoped_refptr<extensions::Extension> LoadExtension(
54 const base::FilePath& extension_root,
55 const std::string& extension_id,
56 extensions::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 base::DictionaryValue* LoadManifest(const base::FilePath& extension_root,
65 // Returns true if the given file path exists and is not zero-length.
66 bool ValidateFilePath(const base::FilePath& path);
68 // Returns true if the icons in the icon set exist. Oherwise, populates
69 // |error| with the |error_message_id| for an invalid file.
70 bool ValidateExtensionIconSet(const ExtensionIconSet& icon_set,
71 const extensions::Extension* extension,
75 // Returns true if the given extension object is valid and consistent.
76 // May also append a series of warning messages to |warnings|, but they
77 // should not prevent the extension from running.
79 // Otherwise, returns false, and a description of the error is
80 // returned in |error|.
81 bool ValidateExtension(const extensions::Extension* extension,
83 std::vector<extensions::InstallWarning>* warnings);
85 // Returns a list of paths (relative to the extension dir) for images that
86 // the browser might load (like themes and page action icons) for the given
88 std::set<base::FilePath> GetBrowserImagePaths(
89 const extensions::Extension* extension);
91 // Returns a list of files that contain private keys inside |extension_dir|.
92 std::vector<base::FilePath> FindPrivateKeyFiles(
93 const base::FilePath& extension_dir);
95 // Cleans up the extension install directory. It can end up with garbage in it
96 // if extensions can't initially be removed when they are uninstalled (eg if a
99 // |extensions_dir| is the install directory to look in. |extension_paths| is a
100 // map from extension id to full installation path.
102 // Obsolete version directories are removed, as are directories that aren't
103 // found in |extension_paths|.
104 void GarbageCollectExtensions(
105 const base::FilePath& extensions_dir,
106 const std::multimap<std::string, base::FilePath>& extension_paths);
108 // Loads extension message catalogs and returns message bundle.
109 // Returns NULL on error, or if extension is not localized.
110 extensions::MessageBundle* LoadMessageBundle(
111 const base::FilePath& extension_path,
112 const std::string& default_locale,
115 // Loads the extension message bundle substitution map. Contains at least
116 // extension_id item.
117 extensions::MessageBundle::SubstitutionMap* LoadMessageBundleSubstitutionMap(
118 const base::FilePath& extension_path,
119 const std::string& extension_id,
120 const std::string& default_locale);
122 // We need to reserve the namespace of entries that start with "_" for future
124 // If any files or directories are found using "_" prefix and are not on
125 // reserved list we return false, and set error message.
126 bool CheckForIllegalFilenames(const base::FilePath& extension_path,
129 // Get a relative file path from a chrome-extension:// URL.
130 base::FilePath ExtensionURLToRelativeFilePath(const GURL& url);
132 // Get a full file path from a chrome-extension-resource:// URL, If the URL
133 // points a file outside of root, this function will return empty FilePath.
134 base::FilePath ExtensionResourceURLToFilePath(const GURL& url,
135 const base::FilePath& root);
137 // Returns a path to a temporary directory for unpacking an extension that will
138 // be installed into |extensions_dir|. Creates the directory if necessary.
139 // The directory will be on the same file system as |extensions_dir| so
140 // that the extension directory can be efficiently renamed into place. Returns
141 // an empty file path on failure.
142 base::FilePath GetInstallTempDir(const base::FilePath& extensions_dir);
144 // Helper function to delete files. This is used to avoid ugly casts which
145 // would be necessary with PostMessage since base::Delete is overloaded.
146 // TODO(skerner): Make a version of Delete that is not overloaded in file_util.
147 void DeleteFile(const base::FilePath& path, bool recursive);
149 } // namespace extension_file_util
151 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_FILE_UTIL_H_