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
;
18 class DictionaryValue
;
22 namespace extensions
{
25 struct InstallWarning
;
28 // Utilities for manipulating the on-disk storage of extensions.
29 namespace extension_file_util
{
31 // Copies |unpacked_source_dir| into the right location under |extensions_dir|.
32 // The destination directory is returned on success, or empty path is returned
34 base::FilePath
InstallExtension(const base::FilePath
& unpacked_source_dir
,
35 const std::string
& id
,
36 const std::string
& version
,
37 const base::FilePath
& extensions_dir
);
39 // Removes all versions of the extension with |id| from |extensions_dir|.
40 void UninstallExtension(const base::FilePath
& extensions_dir
,
41 const std::string
& id
);
43 // Loads and validates an extension from the specified directory. Returns NULL
44 // on failure, with a description of the error in |error|.
45 scoped_refptr
<extensions::Extension
> LoadExtension(
46 const base::FilePath
& extension_root
,
47 extensions::Manifest::Location location
,
51 // The same as LoadExtension except use the provided |extension_id|.
52 scoped_refptr
<extensions::Extension
> LoadExtension(
53 const base::FilePath
& extension_root
,
54 const std::string
& extension_id
,
55 extensions::Manifest::Location location
,
59 // Loads an extension manifest from the specified directory. Returns NULL
60 // on failure, with a description of the error in |error|.
61 base::DictionaryValue
* LoadManifest(const base::FilePath
& extension_root
,
64 // Returns true if the given file path exists and is not zero-length.
65 bool ValidateFilePath(const base::FilePath
& path
);
67 // Returns true if the icons in the icon set exist. Oherwise, populates
68 // |error| with the |error_message_id| for an invalid file.
69 bool ValidateExtensionIconSet(const ExtensionIconSet
& icon_set
,
70 const extensions::Extension
* extension
,
74 // Returns true if the given extension object is valid and consistent.
75 // May also append a series of warning messages to |warnings|, but they
76 // should not prevent the extension from running.
78 // Otherwise, returns false, and a description of the error is
79 // returned in |error|.
80 bool ValidateExtension(const extensions::Extension
* extension
,
82 std::vector
<extensions::InstallWarning
>* warnings
);
84 // Returns a list of paths (relative to the extension dir) for images that
85 // the browser might load (like themes and page action icons) for the given
87 std::set
<base::FilePath
> GetBrowserImagePaths(
88 const extensions::Extension
* extension
);
90 // Returns a list of files that contain private keys inside |extension_dir|.
91 std::vector
<base::FilePath
> FindPrivateKeyFiles(
92 const base::FilePath
& extension_dir
);
94 // Cleans up the extension install directory. It can end up with garbage in it
95 // if extensions can't initially be removed when they are uninstalled (eg if a
98 // |extensions_dir| is the install directory to look in. |extension_paths| is a
99 // map from extension id to full installation path.
101 // Obsolete version directories are removed, as are directories that aren't
102 // found in |extension_paths|.
104 // The "Temp" directory that is used during extension installation only gets
105 // removed if |clean_temp_dir| is true.
106 void GarbageCollectExtensions(
107 const base::FilePath
& extensions_dir
,
108 const std::multimap
<std::string
, base::FilePath
>& extension_paths
,
109 bool clean_temp_dir
);
111 // Loads extension message catalogs and returns message bundle.
112 // Returns NULL on error, or if extension is not localized.
113 extensions::MessageBundle
* LoadMessageBundle(
114 const base::FilePath
& extension_path
,
115 const std::string
& default_locale
,
118 // Loads the extension message bundle substitution map. Contains at least
119 // extension_id item.
120 extensions::MessageBundle::SubstitutionMap
* LoadMessageBundleSubstitutionMap(
121 const base::FilePath
& extension_path
,
122 const std::string
& extension_id
,
123 const std::string
& default_locale
);
125 // We need to reserve the namespace of entries that start with "_" for future
127 // If any files or directories are found using "_" prefix and are not on
128 // reserved list we return false, and set error message.
129 bool CheckForIllegalFilenames(const base::FilePath
& extension_path
,
132 // Returns a path to a temporary directory for unpacking an extension that will
133 // be installed into |extensions_dir|. Creates the directory if necessary.
134 // The directory will be on the same file system as |extensions_dir| so
135 // that the extension directory can be efficiently renamed into place. Returns
136 // an empty file path on failure.
137 base::FilePath
GetInstallTempDir(const base::FilePath
& extensions_dir
);
139 // Helper function to delete files. This is used to avoid ugly casts which
140 // would be necessary with PostMessage since base::Delete is overloaded.
141 // TODO(skerner): Make a version of Delete that is not overloaded in file_util.
142 void DeleteFile(const base::FilePath
& path
, bool recursive
);
144 } // namespace extension_file_util
146 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_FILE_UTIL_H_