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 <string> 9 #include <map> 10 11 #include "base/memory/ref_counted.h" 12 #include "chrome/common/extensions/manifest.h" 13 #include "chrome/common/extensions/message_bundle.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 92 // Returns a list of files that contain private keys inside |extension_dir|. 93 std::vector<base::FilePath> FindPrivateKeyFiles( 94 const base::FilePath& extension_dir); 95 96 // Cleans up the extension install directory. It can end up with garbage in it 97 // if extensions can't initially be removed when they are uninstalled (eg if a 98 // file is in use). 99 // 100 // |extensions_dir| is the install directory to look in. |extension_paths| is a 101 // map from extension id to full installation path. 102 // 103 // Obsolete version directories are removed, as are directories that aren't 104 // found in |extension_paths|. 105 void GarbageCollectExtensions( 106 const base::FilePath& extensions_dir, 107 const std::multimap<std::string, base::FilePath>& extension_paths); 108 109 // Loads extension message catalogs and returns message bundle. 110 // Returns NULL on error, or if extension is not localized. 111 extensions::MessageBundle* LoadMessageBundle( 112 const base::FilePath& extension_path, 113 const std::string& default_locale, 114 std::string* error); 115 116 // Loads the extension message bundle substitution map. Contains at least 117 // extension_id item. 118 extensions::MessageBundle::SubstitutionMap* LoadMessageBundleSubstitutionMap( 119 const base::FilePath& extension_path, 120 const std::string& extension_id, 121 const std::string& default_locale); 122 123 // We need to reserve the namespace of entries that start with "_" for future 124 // use by Chrome. 125 // If any files or directories are found using "_" prefix and are not on 126 // reserved list we return false, and set error message. 127 bool CheckForIllegalFilenames(const base::FilePath& extension_path, 128 std::string* error); 129 130 // Get a relative file path from a chrome-extension:// URL. 131 base::FilePath ExtensionURLToRelativeFilePath(const GURL& url); 132 133 // Get a full file path from a chrome-extension-resource:// URL, If the URL 134 // points a file outside of root, this function will return empty FilePath. 135 base::FilePath ExtensionResourceURLToFilePath(const GURL& url, 136 const base::FilePath& root); 137 138 // Returns a path to a temporary directory for unpacking an extension that will 139 // be installed into |extensions_dir|. Creates the directory if necessary. 140 // The directory will be on the same file system as |extensions_dir| so 141 // that the extension directory can be efficiently renamed into place. Returns 142 // an empty file path on failure. 143 base::FilePath GetInstallTempDir(const base::FilePath& extensions_dir); 144 145 // Helper function to delete files. This is used to avoid ugly casts which 146 // would be necessary with PostMessage since base::Delete is overloaded. 147 // TODO(skerner): Make a version of Delete that is not overloaded in file_util. 148 void DeleteFile(const base::FilePath& path, bool recursive); 149 150 } // namespace extension_file_util 151 152 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_FILE_UTIL_H_ 153