1// Copyright (c) 2011 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#pragma once
8
9#include <string>
10#include <map>
11
12#include "chrome/common/extensions/extension.h"
13
14class Extension;
15class ExtensionMessageBundle;
16class FilePath;
17class GURL;
18
19// Utilties for manipulating the on-disk storage of extensions.
20namespace extension_file_util {
21
22// The name of the directory inside the profile that we store installed
23// extension in.
24extern const char kInstallDirectoryName[];
25
26// Copies |unpacked_source_dir| into the right location under |extensions_dir|.
27// The destination directiory is returned on success, or empty path is returned
28// on failure.
29FilePath InstallExtension(const FilePath& unpacked_source_dir,
30                          const std::string& id,
31                          const std::string& version,
32                          const FilePath& extensions_dir);
33
34// Removes all versions of the extension with |id| from |extensions_dir|.
35void UninstallExtension(const FilePath& extensions_dir,
36                        const std::string& id);
37
38// Loads and validates an extension from the specified directory. Returns NULL
39// on failure, with a description of the error in |error|.
40scoped_refptr<Extension> LoadExtension(const FilePath& extension_root,
41                                       Extension::Location location,
42                                       int flags,
43                                       std::string* error);
44
45// Returns true if the given extension object is valid and consistent.
46// Otherwise, a description of the error is returned in |error|.
47bool ValidateExtension(Extension* extension, std::string* error);
48
49// Cleans up the extension install directory. It can end up with garbage in it
50// if extensions can't initially be removed when they are uninstalled (eg if a
51// file is in use).
52//
53// |extensions_dir| is the install directory to look in. |extension_paths| is a
54// map from extension id to full installation path.
55//
56// Obsolete version directories are removed, as are directories that aren't
57// found in |extension_paths|.
58void GarbageCollectExtensions(
59    const FilePath& extensions_dir,
60    const std::map<std::string, FilePath>& extension_paths);
61
62// Loads extension message catalogs and returns message bundle.
63// Returns NULL on error, or if extension is not localized.
64ExtensionMessageBundle* LoadExtensionMessageBundle(
65    const FilePath& extension_path,
66    const std::string& default_locale,
67    std::string* error);
68
69// We need to reserve the namespace of entries that start with "_" for future
70// use by Chrome.
71// If any files or directories are found using "_" prefix and are not on
72// reserved list we return false, and set error message.
73bool CheckForIllegalFilenames(const FilePath& extension_path,
74                              std::string* error);
75
76// Get a relative file path from a chrome-extension:// URL.
77FilePath ExtensionURLToRelativeFilePath(const GURL& url);
78
79// Get a path to a temp directory for unpacking an extension.
80// This is essentially PathService::Get(chrome::DIR_USER_DATA_TEMP, ...),
81// with a histogram that allows us to understand why it is failing.
82// Return an empty file path on failure.
83FilePath GetUserDataTempDir();
84
85// Helper function to delete files. This is used to avoid ugly casts which
86// would be necessary with PostMessage since file_util::Delete is overloaded.
87// TODO(skerner): Make a version of Delete that is not overloaded in file_util.
88void DeleteFile(const FilePath& path, bool recursive);
89
90}  // namespace extension_file_util
91
92#endif  // CHROME_COMMON_EXTENSIONS_EXTENSION_FILE_UTIL_H_
93