blob: 050515b9290821f20c670ec14c47c92ec939acd1 [file] [log] [blame]
[email protected]f7f3a5f2009-05-01 22:02:341// Copyright (c) 2009 The Chromium Authors. All rights reserved.
[email protected]7713d632008-12-02 07:52:332// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]5b1a0e22009-05-26 19:00:585#ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_H_
6#define CHROME_COMMON_EXTENSIONS_EXTENSION_H_
[email protected]7713d632008-12-02 07:52:337
[email protected]300cc58db2009-08-19 20:45:148#include <map>
[email protected]facd7a7652009-06-05 23:15:029#include <set>
[email protected]7713d632008-12-02 07:52:3310#include <string>
11#include <vector>
12
[email protected]6014d672008-12-05 00:38:2513#include "base/file_path.h"
[email protected]cc655912009-01-29 23:19:1914#include "base/scoped_ptr.h"
[email protected]7713d632008-12-02 07:52:3315#include "base/values.h"
[email protected]cc655912009-01-29 23:19:1916#include "base/version.h"
[email protected]e2eb43112009-05-29 21:19:5417#include "chrome/browser/extensions/user_script_master.h"
[email protected]ba69a7d2009-09-28 21:09:5618#include "chrome/common/extensions/extension_action.h"
[email protected]75e126b932009-09-28 19:38:4919#include "chrome/common/extensions/extension_message_bundle.h"
[email protected]ecabe6ee2009-10-07 22:49:1020#include "chrome/common/extensions/extension_resource.h"
[email protected]42b6f0f82009-09-18 21:07:3921#include "chrome/common/extensions/user_script.h"
[email protected]7197f4992009-03-23 05:05:4922#include "chrome/common/extensions/url_pattern.h"
[email protected]eab9b452009-01-23 20:48:5923#include "googleurl/src/gurl.h"
24
[email protected]c533bb22009-06-03 19:06:1125// Represents a Chrome extension.
[email protected]7713d632008-12-02 07:52:3326class Extension {
27 public:
[email protected]b24d8312009-08-27 06:47:4628 typedef std::vector<URLPattern> HostPermissions;
[email protected]d3cfa482009-10-17 13:54:5729 typedef std::map<const std::string, GURL> URLOverrideMap;
[email protected]b24d8312009-08-27 06:47:4630
[email protected]631cf822009-05-15 07:01:2531 // What an extension was loaded from.
32 enum Location {
33 INVALID,
[email protected]25b34332009-06-05 21:53:1934 INTERNAL, // A crx file from the internal Extensions directory.
35 EXTERNAL_PREF, // A crx file from an external directory (via prefs).
36 EXTERNAL_REGISTRY, // A crx file from an external directory (via eg the
37 // registry on Windows).
38 LOAD // --load-extension.
39 };
40
41 enum State {
[email protected]0c6da502009-08-14 22:32:3942 DISABLED = 0,
[email protected]25b34332009-06-05 21:53:1943 ENABLED,
44 KILLBIT, // Don't install/upgrade (applies to external extensions only).
[email protected]0c6da502009-08-14 22:32:3945
46 NUM_STATES
[email protected]631cf822009-05-15 07:01:2547 };
[email protected]7713d632008-12-02 07:52:3348
[email protected]fbcc40302009-06-12 20:45:4549 enum InstallType {
[email protected]ab6f2b22009-07-28 23:28:3750 INSTALL_ERROR,
[email protected]fbcc40302009-06-12 20:45:4551 DOWNGRADE,
52 REINSTALL,
53 UPGRADE,
54 NEW_INSTALL
55 };
56
[email protected]d2817012009-08-04 06:46:2157 // NOTE: If you change this list, you should also change kIconSizes in the cc
58 // file.
59 enum Icons {
60 EXTENSION_ICON_LARGE = 128,
61 EXTENSION_ICON_MEDIUM = 48,
62 EXTENSION_ICON_SMALL = 32,
63 EXTENSION_ICON_BITTY = 16,
64 };
65
[email protected]c3e3def742009-07-17 07:51:0666 // Icon sizes used by the extension system.
[email protected]d2817012009-08-04 06:46:2167 static const int kIconSizes[];
[email protected]c3e3def742009-07-17 07:51:0668
[email protected]4c4f8192009-10-17 01:03:2669 // Max size (both dimensions) for browser and page actions.
70 static const int kPageActionIconMaxSize;
71 static const int kBrowserActionIconMaxSize;
72
[email protected]35506352009-08-07 18:58:1973 // Each permission is a module that the extension is permitted to use.
[email protected]aeb53b32009-10-29 07:34:4574 static const char* kTabPermission;
75 static const char* kBookmarkPermission;
76
[email protected]35506352009-08-07 18:58:1977 static const char* kPermissionNames[];
78 static const size_t kNumPermissions;
79
[email protected]de768a832009-10-30 05:25:0180 // Experimental extension APIs are guarded by the following flag.
81 static const char* kExperimentalName;
82
[email protected]d6336a92009-08-13 17:25:1283 struct PrivacyBlacklistInfo {
84 FilePath path; // Path to the plain-text blacklist.
85 };
86
[email protected]c533bb22009-06-03 19:06:1187 // An NPAPI plugin included in the extension.
88 struct PluginInfo {
89 FilePath path; // Path to the plugin.
90 bool is_public; // False if only this extension can load this plugin.
91 };
92
[email protected]bbc945542009-07-26 00:11:4293 // A toolstrip and its associated mole.
94 struct ToolstripInfo {
95 ToolstripInfo() : mole_height(0) {}
96
97 GURL toolstrip;
98 GURL mole;
99 int mole_height;
100 };
101
[email protected]6014d672008-12-05 00:38:25102 // The name of the manifest inside an extension.
[email protected]0e292232009-01-22 15:23:34103 static const char kManifestFilename[];
[email protected]6014d672008-12-05 00:38:25104
[email protected]300cc58db2009-08-19 20:45:14105 // The name of locale folder inside an extension.
106 static const char kLocaleFolder[];
107
108 // The name of the messages file inside an extension.
109 static const char kMessagesFilename[];
110
[email protected]25b34332009-06-05 21:53:19111#if defined(OS_WIN)
112 static const char* kExtensionRegistryPath;
113#endif
114
[email protected]37eeb5a2009-02-26 23:36:17115 // The number of bytes in a legal id.
[email protected]fe0e7822009-02-26 23:51:48116 static const size_t kIdSize;
[email protected]37eeb5a2009-02-26 23:36:17117
[email protected]e435d6b72009-07-25 03:15:58118 // The mimetype used for extensions.
119 static const char kMimeType[];
120
[email protected]631cf822009-05-15 07:01:25121 explicit Extension(const FilePath& path);
[email protected]631cf822009-05-15 07:01:25122 virtual ~Extension();
123
[email protected]25b34332009-06-05 21:53:19124 // Checks to see if the extension has a valid ID.
125 static bool IdIsValid(const std::string& id);
126
[email protected]e435d6b72009-07-25 03:15:58127 // Returns true if the specified file is an extension.
128 static bool IsExtension(const FilePath& file_name);
129
[email protected]25b34332009-06-05 21:53:19130 // Whether the |location| is external or not.
131 static inline bool IsExternalLocation(Location location) {
132 return location == Extension::EXTERNAL_PREF ||
133 location == Extension::EXTERNAL_REGISTRY;
134 }
135
[email protected]07c00d992009-03-04 20:27:04136 // Returns an absolute url to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:59137 // |extension_url| argument should be the url() from an Extension object. The
138 // |relative_path| can be untrusted user input. The returned URL will either
139 // be invalid() or a child of |extension_url|.
140 // NOTE: Static so that it can be used from multiple threads.
141 static GURL GetResourceURL(const GURL& extension_url,
142 const std::string& relative_path);
[email protected]3cfbd0e2009-03-18 21:26:24143 GURL GetResourceURL(const std::string& relative_path) {
144 return GetResourceURL(url(), relative_path);
145 }
[email protected]eab9b452009-01-23 20:48:59146
[email protected]ecabe6ee2009-10-07 22:49:10147 // Returns an extension resource object. The |extension_path| argument should
148 // be the path() from an Extension object.
149 // The |relative_path| can be untrusted user input.
[email protected]eab9b452009-01-23 20:48:59150 // NOTE: Static so that it can be used from multiple threads.
[email protected]ecabe6ee2009-10-07 22:49:10151 static ExtensionResource GetResource(const FilePath& extension_path,
152 const std::string& relative_path);
153 ExtensionResource GetResource(const std::string& relative_path) {
154 return GetResource(path(), relative_path);
[email protected]3cfbd0e2009-03-18 21:26:24155 }
[email protected]eab9b452009-01-23 20:48:59156
[email protected]a17f9462009-06-09 02:56:41157 // |input| is expected to be the text of an rsa public or private key. It
158 // tolerates the presence or absence of bracking header/footer like this:
159 // -----(BEGIN|END) [RSA PUBLIC/PRIVATE] KEY-----
160 // and may contain newlines.
161 static bool ParsePEMKeyBytes(const std::string& input, std::string* output);
162
163 // Does a simple base64 encoding of |input| into |output|.
164 static bool ProducePEM(const std::string& input, std::string* output);
165
[email protected]84ac7f32009-10-06 06:17:54166 // Generates an extension ID from arbitrary input. The same input string will
167 // always generate the same output ID.
168 static bool GenerateId(const std::string& input, std::string* output);
[email protected]fbcc40302009-06-12 20:45:45169
[email protected]a17f9462009-06-09 02:56:41170 // Expects base64 encoded |input| and formats into |output| including
171 // the appropriate header & footer.
172 static bool FormatPEMForFileOutput(const std::string input,
173 std::string* output, bool is_public);
174
[email protected]2a409532009-08-28 19:39:44175 // Determine whether |new_extension| has increased privileges compared to
176 // |old_extension|.
177 static bool IsPrivilegeIncrease(Extension* old_extension,
178 Extension* new_extension);
[email protected]b24d8312009-08-27 06:47:46179
[email protected]4a8d3272009-03-10 19:15:08180 // Initialize the extension from a parsed manifest.
[email protected]5bfb1eb0a2009-04-08 18:33:30181 // If |require_id| is true, will return an error if the "id" key is missing
182 // from the value.
183 bool InitFromValue(const DictionaryValue& value, bool require_id,
184 std::string* error);
[email protected]4a8d3272009-03-10 19:15:08185
[email protected]82891262008-12-24 00:21:26186 const FilePath& path() const { return path_; }
[email protected]af1277b2009-07-28 00:47:53187 void set_path(const FilePath& path) { path_ = path; }
[email protected]5bfb1eb0a2009-04-08 18:33:30188 const GURL& url() const { return extension_url_; }
[email protected]631cf822009-05-15 07:01:25189 const Location location() const { return location_; }
190 void set_location(Location location) { location_ = location; }
[email protected]4a8d3272009-03-10 19:15:08191 const std::string& id() const { return id_; }
192 const Version* version() const { return version_.get(); }
193 // String representation of the version number.
194 const std::string VersionString() const;
195 const std::string& name() const { return name_; }
[email protected]fbcc40302009-06-12 20:45:45196 const std::string& public_key() const { return public_key_; }
[email protected]4a8d3272009-03-10 19:15:08197 const std::string& description() const { return description_; }
198 const UserScriptList& content_scripts() const { return content_scripts_; }
[email protected]5d246db22009-10-27 06:17:57199 ExtensionAction* page_action() const { return page_action_.get(); }
200 ExtensionAction* browser_action() const { return browser_action_.get(); }
[email protected]d6336a92009-08-13 17:25:12201 const std::vector<PrivacyBlacklistInfo>& privacy_blacklists() const {
202 return privacy_blacklists_;
203 }
[email protected]c533bb22009-06-03 19:06:11204 const std::vector<PluginInfo>& plugins() const { return plugins_; }
[email protected]c64631652009-04-29 22:24:31205 const GURL& background_url() const { return background_url_; }
[email protected]43919ac92009-10-16 18:34:28206 const GURL& options_url() const { return options_url_; }
[email protected]bbc945542009-07-26 00:11:42207 const std::vector<ToolstripInfo>& toolstrips() const { return toolstrips_; }
[email protected]35506352009-08-07 18:58:19208 const std::vector<std::string>& api_permissions() const {
209 return api_permissions_;
210 }
[email protected]c7ad50f2009-09-11 06:28:15211 const HostPermissions& host_permissions() const {
212 return host_permissions_;
213 }
214
215 // Returns true if the extension has permission to access the host for the
216 // specified URL.
217 bool CanAccessHost(const GURL& url) const;
[email protected]b24d8312009-08-27 06:47:46218
[email protected]aeb53b32009-10-29 07:34:45219 // Returns true if the extension has the specified API permission.
220 bool HasApiPermission(const std::string& permission) const {
221 return std::find(api_permissions_.begin(), api_permissions_.end(),
222 permission) != api_permissions_.end();
223 }
224
[email protected]b24d8312009-08-27 06:47:46225 // Returns the set of hosts that the extension effectively has access to. This
226 // is used in the permissions UI and is a combination of the hosts accessible
227 // through content scripts and the hosts accessible through XHR.
228 const std::set<std::string> GetEffectiveHostPermissions() const;
229
230 // Whether the extension has access to all hosts. This is true if there is
231 // a content script that matches all hosts, or if there is a host permission
232 // for all hosts.
233 bool HasAccessToAllHosts() const;
234
[email protected]b29682ba22009-06-18 19:53:56235 const GURL& update_url() const { return update_url_; }
[email protected]0cb5f502009-10-21 21:35:47236 const std::map<int, std::string>& icons() const { return icons_; }
[email protected]4a8d3272009-03-10 19:15:08237
[email protected]25b34332009-06-05 21:53:19238 // Returns the origin of this extension. This function takes a |registry_path|
239 // so that the registry location can be overwritten during testing.
240 Location ExternalExtensionInstallType(std::string registry_path);
241
242 // Theme-related.
[email protected]631cf822009-05-15 07:01:25243 DictionaryValue* GetThemeImages() const { return theme_images_.get(); }
244 DictionaryValue* GetThemeColors() const { return theme_colors_.get(); }
245 DictionaryValue* GetThemeTints() const { return theme_tints_.get(); }
[email protected]7895ea22009-06-02 20:53:50246 DictionaryValue* GetThemeDisplayProperties() const {
247 return theme_display_properties_.get();
248 }
[email protected]66da08b2009-10-19 22:27:00249 bool IsTheme() const { return is_theme_; }
[email protected]631cf822009-05-15 07:01:25250
[email protected]facd7a7652009-06-05 23:15:02251 // Returns a list of paths (relative to the extension dir) for images that
252 // the browser might load (like themes and page action icons).
253 std::set<FilePath> GetBrowserImages();
254
[email protected]866930682009-08-18 22:53:47255 // Returns an absolute path to the given icon inside of the extension. Returns
256 // an empty FilePath if the extension does not have that icon.
[email protected]ecabe6ee2009-10-07 22:49:10257 ExtensionResource GetIconPath(Icons icon);
[email protected]866930682009-08-18 22:53:47258
[email protected]b6ab96d2009-08-20 18:58:19259 const DictionaryValue* manifest_value() const {
260 return manifest_value_.get();
261 }
262
[email protected]42b6f0f82009-09-18 21:07:39263 // Getter/setter for l10n message bundle.
264 const ExtensionMessageBundle* message_bundle() const {
265 return message_bundle_.get();
[email protected]300cc58db2009-08-19 20:45:14266 }
[email protected]42b6f0f82009-09-18 21:07:39267 void set_message_bundle(ExtensionMessageBundle* message_bundle) {
268 message_bundle_.reset(message_bundle);
[email protected]300cc58db2009-08-19 20:45:14269 }
[email protected]e95ad332009-08-03 19:44:25270
[email protected]86c008e82009-08-28 20:26:05271 // Chrome URL overrides (see ExtensionOverrideUI).
[email protected]d3cfa482009-10-17 13:54:57272 const URLOverrideMap& GetChromeURLOverrides() const {
273 return chrome_url_overrides_;
[email protected]86c008e82009-08-28 20:26:05274 }
275
[email protected]e95ad332009-08-03 19:44:25276 // Runtime data:
277 // Put dynamic data about the state of a running extension below.
278
279 // Whether the background page, if any, is ready. We don't load other
280 // components until then. If there is no background page, we consider it to
281 // be ready.
282 bool GetBackgroundPageReady();
283 void SetBackgroundPageReady();
284
[email protected]4a8d3272009-03-10 19:15:08285 private:
[email protected]3cfbd0e2009-03-18 21:26:24286 // Helper method that loads a UserScript object from a
287 // dictionary in the content_script list of the manifest.
288 bool LoadUserScriptHelper(const DictionaryValue* content_script,
289 int definition_index,
290 std::string* error,
291 UserScript* result);
[email protected]f7f3a5f2009-05-01 22:02:34292
[email protected]5d246db22009-10-27 06:17:57293 // Helper method to load an ExtensionAction from the page_action or
[email protected]92c6f9b92009-10-24 04:35:08294 // browser_action entries in the manifest.
[email protected]5d246db22009-10-27 06:17:57295 ExtensionAction* LoadExtensionActionHelper(
[email protected]92c6f9b92009-10-24 04:35:08296 const DictionaryValue* extension_action, std::string* error);
297
[email protected]e2eb43112009-05-29 21:19:54298 // Figures out if a source contains keys not associated with themes - we
299 // don't want to allow scripts and such to be bundled with themes.
300 bool ContainsNonThemeKeys(const DictionaryValue& source);
301
[email protected]4a8d3272009-03-10 19:15:08302 // The absolute path to the directory the extension is stored in.
303 FilePath path_;
304
305 // The base extension url for the extension.
306 GURL extension_url_;
[email protected]eab9b452009-01-23 20:48:59307
[email protected]631cf822009-05-15 07:01:25308 // The location the extension was loaded from.
309 Location location_;
310
[email protected]7713d632008-12-02 07:52:33311 // A human-readable ID for the extension. The convention is to use something
312 // like 'com.example.myextension', but this is not currently enforced. An
313 // extension's ID is used in things like directory structures and URLs, and
314 // is expected to not change across versions. In the case of conflicts,
315 // updates will only be allowed if the extension can be validated using the
316 // previous version's update key.
[email protected]e1cec06c2008-12-18 01:22:23317 std::string id_;
[email protected]82891262008-12-24 00:21:26318
[email protected]64a02b802009-01-12 19:36:42319 // The extension's version.
[email protected]cc655912009-01-29 23:19:19320 scoped_ptr<Version> version_;
[email protected]64a02b802009-01-12 19:36:42321
[email protected]82891262008-12-24 00:21:26322 // The extension's human-readable name.
[email protected]e1cec06c2008-12-18 01:22:23323 std::string name_;
[email protected]82891262008-12-24 00:21:26324
[email protected]4a8d3272009-03-10 19:15:08325 // An optional longer description of the extension.
[email protected]e1cec06c2008-12-18 01:22:23326 std::string description_;
[email protected]82891262008-12-24 00:21:26327
328 // Paths to the content scripts the extension contains.
[email protected]34aa8dc2009-02-19 07:03:05329 UserScriptList content_scripts_;
[email protected]7713d632008-12-02 07:52:33330
[email protected]37e960e2009-10-13 23:17:50331 // The extension's page action, if any.
[email protected]5d246db22009-10-27 06:17:57332 scoped_ptr<ExtensionAction> page_action_;
[email protected]671e6c1ce2009-09-26 03:18:46333
334 // The extension's browser action, if any.
[email protected]5d246db22009-10-27 06:17:57335 scoped_ptr<ExtensionAction> browser_action_;
[email protected]ec9ac0df2009-10-01 18:06:47336
[email protected]d6336a92009-08-13 17:25:12337 // Optional list of privacy blacklistrom.
338 std::vector<PrivacyBlacklistInfo> privacy_blacklists_;
339
[email protected]c533bb22009-06-03 19:06:11340 // Optional list of NPAPI plugins and associated properties.
341 std::vector<PluginInfo> plugins_;
[email protected]367230c52009-02-21 01:44:30342
[email protected]c64631652009-04-29 22:24:31343 // Optional URL to a master page of which a single instance should be always
344 // loaded in the background.
345 GURL background_url_;
346
[email protected]43919ac92009-10-16 18:34:28347 // Optional URL to a page for setting options/preferences.
348 GURL options_url_;
349
[email protected]bbc945542009-07-26 00:11:42350 // Optional list of toolstrips_ and associated properties.
351 std::vector<ToolstripInfo> toolstrips_;
[email protected]4a8d3272009-03-10 19:15:08352
[email protected]fbcc40302009-06-12 20:45:45353 // The public key ('key' in the manifest) used to sign the contents of the
354 // crx package ('signature' in the manifest)
355 std::string public_key_;
[email protected]cc655912009-01-29 23:19:19356
[email protected]07c00d992009-03-04 20:27:04357 // A map of resource id's to relative file paths.
[email protected]bbb436f2009-05-09 16:51:07358 scoped_ptr<DictionaryValue> theme_images_;
[email protected]4a190632009-05-09 01:07:42359
360 // A map of color names to colors.
[email protected]bbb436f2009-05-09 16:51:07361 scoped_ptr<DictionaryValue> theme_colors_;
[email protected]4a190632009-05-09 01:07:42362
363 // A map of color names to colors.
[email protected]bbb436f2009-05-09 16:51:07364 scoped_ptr<DictionaryValue> theme_tints_;
[email protected]4a190632009-05-09 01:07:42365
[email protected]7895ea22009-06-02 20:53:50366 // A map of display properties.
367 scoped_ptr<DictionaryValue> theme_display_properties_;
368
[email protected]4a190632009-05-09 01:07:42369 // Whether the extension is a theme - if it is, certain things are disabled.
370 bool is_theme_;
[email protected]07c00d992009-03-04 20:27:04371
[email protected]35506352009-08-07 18:58:19372 // The set of module-level APIs this extension can use.
373 std::vector<std::string> api_permissions_;
374
[email protected]c64631652009-04-29 22:24:31375 // The sites this extension has permission to talk to (using XHR, etc).
[email protected]b24d8312009-08-27 06:47:46376 HostPermissions host_permissions_;
[email protected]7197f4992009-03-23 05:05:49377
[email protected]c3e3def742009-07-17 07:51:06378 // The paths to the icons the extension contains mapped by their width.
379 std::map<int, std::string> icons_;
380
[email protected]b29682ba22009-06-18 19:53:56381 // URL for fetching an update manifest
382 GURL update_url_;
[email protected]d6336a92009-08-13 17:25:12383
[email protected]b6ab96d2009-08-20 18:58:19384 // A copy of the manifest that this extension was created from.
385 scoped_ptr<DictionaryValue> manifest_value_;
386
[email protected]42b6f0f82009-09-18 21:07:39387 // Handles the l10n messages replacement and parsing.
388 scoped_ptr<ExtensionMessageBundle> message_bundle_;
[email protected]e95ad332009-08-03 19:44:25389
[email protected]86c008e82009-08-28 20:26:05390 // A map of chrome:// hostnames (newtab, downloads, etc.) to Extension URLs
391 // which override the handling of those URLs.
[email protected]d3cfa482009-10-17 13:54:57392 URLOverrideMap chrome_url_overrides_;
[email protected]86c008e82009-08-28 20:26:05393
[email protected]e95ad332009-08-03 19:44:25394 // Runtime data:
395
396 // True if the background page is ready.
397 bool background_page_ready_;
[email protected]b29682ba22009-06-18 19:53:56398
[email protected]ae7fe712009-07-02 20:33:58399 FRIEND_TEST(ExtensionTest, LoadPageActionHelper);
400
[email protected]894bb502009-05-21 22:39:57401 DISALLOW_COPY_AND_ASSIGN(Extension);
[email protected]7713d632008-12-02 07:52:33402};
403
[email protected]5b1a0e22009-05-26 19:00:58404#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_H_