[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_EXTENSION_H_ |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
[email protected] | 7197f499 | 2009-03-23 05:05:49 | [diff] [blame^] | 10 | #include <map> |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 11 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 12 | #include "base/file_path.h" |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 13 | #include "base/scoped_ptr.h" |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 14 | #include "base/string16.h" |
| 15 | #include "base/values.h" |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 16 | #include "base/version.h" |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 17 | #include "chrome/browser/extensions/user_script_master.h" |
[email protected] | 7197f499 | 2009-03-23 05:05:49 | [diff] [blame^] | 18 | #include "chrome/common/extensions/url_pattern.h" |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 19 | #include "googleurl/src/gurl.h" |
| 20 | |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 21 | // Represents a Chromium extension. |
| 22 | class Extension { |
| 23 | public: |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 24 | Extension() {} |
| 25 | explicit Extension(const FilePath& path); |
[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 26 | explicit Extension(const Extension& path); |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 27 | |
| 28 | // The format for extension manifests that this code understands. |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 29 | static const unsigned int kExpectedFormatVersion = 1; |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 30 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 31 | // The name of the manifest inside an extension. |
[email protected] | 0e29223 | 2009-01-22 15:23:34 | [diff] [blame] | 32 | static const char kManifestFilename[]; |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 33 | |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 34 | // Keys used in JSON representation of extensions. |
[email protected] | 34aa8dc | 2009-02-19 07:03:05 | [diff] [blame] | 35 | static const wchar_t* kContentScriptsKey; |
[email protected] | 7197f499 | 2009-03-23 05:05:49 | [diff] [blame^] | 36 | static const wchar_t* kCssKey; |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 37 | static const wchar_t* kDescriptionKey; |
[email protected] | 1712232 | 2009-01-22 20:03:30 | [diff] [blame] | 38 | static const wchar_t* kFormatVersionKey; |
| 39 | static const wchar_t* kIdKey; |
[email protected] | 34aa8dc | 2009-02-19 07:03:05 | [diff] [blame] | 40 | static const wchar_t* kJsKey; |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 41 | static const wchar_t* kMatchesKey; |
[email protected] | 1712232 | 2009-01-22 20:03:30 | [diff] [blame] | 42 | static const wchar_t* kNameKey; |
[email protected] | 7197f499 | 2009-03-23 05:05:49 | [diff] [blame^] | 43 | static const wchar_t* kPermissionsKey; |
[email protected] | 367230c5 | 2009-02-21 01:44:30 | [diff] [blame] | 44 | static const wchar_t* kPluginsDirKey; |
[email protected] | 7197f499 | 2009-03-23 05:05:49 | [diff] [blame^] | 45 | static const wchar_t* kRunAtKey; |
[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 46 | static const wchar_t* kThemeKey; |
[email protected] | d5cef1b | 2009-03-21 05:36:49 | [diff] [blame] | 47 | static const wchar_t* kToolstripsKey; |
[email protected] | 7197f499 | 2009-03-23 05:05:49 | [diff] [blame^] | 48 | static const wchar_t* kVersionKey; |
| 49 | static const wchar_t* kZipHashKey; |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 50 | |
[email protected] | 0afe827 | 2009-02-14 04:15:16 | [diff] [blame] | 51 | // Some values expected in manifests. |
| 52 | static const char* kRunAtDocumentStartValue; |
| 53 | static const char* kRunAtDocumentEndValue; |
| 54 | |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 55 | // Error messages returned from InitFromValue(). |
[email protected] | 34aa8dc | 2009-02-19 07:03:05 | [diff] [blame] | 56 | static const char* kInvalidContentScriptError; |
| 57 | static const char* kInvalidContentScriptsListError; |
[email protected] | 3cfbd0e | 2009-03-18 21:26:24 | [diff] [blame] | 58 | static const char* kInvalidCssError; |
| 59 | static const char* kInvalidCssListError; |
[email protected] | 1712232 | 2009-01-22 20:03:30 | [diff] [blame] | 60 | static const char* kInvalidDescriptionError; |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 61 | static const char* kInvalidFormatVersionError; |
| 62 | static const char* kInvalidIdError; |
[email protected] | 34aa8dc | 2009-02-19 07:03:05 | [diff] [blame] | 63 | static const char* kInvalidJsError; |
| 64 | static const char* kInvalidJsListError; |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 65 | static const char* kInvalidManifestError; |
| 66 | static const char* kInvalidMatchCountError; |
| 67 | static const char* kInvalidMatchError; |
| 68 | static const char* kInvalidMatchesError; |
| 69 | static const char* kInvalidNameError; |
[email protected] | 3cfbd0e | 2009-03-18 21:26:24 | [diff] [blame] | 70 | static const char* kInvalidPluginsDirError; |
[email protected] | 0afe827 | 2009-02-14 04:15:16 | [diff] [blame] | 71 | static const char* kInvalidRunAtError; |
[email protected] | 3cfbd0e | 2009-03-18 21:26:24 | [diff] [blame] | 72 | static const char* kInvalidToolstripError; |
[email protected] | d5cef1b | 2009-03-21 05:36:49 | [diff] [blame] | 73 | static const char* kInvalidToolstripsError; |
[email protected] | 1712232 | 2009-01-22 20:03:30 | [diff] [blame] | 74 | static const char* kInvalidVersionError; |
[email protected] | 7197f499 | 2009-03-23 05:05:49 | [diff] [blame^] | 75 | static const char* kInvalidPermissionsError; |
| 76 | static const char* kInvalidPermissionCountWarning; |
| 77 | static const char* kInvalidPermissionError; |
| 78 | static const char* kInvalidPermissionSchemeError; |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 79 | static const char* kInvalidZipHashError; |
[email protected] | 3cfbd0e | 2009-03-18 21:26:24 | [diff] [blame] | 80 | static const char* kMissingFileError; |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 81 | |
[email protected] | 37eeb5a | 2009-02-26 23:36:17 | [diff] [blame] | 82 | // The number of bytes in a legal id. |
[email protected] | fe0e782 | 2009-02-26 23:51:48 | [diff] [blame] | 83 | static const size_t kIdSize; |
[email protected] | 37eeb5a | 2009-02-26 23:36:17 | [diff] [blame] | 84 | |
[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 85 | // Returns an absolute url to a resource inside of an extension. The |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 86 | // |extension_url| argument should be the url() from an Extension object. The |
| 87 | // |relative_path| can be untrusted user input. The returned URL will either |
| 88 | // be invalid() or a child of |extension_url|. |
| 89 | // NOTE: Static so that it can be used from multiple threads. |
| 90 | static GURL GetResourceURL(const GURL& extension_url, |
| 91 | const std::string& relative_path); |
[email protected] | 3cfbd0e | 2009-03-18 21:26:24 | [diff] [blame] | 92 | GURL GetResourceURL(const std::string& relative_path) { |
| 93 | return GetResourceURL(url(), relative_path); |
| 94 | } |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 95 | |
[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 96 | // Returns an absolute path to a resource inside of an extension. The |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 97 | // |extension_path| argument should be the path() from an Extension object. |
| 98 | // The |relative_path| can be untrusted user input. The returned path will |
| 99 | // either be empty or a child of extension_path. |
| 100 | // NOTE: Static so that it can be used from multiple threads. |
| 101 | static FilePath GetResourcePath(const FilePath& extension_path, |
| 102 | const std::string& relative_path); |
[email protected] | 3cfbd0e | 2009-03-18 21:26:24 | [diff] [blame] | 103 | FilePath GetResourcePath(const std::string& relative_path) { |
| 104 | return GetResourcePath(path(), relative_path); |
| 105 | } |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 106 | |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 107 | // Initialize the extension from a parsed manifest. |
| 108 | bool InitFromValue(const DictionaryValue& value, std::string* error); |
| 109 | |
[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 110 | // Returns an absolute path to a resource inside of an extension if the |
| 111 | // extension has a theme defined with the given |resource_id|. Otherwise |
| 112 | // the path will be empty. Note that this method is not static as it is |
| 113 | // only intended to be called on an extension which has registered itself |
| 114 | // as providing a theme. |
| 115 | FilePath GetThemeResourcePath(const int resource_id); |
| 116 | |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 117 | const FilePath& path() const { return path_; } |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 118 | const GURL& url() const { return extension_url_; } |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 119 | const std::string& id() const { return id_; } |
| 120 | const Version* version() const { return version_.get(); } |
| 121 | // String representation of the version number. |
| 122 | const std::string VersionString() const; |
| 123 | const std::string& name() const { return name_; } |
| 124 | const std::string& description() const { return description_; } |
| 125 | const UserScriptList& content_scripts() const { return content_scripts_; } |
| 126 | const FilePath& plugins_dir() const { return plugins_dir_; } |
[email protected] | d5cef1b | 2009-03-21 05:36:49 | [diff] [blame] | 127 | const std::vector<std::string>& toolstrips() const { return toolstrips_; } |
[email protected] | 7197f499 | 2009-03-23 05:05:49 | [diff] [blame^] | 128 | const std::vector<URLPattern>& permissions() const { |
| 129 | return permissions_; } |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 130 | |
| 131 | private: |
[email protected] | 3cfbd0e | 2009-03-18 21:26:24 | [diff] [blame] | 132 | // Helper method that loads a UserScript object from a |
| 133 | // dictionary in the content_script list of the manifest. |
| 134 | bool LoadUserScriptHelper(const DictionaryValue* content_script, |
| 135 | int definition_index, |
| 136 | std::string* error, |
| 137 | UserScript* result); |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 138 | // The absolute path to the directory the extension is stored in. |
| 139 | FilePath path_; |
| 140 | |
| 141 | // The base extension url for the extension. |
| 142 | GURL extension_url_; |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 143 | |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 144 | // A human-readable ID for the extension. The convention is to use something |
| 145 | // like 'com.example.myextension', but this is not currently enforced. An |
| 146 | // extension's ID is used in things like directory structures and URLs, and |
| 147 | // is expected to not change across versions. In the case of conflicts, |
| 148 | // updates will only be allowed if the extension can be validated using the |
| 149 | // previous version's update key. |
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 150 | std::string id_; |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 151 | |
[email protected] | 64a02b80 | 2009-01-12 19:36:42 | [diff] [blame] | 152 | // The extension's version. |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 153 | scoped_ptr<Version> version_; |
[email protected] | 64a02b80 | 2009-01-12 19:36:42 | [diff] [blame] | 154 | |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 155 | // The extension's human-readable name. |
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 156 | std::string name_; |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 157 | |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 158 | // An optional longer description of the extension. |
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 159 | std::string description_; |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 160 | |
| 161 | // Paths to the content scripts the extension contains. |
[email protected] | 34aa8dc | 2009-02-19 07:03:05 | [diff] [blame] | 162 | UserScriptList content_scripts_; |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 163 | |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 164 | // Optional absolute path to the directory of NPAPI plugins that the extension |
| 165 | // contains. |
[email protected] | 367230c5 | 2009-02-21 01:44:30 | [diff] [blame] | 166 | FilePath plugins_dir_; |
| 167 | |
[email protected] | d5cef1b | 2009-03-21 05:36:49 | [diff] [blame] | 168 | // Paths to HTML files to be displayed in the toolbar. |
| 169 | std::vector<std::string> toolstrips_; |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 170 | |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 171 | // A SHA1 hash of the contents of the zip file. Note that this key is only |
| 172 | // present in the manifest that's prepended to the zip. The inner manifest |
| 173 | // will not have this key. |
| 174 | std::string zip_hash_; |
| 175 | |
[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 176 | // A map of resource id's to relative file paths. |
| 177 | std::map<const std::wstring, std::string> theme_paths_; |
| 178 | |
[email protected] | 7197f499 | 2009-03-23 05:05:49 | [diff] [blame^] | 179 | std::vector<URLPattern> permissions_; |
| 180 | |
[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 181 | // We implement copy, but not assign. |
| 182 | void operator=(const Extension&); |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 183 | }; |
| 184 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 185 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_H_ |