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
15class ExtensionIconSet;
16
17namespace base {
18class DictionaryValue;
19class FilePath;
20}
21
22namespace extensions {
23class Extension;
24class MessageBundle;
25struct InstallWarning;
26}
27
28// Utilities for manipulating the on-disk storage of extensions.
29namespace extension_file_util {
30
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
33// on failure.
34base::FilePath InstallExtension(const base::FilePath& unpacked_source_dir,
35                                const std::string& id,
36                                const std::string& version,
37                                const base::FilePath& extensions_dir);
38
39// Removes all versions of the extension with |id| from |extensions_dir|.
40void UninstallExtension(const base::FilePath& extensions_dir,
41                        const std::string& id);
42
43// Loads and validates an extension from the specified directory. Returns NULL
44// on failure, with a description of the error in |error|.
45scoped_refptr<extensions::Extension> LoadExtension(
46    const base::FilePath& extension_root,
47    extensions::Manifest::Location location,
48    int flags,
49    std::string* error);
50
51// The same as LoadExtension except use the provided |extension_id|.
52scoped_refptr<extensions::Extension> LoadExtension(
53    const base::FilePath& extension_root,
54    const std::string& extension_id,
55    extensions::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|.
61base::DictionaryValue* LoadManifest(const base::FilePath& extension_root,
62                                    std::string* error);
63
64// Returns true if the given file path exists and is not zero-length.
65bool ValidateFilePath(const base::FilePath& path);
66
67// Returns true if the icons in the icon set exist. Oherwise, populates
68// |error| with the |error_message_id| for an invalid file.
69bool ValidateExtensionIconSet(const ExtensionIconSet& icon_set,
70                              const extensions::Extension* extension,
71                              int error_message_id,
72                              std::string* error);
73
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.
77//
78// Otherwise, returns false, and a description of the error is
79// returned in |error|.
80bool ValidateExtension(const extensions::Extension* extension,
81                       std::string* error,
82                       std::vector<extensions::InstallWarning>* warnings);
83
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
86// extension.
87std::set<base::FilePath> GetBrowserImagePaths(
88    const extensions::Extension* extension);
89
90// Returns a list of files that contain private keys inside |extension_dir|.
91std::vector<base::FilePath> FindPrivateKeyFiles(
92    const base::FilePath& extension_dir);
93
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
96// file is in use).
97//
98// |extensions_dir| is the install directory to look in. |extension_paths| is a
99// map from extension id to full installation path.
100//
101// Obsolete version directories are removed, as are directories that aren't
102// found in |extension_paths|.
103void GarbageCollectExtensions(
104    const base::FilePath& extensions_dir,
105    const std::multimap<std::string, base::FilePath>& extension_paths);
106
107// Loads extension message catalogs and returns message bundle.
108// Returns NULL on error, or if extension is not localized.
109extensions::MessageBundle* LoadMessageBundle(
110    const base::FilePath& extension_path,
111    const std::string& default_locale,
112    std::string* error);
113
114// Loads the extension message bundle substitution map. Contains at least
115// extension_id item.
116extensions::MessageBundle::SubstitutionMap* LoadMessageBundleSubstitutionMap(
117    const base::FilePath& extension_path,
118    const std::string& extension_id,
119    const std::string& default_locale);
120
121// We need to reserve the namespace of entries that start with "_" for future
122// use by Chrome.
123// If any files or directories are found using "_" prefix and are not on
124// reserved list we return false, and set error message.
125bool CheckForIllegalFilenames(const base::FilePath& extension_path,
126                              std::string* error);
127
128// Returns a path to a temporary directory for unpacking an extension that will
129// be installed into |extensions_dir|. Creates the directory if necessary.
130// The directory will be on the same file system as |extensions_dir| so
131// that the extension directory can be efficiently renamed into place. Returns
132// an empty file path on failure.
133base::FilePath GetInstallTempDir(const base::FilePath& extensions_dir);
134
135// Helper function to delete files. This is used to avoid ugly casts which
136// would be necessary with PostMessage since base::Delete is overloaded.
137// TODO(skerner): Make a version of Delete that is not overloaded in file_util.
138void DeleteFile(const base::FilePath& path, bool recursive);
139
140}  // namespace extension_file_util
141
142#endif  // CHROME_COMMON_EXTENSIONS_EXTENSION_FILE_UTIL_H_
143