blob: af1669805c7eed4372a12183b9f868b4b091a3a9 [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]42b6f0f82009-09-18 21:07:3920#include "chrome/common/extensions/user_script.h"
[email protected]7197f4992009-03-23 05:05:4921#include "chrome/common/extensions/url_pattern.h"
[email protected]eab9b452009-01-23 20:48:5922#include "googleurl/src/gurl.h"
23
[email protected]c533bb22009-06-03 19:06:1124// Represents a Chrome extension.
[email protected]7713d632008-12-02 07:52:3325class Extension {
26 public:
[email protected]b24d8312009-08-27 06:47:4627 typedef std::vector<URLPattern> HostPermissions;
28
[email protected]631cf822009-05-15 07:01:2529 // What an extension was loaded from.
30 enum Location {
31 INVALID,
[email protected]25b34332009-06-05 21:53:1932 INTERNAL, // A crx file from the internal Extensions directory.
33 EXTERNAL_PREF, // A crx file from an external directory (via prefs).
34 EXTERNAL_REGISTRY, // A crx file from an external directory (via eg the
35 // registry on Windows).
36 LOAD // --load-extension.
37 };
38
39 enum State {
[email protected]0c6da502009-08-14 22:32:3940 DISABLED = 0,
[email protected]25b34332009-06-05 21:53:1941 ENABLED,
42 KILLBIT, // Don't install/upgrade (applies to external extensions only).
[email protected]0c6da502009-08-14 22:32:3943
44 NUM_STATES
[email protected]631cf822009-05-15 07:01:2545 };
[email protected]7713d632008-12-02 07:52:3346
[email protected]fbcc40302009-06-12 20:45:4547 enum InstallType {
[email protected]ab6f2b22009-07-28 23:28:3748 INSTALL_ERROR,
[email protected]fbcc40302009-06-12 20:45:4549 DOWNGRADE,
50 REINSTALL,
51 UPGRADE,
52 NEW_INSTALL
53 };
54
[email protected]d2817012009-08-04 06:46:2155 // NOTE: If you change this list, you should also change kIconSizes in the cc
56 // file.
57 enum Icons {
58 EXTENSION_ICON_LARGE = 128,
59 EXTENSION_ICON_MEDIUM = 48,
60 EXTENSION_ICON_SMALL = 32,
61 EXTENSION_ICON_BITTY = 16,
62 };
63
[email protected]c3e3def742009-07-17 07:51:0664 // Icon sizes used by the extension system.
[email protected]d2817012009-08-04 06:46:2165 static const int kIconSizes[];
[email protected]c3e3def742009-07-17 07:51:0666
[email protected]35506352009-08-07 18:58:1967 // Each permission is a module that the extension is permitted to use.
68 static const char* kPermissionNames[];
69 static const size_t kNumPermissions;
70
[email protected]d6336a92009-08-13 17:25:1271 struct PrivacyBlacklistInfo {
72 FilePath path; // Path to the plain-text blacklist.
73 };
74
[email protected]c533bb22009-06-03 19:06:1175 // An NPAPI plugin included in the extension.
76 struct PluginInfo {
77 FilePath path; // Path to the plugin.
78 bool is_public; // False if only this extension can load this plugin.
79 };
80
[email protected]bbc945542009-07-26 00:11:4281 // A toolstrip and its associated mole.
82 struct ToolstripInfo {
83 ToolstripInfo() : mole_height(0) {}
84
85 GURL toolstrip;
86 GURL mole;
87 int mole_height;
88 };
89
[email protected]6014d672008-12-05 00:38:2590 // The name of the manifest inside an extension.
[email protected]0e292232009-01-22 15:23:3491 static const char kManifestFilename[];
[email protected]6014d672008-12-05 00:38:2592
[email protected]300cc58db2009-08-19 20:45:1493 // The name of locale folder inside an extension.
94 static const char kLocaleFolder[];
95
96 // The name of the messages file inside an extension.
97 static const char kMessagesFilename[];
98
[email protected]25b34332009-06-05 21:53:1999#if defined(OS_WIN)
100 static const char* kExtensionRegistryPath;
101#endif
102
[email protected]37eeb5a2009-02-26 23:36:17103 // The number of bytes in a legal id.
[email protected]fe0e7822009-02-26 23:51:48104 static const size_t kIdSize;
[email protected]37eeb5a2009-02-26 23:36:17105
[email protected]e435d6b72009-07-25 03:15:58106 // The mimetype used for extensions.
107 static const char kMimeType[];
108
[email protected]e95ad332009-08-03 19:44:25109 Extension()
[email protected]671e6c1ce2009-09-26 03:18:46110 : location_(INVALID), is_theme_(false),
111 background_page_ready_(false) {}
[email protected]631cf822009-05-15 07:01:25112 explicit Extension(const FilePath& path);
[email protected]631cf822009-05-15 07:01:25113 virtual ~Extension();
114
[email protected]fbcc40302009-06-12 20:45:45115 // Resets the id counter. This is only useful for unit tests.
116 static void ResetGeneratedIdCounter() {
117 id_counter_ = 0;
118 }
119
[email protected]25b34332009-06-05 21:53:19120 // Checks to see if the extension has a valid ID.
121 static bool IdIsValid(const std::string& id);
122
[email protected]e435d6b72009-07-25 03:15:58123 // Returns true if the specified file is an extension.
124 static bool IsExtension(const FilePath& file_name);
125
[email protected]25b34332009-06-05 21:53:19126 // Whether the |location| is external or not.
127 static inline bool IsExternalLocation(Location location) {
128 return location == Extension::EXTERNAL_PREF ||
129 location == Extension::EXTERNAL_REGISTRY;
130 }
131
[email protected]07c00d992009-03-04 20:27:04132 // Returns an absolute url to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:59133 // |extension_url| argument should be the url() from an Extension object. The
134 // |relative_path| can be untrusted user input. The returned URL will either
135 // be invalid() or a child of |extension_url|.
136 // NOTE: Static so that it can be used from multiple threads.
137 static GURL GetResourceURL(const GURL& extension_url,
138 const std::string& relative_path);
[email protected]3cfbd0e2009-03-18 21:26:24139 GURL GetResourceURL(const std::string& relative_path) {
140 return GetResourceURL(url(), relative_path);
141 }
[email protected]eab9b452009-01-23 20:48:59142
[email protected]07c00d992009-03-04 20:27:04143 // Returns an absolute path to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:59144 // |extension_path| argument should be the path() from an Extension object.
145 // The |relative_path| can be untrusted user input. The returned path will
146 // either be empty or a child of extension_path.
147 // NOTE: Static so that it can be used from multiple threads.
148 static FilePath GetResourcePath(const FilePath& extension_path,
149 const std::string& relative_path);
[email protected]3cfbd0e2009-03-18 21:26:24150 FilePath GetResourcePath(const std::string& relative_path) {
151 return GetResourcePath(path(), relative_path);
152 }
[email protected]eab9b452009-01-23 20:48:59153
[email protected]a17f9462009-06-09 02:56:41154 // |input| is expected to be the text of an rsa public or private key. It
155 // tolerates the presence or absence of bracking header/footer like this:
156 // -----(BEGIN|END) [RSA PUBLIC/PRIVATE] KEY-----
157 // and may contain newlines.
158 static bool ParsePEMKeyBytes(const std::string& input, std::string* output);
159
160 // Does a simple base64 encoding of |input| into |output|.
161 static bool ProducePEM(const std::string& input, std::string* output);
162
[email protected]fbcc40302009-06-12 20:45:45163 // Note: The result is coverted to lower-case because the browser enforces
164 // hosts to be lower-case in omni-bar.
165 static bool GenerateIdFromPublicKey(const std::string& input,
166 std::string* output);
167
[email protected]a17f9462009-06-09 02:56:41168 // Expects base64 encoded |input| and formats into |output| including
169 // the appropriate header & footer.
170 static bool FormatPEMForFileOutput(const std::string input,
171 std::string* output, bool is_public);
172
[email protected]2a409532009-08-28 19:39:44173 // Determine whether |new_extension| has increased privileges compared to
174 // |old_extension|.
175 static bool IsPrivilegeIncrease(Extension* old_extension,
176 Extension* new_extension);
[email protected]b24d8312009-08-27 06:47:46177
[email protected]4a8d3272009-03-10 19:15:08178 // Initialize the extension from a parsed manifest.
[email protected]5bfb1eb0a2009-04-08 18:33:30179 // If |require_id| is true, will return an error if the "id" key is missing
180 // from the value.
181 bool InitFromValue(const DictionaryValue& value, bool require_id,
182 std::string* error);
[email protected]4a8d3272009-03-10 19:15:08183
[email protected]82891262008-12-24 00:21:26184 const FilePath& path() const { return path_; }
[email protected]af1277b2009-07-28 00:47:53185 void set_path(const FilePath& path) { path_ = path; }
[email protected]5bfb1eb0a2009-04-08 18:33:30186 const GURL& url() const { return extension_url_; }
[email protected]631cf822009-05-15 07:01:25187 const Location location() const { return location_; }
188 void set_location(Location location) { location_ = location; }
[email protected]4a8d3272009-03-10 19:15:08189 const std::string& id() const { return id_; }
190 const Version* version() const { return version_.get(); }
191 // String representation of the version number.
192 const std::string VersionString() const;
193 const std::string& name() const { return name_; }
[email protected]fbcc40302009-06-12 20:45:45194 const std::string& public_key() const { return public_key_; }
[email protected]4a8d3272009-03-10 19:15:08195 const std::string& description() const { return description_; }
196 const UserScriptList& content_scripts() const { return content_scripts_; }
[email protected]ba69a7d2009-09-28 21:09:56197 const ExtensionActionMap& page_actions() const { return page_actions_; }
198 ExtensionAction* browser_action() const { return browser_action_.get(); }
[email protected]d6336a92009-08-13 17:25:12199 const std::vector<PrivacyBlacklistInfo>& privacy_blacklists() const {
200 return privacy_blacklists_;
201 }
[email protected]c533bb22009-06-03 19:06:11202 const std::vector<PluginInfo>& plugins() const { return plugins_; }
[email protected]c64631652009-04-29 22:24:31203 const GURL& background_url() const { return background_url_; }
[email protected]bbc945542009-07-26 00:11:42204 const std::vector<ToolstripInfo>& toolstrips() const { return toolstrips_; }
[email protected]35506352009-08-07 18:58:19205 const std::vector<std::string>& api_permissions() const {
206 return api_permissions_;
207 }
[email protected]c7ad50f2009-09-11 06:28:15208 const HostPermissions& host_permissions() const {
209 return host_permissions_;
210 }
211
212 // Returns true if the extension has permission to access the host for the
213 // specified URL.
214 bool CanAccessHost(const GURL& url) const;
[email protected]b24d8312009-08-27 06:47:46215
216 // Returns the set of hosts that the extension effectively has access to. This
217 // is used in the permissions UI and is a combination of the hosts accessible
218 // through content scripts and the hosts accessible through XHR.
219 const std::set<std::string> GetEffectiveHostPermissions() const;
220
221 // Whether the extension has access to all hosts. This is true if there is
222 // a content script that matches all hosts, or if there is a host permission
223 // for all hosts.
224 bool HasAccessToAllHosts() const;
225
[email protected]b29682ba22009-06-18 19:53:56226 const GURL& update_url() const { return update_url_; }
[email protected]c3e3def742009-07-17 07:51:06227 const std::map<int, std::string>& icons() { return icons_; }
[email protected]4a8d3272009-03-10 19:15:08228
[email protected]671e6c1ce2009-09-26 03:18:46229 // Retrieves a page action or browser action by |id|.
[email protected]ba69a7d2009-09-28 21:09:56230 const ExtensionAction* GetExtensionAction(
231 std::string id, ExtensionAction::ExtensionActionType action_type) const;
[email protected]631cf822009-05-15 07:01:25232
[email protected]25b34332009-06-05 21:53:19233 // Returns the origin of this extension. This function takes a |registry_path|
234 // so that the registry location can be overwritten during testing.
235 Location ExternalExtensionInstallType(std::string registry_path);
236
237 // Theme-related.
[email protected]631cf822009-05-15 07:01:25238 DictionaryValue* GetThemeImages() const { return theme_images_.get(); }
239 DictionaryValue* GetThemeColors() const { return theme_colors_.get(); }
240 DictionaryValue* GetThemeTints() const { return theme_tints_.get(); }
[email protected]7895ea22009-06-02 20:53:50241 DictionaryValue* GetThemeDisplayProperties() const {
242 return theme_display_properties_.get();
243 }
[email protected]631cf822009-05-15 07:01:25244 bool IsTheme() { return is_theme_; }
245
[email protected]facd7a7652009-06-05 23:15:02246 // Returns a list of paths (relative to the extension dir) for images that
247 // the browser might load (like themes and page action icons).
248 std::set<FilePath> GetBrowserImages();
249
[email protected]866930682009-08-18 22:53:47250 // Returns an absolute path to the given icon inside of the extension. Returns
251 // an empty FilePath if the extension does not have that icon.
252 FilePath GetIconPath(Icons icon);
253
[email protected]b6ab96d2009-08-20 18:58:19254 const DictionaryValue* manifest_value() const {
255 return manifest_value_.get();
256 }
257
[email protected]42b6f0f82009-09-18 21:07:39258 // Getter/setter for l10n message bundle.
259 const ExtensionMessageBundle* message_bundle() const {
260 return message_bundle_.get();
[email protected]300cc58db2009-08-19 20:45:14261 }
[email protected]42b6f0f82009-09-18 21:07:39262 void set_message_bundle(ExtensionMessageBundle* message_bundle) {
263 message_bundle_.reset(message_bundle);
[email protected]300cc58db2009-08-19 20:45:14264 }
[email protected]e95ad332009-08-03 19:44:25265
[email protected]86c008e82009-08-28 20:26:05266 // Chrome URL overrides (see ExtensionOverrideUI).
267 DictionaryValue* GetChromeURLOverrides() const {
268 return chrome_url_overrides_.get();
269 }
270
[email protected]e95ad332009-08-03 19:44:25271 // Runtime data:
272 // Put dynamic data about the state of a running extension below.
273
274 // Whether the background page, if any, is ready. We don't load other
275 // components until then. If there is no background page, we consider it to
276 // be ready.
277 bool GetBackgroundPageReady();
278 void SetBackgroundPageReady();
279
[email protected]4a8d3272009-03-10 19:15:08280 private:
[email protected]b55530c2009-06-17 19:07:03281 // Counter used to assign ids to extensions that are loaded using
[email protected]fbcc40302009-06-12 20:45:45282 // --load-extension.
283 static int id_counter_;
284
285 // Returns the next counter id. Intentionally post-incrementing so that first
286 // value is 0.
287 static int NextGeneratedId() { return id_counter_++; }
288
[email protected]3cfbd0e2009-03-18 21:26:24289 // Helper method that loads a UserScript object from a
290 // dictionary in the content_script list of the manifest.
291 bool LoadUserScriptHelper(const DictionaryValue* content_script,
292 int definition_index,
293 std::string* error,
294 UserScript* result);
[email protected]f7f3a5f2009-05-01 22:02:34295
[email protected]ba69a7d2009-09-28 21:09:56296 // Helper method that loads a ExtensionAction object from a
[email protected]671e6c1ce2009-09-26 03:18:46297 // dictionary in the page_action or browser_action section of the manifest.
[email protected]ba69a7d2009-09-28 21:09:56298 ExtensionAction* LoadExtensionActionHelper(
[email protected]671e6c1ce2009-09-26 03:18:46299 const DictionaryValue* contextual_action,
300 int definition_index,
301 std::string* error,
[email protected]ba69a7d2009-09-28 21:09:56302 ExtensionAction::ExtensionActionType action_type);
[email protected]f7f3a5f2009-05-01 22:02:34303
[email protected]e2eb43112009-05-29 21:19:54304 // Figures out if a source contains keys not associated with themes - we
305 // don't want to allow scripts and such to be bundled with themes.
306 bool ContainsNonThemeKeys(const DictionaryValue& source);
307
[email protected]4a8d3272009-03-10 19:15:08308 // The absolute path to the directory the extension is stored in.
309 FilePath path_;
310
311 // The base extension url for the extension.
312 GURL extension_url_;
[email protected]eab9b452009-01-23 20:48:59313
[email protected]631cf822009-05-15 07:01:25314 // The location the extension was loaded from.
315 Location location_;
316
[email protected]7713d632008-12-02 07:52:33317 // A human-readable ID for the extension. The convention is to use something
318 // like 'com.example.myextension', but this is not currently enforced. An
319 // extension's ID is used in things like directory structures and URLs, and
320 // is expected to not change across versions. In the case of conflicts,
321 // updates will only be allowed if the extension can be validated using the
322 // previous version's update key.
[email protected]e1cec06c2008-12-18 01:22:23323 std::string id_;
[email protected]82891262008-12-24 00:21:26324
[email protected]64a02b802009-01-12 19:36:42325 // The extension's version.
[email protected]cc655912009-01-29 23:19:19326 scoped_ptr<Version> version_;
[email protected]64a02b802009-01-12 19:36:42327
[email protected]82891262008-12-24 00:21:26328 // The extension's human-readable name.
[email protected]e1cec06c2008-12-18 01:22:23329 std::string name_;
[email protected]82891262008-12-24 00:21:26330
[email protected]4a8d3272009-03-10 19:15:08331 // An optional longer description of the extension.
[email protected]e1cec06c2008-12-18 01:22:23332 std::string description_;
[email protected]82891262008-12-24 00:21:26333
334 // Paths to the content scripts the extension contains.
[email protected]34aa8dc2009-02-19 07:03:05335 UserScriptList content_scripts_;
[email protected]7713d632008-12-02 07:52:33336
[email protected]f7f3a5f2009-05-01 22:02:34337 // A list of page actions.
[email protected]ba69a7d2009-09-28 21:09:56338 ExtensionActionMap page_actions_;
[email protected]671e6c1ce2009-09-26 03:18:46339
340 // The extension's browser action, if any.
[email protected]ba69a7d2009-09-28 21:09:56341 scoped_ptr<ExtensionAction> browser_action_;
[email protected]f7f3a5f2009-05-01 22:02:34342
[email protected]d6336a92009-08-13 17:25:12343 // Optional list of privacy blacklistrom.
344 std::vector<PrivacyBlacklistInfo> privacy_blacklists_;
345
[email protected]c533bb22009-06-03 19:06:11346 // Optional list of NPAPI plugins and associated properties.
347 std::vector<PluginInfo> plugins_;
[email protected]367230c52009-02-21 01:44:30348
[email protected]c64631652009-04-29 22:24:31349 // Optional URL to a master page of which a single instance should be always
350 // loaded in the background.
351 GURL background_url_;
352
[email protected]bbc945542009-07-26 00:11:42353 // Optional list of toolstrips_ and associated properties.
354 std::vector<ToolstripInfo> toolstrips_;
[email protected]4a8d3272009-03-10 19:15:08355
[email protected]fbcc40302009-06-12 20:45:45356 // The public key ('key' in the manifest) used to sign the contents of the
357 // crx package ('signature' in the manifest)
358 std::string public_key_;
[email protected]cc655912009-01-29 23:19:19359
[email protected]07c00d992009-03-04 20:27:04360 // A map of resource id's to relative file paths.
[email protected]bbb436f2009-05-09 16:51:07361 scoped_ptr<DictionaryValue> theme_images_;
[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_colors_;
[email protected]4a190632009-05-09 01:07:42365
366 // A map of color names to colors.
[email protected]bbb436f2009-05-09 16:51:07367 scoped_ptr<DictionaryValue> theme_tints_;
[email protected]4a190632009-05-09 01:07:42368
[email protected]7895ea22009-06-02 20:53:50369 // A map of display properties.
370 scoped_ptr<DictionaryValue> theme_display_properties_;
371
[email protected]4a190632009-05-09 01:07:42372 // Whether the extension is a theme - if it is, certain things are disabled.
373 bool is_theme_;
[email protected]07c00d992009-03-04 20:27:04374
[email protected]35506352009-08-07 18:58:19375 // The set of module-level APIs this extension can use.
376 std::vector<std::string> api_permissions_;
377
[email protected]c64631652009-04-29 22:24:31378 // The sites this extension has permission to talk to (using XHR, etc).
[email protected]b24d8312009-08-27 06:47:46379 HostPermissions host_permissions_;
[email protected]7197f4992009-03-23 05:05:49380
[email protected]c3e3def742009-07-17 07:51:06381 // The paths to the icons the extension contains mapped by their width.
382 std::map<int, std::string> icons_;
383
[email protected]b29682ba22009-06-18 19:53:56384 // URL for fetching an update manifest
385 GURL update_url_;
[email protected]d6336a92009-08-13 17:25:12386
[email protected]b6ab96d2009-08-20 18:58:19387 // A copy of the manifest that this extension was created from.
388 scoped_ptr<DictionaryValue> manifest_value_;
389
[email protected]42b6f0f82009-09-18 21:07:39390 // Handles the l10n messages replacement and parsing.
391 scoped_ptr<ExtensionMessageBundle> message_bundle_;
[email protected]e95ad332009-08-03 19:44:25392
[email protected]86c008e82009-08-28 20:26:05393 // A map of chrome:// hostnames (newtab, downloads, etc.) to Extension URLs
394 // which override the handling of those URLs.
395 scoped_ptr<DictionaryValue> chrome_url_overrides_;
396
[email protected]e95ad332009-08-03 19:44:25397 // Runtime data:
398
399 // True if the background page is ready.
400 bool background_page_ready_;
[email protected]b29682ba22009-06-18 19:53:56401
[email protected]ae7fe712009-07-02 20:33:58402 FRIEND_TEST(ExtensionTest, LoadPageActionHelper);
403
[email protected]894bb502009-05-21 22:39:57404 DISALLOW_COPY_AND_ASSIGN(Extension);
[email protected]7713d632008-12-02 07:52:33405};
406
[email protected]5b1a0e22009-05-26 19:00:58407#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_H_