[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 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] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 19 | #include "chrome/common/page_action.h" |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 20 | #include "googleurl/src/gurl.h" |
[email protected] | c43c668 | 2009-05-19 14:51:44 | [diff] [blame^] | 21 | #include "third_party/skia/include/core/SkColor.h" |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 22 | |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 23 | // Represents a Chromium extension. |
| 24 | class Extension { |
| 25 | public: |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 26 | // What an extension was loaded from. |
| 27 | enum Location { |
| 28 | INVALID, |
| 29 | INTERNAL, // A crx file from the internal Extensions directory |
| 30 | EXTERNAL, // A crx file from an external directory (via eg the registry |
| 31 | // on Windows) |
| 32 | LOAD // --load-extension |
| 33 | }; |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 34 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 35 | // The name of the manifest inside an extension. |
[email protected] | 0e29223 | 2009-01-22 15:23:34 | [diff] [blame] | 36 | static const char kManifestFilename[]; |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 37 | |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 38 | // Keys used in JSON representation of extensions. |
[email protected] | 34aa8dc | 2009-02-19 07:03:05 | [diff] [blame] | 39 | static const wchar_t* kContentScriptsKey; |
[email protected] | 7197f499 | 2009-03-23 05:05:49 | [diff] [blame] | 40 | static const wchar_t* kCssKey; |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 41 | static const wchar_t* kDescriptionKey; |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 42 | static const wchar_t* kIconPathKey; |
[email protected] | 1712232 | 2009-01-22 20:03:30 | [diff] [blame] | 43 | static const wchar_t* kIdKey; |
[email protected] | 34aa8dc | 2009-02-19 07:03:05 | [diff] [blame] | 44 | static const wchar_t* kJsKey; |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 45 | static const wchar_t* kMatchesKey; |
[email protected] | 1712232 | 2009-01-22 20:03:30 | [diff] [blame] | 46 | static const wchar_t* kNameKey; |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 47 | static const wchar_t* kPageActionsKey; |
[email protected] | 7197f499 | 2009-03-23 05:05:49 | [diff] [blame] | 48 | static const wchar_t* kPermissionsKey; |
[email protected] | 367230c5 | 2009-02-21 01:44:30 | [diff] [blame] | 49 | static const wchar_t* kPluginsDirKey; |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 50 | static const wchar_t* kBackgroundKey; |
[email protected] | 7197f499 | 2009-03-23 05:05:49 | [diff] [blame] | 51 | static const wchar_t* kRunAtKey; |
[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 52 | static const wchar_t* kThemeKey; |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 53 | static const wchar_t* kThemeImagesKey; |
| 54 | static const wchar_t* kThemeColorsKey; |
| 55 | static const wchar_t* kThemeTintsKey; |
[email protected] | d5cef1b | 2009-03-21 05:36:49 | [diff] [blame] | 56 | static const wchar_t* kToolstripsKey; |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 57 | static const wchar_t* kTooltipKey; |
| 58 | static const wchar_t* kTypeKey; |
[email protected] | 7197f499 | 2009-03-23 05:05:49 | [diff] [blame] | 59 | static const wchar_t* kVersionKey; |
| 60 | static const wchar_t* kZipHashKey; |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 61 | |
[email protected] | 0afe827 | 2009-02-14 04:15:16 | [diff] [blame] | 62 | // Some values expected in manifests. |
| 63 | static const char* kRunAtDocumentStartValue; |
| 64 | static const char* kRunAtDocumentEndValue; |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 65 | static const char* kPageActionTypeTab; |
| 66 | static const char* kPageActionTypePermanent; |
[email protected] | 0afe827 | 2009-02-14 04:15:16 | [diff] [blame] | 67 | |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 68 | // Error messages returned from InitFromValue(). |
[email protected] | 34aa8dc | 2009-02-19 07:03:05 | [diff] [blame] | 69 | static const char* kInvalidContentScriptError; |
| 70 | static const char* kInvalidContentScriptsListError; |
[email protected] | 3cfbd0e | 2009-03-18 21:26:24 | [diff] [blame] | 71 | static const char* kInvalidCssError; |
| 72 | static const char* kInvalidCssListError; |
[email protected] | 1712232 | 2009-01-22 20:03:30 | [diff] [blame] | 73 | static const char* kInvalidDescriptionError; |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 74 | static const char* kInvalidIdError; |
[email protected] | 34aa8dc | 2009-02-19 07:03:05 | [diff] [blame] | 75 | static const char* kInvalidJsError; |
| 76 | static const char* kInvalidJsListError; |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 77 | static const char* kInvalidManifestError; |
| 78 | static const char* kInvalidMatchCountError; |
| 79 | static const char* kInvalidMatchError; |
| 80 | static const char* kInvalidMatchesError; |
| 81 | static const char* kInvalidNameError; |
[email protected] | 3cfbd0e | 2009-03-18 21:26:24 | [diff] [blame] | 82 | static const char* kInvalidPluginsDirError; |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 83 | static const char* kInvalidBackgroundError; |
[email protected] | 0afe827 | 2009-02-14 04:15:16 | [diff] [blame] | 84 | static const char* kInvalidRunAtError; |
[email protected] | 3cfbd0e | 2009-03-18 21:26:24 | [diff] [blame] | 85 | static const char* kInvalidToolstripError; |
[email protected] | d5cef1b | 2009-03-21 05:36:49 | [diff] [blame] | 86 | static const char* kInvalidToolstripsError; |
[email protected] | 1712232 | 2009-01-22 20:03:30 | [diff] [blame] | 87 | static const char* kInvalidVersionError; |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 88 | static const char* kInvalidPageActionError; |
| 89 | static const char* kInvalidPageActionsListError; |
| 90 | static const char* kInvalidPageActionIconPathError; |
| 91 | static const char* kInvalidPageActionTooltipError; |
| 92 | static const char* kInvalidPageActionTypeValueError; |
[email protected] | 7197f499 | 2009-03-23 05:05:49 | [diff] [blame] | 93 | static const char* kInvalidPermissionsError; |
| 94 | static const char* kInvalidPermissionCountWarning; |
| 95 | static const char* kInvalidPermissionError; |
| 96 | static const char* kInvalidPermissionSchemeError; |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 97 | static const char* kInvalidZipHashError; |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 98 | static const char* kInvalidThemeError; |
| 99 | static const char* kInvalidThemeImagesMissingError; |
| 100 | static const char* kInvalidThemeImagesError; |
| 101 | static const char* kInvalidThemeColorsError; |
| 102 | static const char* kInvalidThemeTintsError; |
[email protected] | 3cfbd0e | 2009-03-18 21:26:24 | [diff] [blame] | 103 | static const char* kMissingFileError; |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 104 | static const char* kMissingPageActionIcon; |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 105 | |
[email protected] | 37eeb5a | 2009-02-26 23:36:17 | [diff] [blame] | 106 | // The number of bytes in a legal id. |
[email protected] | fe0e782 | 2009-02-26 23:51:48 | [diff] [blame] | 107 | static const size_t kIdSize; |
[email protected] | 37eeb5a | 2009-02-26 23:36:17 | [diff] [blame] | 108 | |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 109 | Extension() : location_(INVALID) {} |
| 110 | explicit Extension(const FilePath& path); |
| 111 | explicit Extension(const Extension& other); |
| 112 | virtual ~Extension(); |
| 113 | |
[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 114 | // Returns an absolute url to a resource inside of an extension. The |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 115 | // |extension_url| argument should be the url() from an Extension object. The |
| 116 | // |relative_path| can be untrusted user input. The returned URL will either |
| 117 | // be invalid() or a child of |extension_url|. |
| 118 | // NOTE: Static so that it can be used from multiple threads. |
| 119 | static GURL GetResourceURL(const GURL& extension_url, |
| 120 | const std::string& relative_path); |
[email protected] | 3cfbd0e | 2009-03-18 21:26:24 | [diff] [blame] | 121 | GURL GetResourceURL(const std::string& relative_path) { |
| 122 | return GetResourceURL(url(), relative_path); |
| 123 | } |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 124 | |
[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 125 | // Returns an absolute path to a resource inside of an extension. The |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 126 | // |extension_path| argument should be the path() from an Extension object. |
| 127 | // The |relative_path| can be untrusted user input. The returned path will |
| 128 | // either be empty or a child of extension_path. |
| 129 | // NOTE: Static so that it can be used from multiple threads. |
| 130 | static FilePath GetResourcePath(const FilePath& extension_path, |
| 131 | const std::string& relative_path); |
[email protected] | 3cfbd0e | 2009-03-18 21:26:24 | [diff] [blame] | 132 | FilePath GetResourcePath(const std::string& relative_path) { |
| 133 | return GetResourcePath(path(), relative_path); |
| 134 | } |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 135 | |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 136 | // Initialize the extension from a parsed manifest. |
[email protected] | 5bfb1eb0a | 2009-04-08 18:33:30 | [diff] [blame] | 137 | // If |require_id| is true, will return an error if the "id" key is missing |
| 138 | // from the value. |
| 139 | bool InitFromValue(const DictionaryValue& value, bool require_id, |
| 140 | std::string* error); |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 141 | |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 142 | const FilePath& path() const { return path_; } |
[email protected] | 5bfb1eb0a | 2009-04-08 18:33:30 | [diff] [blame] | 143 | const GURL& url() const { return extension_url_; } |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 144 | const Location location() const { return location_; } |
| 145 | void set_location(Location location) { location_ = location; } |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 146 | const std::string& id() const { return id_; } |
| 147 | const Version* version() const { return version_.get(); } |
| 148 | // String representation of the version number. |
| 149 | const std::string VersionString() const; |
| 150 | const std::string& name() const { return name_; } |
| 151 | const std::string& description() const { return description_; } |
| 152 | const UserScriptList& content_scripts() const { return content_scripts_; } |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 153 | const PageActionMap& page_actions() const { return page_actions_; } |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 154 | const FilePath& plugins_dir() const { return plugins_dir_; } |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 155 | const GURL& background_url() const { return background_url_; } |
[email protected] | d5cef1b | 2009-03-21 05:36:49 | [diff] [blame] | 156 | const std::vector<std::string>& toolstrips() const { return toolstrips_; } |
[email protected] | 7197f499 | 2009-03-23 05:05:49 | [diff] [blame] | 157 | const std::vector<URLPattern>& permissions() const { |
| 158 | return permissions_; } |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 159 | |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 160 | // Retrieves a page action by |id|. |
| 161 | const PageAction* GetPageAction(std::string id) const; |
| 162 | |
| 163 | // Theme-related |
| 164 | DictionaryValue* GetThemeImages() const { return theme_images_.get(); } |
| 165 | DictionaryValue* GetThemeColors() const { return theme_colors_.get(); } |
| 166 | DictionaryValue* GetThemeTints() const { return theme_tints_.get(); } |
| 167 | bool IsTheme() { return is_theme_; } |
| 168 | |
| 169 | // It doesn't really make sense to 'uninstall' extensions loaded from |
| 170 | // --load-extension or external locations. |
| 171 | const bool is_uninstallable() const { return location_ == INTERNAL; } |
| 172 | |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 173 | private: |
[email protected] | 3cfbd0e | 2009-03-18 21:26:24 | [diff] [blame] | 174 | // Helper method that loads a UserScript object from a |
| 175 | // dictionary in the content_script list of the manifest. |
| 176 | bool LoadUserScriptHelper(const DictionaryValue* content_script, |
| 177 | int definition_index, |
| 178 | std::string* error, |
| 179 | UserScript* result); |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 180 | |
| 181 | // Helper method that loads a PageAction object from a |
| 182 | // dictionary in the page_action list of the manifest. |
[email protected] | ce5c450 | 2009-05-06 16:46:11 | [diff] [blame] | 183 | PageAction* LoadPageActionHelper(const DictionaryValue* page_action, |
| 184 | int definition_index, |
| 185 | std::string* error); |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 186 | |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 187 | // The absolute path to the directory the extension is stored in. |
| 188 | FilePath path_; |
| 189 | |
| 190 | // The base extension url for the extension. |
| 191 | GURL extension_url_; |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame] | 192 | |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 193 | // The location the extension was loaded from. |
| 194 | Location location_; |
| 195 | |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 196 | // A human-readable ID for the extension. The convention is to use something |
| 197 | // like 'com.example.myextension', but this is not currently enforced. An |
| 198 | // extension's ID is used in things like directory structures and URLs, and |
| 199 | // is expected to not change across versions. In the case of conflicts, |
| 200 | // updates will only be allowed if the extension can be validated using the |
| 201 | // previous version's update key. |
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 202 | std::string id_; |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 203 | |
[email protected] | 64a02b80 | 2009-01-12 19:36:42 | [diff] [blame] | 204 | // The extension's version. |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 205 | scoped_ptr<Version> version_; |
[email protected] | 64a02b80 | 2009-01-12 19:36:42 | [diff] [blame] | 206 | |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 207 | // The extension's human-readable name. |
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 208 | std::string name_; |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 209 | |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 210 | // An optional longer description of the extension. |
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 211 | std::string description_; |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 212 | |
| 213 | // Paths to the content scripts the extension contains. |
[email protected] | 34aa8dc | 2009-02-19 07:03:05 | [diff] [blame] | 214 | UserScriptList content_scripts_; |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 215 | |
[email protected] | f7f3a5f | 2009-05-01 22:02:34 | [diff] [blame] | 216 | // A list of page actions. |
| 217 | PageActionMap page_actions_; |
| 218 | |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 219 | // Optional absolute path to the directory of NPAPI plugins that the extension |
| 220 | // contains. |
[email protected] | 367230c5 | 2009-02-21 01:44:30 | [diff] [blame] | 221 | FilePath plugins_dir_; |
| 222 | |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 223 | // Optional URL to a master page of which a single instance should be always |
| 224 | // loaded in the background. |
| 225 | GURL background_url_; |
| 226 | |
[email protected] | d5cef1b | 2009-03-21 05:36:49 | [diff] [blame] | 227 | // Paths to HTML files to be displayed in the toolbar. |
| 228 | std::vector<std::string> toolstrips_; |
[email protected] | 4a8d327 | 2009-03-10 19:15:08 | [diff] [blame] | 229 | |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 230 | // A SHA1 hash of the contents of the zip file. Note that this key is only |
| 231 | // present in the manifest that's prepended to the zip. The inner manifest |
| 232 | // will not have this key. |
| 233 | std::string zip_hash_; |
| 234 | |
[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 235 | // A map of resource id's to relative file paths. |
[email protected] | bbb436f | 2009-05-09 16:51:07 | [diff] [blame] | 236 | scoped_ptr<DictionaryValue> theme_images_; |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 237 | |
| 238 | // A map of color names to colors. |
[email protected] | bbb436f | 2009-05-09 16:51:07 | [diff] [blame] | 239 | scoped_ptr<DictionaryValue> theme_colors_; |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 240 | |
| 241 | // A map of color names to colors. |
[email protected] | bbb436f | 2009-05-09 16:51:07 | [diff] [blame] | 242 | scoped_ptr<DictionaryValue> theme_tints_; |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 243 | |
| 244 | // Whether the extension is a theme - if it is, certain things are disabled. |
| 245 | bool is_theme_; |
[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 246 | |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 247 | // The sites this extension has permission to talk to (using XHR, etc). |
[email protected] | 7197f499 | 2009-03-23 05:05:49 | [diff] [blame] | 248 | std::vector<URLPattern> permissions_; |
| 249 | |
[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 250 | // We implement copy, but not assign. |
| 251 | void operator=(const Extension&); |
[email protected] | 7713d63 | 2008-12-02 07:52:33 | [diff] [blame] | 252 | }; |
| 253 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 254 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_H_ |