blob: 861cad661b23b22adfb4e344dc7f520f44f4e733 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 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]32b76ef2010-07-26 23:08:247#pragma once
[email protected]7713d632008-12-02 07:52:338
[email protected]300cc58db2009-08-19 20:45:149#include <map>
[email protected]facd7a7652009-06-05 23:15:0210#include <set>
[email protected]7713d632008-12-02 07:52:3311#include <string>
12#include <vector>
13
[email protected]6014d672008-12-05 00:38:2514#include "base/file_path.h"
[email protected]19118d52010-07-26 22:13:4215#include "base/gtest_prod_util.h"
[email protected]3b63f8f42011-03-28 01:54:1516#include "base/memory/ref_counted.h"
17#include "base/memory/scoped_ptr.h"
[email protected]d83a5602010-09-16 00:22:4818#include "chrome/common/extensions/extension_constants.h"
[email protected]867a73e12010-03-19 20:45:4619#include "chrome/common/extensions/extension_extent.h"
[email protected]807871f2010-09-16 01:04:4820#include "chrome/common/extensions/extension_icon_set.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"
[email protected]08397d52011-02-05 01:53:3824#include "ui/gfx/size.h"
[email protected]eab9b452009-01-23 20:48:5925
[email protected]3bb84992010-08-26 17:23:4626class DictionaryValue;
[email protected]942690b132010-05-11 06:42:1427class ExtensionAction;
28class ExtensionResource;
[email protected]e2dffe02011-01-20 20:30:0629class ExtensionSidebarDefaults;
[email protected]12802702010-07-09 19:43:0930class SkBitmap;
[email protected]daf66aa2010-08-06 06:24:2831class Version;
[email protected]942690b132010-05-11 06:42:1432
[email protected]f0755532010-06-22 07:27:2533// Represents a Chrome extension.
[email protected]66e4eb32010-10-27 20:37:4134class Extension : public base::RefCountedThreadSafe<Extension> {
[email protected]7713d632008-12-02 07:52:3335 public:
[email protected]d3cfa482009-10-17 13:54:5736 typedef std::map<const std::string, GURL> URLOverrideMap;
[email protected]10fb1992010-10-08 09:00:1737 typedef std::vector<std::string> ScriptingWhitelist;
[email protected]b24d8312009-08-27 06:47:4638
[email protected]631cf822009-05-15 07:01:2539 // What an extension was loaded from.
[email protected]9b217652010-10-08 22:04:2340 // NOTE: These values are stored as integers in the preferences and used
41 // in histograms so don't remove or reorder existing items. Just append
42 // to the end.
[email protected]631cf822009-05-15 07:01:2543 enum Location {
44 INVALID,
[email protected]25b34332009-06-05 21:53:1945 INTERNAL, // A crx file from the internal Extensions directory.
46 EXTERNAL_PREF, // A crx file from an external directory (via prefs).
47 EXTERNAL_REGISTRY, // A crx file from an external directory (via eg the
48 // registry on Windows).
[email protected]1952c7d2010-03-04 23:48:3449 LOAD, // --load-extension.
[email protected]8ef78fd2010-08-19 17:14:3250 COMPONENT, // An integral component of Chrome itself, which
51 // happens to be implemented as an extension. We don't
52 // show these in the management UI.
[email protected]4d2913e32010-11-30 00:28:5553 EXTERNAL_PREF_DOWNLOAD, // A crx file from an external directory (via
54 // prefs), installed from an update URL.
55 EXTERNAL_POLICY_DOWNLOAD, // A crx file from an external directory (via
56 // admin policies), installed from an update URL.
[email protected]04cb7542010-10-25 10:50:0657
58 NUM_LOCATIONS
[email protected]25b34332009-06-05 21:53:1959 };
60
61 enum State {
[email protected]0c6da502009-08-14 22:32:3962 DISABLED = 0,
[email protected]25b34332009-06-05 21:53:1963 ENABLED,
64 KILLBIT, // Don't install/upgrade (applies to external extensions only).
[email protected]0c6da502009-08-14 22:32:3965
66 NUM_STATES
[email protected]631cf822009-05-15 07:01:2567 };
[email protected]7713d632008-12-02 07:52:3368
[email protected]fbcc40302009-06-12 20:45:4569 enum InstallType {
[email protected]ab6f2b22009-07-28 23:28:3770 INSTALL_ERROR,
[email protected]fbcc40302009-06-12 20:45:4571 DOWNGRADE,
72 REINSTALL,
73 UPGRADE,
74 NEW_INSTALL
75 };
76
[email protected]d2817012009-08-04 06:46:2177 // NOTE: If you change this list, you should also change kIconSizes in the cc
78 // file.
79 enum Icons {
80 EXTENSION_ICON_LARGE = 128,
81 EXTENSION_ICON_MEDIUM = 48,
82 EXTENSION_ICON_SMALL = 32,
[email protected]39382942010-03-23 15:57:0983 EXTENSION_ICON_SMALLISH = 24,
[email protected]d2817012009-08-04 06:46:2184 EXTENSION_ICON_BITTY = 16,
85 };
86
[email protected]7fa19f82010-12-21 19:40:0887 // Do not change the order of entries or remove entries in this list
88 // as this is used in UMA_HISTOGRAM_ENUMERATIONs about extensions.
89 enum Type {
[email protected]9b217652010-10-08 22:04:2390 TYPE_UNKNOWN = 0,
91 TYPE_EXTENSION,
92 TYPE_THEME,
93 TYPE_USER_SCRIPT,
94 TYPE_HOSTED_APP,
95 TYPE_PACKAGED_APP
96 };
97
[email protected]92888082010-10-18 19:24:5798 // An NPAPI plugin included in the extension.
99 struct PluginInfo {
100 FilePath path; // Path to the plugin.
101 bool is_public; // False if only this extension can load this plugin.
102 };
103
[email protected]a4a38c12010-12-23 16:43:56104 struct TtsVoice {
105 std::string voice_name;
106 std::string locale;
107 std::string gender;
108 };
109
[email protected]0df165f2010-09-28 16:49:40110 // A permission is defined by its |name| (what is used in the manifest),
111 // and the |message_id| that's used by install/update UI.
112 struct Permission {
113 const char* const name;
114 const int message_id;
115 };
116
[email protected]83048a22011-03-29 00:14:13117 enum InitFromValueFlags {
118 NO_FLAGS = 0,
119
120 // Usually, the id of an extension is generated by the "key" property of
121 // its manifest, but if |REQUIRE_KEY| is not set, a temporary ID will be
122 // generated based on the path.
123 REQUIRE_KEY = 1 << 0,
124
125 // |STRICT_ERROR_CHECKS| enables extra error checking, such as
126 // checks that URL patterns do not contain ports. This error
127 // checking may find an error that a previous version of
128 // Chrome did not flag. To avoid errors in installed extensions
129 // when Chrome is upgraded, strict error checking is only enabled
130 // when loading extensions as a developer would (such as loading
131 // an unpacked extension), or when loading an extension that is
132 // tied to a specific version of Chrome (such as a component
133 // extension). Most callers will set the |STRICT_ERROR_CHECKS| bit when
134 // Extension::ShouldDoStrictErrorChecking(location) returns true.
135 STRICT_ERROR_CHECKS = 1 << 1,
136 };
137
[email protected]66e4eb32010-10-27 20:37:41138 static scoped_refptr<Extension> Create(const FilePath& path,
139 Location location,
140 const DictionaryValue& value,
[email protected]83048a22011-03-29 00:14:13141 int flags,
[email protected]66e4eb32010-10-27 20:37:41142 std::string* error);
143
[email protected]4d2913e32010-11-30 00:28:55144 // Return the update url used by gallery/webstore extensions.
145 static GURL GalleryUpdateUrl(bool secure);
146
[email protected]0df165f2010-09-28 16:49:40147 // The install message id for |permission|. Returns 0 if none exists.
148 static int GetPermissionMessageId(const std::string& permission);
149
[email protected]a2a098d2010-09-29 19:42:55150 // Returns the full list of permission messages that this extension
151 // should display at install time.
[email protected]9adb9692010-10-29 23:14:02152 std::vector<string16> GetPermissionMessages() const;
[email protected]a2a098d2010-09-29 19:42:55153
[email protected]d6a5c78c2010-12-07 05:18:15154 // Returns the distinct hosts that should be displayed in the install UI
155 // for the URL patterns |list|. This discards some of the detail that is
156 // present in the manifest to make it as easy as possible to process by
157 // users. In particular we disregard the scheme and path components of
158 // URLPatterns and de-dupe the result, which includes filtering out common
[email protected]d7d4a402011-03-08 18:27:51159 // hosts with differing RCDs (aka Registry Controlled Domains, most of which
160 // are Top Level Domains but also include exceptions like co.uk).
161 // NOTE: when de-duping hosts the preferred RCD will be returned, given this
162 // order of preference: .com, .net, .org, first in list.
[email protected]d6a5c78c2010-12-07 05:18:15163 static std::vector<std::string> GetDistinctHostsForDisplay(
164 const URLPatternList& list);
165
166 // Compares two URLPatternLists for security equality by returning whether
167 // the URL patterns in |new_list| contain additional distinct hosts compared
168 // to |old_list|.
169 static bool IsElevatedHostList(
170 const URLPatternList& old_list, const URLPatternList& new_list);
[email protected]0df165f2010-09-28 16:49:40171
[email protected]c3e3def742009-07-17 07:51:06172 // Icon sizes used by the extension system.
[email protected]d2817012009-08-04 06:46:21173 static const int kIconSizes[];
[email protected]c3e3def742009-07-17 07:51:06174
[email protected]4c4f8192009-10-17 01:03:26175 // Max size (both dimensions) for browser and page actions.
176 static const int kPageActionIconMaxSize;
177 static const int kBrowserActionIconMaxSize;
[email protected]e2dffe02011-01-20 20:30:06178 static const int kSidebarIconMaxSize;
[email protected]4c4f8192009-10-17 01:03:26179
[email protected]35506352009-08-07 18:58:19180 // Each permission is a module that the extension is permitted to use.
[email protected]218990c2010-06-26 01:21:07181 //
[email protected]0df165f2010-09-28 16:49:40182 // NOTE: To add a new permission, define it here, and add an entry to
183 // Extension::kPermissions.
[email protected]9dcf8f12010-09-02 20:39:19184 static const char kBackgroundPermission[];
185 static const char kBookmarkPermission[];
[email protected]598bbcc2011-02-24 10:03:25186 static const char kContentSettingsPermission[];
[email protected]9dcf8f12010-09-02 20:39:19187 static const char kContextMenusPermission[];
188 static const char kCookiePermission[];
[email protected]61b55b62011-03-24 09:03:10189 static const char kChromeosInfoPrivatePermissions[];
[email protected]91ba3312011-03-17 20:39:22190 static const char kDebuggerPermission[];
[email protected]9dcf8f12010-09-02 20:39:19191 static const char kExperimentalPermission[];
[email protected]55d9bed2011-03-25 20:37:59192 static const char kFileSystemPermission[];
193 static const char kFileBrowserPrivatePermission[];
[email protected]9dcf8f12010-09-02 20:39:19194 static const char kGeolocationPermission[];
195 static const char kHistoryPermission[];
196 static const char kIdlePermission[];
[email protected]eb255772010-10-04 22:10:46197 static const char kManagementPermission[];
[email protected]9dcf8f12010-09-02 20:39:19198 static const char kNotificationPermission[];
199 static const char kProxyPermission[];
200 static const char kTabPermission[];
201 static const char kUnlimitedStoragePermission[];
[email protected]9dcf8f12010-09-02 20:39:19202 static const char kWebstorePrivatePermission[];
[email protected]aeb53b32009-10-29 07:34:45203
[email protected]0df165f2010-09-28 16:49:40204 static const Permission kPermissions[];
[email protected]35506352009-08-07 18:58:19205 static const size_t kNumPermissions;
[email protected]9dcf8f12010-09-02 20:39:19206 static const char* const kHostedAppPermissionNames[];
[email protected]b746f372010-08-29 21:39:32207 static const size_t kNumHostedAppPermissions;
[email protected]35506352009-08-07 18:58:19208
[email protected]03b612f2010-08-13 21:09:21209 // The old name for the unlimited storage permission, which is deprecated but
210 // still accepted as meaning the same thing as kUnlimitedStoragePermission.
[email protected]9dcf8f12010-09-02 20:39:19211 static const char kOldUnlimitedStoragePermission[];
[email protected]03b612f2010-08-13 21:09:21212
[email protected]8d888c12010-11-30 00:00:25213 // Valid schemes for web extent URLPatterns.
214 static const int kValidWebExtentSchemes;
215
[email protected]f71f7e62010-12-07 03:45:33216 // Valid schemes for host permission URLPatterns.
217 static const int kValidHostPermissionSchemes;
218
[email protected]b746f372010-08-29 21:39:32219 // Returns true if the string is one of the known hosted app permissions (see
220 // kHostedAppPermissionNames).
221 static bool IsHostedAppPermission(const std::string& permission);
222
[email protected]6014d672008-12-05 00:38:25223 // The name of the manifest inside an extension.
[email protected]99efb7b12009-12-18 02:39:16224 static const FilePath::CharType kManifestFilename[];
[email protected]6014d672008-12-05 00:38:25225
[email protected]300cc58db2009-08-19 20:45:14226 // The name of locale folder inside an extension.
[email protected]99efb7b12009-12-18 02:39:16227 static const FilePath::CharType kLocaleFolder[];
[email protected]300cc58db2009-08-19 20:45:14228
229 // The name of the messages file inside an extension.
[email protected]99efb7b12009-12-18 02:39:16230 static const FilePath::CharType kMessagesFilename[];
[email protected]300cc58db2009-08-19 20:45:14231
[email protected]25b34332009-06-05 21:53:19232#if defined(OS_WIN)
[email protected]9dcf8f12010-09-02 20:39:19233 static const char kExtensionRegistryPath[];
[email protected]25b34332009-06-05 21:53:19234#endif
235
[email protected]37eeb5a2009-02-26 23:36:17236 // The number of bytes in a legal id.
[email protected]fe0e7822009-02-26 23:51:48237 static const size_t kIdSize;
[email protected]37eeb5a2009-02-26 23:36:17238
[email protected]e435d6b72009-07-25 03:15:58239 // The mimetype used for extensions.
240 static const char kMimeType[];
241
[email protected]25b34332009-06-05 21:53:19242 // Checks to see if the extension has a valid ID.
243 static bool IdIsValid(const std::string& id);
244
[email protected]4ead6f72010-10-13 19:54:18245 // Generate an ID for an extension in the given path.
[email protected]28d7479b2011-03-09 21:33:27246 // Used while developing extensions, before they have a key.
[email protected]4ead6f72010-10-13 19:54:18247 static std::string GenerateIdForPath(const FilePath& file_name);
248
[email protected]e435d6b72009-07-25 03:15:58249 // Returns true if the specified file is an extension.
250 static bool IsExtension(const FilePath& file_name);
251
[email protected]25b34332009-06-05 21:53:19252 // Whether the |location| is external or not.
253 static inline bool IsExternalLocation(Location location) {
254 return location == Extension::EXTERNAL_PREF ||
[email protected]8ef78fd2010-08-19 17:14:32255 location == Extension::EXTERNAL_REGISTRY ||
[email protected]04cb7542010-10-25 10:50:06256 location == Extension::EXTERNAL_PREF_DOWNLOAD ||
257 location == Extension::EXTERNAL_POLICY_DOWNLOAD;
258 }
259
260 // Whether extensions with |location| are auto-updatable or not.
261 static inline bool IsAutoUpdateableLocation(Location location) {
262 // Only internal and external extensions can be autoupdated.
263 return location == Extension::INTERNAL ||
264 IsExternalLocation(location);
[email protected]25b34332009-06-05 21:53:19265 }
266
[email protected]542258c2011-03-04 21:25:31267 // Whether extensions with |location| should be loaded with strict
268 // error checking. Strict error checks may flag errors older versions
269 // of chrome did not detect. To avoid breaking installed extensions,
270 // strict checks are disabled unless the location indicates that the
271 // developer is loading the extension, or the extension is a component
272 // of chrome.
273 static inline bool ShouldDoStrictErrorChecking(Location location) {
274 return location == Extension::LOAD ||
275 location == Extension::COMPONENT;
276 }
277
[email protected]7fa19f82010-12-21 19:40:08278 // See Type definition above.
279 Type GetType() const;
[email protected]9b217652010-10-08 22:04:23280
[email protected]07c00d992009-03-04 20:27:04281 // Returns an absolute url to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:59282 // |extension_url| argument should be the url() from an Extension object. The
283 // |relative_path| can be untrusted user input. The returned URL will either
284 // be invalid() or a child of |extension_url|.
285 // NOTE: Static so that it can be used from multiple threads.
286 static GURL GetResourceURL(const GURL& extension_url,
287 const std::string& relative_path);
[email protected]cffd7892010-08-26 17:43:28288 GURL GetResourceURL(const std::string& relative_path) const {
[email protected]3cfbd0e2009-03-18 21:26:24289 return GetResourceURL(url(), relative_path);
290 }
[email protected]eab9b452009-01-23 20:48:59291
[email protected]99efb7b12009-12-18 02:39:16292 // Returns an extension resource object. |relative_path| should be UTF8
293 // encoded.
[email protected]9adb9692010-10-29 23:14:02294 ExtensionResource GetResource(const std::string& relative_path) const;
[email protected]99efb7b12009-12-18 02:39:16295
296 // As above, but with |relative_path| following the file system's encoding.
[email protected]9adb9692010-10-29 23:14:02297 ExtensionResource GetResource(const FilePath& relative_path) const;
[email protected]eab9b452009-01-23 20:48:59298
[email protected]a17f9462009-06-09 02:56:41299 // |input| is expected to be the text of an rsa public or private key. It
300 // tolerates the presence or absence of bracking header/footer like this:
301 // -----(BEGIN|END) [RSA PUBLIC/PRIVATE] KEY-----
302 // and may contain newlines.
303 static bool ParsePEMKeyBytes(const std::string& input, std::string* output);
304
305 // Does a simple base64 encoding of |input| into |output|.
306 static bool ProducePEM(const std::string& input, std::string* output);
307
[email protected]84ac7f32009-10-06 06:17:54308 // Generates an extension ID from arbitrary input. The same input string will
309 // always generate the same output ID.
310 static bool GenerateId(const std::string& input, std::string* output);
[email protected]fbcc40302009-06-12 20:45:45311
[email protected]a17f9462009-06-09 02:56:41312 // Expects base64 encoded |input| and formats into |output| including
313 // the appropriate header & footer.
[email protected]e0d08192011-03-29 19:02:50314 static bool FormatPEMForFileOutput(const std::string& input,
315 std::string* output,
316 bool is_public);
[email protected]a17f9462009-06-09 02:56:41317
[email protected]2a409532009-08-28 19:39:44318 // Determine whether |new_extension| has increased privileges compared to
[email protected]8d888c12010-11-30 00:00:25319 // its previously granted permissions, specified by |granted_apis|,
320 // |granted_extent| and |granted_full_access|.
321 static bool IsPrivilegeIncrease(const bool granted_full_access,
322 const std::set<std::string>& granted_apis,
323 const ExtensionExtent& granted_extent,
[email protected]9adb9692010-10-29 23:14:02324 const Extension* new_extension);
[email protected]b24d8312009-08-27 06:47:46325
[email protected]c690a9812009-12-17 05:55:32326 // Given an extension and icon size, read it if present and decode it into
[email protected]ae2e0f92010-04-06 20:32:23327 // result. In the browser process, this will DCHECK if not called on the
328 // file thread. To easily load extension images on the UI thread, see
329 // ImageLoadingTracker.
[email protected]9adb9692010-10-29 23:14:02330 static void DecodeIcon(const Extension* extension,
[email protected]c690a9812009-12-17 05:55:32331 Icons icon_size,
332 scoped_ptr<SkBitmap>* result);
333
334 // Given an icon_path and icon size, read it if present and decode it into
[email protected]ae2e0f92010-04-06 20:32:23335 // result. In the browser process, this will DCHECK if not called on the
336 // file thread. To easily load extension images on the UI thread, see
337 // ImageLoadingTracker.
[email protected]c690a9812009-12-17 05:55:32338 static void DecodeIconFromPath(const FilePath& icon_path,
339 Icons icon_size,
340 scoped_ptr<SkBitmap>* result);
341
[email protected]a807bbe2010-04-14 10:51:19342 // Returns the base extension url for a given |extension_id|.
343 static GURL GetBaseURLFromExtensionId(const std::string& extension_id);
344
[email protected]ec5b50d2010-10-09 16:35:18345 // Returns the url prefix for the extension/apps gallery. Can be set via the
[email protected]ce3eb7b62010-09-14 13:57:44346 // --apps-gallery-url switch. The URL returned will not contain a trailing
[email protected]d3071992010-10-08 15:24:07347 // slash. Do not use this as a prefix/extent for the store. Instead see
[email protected]eaa7dd182010-12-14 11:09:00348 // ExtensionService::GetWebStoreApp or
349 // ExtensionService::IsDownloadFromGallery
[email protected]d3071992010-10-08 15:24:07350 static std::string ChromeStoreLaunchURL();
[email protected]a0cf04a2010-06-23 03:29:55351
[email protected]be7e5cb2010-10-04 12:53:17352 // Adds an extension to the scripting whitelist. Used for testing only.
[email protected]10fb1992010-10-08 09:00:17353 static void SetScriptingWhitelist(const ScriptingWhitelist& whitelist);
[email protected]2a521c52011-01-26 18:45:21354 static const ScriptingWhitelist* GetScriptingWhitelist();
[email protected]be7e5cb2010-10-04 12:53:17355
[email protected]aeb53b32009-10-29 07:34:45356 // Returns true if the extension has the specified API permission.
[email protected]0df165f2010-09-28 16:49:40357 static bool HasApiPermission(const std::set<std::string>& api_permissions,
[email protected]246c05f2010-09-10 09:12:11358 const std::string& function_name);
[email protected]583d45c12010-08-31 02:48:12359
[email protected]8d888c12010-11-30 00:00:25360 // Whether the |effective_host_permissions| and |api_permissions| include
361 // effective access to all hosts. See the non-static version of the method
362 // for more details.
363 static bool HasEffectiveAccessToAllHosts(
364 const ExtensionExtent& effective_host_permissions,
365 const std::set<std::string>& api_permissions);
366
[email protected]246c05f2010-09-10 09:12:11367 bool HasApiPermission(const std::string& function_name) const {
368 return HasApiPermission(this->api_permissions(), function_name);
[email protected]aeb53b32009-10-29 07:34:45369 }
370
[email protected]4361c7c2010-09-30 21:57:53371 const ExtensionExtent& GetEffectiveHostPermissions() const {
[email protected]6f229e82010-11-02 17:47:26372 return effective_host_permissions_;
[email protected]4361c7c2010-09-30 21:57:53373 }
[email protected]b24d8312009-08-27 06:47:46374
[email protected]fbd17cf2010-04-28 23:52:56375 // Whether or not the extension is allowed permission for a URL pattern from
376 // the manifest. http, https, and chrome://favicon/ is allowed for all
377 // extensions, while component extensions are allowed access to
378 // chrome://resources.
[email protected]a3caa822010-10-26 18:10:00379 bool CanSpecifyHostPermission(const URLPattern& pattern) const;
[email protected]fbd17cf2010-04-28 23:52:56380
[email protected]584b8e3f2010-04-10 00:23:37381 // Whether the extension has access to the given URL.
382 bool HasHostPermission(const GURL& url) const;
383
[email protected]0df165f2010-09-28 16:49:40384 // Whether the extension has effective access to all hosts. This is true if
385 // there is a content script that matches all hosts, if there is a host
386 // permission grants access to all hosts (like <all_urls>) or an api
387 // permission that effectively grants access to all hosts (e.g. proxy,
388 // network, etc.)
389 bool HasEffectiveAccessToAllHosts() const;
[email protected]b24d8312009-08-27 06:47:46390
[email protected]8d888c12010-11-30 00:00:25391 // Whether the extension effectively has all permissions (for example, by
392 // having an NPAPI plugin).
393 bool HasFullPermissions() const;
394
[email protected]5df6a5d2011-01-26 07:39:12395 // Whether context menu should be shown for page and browser actions.
396 bool ShowConfigureContextMenus() const;
397
[email protected]37cd64d2010-10-25 18:17:58398 // Returns the Homepage URL for this extension. If homepage_url was not
399 // specified in the manifest, this returns the Google Gallery URL. For
[email protected]bfa90a3a2010-04-28 15:43:23400 // third-party extensions, this returns a blank GURL.
[email protected]37cd64d2010-10-25 18:17:58401 GURL GetHomepageURL() const;
[email protected]bfa90a3a2010-04-28 15:43:23402
[email protected]facd7a7652009-06-05 23:15:02403 // Returns a list of paths (relative to the extension dir) for images that
404 // the browser might load (like themes and page action icons).
[email protected]9adb9692010-10-29 23:14:02405 std::set<FilePath> GetBrowserImages() const;
[email protected]facd7a7652009-06-05 23:15:02406
[email protected]807871f2010-09-16 01:04:48407 // Get an extension icon as a resource or URL.
[email protected]9adb9692010-10-29 23:14:02408 ExtensionResource GetIconResource(
409 int size, ExtensionIconSet::MatchType match_type) const;
410 GURL GetIconURL(int size, ExtensionIconSet::MatchType match_type) const;
[email protected]f34e79632010-03-17 02:34:08411
[email protected]867a73e12010-03-19 20:45:46412 // Gets the fully resolved absolute launch URL.
413 GURL GetFullLaunchURL() const;
[email protected]2a521c52011-01-26 18:45:21414
[email protected]6f229e82010-11-02 17:47:26415 // Image cache related methods. These are only valid on the UI thread and
416 // not maintained by this class. See ImageLoadingTracker for usage. The
417 // |original_size| parameter should be the size of the image at |source|
418 // before any scaling may have been done to produce the pixels in |image|.
419 void SetCachedImage(const ExtensionResource& source,
420 const SkBitmap& image,
421 const gfx::Size& original_size) const;
422 bool HasCachedImage(const ExtensionResource& source,
423 const gfx::Size& max_size) const;
424 SkBitmap GetCachedImage(const ExtensionResource& source,
425 const gfx::Size& max_size) const;
[email protected]2a521c52011-01-26 18:45:21426
427 // Returns true if this extension can execute script on a page. If a
428 // UserScript object is passed, permission to run that specific script is
429 // checked (using its matches list). Otherwise, permission to execute script
430 // programmatically is checked (using the extension's host permission).
431 //
432 // This method is also aware of certain special pages that extensions are
433 // usually not allowed to run script on.
434 bool CanExecuteScriptOnPage(const GURL& page_url,
435 UserScript* script,
436 std::string* error) const;
437
[email protected]6f229e82010-11-02 17:47:26438 // Returns true if this extension is a COMPONENT extension, or if it is
439 // on the whitelist of extensions that can script all pages.
440 bool CanExecuteScriptEverywhere() const;
441
[email protected]5efbfe012011-02-22 23:07:18442 // Returns true if this extension is allowed to obtain the contents of a
443 // page as an image. Since a page may contain sensitive information, this
444 // is restricted to the extension's host permissions as well as the
445 // extension page itself.
446 bool CanCaptureVisiblePage(const GURL& page_url, std::string* error) const;
447
[email protected]a65882c2010-11-12 15:15:09448 // Returns true if this extension updates itself using the extension
449 // gallery.
450 bool UpdatesFromGallery() const;
451
[email protected]cca147172011-02-17 01:29:29452 // Returns true if this extension or app includes areas within |origin|.
453 bool OverlapsWithOrigin(const GURL& origin) const;
454
[email protected]6f229e82010-11-02 17:47:26455 // Accessors:
456
457 const FilePath& path() const { return path_; }
458 const GURL& url() const { return extension_url_; }
459 Location location() const { return location_; }
460 const std::string& id() const { return id_; }
461 const Version* version() const { return version_.get(); }
462 const std::string VersionString() const;
463 const std::string& name() const { return name_; }
464 const std::string& public_key() const { return public_key_; }
465 const std::string& description() const { return description_; }
466 bool converted_from_user_script() const {
467 return converted_from_user_script_;
468 }
469 const UserScriptList& content_scripts() const { return content_scripts_; }
470 ExtensionAction* page_action() const { return page_action_.get(); }
471 ExtensionAction* browser_action() const { return browser_action_.get(); }
[email protected]e2dffe02011-01-20 20:30:06472 ExtensionSidebarDefaults* sidebar_defaults() const {
473 return sidebar_defaults_.get();
474 }
[email protected]6f229e82010-11-02 17:47:26475 const std::vector<PluginInfo>& plugins() const { return plugins_; }
476 const GURL& background_url() const { return background_url_; }
477 const GURL& options_url() const { return options_url_; }
478 const GURL& devtools_url() const { return devtools_url_; }
479 const std::vector<GURL>& toolstrips() const { return toolstrips_; }
480 const std::set<std::string>& api_permissions() const {
481 return api_permissions_;
482 }
483 const URLPatternList& host_permissions() const { return host_permissions_; }
484 const GURL& update_url() const { return update_url_; }
485 const ExtensionIconSet& icons() const { return icons_; }
486 const DictionaryValue* manifest_value() const {
487 return manifest_value_.get();
488 }
489 const std::string default_locale() const { return default_locale_; }
490 const URLOverrideMap& GetChromeURLOverrides() const {
491 return chrome_url_overrides_;
492 }
493 const std::string omnibox_keyword() const { return omnibox_keyword_; }
494 bool incognito_split_mode() const { return incognito_split_mode_; }
[email protected]a4a38c12010-12-23 16:43:56495 const std::vector<TtsVoice>& tts_voices() const { return tts_voices_; }
[email protected]6f229e82010-11-02 17:47:26496
497 // App-related.
498 bool is_app() const { return is_app_; }
499 bool is_hosted_app() const { return is_app() && !web_extent().is_empty(); }
500 bool is_packaged_app() const { return is_app() && web_extent().is_empty(); }
[email protected]d9696672011-03-15 22:45:09501 bool is_storage_isolated() const { return is_app() && is_storage_isolated_; }
[email protected]6f229e82010-11-02 17:47:26502 const ExtensionExtent& web_extent() const { return extent_; }
503 const std::string& launch_local_path() const { return launch_local_path_; }
504 const std::string& launch_web_url() const { return launch_web_url_; }
505 extension_misc::LaunchContainer launch_container() const {
506 return launch_container_;
507 }
508 int launch_width() const { return launch_width_; }
509 int launch_height() const { return launch_height_; }
510
511 // Theme-related.
512 bool is_theme() const { return is_theme_; }
513 DictionaryValue* GetThemeImages() const { return theme_images_.get(); }
514 DictionaryValue* GetThemeColors() const {return theme_colors_.get(); }
515 DictionaryValue* GetThemeTints() const { return theme_tints_.get(); }
516 DictionaryValue* GetThemeDisplayProperties() const {
517 return theme_display_properties_.get();
518 }
519
[email protected]4a8d3272009-03-10 19:15:08520 private:
[email protected]66e4eb32010-10-27 20:37:41521 friend class base::RefCountedThreadSafe<Extension>;
522
[email protected]d7e9a862010-11-03 21:57:49523 // We keep a cache of images loaded from extension resources based on their
524 // path and a string representation of a size that may have been used to
525 // scale it (or the empty string if the image is at its original size).
526 typedef std::pair<FilePath, std::string> ImageCacheKey;
527 typedef std::map<ImageCacheKey, SkBitmap> ImageCache;
528
[email protected]4ead6f72010-10-13 19:54:18529 // Normalize the path for use by the extension. On Windows, this will make
530 // sure the drive letter is uppercase.
531 static FilePath MaybeNormalizePath(const FilePath& path);
532
[email protected]d6a5c78c2010-12-07 05:18:15533 // Returns the distinct hosts that can be displayed in the install UI or be
534 // used for privilege comparisons. This discards some of the detail that is
535 // present in the manifest to make it as easy as possible to process by users.
536 // In particular we disregard the scheme and path components of URLPatterns
537 // and de-dupe the result, which includes filtering out common hosts with
538 // differing RCDs. If |include_rcd| is true, then the de-duped result
539 // will be the first full entry, including its RCD. So if the list was
540 // "*.google.co.uk" and "*.google.com", the returned value would just be
541 // "*.google.co.uk". Keeping the RCD in the result is useful for display
542 // purposes when you want to show the user one sample hostname from the list.
543 // If you need to compare two URLPatternLists for security equality, then set
544 // |include_rcd| to false, which will return a result like "*.google.",
545 // regardless of the order of the patterns.
546 static std::vector<std::string> GetDistinctHosts(
547 const URLPatternList& host_patterns, bool include_rcd);
548
[email protected]66e4eb32010-10-27 20:37:41549 Extension(const FilePath& path, Location location);
550 ~Extension();
551
552 // Initialize the extension from a parsed manifest.
[email protected]83048a22011-03-29 00:14:13553 bool InitFromValue(const DictionaryValue& value, int flags,
554 std::string* error);
[email protected]66e4eb32010-10-27 20:37:41555
[email protected]052c92702010-06-25 07:25:52556 // Helper function for implementing HasCachedImage/GetCachedImage. A return
557 // value of NULL means there is no matching image cached (we allow caching an
558 // empty SkBitmap).
559 SkBitmap* GetCachedImageImpl(const ExtensionResource& source,
[email protected]9adb9692010-10-29 23:14:02560 const gfx::Size& max_size) const;
[email protected]d9ad80f2010-03-30 20:40:18561
[email protected]3cfbd0e2009-03-18 21:26:24562 // Helper method that loads a UserScript object from a
563 // dictionary in the content_script list of the manifest.
564 bool LoadUserScriptHelper(const DictionaryValue* content_script,
565 int definition_index,
[email protected]542258c2011-03-04 21:25:31566 URLPattern::ParseOption parse_strictness,
[email protected]3cfbd0e2009-03-18 21:26:24567 std::string* error,
568 UserScript* result);
[email protected]f7f3a5f2009-05-01 22:02:34569
[email protected]6657afa62009-11-04 02:15:20570 // Helper method that loads either the include_globs or exclude_globs list
571 // from an entry in the content_script lists of the manifest.
572 bool LoadGlobsHelper(const DictionaryValue* content_script,
573 int content_script_index,
[email protected]e2194742010-08-12 05:54:34574 const char* globs_property_name,
[email protected]6657afa62009-11-04 02:15:20575 std::string* error,
[email protected]11f4857282009-11-13 19:56:17576 void(UserScript::*add_method)(const std::string& glob),
[email protected]6657afa62009-11-04 02:15:20577 UserScript *instance);
578
[email protected]867a73e12010-03-19 20:45:46579 // Helpers to load various chunks of the manifest.
[email protected]3ba0fd32010-06-19 05:39:10580 bool LoadIsApp(const DictionaryValue* manifest, std::string* error);
[email protected]542258c2011-03-04 21:25:31581 bool LoadExtent(const DictionaryValue* manifest,
582 const char* key,
583 ExtensionExtent* extent,
584 const char* list_error,
585 const char* value_error,
586 URLPattern::ParseOption parse_strictness,
587 std::string* error);
[email protected]867a73e12010-03-19 20:45:46588 bool LoadLaunchContainer(const DictionaryValue* manifest, std::string* error);
589 bool LoadLaunchURL(const DictionaryValue* manifest, std::string* error);
[email protected]d9696672011-03-15 22:45:09590 bool LoadAppIsolation(const DictionaryValue* manifest, std::string* error);
[email protected]cbf4d1912010-08-12 18:24:57591 bool EnsureNotHybridApp(const DictionaryValue* manifest, std::string* error);
[email protected]867a73e12010-03-19 20:45:46592
[email protected]5d246db22009-10-27 06:17:57593 // Helper method to load an ExtensionAction from the page_action or
[email protected]92c6f9b92009-10-24 04:35:08594 // browser_action entries in the manifest.
[email protected]5d246db22009-10-27 06:17:57595 ExtensionAction* LoadExtensionActionHelper(
[email protected]92c6f9b92009-10-24 04:35:08596 const DictionaryValue* extension_action, std::string* error);
597
[email protected]e2dffe02011-01-20 20:30:06598 // Helper method to load an ExtensionSidebarDefaults from the sidebar manifest
599 // entry.
600 ExtensionSidebarDefaults* LoadExtensionSidebarDefaults(
601 const DictionaryValue* sidebar, std::string* error);
602
[email protected]4361c7c2010-09-30 21:57:53603 // Calculates the effective host permissions from the permissions and content
604 // script petterns.
605 void InitEffectiveHostPermissions();
606
[email protected]2f6698b2010-10-14 00:58:21607 // Returns true if the extension has more than one "UI surface". For example,
608 // an extension that has a browser action and a page action.
609 bool HasMultipleUISurfaces() const;
610
[email protected]e2eb43112009-05-29 21:19:54611 // Figures out if a source contains keys not associated with themes - we
612 // don't want to allow scripts and such to be bundled with themes.
[email protected]9adb9692010-10-29 23:14:02613 bool ContainsNonThemeKeys(const DictionaryValue& source) const;
[email protected]e2eb43112009-05-29 21:19:54614
[email protected]1952c7d2010-03-04 23:48:34615 // Returns true if the string is one of the known api permissions (see
[email protected]0df165f2010-09-28 16:49:40616 // kPermissions).
[email protected]9adb9692010-10-29 23:14:02617 bool IsAPIPermission(const std::string& permission) const;
[email protected]1952c7d2010-03-04 23:48:34618
[email protected]a2a098d2010-09-29 19:42:55619 // The set of unique API install messages that the extension has.
620 // NOTE: This only includes messages related to permissions declared in the
621 // "permissions" key in the manifest. Permissions implied from other features
622 // of the manifest, like plugins and content scripts are not included.
[email protected]9adb9692010-10-29 23:14:02623 std::set<string16> GetSimplePermissionMessages() const;
[email protected]a2a098d2010-09-29 19:42:55624
625 // The permission message displayed related to the host permissions for
626 // this extension.
[email protected]9adb9692010-10-29 23:14:02627 string16 GetHostPermissionMessage() const;
[email protected]a2a098d2010-09-29 19:42:55628
[email protected]d7e9a862010-11-03 21:57:49629 // Cached images for this extension. This should only be touched on the UI
630 // thread.
631 mutable ImageCache image_cache_;
[email protected]1e8c93f2010-02-08 22:58:31632
[email protected]6f229e82010-11-02 17:47:26633 // A persistent, globally unique ID. An extension's ID is used in things
634 // like directory structures and URLs, and is expected to not change across
635 // versions. It is generated as a SHA-256 hash of the extension's public
636 // key, or as a hash of the path in the case of unpacked extensions.
637 std::string id_;
638
639 // The extension's human-readable name. Name is used for display purpose. It
640 // might be wrapped with unicode bidi control characters so that it is
641 // displayed correctly in RTL context.
642 // NOTE: Name is UTF-8 and may contain non-ascii characters.
643 std::string name_;
644
645 // The absolute path to the directory the extension is stored in.
646 FilePath path_;
647
648 // Default locale for fall back. Can be empty if extension is not localized.
649 std::string default_locale_;
650
651 // If true, a separate process will be used for the extension in incognito
652 // mode.
653 bool incognito_split_mode_;
654
655 // Defines the set of URLs in the extension's web content.
656 ExtensionExtent extent_;
657
658 // The set of host permissions that the extension effectively has access to,
659 // which is a merge of host_permissions_ and all of the match patterns in
660 // any content scripts the extension has. This is used to determine which
661 // URLs have the ability to load an extension's resources via embedded
662 // chrome-extension: URLs (see extension_protocols.cc).
663 ExtensionExtent effective_host_permissions_;
664
665 // The set of module-level APIs this extension can use.
666 std::set<std::string> api_permissions_;
667
668 // The icons for the extension.
669 ExtensionIconSet icons_;
670
671 // The base extension url for the extension.
672 GURL extension_url_;
673
674 // The location the extension was loaded from.
675 Location location_;
676
677 // The extension's version.
678 scoped_ptr<Version> version_;
679
680 // An optional longer description of the extension.
681 std::string description_;
682
683 // True if the extension was generated from a user script. (We show slightly
684 // different UI if so).
685 bool converted_from_user_script_;
686
687 // Paths to the content scripts the extension contains.
688 UserScriptList content_scripts_;
689
690 // The extension's page action, if any.
691 scoped_ptr<ExtensionAction> page_action_;
692
693 // The extension's browser action, if any.
694 scoped_ptr<ExtensionAction> browser_action_;
695
[email protected]e2dffe02011-01-20 20:30:06696 // The extension's sidebar, if any.
697 scoped_ptr<ExtensionSidebarDefaults> sidebar_defaults_;
698
[email protected]6f229e82010-11-02 17:47:26699 // Optional list of NPAPI plugins and associated properties.
700 std::vector<PluginInfo> plugins_;
701
702 // Optional URL to a master page of which a single instance should be always
703 // loaded in the background.
704 GURL background_url_;
705
706 // Optional URL to a page for setting options/preferences.
707 GURL options_url_;
708
709 // Optional URL to a devtools extension page.
710 GURL devtools_url_;
711
712 // Optional list of toolstrips and associated properties.
713 std::vector<GURL> toolstrips_;
714
715 // The public key used to sign the contents of the crx package.
716 std::string public_key_;
717
718 // A map of resource id's to relative file paths.
719 scoped_ptr<DictionaryValue> theme_images_;
720
721 // A map of color names to colors.
722 scoped_ptr<DictionaryValue> theme_colors_;
723
724 // A map of color names to colors.
725 scoped_ptr<DictionaryValue> theme_tints_;
726
727 // A map of display properties.
728 scoped_ptr<DictionaryValue> theme_display_properties_;
729
730 // Whether the extension is a theme.
731 bool is_theme_;
732
733 // The sites this extension has permission to talk to (using XHR, etc).
734 URLPatternList host_permissions_;
735
736 // The homepage for this extension. Useful if it is not hosted by Google and
737 // therefore does not have a Gallery URL.
738 GURL homepage_url_;
739
740 // URL for fetching an update manifest
741 GURL update_url_;
742
743 // A copy of the manifest that this extension was created from.
744 scoped_ptr<DictionaryValue> manifest_value_;
745
746 // A map of chrome:// hostnames (newtab, downloads, etc.) to Extension URLs
747 // which override the handling of those URLs. (see ExtensionOverrideUI).
748 URLOverrideMap chrome_url_overrides_;
749
750 // Whether this extension uses app features.
751 bool is_app_;
752
[email protected]d9696672011-03-15 22:45:09753 // Whether this extension requests isolated storage.
754 bool is_storage_isolated_;
755
[email protected]6f229e82010-11-02 17:47:26756 // The local path inside the extension to use with the launcher.
757 std::string launch_local_path_;
758
759 // A web url to use with the launcher. Note that this might be relative or
760 // absolute. If relative, it is relative to web_origin.
761 std::string launch_web_url_;
762
[email protected]4e595682011-02-09 17:07:02763 // The window type that an app's manifest specifies to launch into.
764 // This is not always the window type an app will open into, because
765 // users can override the way each app launches. See
766 // ExtensionPrefs::GetLaunchContainer(), which looks at a per-app pref
767 // to decide what container an app will launch in.
[email protected]6f229e82010-11-02 17:47:26768 extension_misc::LaunchContainer launch_container_;
769
770 // The default size of the container when launching. Only respected for
771 // containers like panels and windows.
772 int launch_width_;
773 int launch_height_;
774
775 // The Omnibox keyword for this extension, or empty if there is none.
776 std::string omnibox_keyword_;
777
[email protected]a4a38c12010-12-23 16:43:56778 // List of text-to-speech voices that this extension provides, if any.
779 std::vector<TtsVoice> tts_voices_;
780
[email protected]eaa7dd182010-12-14 11:09:00781 FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
[email protected]5eb375e92010-11-26 07:50:41782 UpdateExtensionPreservesLocation);
[email protected]19118d52010-07-26 22:13:42783 FRIEND_TEST_ALL_PREFIXES(ExtensionTest, LoadPageActionHelper);
[email protected]66e4eb32010-10-27 20:37:41784 FRIEND_TEST_ALL_PREFIXES(ExtensionTest, InitFromValueInvalid);
785 FRIEND_TEST_ALL_PREFIXES(ExtensionTest, InitFromValueValid);
786 FRIEND_TEST_ALL_PREFIXES(ExtensionTest, InitFromValueValidNameInRTL);
[email protected]19118d52010-07-26 22:13:42787 FRIEND_TEST_ALL_PREFIXES(TabStripModelTest, Apps);
[email protected]ae7fe712009-07-02 20:33:58788
[email protected]894bb502009-05-21 22:39:57789 DISALLOW_COPY_AND_ASSIGN(Extension);
[email protected]7713d632008-12-02 07:52:33790};
791
[email protected]9adb9692010-10-29 23:14:02792typedef std::vector< scoped_refptr<const Extension> > ExtensionList;
[email protected]ec5b50d2010-10-09 16:35:18793typedef std::set<std::string> ExtensionIdSet;
[email protected]b1748b1d82009-11-30 20:32:56794
[email protected]c6d474f82009-12-16 21:11:06795// Handy struct to pass core extension info around.
796struct ExtensionInfo {
797 ExtensionInfo(const DictionaryValue* manifest,
798 const std::string& id,
799 const FilePath& path,
[email protected]3bb84992010-08-26 17:23:46800 Extension::Location location);
801 ~ExtensionInfo();
[email protected]c6d474f82009-12-16 21:11:06802
803 scoped_ptr<DictionaryValue> extension_manifest;
804 std::string extension_id;
805 FilePath extension_path;
806 Extension::Location extension_location;
807
808 private:
809 DISALLOW_COPY_AND_ASSIGN(ExtensionInfo);
810};
811
[email protected]211030342010-09-30 18:41:06812// Struct used for the details of the EXTENSION_UNINSTALLED
813// notification.
814struct UninstalledExtensionInfo {
815 explicit UninstalledExtensionInfo(const Extension& extension);
[email protected]9b2331d92010-10-04 23:11:19816 ~UninstalledExtensionInfo();
[email protected]211030342010-09-30 18:41:06817
818 std::string extension_id;
819 std::set<std::string> extension_api_permissions;
[email protected]7fa19f82010-12-21 19:40:08820 Extension::Type extension_type;
[email protected]76c25112010-10-01 22:37:21821 GURL update_url;
[email protected]211030342010-09-30 18:41:06822};
823
[email protected]a9f39a312010-12-23 22:14:27824struct UnloadedExtensionInfo {
825 enum Reason {
826 DISABLE, // The extension is being disabled.
827 UPDATE, // The extension is being updated to a newer version.
828 UNINSTALL, // The extension is being uninstalled.
829 };
830
831 Reason reason;
832
833 // Was the extension already disabled?
834 bool already_disabled;
835
836 // The extension being unloaded - this should always be non-NULL.
837 const Extension* extension;
838
839 UnloadedExtensionInfo(const Extension* extension, Reason reason);
840};
841
[email protected]5b1a0e22009-05-26 19:00:58842#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_H_