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 16class ExtensionIconSet; 17class GURL; 18 19namespace base { 20class FilePath; 21} 22 23namespace extensions { 24class Extension; 25struct InstallWarning; 26class MessageBundle; 27 28// Utilities for manipulating the on-disk storage of extensions. 29namespace file_util { 30 31extern const base::FilePath::CharType kTempDirectoryName[]; 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|. 61base::DictionaryValue* LoadManifest(const base::FilePath& extension_root, 62 std::string* error); 63 64// Convenience overload for specifying a manifest filename. 65base::DictionaryValue* LoadManifest( 66 const base::FilePath& extension_root, 67 const base::FilePath::CharType* manifest_filename, 68 std::string* error); 69 70// Returns true if the given extension object is valid and consistent. 71// May also append a series of warning messages to |warnings|, but they 72// should not prevent the extension from running. 73// 74// Otherwise, returns false, and a description of the error is 75// returned in |error|. 76bool ValidateExtension(const Extension* extension, 77 std::string* error, 78 std::vector<InstallWarning>* warnings); 79 80// Returns a list of files that contain private keys inside |extension_dir|. 81std::vector<base::FilePath> FindPrivateKeyFiles( 82 const base::FilePath& extension_dir); 83 84// We need to reserve the namespace of entries that start with "_" for future 85// use by Chrome. 86// If any files or directories are found using "_" prefix and are not on 87// reserved list we return false, and set error message. 88bool CheckForIllegalFilenames(const base::FilePath& extension_path, 89 std::string* error); 90 91// Returns a path to a temporary directory for unpacking an extension that will 92// be installed into |extensions_dir|. Creates the directory if necessary. 93// The directory will be on the same file system as |extensions_dir| so 94// that the extension directory can be efficiently renamed into place. Returns 95// an empty file path on failure. 96base::FilePath GetInstallTempDir(const base::FilePath& extensions_dir); 97 98// Helper function to delete files. This is used to avoid ugly casts which 99// would be necessary with PostMessage since base::Delete is overloaded. 100// TODO(skerner): Make a version of Delete that is not overloaded in file_util. 101void DeleteFile(const base::FilePath& path, bool recursive); 102 103// Get a relative file path from a chrome-extension:// URL. 104base::FilePath ExtensionURLToRelativeFilePath(const GURL& url); 105 106// Get a full file path from a chrome-extension-resource:// URL, If the URL 107// points a file outside of root, this function will return empty FilePath. 108base::FilePath ExtensionResourceURLToFilePath(const GURL& url, 109 const base::FilePath& root); 110 111// Returns true if the icons in the icon set exist. Oherwise, populates 112// |error| with the |error_message_id| for an invalid file. 113bool ValidateExtensionIconSet(const ExtensionIconSet& icon_set, 114 const Extension* extension, 115 int error_message_id, 116 std::string* error); 117 118// Loads extension message catalogs and returns message bundle. 119// Returns NULL on error or if the extension is not localized. 120MessageBundle* LoadMessageBundle(const base::FilePath& extension_path, 121 const std::string& default_locale, 122 std::string* error); 123 124// Loads the extension message bundle substitution map. Contains at least 125// the extension_id item. 126std::map<std::string, std::string>* LoadMessageBundleSubstitutionMap( 127 const base::FilePath& extension_path, 128 const std::string& extension_id, 129 const std::string& default_locale); 130 131// Helper functions for getting paths for files used in content verification. 132base::FilePath GetVerifiedContentsPath(const base::FilePath& extension_path); 133base::FilePath GetComputedHashesPath(const base::FilePath& extension_path); 134 135} // namespace file_util 136} // namespace extensions 137 138#endif // EXTENSIONS_COMMON_FILE_UTIL_H_ 139