blob: c96ec0a9a4405f3e072326930fa63b893a208e37 [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;
29
[email protected]631cf822009-05-15 07:01:2530 // What an extension was loaded from.
31 enum Location {
32 INVALID,
[email protected]25b34332009-06-05 21:53:1933 INTERNAL, // A crx file from the internal Extensions directory.
34 EXTERNAL_PREF, // A crx file from an external directory (via prefs).
35 EXTERNAL_REGISTRY, // A crx file from an external directory (via eg the
36 // registry on Windows).
37 LOAD // --load-extension.
38 };
39
40 enum State {
[email protected]0c6da502009-08-14 22:32:3941 DISABLED = 0,
[email protected]25b34332009-06-05 21:53:1942 ENABLED,
43 KILLBIT, // Don't install/upgrade (applies to external extensions only).
[email protected]0c6da502009-08-14 22:32:3944
45 NUM_STATES
[email protected]631cf822009-05-15 07:01:2546 };
[email protected]7713d632008-12-02 07:52:3347
[email protected]fbcc40302009-06-12 20:45:4548 enum InstallType {
[email protected]ab6f2b22009-07-28 23:28:3749 INSTALL_ERROR,
[email protected]fbcc40302009-06-12 20:45:4550 DOWNGRADE,
51 REINSTALL,
52 UPGRADE,
53 NEW_INSTALL
54 };
55
[email protected]d2817012009-08-04 06:46:2156 // NOTE: If you change this list, you should also change kIconSizes in the cc
57 // file.
58 enum Icons {
59 EXTENSION_ICON_LARGE = 128,
60 EXTENSION_ICON_MEDIUM = 48,
61 EXTENSION_ICON_SMALL = 32,
62 EXTENSION_ICON_BITTY = 16,
63 };
64
[email protected]c3e3def742009-07-17 07:51:0665 // Icon sizes used by the extension system.
[email protected]d2817012009-08-04 06:46:2166 static const int kIconSizes[];
[email protected]c3e3def742009-07-17 07:51:0667
[email protected]35506352009-08-07 18:58:1968 // Each permission is a module that the extension is permitted to use.
69 static const char* kPermissionNames[];
70 static const size_t kNumPermissions;
71
[email protected]d6336a92009-08-13 17:25:1272 struct PrivacyBlacklistInfo {
73 FilePath path; // Path to the plain-text blacklist.
74 };
75
[email protected]c533bb22009-06-03 19:06:1176 // An NPAPI plugin included in the extension.
77 struct PluginInfo {
78 FilePath path; // Path to the plugin.
79 bool is_public; // False if only this extension can load this plugin.
80 };
81
[email protected]bbc945542009-07-26 00:11:4282 // A toolstrip and its associated mole.
83 struct ToolstripInfo {
84 ToolstripInfo() : mole_height(0) {}
85
86 GURL toolstrip;
87 GURL mole;
88 int mole_height;
89 };
90
[email protected]6014d672008-12-05 00:38:2591 // The name of the manifest inside an extension.
[email protected]0e292232009-01-22 15:23:3492 static const char kManifestFilename[];
[email protected]6014d672008-12-05 00:38:2593
[email protected]300cc58db2009-08-19 20:45:1494 // The name of locale folder inside an extension.
95 static const char kLocaleFolder[];
96
97 // The name of the messages file inside an extension.
98 static const char kMessagesFilename[];
99
[email protected]25b34332009-06-05 21:53:19100#if defined(OS_WIN)
101 static const char* kExtensionRegistryPath;
102#endif
103
[email protected]37eeb5a2009-02-26 23:36:17104 // The number of bytes in a legal id.
[email protected]fe0e7822009-02-26 23:51:48105 static const size_t kIdSize;
[email protected]37eeb5a2009-02-26 23:36:17106
[email protected]e435d6b72009-07-25 03:15:58107 // The mimetype used for extensions.
108 static const char kMimeType[];
109
[email protected]631cf822009-05-15 07:01:25110 explicit Extension(const FilePath& path);
[email protected]631cf822009-05-15 07:01:25111 virtual ~Extension();
112
[email protected]25b34332009-06-05 21:53:19113 // Checks to see if the extension has a valid ID.
114 static bool IdIsValid(const std::string& id);
115
[email protected]e435d6b72009-07-25 03:15:58116 // Returns true if the specified file is an extension.
117 static bool IsExtension(const FilePath& file_name);
118
[email protected]25b34332009-06-05 21:53:19119 // Whether the |location| is external or not.
120 static inline bool IsExternalLocation(Location location) {
121 return location == Extension::EXTERNAL_PREF ||
122 location == Extension::EXTERNAL_REGISTRY;
123 }
124
[email protected]07c00d992009-03-04 20:27:04125 // Returns an absolute url to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:59126 // |extension_url| argument should be the url() from an Extension object. The
127 // |relative_path| can be untrusted user input. The returned URL will either
128 // be invalid() or a child of |extension_url|.
129 // NOTE: Static so that it can be used from multiple threads.
130 static GURL GetResourceURL(const GURL& extension_url,
131 const std::string& relative_path);
[email protected]3cfbd0e2009-03-18 21:26:24132 GURL GetResourceURL(const std::string& relative_path) {
133 return GetResourceURL(url(), relative_path);
134 }
[email protected]eab9b452009-01-23 20:48:59135
[email protected]ecabe6ee2009-10-07 22:49:10136 // Returns an extension resource object. The |extension_path| argument should
137 // be the path() from an Extension object.
138 // The |relative_path| can be untrusted user input.
[email protected]eab9b452009-01-23 20:48:59139 // NOTE: Static so that it can be used from multiple threads.
[email protected]ecabe6ee2009-10-07 22:49:10140 static ExtensionResource GetResource(const FilePath& extension_path,
141 const std::string& relative_path);
142 ExtensionResource GetResource(const std::string& relative_path) {
143 return GetResource(path(), relative_path);
[email protected]3cfbd0e2009-03-18 21:26:24144 }
[email protected]eab9b452009-01-23 20:48:59145
[email protected]a17f9462009-06-09 02:56:41146 // |input| is expected to be the text of an rsa public or private key. It
147 // tolerates the presence or absence of bracking header/footer like this:
148 // -----(BEGIN|END) [RSA PUBLIC/PRIVATE] KEY-----
149 // and may contain newlines.
150 static bool ParsePEMKeyBytes(const std::string& input, std::string* output);
151
152 // Does a simple base64 encoding of |input| into |output|.
153 static bool ProducePEM(const std::string& input, std::string* output);
154
[email protected]84ac7f32009-10-06 06:17:54155 // Generates an extension ID from arbitrary input. The same input string will
156 // always generate the same output ID.
157 static bool GenerateId(const std::string& input, std::string* output);
[email protected]fbcc40302009-06-12 20:45:45158
[email protected]a17f9462009-06-09 02:56:41159 // Expects base64 encoded |input| and formats into |output| including
160 // the appropriate header & footer.
161 static bool FormatPEMForFileOutput(const std::string input,
162 std::string* output, bool is_public);
163
[email protected]2a409532009-08-28 19:39:44164 // Determine whether |new_extension| has increased privileges compared to
165 // |old_extension|.
166 static bool IsPrivilegeIncrease(Extension* old_extension,
167 Extension* new_extension);
[email protected]b24d8312009-08-27 06:47:46168
[email protected]4a8d3272009-03-10 19:15:08169 // Initialize the extension from a parsed manifest.
[email protected]5bfb1eb0a2009-04-08 18:33:30170 // If |require_id| is true, will return an error if the "id" key is missing
171 // from the value.
172 bool InitFromValue(const DictionaryValue& value, bool require_id,
173 std::string* error);
[email protected]4a8d3272009-03-10 19:15:08174
[email protected]82891262008-12-24 00:21:26175 const FilePath& path() const { return path_; }
[email protected]af1277b2009-07-28 00:47:53176 void set_path(const FilePath& path) { path_ = path; }
[email protected]5bfb1eb0a2009-04-08 18:33:30177 const GURL& url() const { return extension_url_; }
[email protected]631cf822009-05-15 07:01:25178 const Location location() const { return location_; }
179 void set_location(Location location) { location_ = location; }
[email protected]4a8d3272009-03-10 19:15:08180 const std::string& id() const { return id_; }
181 const Version* version() const { return version_.get(); }
182 // String representation of the version number.
183 const std::string VersionString() const;
184 const std::string& name() const { return name_; }
[email protected]fbcc40302009-06-12 20:45:45185 const std::string& public_key() const { return public_key_; }
[email protected]4a8d3272009-03-10 19:15:08186 const std::string& description() const { return description_; }
187 const UserScriptList& content_scripts() const { return content_scripts_; }
[email protected]37e960e2009-10-13 23:17:50188 ExtensionAction* page_action() const { return page_action_.get(); }
[email protected]ba69a7d2009-09-28 21:09:56189 ExtensionAction* browser_action() const { return browser_action_.get(); }
[email protected]ec9ac0df2009-10-01 18:06:47190 ExtensionActionState* browser_action_state() {
191 return browser_action_state_.get();
192 }
[email protected]d6336a92009-08-13 17:25:12193 const std::vector<PrivacyBlacklistInfo>& privacy_blacklists() const {
194 return privacy_blacklists_;
195 }
[email protected]c533bb22009-06-03 19:06:11196 const std::vector<PluginInfo>& plugins() const { return plugins_; }
[email protected]c64631652009-04-29 22:24:31197 const GURL& background_url() const { return background_url_; }
[email protected]43919ac92009-10-16 18:34:28198 const GURL& options_url() const { return options_url_; }
[email protected]bbc945542009-07-26 00:11:42199 const std::vector<ToolstripInfo>& toolstrips() const { return toolstrips_; }
[email protected]35506352009-08-07 18:58:19200 const std::vector<std::string>& api_permissions() const {
201 return api_permissions_;
202 }
[email protected]c7ad50f2009-09-11 06:28:15203 const HostPermissions& host_permissions() const {
204 return host_permissions_;
205 }
206
207 // Returns true if the extension has permission to access the host for the
208 // specified URL.
209 bool CanAccessHost(const GURL& url) const;
[email protected]b24d8312009-08-27 06:47:46210
211 // Returns the set of hosts that the extension effectively has access to. This
212 // is used in the permissions UI and is a combination of the hosts accessible
213 // through content scripts and the hosts accessible through XHR.
214 const std::set<std::string> GetEffectiveHostPermissions() const;
215
216 // Whether the extension has access to all hosts. This is true if there is
217 // a content script that matches all hosts, or if there is a host permission
218 // for all hosts.
219 bool HasAccessToAllHosts() const;
220
[email protected]b29682ba22009-06-18 19:53:56221 const GURL& update_url() const { return update_url_; }
[email protected]c3e3def742009-07-17 07:51:06222 const std::map<int, std::string>& icons() { return icons_; }
[email protected]4a8d3272009-03-10 19:15:08223
[email protected]25b34332009-06-05 21:53:19224 // Returns the origin of this extension. This function takes a |registry_path|
225 // so that the registry location can be overwritten during testing.
226 Location ExternalExtensionInstallType(std::string registry_path);
227
228 // Theme-related.
[email protected]631cf822009-05-15 07:01:25229 DictionaryValue* GetThemeImages() const { return theme_images_.get(); }
230 DictionaryValue* GetThemeColors() const { return theme_colors_.get(); }
231 DictionaryValue* GetThemeTints() const { return theme_tints_.get(); }
[email protected]7895ea22009-06-02 20:53:50232 DictionaryValue* GetThemeDisplayProperties() const {
233 return theme_display_properties_.get();
234 }
[email protected]631cf822009-05-15 07:01:25235 bool IsTheme() { return is_theme_; }
236
[email protected]facd7a7652009-06-05 23:15:02237 // Returns a list of paths (relative to the extension dir) for images that
238 // the browser might load (like themes and page action icons).
239 std::set<FilePath> GetBrowserImages();
240
[email protected]866930682009-08-18 22:53:47241 // Returns an absolute path to the given icon inside of the extension. Returns
242 // an empty FilePath if the extension does not have that icon.
[email protected]ecabe6ee2009-10-07 22:49:10243 ExtensionResource GetIconPath(Icons icon);
[email protected]866930682009-08-18 22:53:47244
[email protected]b6ab96d2009-08-20 18:58:19245 const DictionaryValue* manifest_value() const {
246 return manifest_value_.get();
247 }
248
[email protected]42b6f0f82009-09-18 21:07:39249 // Getter/setter for l10n message bundle.
250 const ExtensionMessageBundle* message_bundle() const {
251 return message_bundle_.get();
[email protected]300cc58db2009-08-19 20:45:14252 }
[email protected]42b6f0f82009-09-18 21:07:39253 void set_message_bundle(ExtensionMessageBundle* message_bundle) {
254 message_bundle_.reset(message_bundle);
[email protected]300cc58db2009-08-19 20:45:14255 }
[email protected]e95ad332009-08-03 19:44:25256
[email protected]86c008e82009-08-28 20:26:05257 // Chrome URL overrides (see ExtensionOverrideUI).
258 DictionaryValue* GetChromeURLOverrides() const {
259 return chrome_url_overrides_.get();
260 }
261
[email protected]e95ad332009-08-03 19:44:25262 // Runtime data:
263 // Put dynamic data about the state of a running extension below.
264
265 // Whether the background page, if any, is ready. We don't load other
266 // components until then. If there is no background page, we consider it to
267 // be ready.
268 bool GetBackgroundPageReady();
269 void SetBackgroundPageReady();
270
[email protected]4a8d3272009-03-10 19:15:08271 private:
[email protected]3cfbd0e2009-03-18 21:26:24272 // Helper method that loads a UserScript object from a
273 // dictionary in the content_script list of the manifest.
274 bool LoadUserScriptHelper(const DictionaryValue* content_script,
275 int definition_index,
276 std::string* error,
277 UserScript* result);
[email protected]f7f3a5f2009-05-01 22:02:34278
[email protected]ba69a7d2009-09-28 21:09:56279 // Helper method that loads a ExtensionAction object from a
[email protected]671e6c1ce2009-09-26 03:18:46280 // dictionary in the page_action or browser_action section of the manifest.
[email protected]ba69a7d2009-09-28 21:09:56281 ExtensionAction* LoadExtensionActionHelper(
[email protected]671e6c1ce2009-09-26 03:18:46282 const DictionaryValue* contextual_action,
[email protected]671e6c1ce2009-09-26 03:18:46283 std::string* error,
[email protected]ba69a7d2009-09-28 21:09:56284 ExtensionAction::ExtensionActionType action_type);
[email protected]f7f3a5f2009-05-01 22:02:34285
[email protected]e2eb43112009-05-29 21:19:54286 // Figures out if a source contains keys not associated with themes - we
287 // don't want to allow scripts and such to be bundled with themes.
288 bool ContainsNonThemeKeys(const DictionaryValue& source);
289
[email protected]4a8d3272009-03-10 19:15:08290 // The absolute path to the directory the extension is stored in.
291 FilePath path_;
292
293 // The base extension url for the extension.
294 GURL extension_url_;
[email protected]eab9b452009-01-23 20:48:59295
[email protected]631cf822009-05-15 07:01:25296 // The location the extension was loaded from.
297 Location location_;
298
[email protected]7713d632008-12-02 07:52:33299 // A human-readable ID for the extension. The convention is to use something
300 // like 'com.example.myextension', but this is not currently enforced. An
301 // extension's ID is used in things like directory structures and URLs, and
302 // is expected to not change across versions. In the case of conflicts,
303 // updates will only be allowed if the extension can be validated using the
304 // previous version's update key.
[email protected]e1cec06c2008-12-18 01:22:23305 std::string id_;
[email protected]82891262008-12-24 00:21:26306
[email protected]64a02b802009-01-12 19:36:42307 // The extension's version.
[email protected]cc655912009-01-29 23:19:19308 scoped_ptr<Version> version_;
[email protected]64a02b802009-01-12 19:36:42309
[email protected]82891262008-12-24 00:21:26310 // The extension's human-readable name.
[email protected]e1cec06c2008-12-18 01:22:23311 std::string name_;
[email protected]82891262008-12-24 00:21:26312
[email protected]4a8d3272009-03-10 19:15:08313 // An optional longer description of the extension.
[email protected]e1cec06c2008-12-18 01:22:23314 std::string description_;
[email protected]82891262008-12-24 00:21:26315
316 // Paths to the content scripts the extension contains.
[email protected]34aa8dc2009-02-19 07:03:05317 UserScriptList content_scripts_;
[email protected]7713d632008-12-02 07:52:33318
[email protected]37e960e2009-10-13 23:17:50319 // The extension's page action, if any.
320 scoped_ptr<ExtensionAction> page_action_;
[email protected]671e6c1ce2009-09-26 03:18:46321
322 // The extension's browser action, if any.
[email protected]ba69a7d2009-09-28 21:09:56323 scoped_ptr<ExtensionAction> browser_action_;
[email protected]f7f3a5f2009-05-01 22:02:34324
[email protected]ec9ac0df2009-10-01 18:06:47325 // The state of the browser action. Valid iff browser_action_ is non-NULL.
326 scoped_ptr<ExtensionActionState> browser_action_state_;
327
[email protected]d6336a92009-08-13 17:25:12328 // Optional list of privacy blacklistrom.
329 std::vector<PrivacyBlacklistInfo> privacy_blacklists_;
330
[email protected]c533bb22009-06-03 19:06:11331 // Optional list of NPAPI plugins and associated properties.
332 std::vector<PluginInfo> plugins_;
[email protected]367230c52009-02-21 01:44:30333
[email protected]c64631652009-04-29 22:24:31334 // Optional URL to a master page of which a single instance should be always
335 // loaded in the background.
336 GURL background_url_;
337
[email protected]43919ac92009-10-16 18:34:28338 // Optional URL to a page for setting options/preferences.
339 GURL options_url_;
340
[email protected]bbc945542009-07-26 00:11:42341 // Optional list of toolstrips_ and associated properties.
342 std::vector<ToolstripInfo> toolstrips_;
[email protected]4a8d3272009-03-10 19:15:08343
[email protected]fbcc40302009-06-12 20:45:45344 // The public key ('key' in the manifest) used to sign the contents of the
345 // crx package ('signature' in the manifest)
346 std::string public_key_;
[email protected]cc655912009-01-29 23:19:19347
[email protected]07c00d992009-03-04 20:27:04348 // A map of resource id's to relative file paths.
[email protected]bbb436f2009-05-09 16:51:07349 scoped_ptr<DictionaryValue> theme_images_;
[email protected]4a190632009-05-09 01:07:42350
351 // A map of color names to colors.
[email protected]bbb436f2009-05-09 16:51:07352 scoped_ptr<DictionaryValue> theme_colors_;
[email protected]4a190632009-05-09 01:07:42353
354 // A map of color names to colors.
[email protected]bbb436f2009-05-09 16:51:07355 scoped_ptr<DictionaryValue> theme_tints_;
[email protected]4a190632009-05-09 01:07:42356
[email protected]7895ea22009-06-02 20:53:50357 // A map of display properties.
358 scoped_ptr<DictionaryValue> theme_display_properties_;
359
[email protected]4a190632009-05-09 01:07:42360 // Whether the extension is a theme - if it is, certain things are disabled.
361 bool is_theme_;
[email protected]07c00d992009-03-04 20:27:04362
[email protected]35506352009-08-07 18:58:19363 // The set of module-level APIs this extension can use.
364 std::vector<std::string> api_permissions_;
365
[email protected]c64631652009-04-29 22:24:31366 // The sites this extension has permission to talk to (using XHR, etc).
[email protected]b24d8312009-08-27 06:47:46367 HostPermissions host_permissions_;
[email protected]7197f4992009-03-23 05:05:49368
[email protected]c3e3def742009-07-17 07:51:06369 // The paths to the icons the extension contains mapped by their width.
370 std::map<int, std::string> icons_;
371
[email protected]b29682ba22009-06-18 19:53:56372 // URL for fetching an update manifest
373 GURL update_url_;
[email protected]d6336a92009-08-13 17:25:12374
[email protected]b6ab96d2009-08-20 18:58:19375 // A copy of the manifest that this extension was created from.
376 scoped_ptr<DictionaryValue> manifest_value_;
377
[email protected]42b6f0f82009-09-18 21:07:39378 // Handles the l10n messages replacement and parsing.
379 scoped_ptr<ExtensionMessageBundle> message_bundle_;
[email protected]e95ad332009-08-03 19:44:25380
[email protected]86c008e82009-08-28 20:26:05381 // A map of chrome:// hostnames (newtab, downloads, etc.) to Extension URLs
382 // which override the handling of those URLs.
383 scoped_ptr<DictionaryValue> chrome_url_overrides_;
384
[email protected]e95ad332009-08-03 19:44:25385 // Runtime data:
386
387 // True if the background page is ready.
388 bool background_page_ready_;
[email protected]b29682ba22009-06-18 19:53:56389
[email protected]ae7fe712009-07-02 20:33:58390 FRIEND_TEST(ExtensionTest, LoadPageActionHelper);
391
[email protected]894bb502009-05-21 22:39:57392 DISALLOW_COPY_AND_ASSIGN(Extension);
[email protected]7713d632008-12-02 07:52:33393};
394
[email protected]5b1a0e22009-05-26 19:00:58395#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_H_