- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / extension_file_util.h
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.
4
5 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_FILE_UTIL_H_
6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_FILE_UTIL_H_
7
8 #include <map>
9 #include <string>
10
11 #include "base/memory/ref_counted.h"
12 #include "chrome/common/extensions/message_bundle.h"
13 #include "extensions/common/manifest.h"
14
15 class ExtensionIconSet;
16 class GURL;
17
18 namespace base {
19 class DictionaryValue;
20 class FilePath;
21 }
22
23 namespace extensions {
24 class Extension;
25 class MessageBundle;
26 struct InstallWarning;
27 }
28
29 // Utilities for manipulating the on-disk storage of extensions.
30 namespace extension_file_util {
31
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
34 // on failure.
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);
39
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);
43
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,
49     int flags,
50     std::string* error);
51
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,
57     int flags,
58     std::string* error);
59
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,
63                                     std::string* error);
64
65 // Returns true if the given file path exists and is not zero-length.
66 bool ValidateFilePath(const base::FilePath& path);
67
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,
72                               int error_message_id,
73                               std::string* error);
74
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.
78 //
79 // Otherwise, returns false, and a description of the error is
80 // returned in |error|.
81 bool ValidateExtension(const extensions::Extension* extension,
82                        std::string* error,
83                        std::vector<extensions::InstallWarning>* warnings);
84
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
87 // extension.
88 std::set<base::FilePath> GetBrowserImagePaths(
89     const extensions::Extension* extension);
90
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);
94
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
97 // file is in use).
98 //
99 // |extensions_dir| is the install directory to look in. |extension_paths| is a
100 // map from extension id to full installation path.
101 //
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);
107
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,
113     std::string* error);
114
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);
121
122 // We need to reserve the namespace of entries that start with "_" for future
123 // use by Chrome.
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,
127                               std::string* error);
128
129 // Get a relative file path from a chrome-extension:// URL.
130 base::FilePath ExtensionURLToRelativeFilePath(const GURL& url);
131
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);
136
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);
143
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);
148
149 }  // namespace extension_file_util
150
151 #endif  // CHROME_COMMON_EXTENSIONS_EXTENSION_FILE_UTIL_H_