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.
4
5#ifndef EXTENSIONS_COMMON_FILE_UTIL_H_
6#define EXTENSIONS_COMMON_FILE_UTIL_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
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"
16#include "third_party/skia/include/core/SkColor.h"
17
18class ExtensionIconSet;
19class GURL;
20
21namespace extensions {
22class Extension;
23struct InstallWarning;
24
25// Utilities for manipulating the on-disk storage of extensions.
26namespace file_util {
27
28extern const base::FilePath::CharType kTempDirectoryName[];
29
30// Sets the flag to enable safe installation (i.e. flush all installed files).
31void SetUseSafeInstallation(bool use_safe_installation);
32
33// Copies |unpacked_source_dir| into the right location under |extensions_dir|.
34// The destination directory is returned on success, or empty path is returned
35// on failure.
36base::FilePath InstallExtension(const base::FilePath& unpacked_source_dir,
37 const std::string& id,
38 const std::string& version,
39 const base::FilePath& extensions_dir);
40
41// Removes all versions of the extension with |id| from |extensions_dir|.
42void UninstallExtension(const base::FilePath& extensions_dir,
43 const std::string& id);
44
45// Loads and validates an extension from the specified directory. Returns NULL
46// on failure, with a description of the error in |error|.
47scoped_refptr<Extension> LoadExtension(const base::FilePath& extension_root,
48 Manifest::Location location,
49 int flags,
50 std::string* error);
51
52// The same as LoadExtension except use the provided |extension_id|.
53scoped_refptr<Extension> LoadExtension(const base::FilePath& extension_root,
54 const std::string& extension_id,
55 Manifest::Location location,
56 int flags,
57 std::string* error);
58
59// Loads an extension manifest from the specified directory. Returns NULL
60// on failure, with a description of the error in |error|.
61std::unique_ptr<base::DictionaryValue> LoadManifest(
62 const base::FilePath& extension_root,
63 std::string* error);
64
65// Convenience overload for specifying a manifest filename.
66std::unique_ptr<base::DictionaryValue> LoadManifest(
67 const base::FilePath& extension_root,
68 const base::FilePath::CharType* manifest_filename,
69 std::string* error);
70
71// Returns true if the given extension object is valid and consistent.
72// May also append a series of warning messages to |warnings|, but they
73// should not prevent the extension from running.
74//
75// Otherwise, returns false, and a description of the error is
76// returned in |error|.
77bool ValidateExtension(const Extension* extension,
78 std::string* error,
79 std::vector<InstallWarning>* warnings);
80
81// Returns a list of files that contain private keys inside |extension_dir|.
82std::vector<base::FilePath> FindPrivateKeyFiles(
83 const base::FilePath& extension_dir);
84
85// We need to reserve the namespace of entries that start with "_" for future
86// use by Chrome.
87// If any files or directories are found using "_" prefix and are not on
88// reserved list we return false, and set error message.
89bool CheckForIllegalFilenames(const base::FilePath& extension_path,
90 std::string* error);
91
92// We need to reserve the names of special Windows filenames, such as
93// "com2.zip."
94// If any files or directories are found to be using a reserved Windows
95// filename, we return false, and set error message.
96bool CheckForWindowsReservedFilenames(const base::FilePath& extension_dir,
97 std::string* error);
98
99// Returns a path to a temporary directory for unpacking an extension that will
100// be installed into |extensions_dir|. Creates the directory if necessary.
101// The directory will be on the same file system as |extensions_dir| so
102// that the extension directory can be efficiently renamed into place. Returns
103// an empty file path on failure.
104base::FilePath GetInstallTempDir(const base::FilePath& extensions_dir);
105
106// Helper function to delete files. This is used to avoid ugly casts which
107// would be necessary with PostMessage since base::Delete is overloaded.
108// TODO(skerner): Make a version of Delete that is not overloaded in file_util.
109void DeleteFile(const base::FilePath& path, bool recursive);
110
111// Get a relative file path from a chrome-extension:// URL.
112base::FilePath ExtensionURLToRelativeFilePath(const GURL& url);
113
114// If |value| is true, when ValidateExtensionIconSet is called for unpacked
115// extensions, an icon which is not sufficiently visible will be reported as
116// an error.
117void SetReportErrorForInvisibleIconForTesting(bool value);
118
119// Returns true if the icons in |icon_set| exist. Otherwise, populates
120// |error| with the |error_message_id| for an invalid file. If an icon
121// is not sufficiently visible, and error checking is enabled, |error|
122// is populated with a different message, rather than one specified
123// by |error_message_id|.
124bool ValidateExtensionIconSet(const ExtensionIconSet& icon_set,
125 const Extension* extension,
126 int error_message_id,
127 SkColor background_color,
128 std::string* error);
129
130// Loads extension message catalogs and returns message bundle.
131// Returns NULL on error or if the extension is not localized.
132MessageBundle* LoadMessageBundle(const base::FilePath& extension_path,
133 const std::string& default_locale,
134 std::string* error);
135
136// Loads the extension message bundle substitution map. Contains at least
137// the extension_id item.
138MessageBundle::SubstitutionMap* LoadMessageBundleSubstitutionMap(
139 const base::FilePath& extension_path,
140 const std::string& extension_id,
141 const std::string& default_locale);
142
143// Loads the extension message bundle substitution map for a non-localized
144// extension. Contains only the extension_id item.
145// This doesn't require hitting disk, so it's safe to call on any thread.
146MessageBundle::SubstitutionMap* LoadNonLocalizedMessageBundleSubstitutionMap(
147 const std::string& extension_id);
148
149// Loads the extension message bundle substitution map from the specified paths.
150// Contains at least the extension_id item.
151MessageBundle::SubstitutionMap* LoadMessageBundleSubstitutionMapFromPaths(
152 const std::vector<base::FilePath>& paths,
153 const std::string& extension_id,
154 const std::string& default_locale);
155
156// Helper functions for getting paths for files used in content verification.
157base::FilePath GetVerifiedContentsPath(const base::FilePath& extension_path);
158base::FilePath GetComputedHashesPath(const base::FilePath& extension_path);
159
160// Helper function to get path used for the indexed ruleset by the Declarative
161// Net Request API.
162base::FilePath GetIndexedRulesetPath(const base::FilePath& extension_path);
163
164// Returns the list of file-paths reserved for use by the Extension system in
165// the kMetadataFolder.
166std::vector<base::FilePath> GetReservedMetadataFilePaths(
167 const base::FilePath& extension_path);
168
169} // namespace file_util
170} // namespace extensions
171
172#endif // EXTENSIONS_COMMON_FILE_UTIL_H_
173