Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / chrome / common / extensions / extension_file_util.h
blobfd1354d4c1829831a9457b2347db637a3147086f
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_
8 #include <string>
9 #include <map>
11 #include "chrome/common/extensions/extension.h"
12 #include "chrome/common/extensions/message_bundle.h"
14 class FilePath;
15 class GURL;
17 namespace base {
18 class DictionaryValue;
21 namespace extensions {
22 class MessageBundle;
25 // Utilities for manipulating the on-disk storage of extensions.
26 namespace extension_file_util {
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 FilePath InstallExtension(const FilePath& unpacked_source_dir,
32 const std::string& id,
33 const std::string& version,
34 const FilePath& extensions_dir);
36 // Removes all versions of the extension with |id| from |extensions_dir|.
37 void UninstallExtension(const FilePath& extensions_dir,
38 const std::string& id);
40 // Loads and validates an extension from the specified directory. Returns NULL
41 // on failure, with a description of the error in |error|.
42 scoped_refptr<extensions::Extension> LoadExtension(
43 const FilePath& extension_root,
44 extensions::Extension::Location location,
45 int flags,
46 std::string* error);
48 // The same as LoadExtension except use the provided |extension_id|.
49 scoped_refptr<extensions::Extension> LoadExtension(
50 const FilePath& extension_root,
51 const std::string& extension_id,
52 extensions::Extension::Location location,
53 int flags,
54 std::string* error);
56 // Loads an extension manifest from the specified directory. Returns NULL
57 // on failure, with a description of the error in |error|.
58 base::DictionaryValue* LoadManifest(const FilePath& extension_root,
59 std::string* error);
61 // Returns true if the given file path exists and is not zero-length.
62 bool ValidateFilePath(const FilePath& path);
64 // Returns true if the given extension object is valid and consistent.
65 // May also append a series of warning messages to |warnings|, but they
66 // should not prevent the extension from running.
68 // Otherwise, returns false, and a description of the error is
69 // returned in |error|.
70 bool ValidateExtension(const extensions::Extension* extension,
71 std::string* error,
72 extensions::Extension::InstallWarningVector* warnings);
74 // Returns a list of files that contain private keys inside |extension_dir|.
75 std::vector<FilePath> FindPrivateKeyFiles(const FilePath& extension_dir);
77 // Cleans up the extension install directory. It can end up with garbage in it
78 // if extensions can't initially be removed when they are uninstalled (eg if a
79 // file is in use).
81 // |extensions_dir| is the install directory to look in. |extension_paths| is a
82 // map from extension id to full installation path.
84 // Obsolete version directories are removed, as are directories that aren't
85 // found in |extension_paths|.
86 void GarbageCollectExtensions(
87 const FilePath& extensions_dir,
88 const std::multimap<std::string, FilePath>& extension_paths);
90 // Loads extension message catalogs and returns message bundle.
91 // Returns NULL on error, or if extension is not localized.
92 extensions::MessageBundle* LoadMessageBundle(
93 const FilePath& extension_path,
94 const std::string& default_locale,
95 std::string* error);
97 // Loads the extension message bundle substitution map. Contains at least
98 // extension_id item.
99 extensions::MessageBundle::SubstitutionMap* LoadMessageBundleSubstitutionMap(
100 const FilePath& extension_path,
101 const std::string& extension_id,
102 const std::string& default_locale);
104 // We need to reserve the namespace of entries that start with "_" for future
105 // use by Chrome.
106 // If any files or directories are found using "_" prefix and are not on
107 // reserved list we return false, and set error message.
108 bool CheckForIllegalFilenames(const FilePath& extension_path,
109 std::string* error);
111 // Get a relative file path from a chrome-extension:// URL.
112 FilePath ExtensionURLToRelativeFilePath(const GURL& url);
114 // Get a full file path from a chrome-extension-resource:// URL, If the URL
115 // points a file outside of root, this function will return empty FilePath.
116 FilePath ExtensionResourceURLToFilePath(const GURL& url, const FilePath& root);
118 // Returns a path to a temporary directory for unpacking an extension that will
119 // be installed into |extensions_dir|. Creates the directory if necessary.
120 // The directory will be on the same file system as |extensions_dir| so
121 // that the extension directory can be efficiently renamed into place. Returns
122 // an empty file path on failure.
123 FilePath GetInstallTempDir(const FilePath& extensions_dir);
125 // Helper function to delete files. This is used to avoid ugly casts which
126 // would be necessary with PostMessage since file_util::Delete is overloaded.
127 // TODO(skerner): Make a version of Delete that is not overloaded in file_util.
128 void DeleteFile(const FilePath& path, bool recursive);
130 } // namespace extension_file_util
132 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_FILE_UTIL_H_