blob: 10b143023e91713896440f3ee62abf76bd1a9949 [file] [log] [blame]
[email protected]7eeab9ec2013-01-15 04:08:331// Copyright (c) 2013 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]8629c542012-04-20 03:40:038#include <algorithm>
[email protected]18049dc2012-06-19 23:12:559#include <iosfwd>
[email protected]300cc58db2009-08-19 20:45:1410#include <map>
[email protected]facd7a7652009-06-05 23:15:0211#include <set>
[email protected]7713d632008-12-02 07:52:3312#include <string>
[email protected]58f62cf2012-03-09 10:45:1113#include <utility>
[email protected]7713d632008-12-02 07:52:3314#include <vector>
15
[email protected]57999812013-02-24 05:40:5216#include "base/files/file_path.h"
[email protected]8f270be2011-12-21 21:15:2217#include "base/hash_tables.h"
[email protected]b6b805e92011-04-16 09:24:1418#include "base/memory/linked_ptr.h"
[email protected]3b63f8f42011-03-28 01:54:1519#include "base/memory/ref_counted.h"
20#include "base/memory/scoped_ptr.h"
[email protected]902fd7b2011-07-27 18:42:3121#include "base/synchronization/lock.h"
[email protected]7eeab9ec2013-01-15 04:08:3322#include "base/threading/thread_checker.h"
[email protected]d83a5602010-09-16 00:22:4823#include "chrome/common/extensions/extension_constants.h"
[email protected]1d5e58b2013-01-31 08:41:4024#include "chrome/common/extensions/manifest.h"
[email protected]bebe1d02012-08-02 20:17:0925#include "chrome/common/extensions/permissions/api_permission.h"
26#include "chrome/common/extensions/permissions/permission_message.h"
[email protected]885c0e92012-11-13 20:27:4227#include "chrome/common/extensions/user_script.h"
[email protected]993da5e2013-03-23 21:25:1628#include "extensions/common/extension_resource.h"
[email protected]1d5e58b2013-01-31 08:41:4029#include "extensions/common/install_warning.h"
[email protected]885c0e92012-11-13 20:27:4230#include "extensions/common/url_pattern.h"
[email protected]e9f541a2012-11-19 21:52:3131#include "extensions/common/url_pattern_set.h"
[email protected]eab9b452009-01-23 20:48:5932#include "googleurl/src/gurl.h"
[email protected]f5532472012-02-23 13:00:5533#include "ui/base/accelerators/accelerator.h"
[email protected]08397d52011-02-05 01:53:3834#include "ui/gfx/size.h"
[email protected]eab9b452009-01-23 20:48:5935
[email protected]0f34d9082012-10-08 19:16:4436class ExtensionAction;
[email protected]12802702010-07-09 19:43:0937class SkBitmap;
[email protected]942690b132010-05-11 06:42:1438
[email protected]f3a1c642011-07-12 19:15:0339namespace base {
40class DictionaryValue;
41class ListValue;
[email protected]1f04ef42013-04-22 07:35:5042class Version;
[email protected]f3a1c642011-07-12 19:15:0343}
44
[email protected]0f34d9082012-10-08 19:16:4445namespace gfx {
46class ImageSkia;
47}
48
[email protected]1c321ee2012-05-21 03:02:3449namespace extensions {
[email protected]465594632013-02-15 19:50:2850class APIPermissionSet;
[email protected]bebe1d02012-08-02 20:17:0951class PermissionSet;
[email protected]1c321ee2012-05-21 03:02:3452
[email protected]f0755532010-06-22 07:27:2553// Represents a Chrome extension.
[email protected]5cd56342013-04-03 19:50:4754// Once created, an Extension object is immutable, with the exception of its
55// RuntimeData. This makes it safe to use on any thread, since access to the
56// RuntimeData is protected by a lock.
[email protected]66e4eb32010-10-27 20:37:4157class Extension : public base::RefCountedThreadSafe<Extension> {
[email protected]7713d632008-12-02 07:52:3358 public:
[email protected]d356c982012-12-12 19:32:5559 struct ManifestData;
[email protected]1e0f45a2012-06-13 00:31:0660
[email protected]10fb1992010-10-08 09:00:1761 typedef std::vector<std::string> ScriptingWhitelist;
[email protected]d356c982012-12-12 19:32:5562 typedef std::map<const std::string, linked_ptr<ManifestData> >
63 ManifestDataMap;
[email protected]b24d8312009-08-27 06:47:4664
[email protected]25b34332009-06-05 21:53:1965 enum State {
[email protected]0c6da502009-08-14 22:32:3966 DISABLED = 0,
[email protected]25b34332009-06-05 21:53:1967 ENABLED,
[email protected]79c833b52011-04-05 18:31:0168 // An external extension that the user uninstalled. We should not reinstall
69 // such extensions on startup.
70 EXTERNAL_EXTENSION_UNINSTALLED,
[email protected]8c484b742012-11-29 06:05:3671 // Special state for component extensions, since they are always loaded by
72 // the component loader, and should never be auto-installed on startup.
73 ENABLED_COMPONENT,
[email protected]0c6da502009-08-14 22:32:3974 NUM_STATES
[email protected]631cf822009-05-15 07:01:2575 };
[email protected]7713d632008-12-02 07:52:3376
[email protected]44d62b62012-04-11 00:06:0377 // Used to record the reason an extension was disabled.
[email protected]eb5e4f92012-08-15 23:33:2878 enum DeprecatedDisableReason {
79 DEPRECATED_DISABLE_UNKNOWN,
80 DEPRECATED_DISABLE_USER_ACTION,
81 DEPRECATED_DISABLE_PERMISSIONS_INCREASE,
82 DEPRECATED_DISABLE_RELOAD,
83 DEPRECATED_DISABLE_LAST, // Not used.
84 };
85
[email protected]44d62b62012-04-11 00:06:0386 enum DisableReason {
[email protected]eb5e4f92012-08-15 23:33:2887 DISABLE_NONE = 0,
88 DISABLE_USER_ACTION = 1 << 0,
89 DISABLE_PERMISSIONS_INCREASE = 1 << 1,
90 DISABLE_RELOAD = 1 << 2,
[email protected]215a7be2012-10-22 19:53:4291 DISABLE_UNSUPPORTED_REQUIREMENT = 1 << 3,
92 DISABLE_SIDELOAD_WIPEOUT = 1 << 4,
[email protected]f56c65ea62012-12-10 22:57:2193 DISABLE_UNKNOWN_FROM_SYNC = 1 << 5,
[email protected]44d62b62012-04-11 00:06:0394 };
95
[email protected]fbcc40302009-06-12 20:45:4596 enum InstallType {
[email protected]ab6f2b22009-07-28 23:28:3797 INSTALL_ERROR,
[email protected]fbcc40302009-06-12 20:45:4598 DOWNGRADE,
99 REINSTALL,
100 UPGRADE,
101 NEW_INSTALL
102 };
103
[email protected]3bdba0d2011-08-23 07:17:30104 enum SyncType {
105 SYNC_TYPE_NONE = 0,
106 SYNC_TYPE_EXTENSION,
107 SYNC_TYPE_APP
108 };
109
[email protected]65378f52011-04-08 02:31:23110 // An NaCl module included in the extension.
111 struct NaClModuleInfo {
[email protected]84396dbc2011-04-14 06:33:42112 GURL url;
[email protected]65378f52011-04-08 02:31:23113 std::string mime_type;
114 };
115
[email protected]d356c982012-12-12 19:32:55116 // A base class for parsed manifest data that APIs want to store on
117 // the extension. Related to base::SupportsUserData, but with an immutable
118 // thread-safe interface to match Extension.
119 struct ManifestData {
120 virtual ~ManifestData() {}
121 };
122
[email protected]83048a22011-03-29 00:14:13123 enum InitFromValueFlags {
124 NO_FLAGS = 0,
125
126 // Usually, the id of an extension is generated by the "key" property of
127 // its manifest, but if |REQUIRE_KEY| is not set, a temporary ID will be
128 // generated based on the path.
129 REQUIRE_KEY = 1 << 0,
130
[email protected]3f53dfe2011-11-30 01:18:29131 // Requires the extension to have an up-to-date manifest version.
132 // Typically, we'll support multiple manifest versions during a version
[email protected]26367b62012-10-04 23:03:32133 // transition. This flag signals that we want to require the most modern
[email protected]3f53dfe2011-11-30 01:18:29134 // manifest version that Chrome understands.
135 REQUIRE_MODERN_MANIFEST_VERSION = 1 << 1,
136
[email protected]3aff9ad2011-04-01 20:26:48137 // |ALLOW_FILE_ACCESS| indicates that the user is allowing this extension
138 // to have file access. If it's not present, then permissions and content
139 // scripts that match file:/// URLs will be filtered out.
[email protected]ed3b9b12012-05-31 18:37:51140 ALLOW_FILE_ACCESS = 1 << 2,
[email protected]620db1762011-07-15 21:57:34141
142 // |FROM_WEBSTORE| indicates that the extension was installed from the
143 // Chrome Web Store.
[email protected]ed3b9b12012-05-31 18:37:51144 FROM_WEBSTORE = 1 << 3,
[email protected]e805baf2011-07-26 18:23:05145
146 // |FROM_BOOKMARK| indicates the extension was created using a mock App
147 // created from a bookmark.
[email protected]ed3b9b12012-05-31 18:37:51148 FROM_BOOKMARK = 1 << 4,
[email protected]3d41d432012-04-20 20:47:58149
150 // |FOLLOW_SYMLINKS_ANYWHERE| means that resources can be symlinks to
151 // anywhere in the filesystem, rather than being restricted to the
152 // extension directory.
[email protected]ed3b9b12012-05-31 18:37:51153 FOLLOW_SYMLINKS_ANYWHERE = 1 << 5,
[email protected]ab55c2b2012-06-01 23:55:03154
155 // |ERROR_ON_PRIVATE_KEY| means that private keys inside an
156 // extension should be errors rather than warnings.
157 ERROR_ON_PRIVATE_KEY = 1 << 6,
[email protected]e33bbc22012-08-27 22:05:46158
159 // |WAS_INSTALLED_BY_DEFAULT| installed by default when the profile was
160 // created.
161 WAS_INSTALLED_BY_DEFAULT = 1 << 7,
[email protected]83048a22011-03-29 00:14:13162 };
163
[email protected]a7329162013-02-07 19:21:48164 static scoped_refptr<Extension> Create(const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:40165 Manifest::Location location,
[email protected]f3a1c642011-07-12 19:15:03166 const base::DictionaryValue& value,
[email protected]83048a22011-03-29 00:14:13167 int flags,
[email protected]66e4eb32010-10-27 20:37:41168 std::string* error);
169
[email protected]87c655e2011-07-01 21:42:00170 // In a few special circumstances, we want to create an Extension and give it
[email protected]f5bf1842012-02-15 02:52:26171 // an explicit id. Most consumers should just use the other Create() method.
[email protected]a7329162013-02-07 19:21:48172 static scoped_refptr<Extension> Create(const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:40173 Manifest::Location location,
[email protected]58f62cf2012-03-09 10:45:11174 const base::DictionaryValue& value,
175 int flags,
176 const std::string& explicit_id,
177 std::string* error);
[email protected]87c655e2011-07-01 21:42:00178
[email protected]8d888c12010-11-30 00:00:25179 // Valid schemes for web extent URLPatterns.
180 static const int kValidWebExtentSchemes;
181
[email protected]f71f7e62010-12-07 03:45:33182 // Valid schemes for host permission URLPatterns.
183 static const int kValidHostPermissionSchemes;
184
[email protected]25b34332009-06-05 21:53:19185#if defined(OS_WIN)
[email protected]9dcf8f12010-09-02 20:39:19186 static const char kExtensionRegistryPath[];
[email protected]25b34332009-06-05 21:53:19187#endif
188
[email protected]e435d6b72009-07-25 03:15:58189 // The mimetype used for extensions.
190 static const char kMimeType[];
191
[email protected]25b34332009-06-05 21:53:19192 // Checks to see if the extension has a valid ID.
193 static bool IdIsValid(const std::string& id);
194
[email protected]e435d6b72009-07-25 03:15:58195 // Returns true if the specified file is an extension.
[email protected]a7329162013-02-07 19:21:48196 static bool IsExtension(const base::FilePath& file_name);
[email protected]e435d6b72009-07-25 03:15:58197
[email protected]fc6b0612012-03-29 13:40:06198 // Fills the |info| dictionary with basic information about the extension.
199 // |enabled| is injected for easier testing.
200 void GetBasicInfo(bool enabled, base::DictionaryValue* info) const;
201
[email protected]1d5e58b2013-01-31 08:41:40202 // See Type definition in Manifest.
203 Manifest::Type GetType() const;
[email protected]9b217652010-10-08 22:04:23204
[email protected]07c00d992009-03-04 20:27:04205 // Returns an absolute url to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:59206 // |extension_url| argument should be the url() from an Extension object. The
207 // |relative_path| can be untrusted user input. The returned URL will either
208 // be invalid() or a child of |extension_url|.
209 // NOTE: Static so that it can be used from multiple threads.
210 static GURL GetResourceURL(const GURL& extension_url,
211 const std::string& relative_path);
[email protected]cffd7892010-08-26 17:43:28212 GURL GetResourceURL(const std::string& relative_path) const {
[email protected]3cfbd0e2009-03-18 21:26:24213 return GetResourceURL(url(), relative_path);
214 }
[email protected]eab9b452009-01-23 20:48:59215
[email protected]f59a8052012-06-20 22:25:00216 // Returns true if the resource matches a pattern in the pattern_set.
217 bool ResourceMatches(const URLPatternSet& pattern_set,
218 const std::string& resource) const;
219
[email protected]99efb7b12009-12-18 02:39:16220 // Returns an extension resource object. |relative_path| should be UTF8
221 // encoded.
[email protected]9adb9692010-10-29 23:14:02222 ExtensionResource GetResource(const std::string& relative_path) const;
[email protected]99efb7b12009-12-18 02:39:16223
224 // As above, but with |relative_path| following the file system's encoding.
[email protected]a7329162013-02-07 19:21:48225 ExtensionResource GetResource(const base::FilePath& relative_path) const;
[email protected]eab9b452009-01-23 20:48:59226
[email protected]a17f9462009-06-09 02:56:41227 // |input| is expected to be the text of an rsa public or private key. It
228 // tolerates the presence or absence of bracking header/footer like this:
229 // -----(BEGIN|END) [RSA PUBLIC/PRIVATE] KEY-----
230 // and may contain newlines.
231 static bool ParsePEMKeyBytes(const std::string& input, std::string* output);
232
233 // Does a simple base64 encoding of |input| into |output|.
234 static bool ProducePEM(const std::string& input, std::string* output);
235
236 // Expects base64 encoded |input| and formats into |output| including
237 // the appropriate header & footer.
[email protected]e0d08192011-03-29 19:02:50238 static bool FormatPEMForFileOutput(const std::string& input,
239 std::string* output,
240 bool is_public);
[email protected]a17f9462009-06-09 02:56:41241
[email protected]a807bbe2010-04-14 10:51:19242 // Returns the base extension url for a given |extension_id|.
243 static GURL GetBaseURLFromExtensionId(const std::string& extension_id);
244
[email protected]be7e5cb2010-10-04 12:53:17245 // Adds an extension to the scripting whitelist. Used for testing only.
[email protected]10fb1992010-10-08 09:00:17246 static void SetScriptingWhitelist(const ScriptingWhitelist& whitelist);
[email protected]2a521c52011-01-26 18:45:21247 static const ScriptingWhitelist* GetScriptingWhitelist();
[email protected]be7e5cb2010-10-04 12:53:17248
[email protected]902fd7b2011-07-27 18:42:31249 // Parses the host and api permissions from the specified permission |key|
[email protected]d41e2152012-02-24 04:20:27250 // from |manifest_|.
251 bool ParsePermissions(const char* key,
[email protected]fc670822011-12-17 09:33:49252 string16* error,
[email protected]c2e66e12012-06-27 06:27:06253 APIPermissionSet* api_permissions,
[email protected]902fd7b2011-07-27 18:42:31254 URLPatternSet* host_permissions);
255
[email protected]d624b0b2012-12-12 04:05:19256 // Returns true if this extension has the given permission. Prefer
257 // IsExtensionWithPermissionOrSuggestInConsole when developers may be using an
258 // api that requires a permission they didn't know about, e.g. open web apis.
[email protected]c2e66e12012-06-27 06:27:06259 bool HasAPIPermission(APIPermission::ID permission) const;
[email protected]0d3e4a22011-06-23 19:02:52260 bool HasAPIPermission(const std::string& function_name) const;
[email protected]3d0e2262012-08-02 15:32:16261 bool HasAPIPermissionForTab(int tab_id, APIPermission::ID permission) const;
[email protected]583d45c12010-08-31 02:48:12262
[email protected]ecb9ad12012-08-21 13:02:15263 bool CheckAPIPermissionWithParam(APIPermission::ID permission,
264 const APIPermission::CheckParam* param) const;
[email protected]1d8b79a2012-08-16 20:22:54265
[email protected]0d3e4a22011-06-23 19:02:52266 const URLPatternSet& GetEffectiveHostPermissions() const;
[email protected]b24d8312009-08-27 06:47:46267
[email protected]902fd7b2011-07-27 18:42:31268 // Returns true if the extension can silently increase its permission level.
[email protected]0d904312012-01-25 23:00:16269 // Users must approve permissions for unpacked and packed extensions in the
270 // following situations:
271 // - when installing or upgrading packed extensions
272 // - when installing unpacked extensions that have NPAPI plugins
273 // - when either type of extension requests optional permissions
[email protected]902fd7b2011-07-27 18:42:31274 bool CanSilentlyIncreasePermissions() const;
275
[email protected]584b8e3f2010-04-10 00:23:37276 // Whether the extension has access to the given URL.
277 bool HasHostPermission(const GURL& url) const;
278
[email protected]0df165f2010-09-28 16:49:40279 // Whether the extension has effective access to all hosts. This is true if
280 // there is a content script that matches all hosts, if there is a host
281 // permission grants access to all hosts (like <all_urls>) or an api
282 // permission that effectively grants access to all hosts (e.g. proxy,
283 // network, etc.)
284 bool HasEffectiveAccessToAllHosts() const;
[email protected]b24d8312009-08-27 06:47:46285
[email protected]8d888c12010-11-30 00:00:25286 // Whether the extension effectively has all permissions (for example, by
287 // having an NPAPI plugin).
288 bool HasFullPermissions() const;
289
[email protected]902fd7b2011-07-27 18:42:31290 // Returns the full list of permission messages that this extension
291 // should display at install time.
[email protected]c2e66e12012-06-27 06:27:06292 PermissionMessages GetPermissionMessages() const;
[email protected]902fd7b2011-07-27 18:42:31293
294 // Returns the full list of permission messages that this extension
295 // should display at install time. The messages are returned as strings
296 // for convenience.
297 std::vector<string16> GetPermissionMessageStrings() const;
298
[email protected]b0d1d0b2012-11-02 21:19:03299 // Returns true if the extension does not require permission warnings
300 // to be displayed at install time.
301 bool ShouldSkipPermissionWarnings() const;
302
[email protected]902fd7b2011-07-27 18:42:31303 // Sets the active |permissions|.
[email protected]c2e66e12012-06-27 06:27:06304 void SetActivePermissions(const PermissionSet* permissions) const;
[email protected]902fd7b2011-07-27 18:42:31305
306 // Gets the extension's active permission set.
[email protected]c2e66e12012-06-27 06:27:06307 scoped_refptr<const PermissionSet> GetActivePermissions() const;
[email protected]902fd7b2011-07-27 18:42:31308
[email protected]5df6a5d2011-01-26 07:39:12309 // Whether context menu should be shown for page and browser actions.
310 bool ShowConfigureContextMenus() const;
311
[email protected]867a73e12010-03-19 20:45:46312 // Gets the fully resolved absolute launch URL.
313 GURL GetFullLaunchURL() const;
[email protected]2a521c52011-01-26 18:45:21314
[email protected]2a521c52011-01-26 18:45:21315 // Returns true if this extension can execute script on a page. If a
316 // UserScript object is passed, permission to run that specific script is
317 // checked (using its matches list). Otherwise, permission to execute script
318 // programmatically is checked (using the extension's host permission).
319 //
320 // This method is also aware of certain special pages that extensions are
321 // usually not allowed to run script on.
[email protected]78bdfd662012-08-23 05:53:18322 bool CanExecuteScriptOnPage(const GURL& document_url,
323 const GURL& top_document_url,
[email protected]fc5e65d6b2012-06-13 00:22:57324 int tab_id,
[email protected]3aff9ad2011-04-01 20:26:48325 const UserScript* script,
[email protected]2a521c52011-01-26 18:45:21326 std::string* error) const;
327
[email protected]6f229e82010-11-02 17:47:26328 // Returns true if this extension is a COMPONENT extension, or if it is
329 // on the whitelist of extensions that can script all pages.
330 bool CanExecuteScriptEverywhere() const;
331
[email protected]5efbfe012011-02-22 23:07:18332 // Returns true if this extension is allowed to obtain the contents of a
333 // page as an image. Since a page may contain sensitive information, this
334 // is restricted to the extension's host permissions as well as the
335 // extension page itself.
[email protected]fc5e65d6b2012-06-13 00:22:57336 bool CanCaptureVisiblePage(const GURL& page_url,
337 int tab_id,
338 std::string* error) const;
[email protected]5efbfe012011-02-22 23:07:18339
[email protected]a65882c2010-11-12 15:15:09340 // Returns true if this extension updates itself using the extension
341 // gallery.
342 bool UpdatesFromGallery() const;
343
[email protected]cca147172011-02-17 01:29:29344 // Returns true if this extension or app includes areas within |origin|.
345 bool OverlapsWithOrigin(const GURL& origin) const;
346
[email protected]3bdba0d2011-08-23 07:17:30347 // Returns the sync bucket to use for this extension.
348 SyncType GetSyncType() const;
349
[email protected]b873cd92012-02-09 21:51:48350 // Returns true if the extension should be synced.
351 bool IsSyncable() const;
352
[email protected]7e0f92b2012-11-09 03:51:04353 // Returns true if the extension requires a valid ordinal for sorting, e.g.,
354 // for displaying in a launcher or new tab page.
355 bool RequiresSortOrdinal() const;
356
357 // Returns true if the extension should be displayed in the app launcher.
358 bool ShouldDisplayInAppLauncher() const;
359
360 // Returns true if the extension should be displayed in the browser NTP.
361 bool ShouldDisplayInNewTabPage() const;
[email protected]b873cd92012-02-09 21:51:48362
[email protected]e0b3de72012-05-01 01:21:34363 // Returns true if the extension should be displayed in the extension
364 // settings page (i.e. chrome://extensions).
365 bool ShouldDisplayInExtensionSettings() const;
366
[email protected]fc5e65d6b2012-06-13 00:22:57367 // Gets the tab-specific host permissions of |tab_id|, or NULL if there
368 // aren't any.
[email protected]3d0e2262012-08-02 15:32:16369 scoped_refptr<const PermissionSet> GetTabSpecificPermissions(int tab_id)
370 const;
[email protected]fc5e65d6b2012-06-13 00:22:57371
[email protected]3d0e2262012-08-02 15:32:16372 // Updates the tab-specific permissions of |tab_id| to include those from
373 // |permissions|.
[email protected]6144057e2012-08-02 19:02:37374 void UpdateTabSpecificPermissions(
375 int tab_id,
376 scoped_refptr<const PermissionSet> permissions) const;
[email protected]fc5e65d6b2012-06-13 00:22:57377
[email protected]3d0e2262012-08-02 15:32:16378 // Clears the tab-specific permissions of |tab_id|.
379 void ClearTabSpecificPermissions(int tab_id) const;
[email protected]fc5e65d6b2012-06-13 00:22:57380
[email protected]d356c982012-12-12 19:32:55381 // Get the manifest data associated with the key, or NULL if there is none.
382 // Can only be called after InitValue is finished.
383 ManifestData* GetManifestData(const std::string& key) const;
384
385 // Sets |data| to be associated with the key. Takes ownership of |data|.
386 // Can only be called before InitValue is finished. Not thread-safe;
387 // all SetManifestData calls should be on only one thread.
388 void SetManifestData(const std::string& key, ManifestData* data);
389
[email protected]6f229e82010-11-02 17:47:26390 // Accessors:
391
[email protected]a7329162013-02-07 19:21:48392 const base::FilePath& path() const { return path_; }
[email protected]6f229e82010-11-02 17:47:26393 const GURL& url() const { return extension_url_; }
[email protected]1d5e58b2013-01-31 08:41:40394 Manifest::Location location() const;
[email protected]d41e2152012-02-24 04:20:27395 const std::string& id() const;
[email protected]1f04ef42013-04-22 07:35:50396 const base::Version* version() const { return version_.get(); }
[email protected]6f229e82010-11-02 17:47:26397 const std::string VersionString() const;
398 const std::string& name() const { return name_; }
[email protected]701d1e82012-05-14 05:34:19399 const std::string& non_localized_name() const { return non_localized_name_; }
[email protected]200423d2012-06-05 01:16:06400 // Base64-encoded version of the key used to sign this extension.
401 // In pseudocode, returns
402 // base::Base64Encode(RSAPrivateKey(pem_file).ExportPublicKey()).
403 const std::string& public_key() const { return public_key_; }
[email protected]6f229e82010-11-02 17:47:26404 const std::string& description() const { return description_; }
[email protected]a47c8a22011-11-17 18:40:31405 int manifest_version() const { return manifest_version_; }
[email protected]6f229e82010-11-02 17:47:26406 bool converted_from_user_script() const {
407 return converted_from_user_script_;
408 }
[email protected]65378f52011-04-08 02:31:23409 const std::vector<NaClModuleInfo>& nacl_modules() const {
410 return nacl_modules_;
411 }
[email protected]c2e66e12012-06-27 06:27:06412 const PermissionSet* optional_permission_set() const {
[email protected]902fd7b2011-07-27 18:42:31413 return optional_permission_set_.get();
414 }
[email protected]c2e66e12012-06-27 06:27:06415 const PermissionSet* required_permission_set() const {
[email protected]902fd7b2011-07-27 18:42:31416 return required_permission_set_.get();
[email protected]6f229e82010-11-02 17:47:26417 }
[email protected]465594632013-02-15 19:50:28418 // Returns the temporary APIPermissionSet used in initialization.
419 // (NULL after initialization is completed.)
420 APIPermissionSet* initial_api_permissions() {
421 return initial_api_permissions_.get();
422 }
[email protected]9367eabc2013-03-01 01:29:29423 const APIPermissionSet* initial_api_permissions() const {
424 return initial_api_permissions_.get();
425 }
[email protected]23b3c0a2013-01-16 23:36:36426 // Appends |new_warning[s]| to install_warnings_.
427 void AddInstallWarning(const InstallWarning& new_warning);
[email protected]1d5e58b2013-01-31 08:41:40428 void AddInstallWarnings(const std::vector<InstallWarning>& new_warnings);
429 const std::vector<InstallWarning>& install_warnings() const {
[email protected]8629c542012-04-20 03:40:03430 return install_warnings_;
431 }
[email protected]953620b2011-12-04 00:55:32432 const extensions::Manifest* manifest() const {
[email protected]e9629d772012-08-06 19:44:46433 return manifest_.get();
[email protected]6f229e82010-11-02 17:47:26434 }
[email protected]3aff9ad2011-04-01 20:26:48435 bool wants_file_access() const { return wants_file_access_; }
[email protected]334ec0a2013-03-24 01:36:16436 // TODO(rdevlin.cronin): This is needed for ContentScriptsHandler, and should
437 // be moved out as part of crbug.com/159265. This should not be used anywhere
438 // else.
439 void set_wants_file_access(bool wants_file_access) {
440 wants_file_access_ = wants_file_access;
441 }
[email protected]2af352b2011-07-22 08:21:23442 int creation_flags() const { return creation_flags_; }
443 bool from_webstore() const { return (creation_flags_ & FROM_WEBSTORE) != 0; }
[email protected]e805baf2011-07-26 18:23:05444 bool from_bookmark() const { return (creation_flags_ & FROM_BOOKMARK) != 0; }
[email protected]e33bbc22012-08-27 22:05:46445 bool was_installed_by_default() const {
446 return (creation_flags_ & WAS_INSTALLED_BY_DEFAULT) != 0;
447 }
[email protected]3aff9ad2011-04-01 20:26:48448
[email protected]6f229e82010-11-02 17:47:26449 // App-related.
[email protected]21c01042013-03-10 23:41:14450 bool is_app() const;
[email protected]d41e2152012-02-24 04:20:27451 bool is_platform_app() const;
452 bool is_hosted_app() const;
[email protected]c4f459d2012-09-28 04:40:10453 bool is_legacy_packaged_app() const;
[email protected]ff05a4b102012-12-19 00:12:06454 bool is_extension() const;
[email protected]cdc7b1f42012-12-07 19:39:48455 bool can_be_incognito_enabled() const;
[email protected]636ee43282013-01-12 15:58:00456 void AddWebExtentPattern(const URLPattern& pattern);
[email protected]cced75a2011-05-20 08:31:12457 const URLPatternSet& web_extent() const { return extent_; }
[email protected]6f229e82010-11-02 17:47:26458 const std::string& launch_local_path() const { return launch_local_path_; }
459 const std::string& launch_web_url() const { return launch_web_url_; }
460 extension_misc::LaunchContainer launch_container() const {
461 return launch_container_;
462 }
[email protected]dc37b002012-04-23 23:02:26463 int launch_width() const { return launch_width_; }
464 int launch_height() const { return launch_height_; }
[email protected]6f229e82010-11-02 17:47:26465
466 // Theme-related.
[email protected]d41e2152012-02-24 04:20:27467 bool is_theme() const;
[email protected]6f229e82010-11-02 17:47:26468
[email protected]4a8d3272009-03-10 19:15:08469 private:
[email protected]66e4eb32010-10-27 20:37:41470 friend class base::RefCountedThreadSafe<Extension>;
471
[email protected]902fd7b2011-07-27 18:42:31472 class RuntimeData {
473 public:
474 RuntimeData();
[email protected]c2e66e12012-06-27 06:27:06475 explicit RuntimeData(const PermissionSet* active);
[email protected]902fd7b2011-07-27 18:42:31476 ~RuntimeData();
477
[email protected]c2e66e12012-06-27 06:27:06478 void SetActivePermissions(const PermissionSet* active);
479 scoped_refptr<const PermissionSet> GetActivePermissions() const;
[email protected]902fd7b2011-07-27 18:42:31480
[email protected]3d0e2262012-08-02 15:32:16481 scoped_refptr<const PermissionSet> GetTabSpecificPermissions(int tab_id)
482 const;
[email protected]6144057e2012-08-02 19:02:37483 void UpdateTabSpecificPermissions(
484 int tab_id,
485 scoped_refptr<const PermissionSet> permissions);
[email protected]3d0e2262012-08-02 15:32:16486 void ClearTabSpecificPermissions(int tab_id);
[email protected]fc5e65d6b2012-06-13 00:22:57487
[email protected]902fd7b2011-07-27 18:42:31488 private:
489 friend class base::RefCountedThreadSafe<RuntimeData>;
[email protected]fc5e65d6b2012-06-13 00:22:57490
[email protected]c2e66e12012-06-27 06:27:06491 scoped_refptr<const PermissionSet> active_permissions_;
[email protected]fc5e65d6b2012-06-13 00:22:57492
[email protected]3d0e2262012-08-02 15:32:16493 typedef std::map<int, scoped_refptr<const PermissionSet> >
494 TabPermissionsMap;
495 TabPermissionsMap tab_specific_permissions_;
[email protected]902fd7b2011-07-27 18:42:31496 };
497
[email protected]d41e2152012-02-24 04:20:27498 // Chooses the extension ID for an extension based on a variety of criteria.
499 // The chosen ID will be set in |manifest|.
500 static bool InitExtensionID(extensions::Manifest* manifest,
[email protected]a7329162013-02-07 19:21:48501 const base::FilePath& path,
[email protected]d41e2152012-02-24 04:20:27502 const std::string& explicit_id,
503 int creation_flags,
504 string16* error);
505
[email protected]87c655e2011-07-01 21:42:00506 // Returns true if this extension id is from a trusted provider.
507 static bool IsTrustedId(const std::string& id);
508
[email protected]a7329162013-02-07 19:21:48509 Extension(const base::FilePath& path,
510 scoped_ptr<extensions::Manifest> manifest);
[email protected]d356c982012-12-12 19:32:55511 virtual ~Extension();
[email protected]66e4eb32010-10-27 20:37:41512
513 // Initialize the extension from a parsed manifest.
[email protected]d41e2152012-02-24 04:20:27514 // TODO(aa): Rename to just Init()? There's no Value here anymore.
515 // TODO(aa): It is really weird the way this class essentially contains a copy
516 // of the underlying DictionaryValue in its members. We should decide to
517 // either wrap the DictionaryValue and go with that only, or we should parse
518 // into strong types and discard the value. But doing both is bad.
519 bool InitFromValue(int flags, string16* error);
[email protected]66e4eb32010-10-27 20:37:41520
[email protected]58f62cf2012-03-09 10:45:11521 // The following are helpers for InitFromValue to load various features of the
522 // extension from the manifest.
523
[email protected]465594632013-02-15 19:50:28524 bool LoadAppIsolation(string16* error);
[email protected]58f62cf2012-03-09 10:45:11525
526 bool LoadRequiredFeatures(string16* error);
527 bool LoadName(string16* error);
528 bool LoadVersion(string16* error);
529
530 bool LoadAppFeatures(string16* error);
[email protected]d41e2152012-02-24 04:20:27531 bool LoadExtent(const char* key,
532 URLPatternSet* extent,
533 const char* list_error,
534 const char* value_error,
535 string16* error);
536 bool LoadLaunchContainer(string16* error);
537 bool LoadLaunchURL(string16* error);
[email protected]10253da2012-03-09 04:06:42538
[email protected]465594632013-02-15 19:50:28539 bool LoadSharedFeatures(string16* error);
[email protected]58f62cf2012-03-09 10:45:11540 bool LoadDescription(string16* error);
541 bool LoadManifestVersion(string16* error);
[email protected]58f62cf2012-03-09 10:45:11542 bool LoadNaClModules(string16* error);
[email protected]66008002013-01-08 09:09:13543
[email protected]2f6698b2010-10-14 00:58:21544 // Returns true if the extension has more than one "UI surface". For example,
545 // an extension that has a browser action and a page action.
546 bool HasMultipleUISurfaces() const;
547
[email protected]be9d9c82011-07-13 04:17:31548 // Updates the launch URL and extents for the extension using the given
549 // |override_url|.
550 void OverrideLaunchUrl(const GURL& override_url);
551
[email protected]ae655e4e2012-03-16 21:47:55552 // Custom checks for the experimental permission that can't be expressed in
553 // _permission_features.json.
[email protected]5eddc3e2011-10-26 04:33:31554 bool CanSpecifyExperimentalPermission() const;
[email protected]5eddc3e2011-10-26 04:33:31555
556 // Checks whether the host |pattern| is allowed for this extension, given API
557 // permissions |permissions|.
558 bool CanSpecifyHostPermission(const URLPattern& pattern,
[email protected]465594632013-02-15 19:50:28559 const APIPermissionSet& permissions) const;
[email protected]5eddc3e2011-10-26 04:33:31560
[email protected]488e6502012-09-07 14:17:34561 bool CheckMinimumChromeVersion(string16* error) const;
[email protected]488e6502012-09-07 14:17:34562
[email protected]e92169e2012-05-04 22:49:32563 // Check that platform app features are valid. Called after InitFromValue.
[email protected]9a494872013-03-02 03:05:45564 bool CheckPlatformAppFeatures(string16* error) const;
[email protected]e92169e2012-05-04 22:49:32565
[email protected]f1bab9c92012-05-11 21:15:10566 // Check that features don't conflict. Called after InitFromValue.
[email protected]9a494872013-03-02 03:05:45567 bool CheckConflictingFeatures(string16* error) const;
[email protected]f1bab9c92012-05-11 21:15:10568
[email protected]6f229e82010-11-02 17:47:26569 // The extension's human-readable name. Name is used for display purpose. It
570 // might be wrapped with unicode bidi control characters so that it is
571 // displayed correctly in RTL context.
572 // NOTE: Name is UTF-8 and may contain non-ascii characters.
573 std::string name_;
574
[email protected]701d1e82012-05-14 05:34:19575 // A non-localized version of the extension's name. This is useful for
576 // debug output.
577 std::string non_localized_name_;
578
[email protected]a47c8a22011-11-17 18:40:31579 // The version of this extension's manifest. We increase the manifest
580 // version when making breaking changes to the extension system.
581 // Version 1 was the first manifest version (implied by a lack of a
582 // manifest_version attribute in the extension's manifest). We initialize
583 // this member variable to 0 to distinguish the "uninitialized" case from
584 // the case when we know the manifest version actually is 1.
585 int manifest_version_;
586
[email protected]d41e2152012-02-24 04:20:27587 // The absolute path to the directory the extension is stored in.
[email protected]a7329162013-02-07 19:21:48588 base::FilePath path_;
[email protected]d41e2152012-02-24 04:20:27589
[email protected]6f229e82010-11-02 17:47:26590 // Defines the set of URLs in the extension's web content.
[email protected]cced75a2011-05-20 08:31:12591 URLPatternSet extent_;
[email protected]6f229e82010-11-02 17:47:26592
[email protected]902fd7b2011-07-27 18:42:31593 // The extension runtime data.
594 mutable base::Lock runtime_data_lock_;
595 mutable RuntimeData runtime_data_;
596
[email protected]465594632013-02-15 19:50:28597 // The API permission set; used during extension initialization.
598 // Cleared after permissions are finalized by SetActivePermissions.
599 scoped_ptr<APIPermissionSet> initial_api_permissions_;
600
[email protected]902fd7b2011-07-27 18:42:31601 // The set of permissions the extension can request at runtime.
[email protected]c2e66e12012-06-27 06:27:06602 scoped_refptr<const PermissionSet> optional_permission_set_;
[email protected]902fd7b2011-07-27 18:42:31603
604 // The extension's required / default set of permissions.
[email protected]c2e66e12012-06-27 06:27:06605 scoped_refptr<const PermissionSet> required_permission_set_;
[email protected]6f229e82010-11-02 17:47:26606
[email protected]8629c542012-04-20 03:40:03607 // Any warnings that occurred when trying to create/parse the extension.
[email protected]1d5e58b2013-01-31 08:41:40608 std::vector<InstallWarning> install_warnings_;
[email protected]8629c542012-04-20 03:40:03609
[email protected]6f229e82010-11-02 17:47:26610 // The base extension url for the extension.
611 GURL extension_url_;
612
[email protected]6f229e82010-11-02 17:47:26613 // The extension's version.
[email protected]1f04ef42013-04-22 07:35:50614 scoped_ptr<base::Version> version_;
[email protected]6f229e82010-11-02 17:47:26615
616 // An optional longer description of the extension.
617 std::string description_;
618
619 // True if the extension was generated from a user script. (We show slightly
620 // different UI if so).
621 bool converted_from_user_script_;
622
[email protected]65378f52011-04-08 02:31:23623 // Optional list of NaCl modules and associated properties.
624 std::vector<NaClModuleInfo> nacl_modules_;
625
[email protected]6f229e82010-11-02 17:47:26626 // The public key used to sign the contents of the crx package.
627 std::string public_key_;
628
[email protected]58f62cf2012-03-09 10:45:11629 // The manifest from which this extension was created.
[email protected]e9629d772012-08-06 19:44:46630 scoped_ptr<Manifest> manifest_;
[email protected]6f229e82010-11-02 17:47:26631
[email protected]d356c982012-12-12 19:32:55632 // Stored parsed manifest data.
633 ManifestDataMap manifest_data_;
634
635 // Set to true at the end of InitValue when initialization is finished.
636 bool finished_parsing_manifest_;
637
[email protected]7eeab9ec2013-01-15 04:08:33638 // Ensures that any call to GetManifestData() prior to finishing
639 // initialization happens from the same thread (this can happen when certain
640 // parts of the initialization process need information from previous parts).
641 base::ThreadChecker thread_checker_;
642
[email protected]6f229e82010-11-02 17:47:26643 // The local path inside the extension to use with the launcher.
644 std::string launch_local_path_;
645
646 // A web url to use with the launcher. Note that this might be relative or
647 // absolute. If relative, it is relative to web_origin.
648 std::string launch_web_url_;
649
[email protected]4e595682011-02-09 17:07:02650 // The window type that an app's manifest specifies to launch into.
651 // This is not always the window type an app will open into, because
652 // users can override the way each app launches. See
653 // ExtensionPrefs::GetLaunchContainer(), which looks at a per-app pref
654 // to decide what container an app will launch in.
[email protected]6f229e82010-11-02 17:47:26655 extension_misc::LaunchContainer launch_container_;
656
657 // The default size of the container when launching. Only respected for
658 // containers like panels and windows.
659 int launch_width_;
660 int launch_height_;
661
[email protected]7e0f92b2012-11-09 03:51:04662 // Should this app be shown in the app launcher.
[email protected]ed1a204f2012-09-22 00:28:44663 bool display_in_launcher_;
664
[email protected]7e0f92b2012-11-09 03:51:04665 // Should this app be shown in the browser New Tab Page.
666 bool display_in_new_tab_page_;
667
[email protected]3aff9ad2011-04-01 20:26:48668 // Whether the extension has host permissions or user script patterns that
669 // imply access to file:/// scheme URLs (the user may not have actually
670 // granted it that access).
671 bool wants_file_access_;
672
[email protected]2af352b2011-07-22 08:21:23673 // The flags that were passed to InitFromValue.
674 int creation_flags_;
[email protected]620db1762011-07-15 21:57:34675
[email protected]894bb502009-05-21 22:39:57676 DISALLOW_COPY_AND_ASSIGN(Extension);
[email protected]7713d632008-12-02 07:52:33677};
678
[email protected]bc151cf92013-02-12 04:57:26679typedef std::vector<scoped_refptr<const Extension> > ExtensionList;
[email protected]ec5b50d2010-10-09 16:35:18680typedef std::set<std::string> ExtensionIdSet;
[email protected]82590cb2012-09-28 04:14:08681typedef std::vector<std::string> ExtensionIdList;
[email protected]b1748b1d82009-11-30 20:32:56682
[email protected]c6d474f82009-12-16 21:11:06683// Handy struct to pass core extension info around.
684struct ExtensionInfo {
[email protected]f3a1c642011-07-12 19:15:03685 ExtensionInfo(const base::DictionaryValue* manifest,
[email protected]c6d474f82009-12-16 21:11:06686 const std::string& id,
[email protected]a7329162013-02-07 19:21:48687 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:40688 Manifest::Location location);
[email protected]3bb84992010-08-26 17:23:46689 ~ExtensionInfo();
[email protected]c6d474f82009-12-16 21:11:06690
[email protected]f3a1c642011-07-12 19:15:03691 scoped_ptr<base::DictionaryValue> extension_manifest;
[email protected]c6d474f82009-12-16 21:11:06692 std::string extension_id;
[email protected]a7329162013-02-07 19:21:48693 base::FilePath extension_path;
[email protected]1d5e58b2013-01-31 08:41:40694 Manifest::Location extension_location;
[email protected]c6d474f82009-12-16 21:11:06695
696 private:
697 DISALLOW_COPY_AND_ASSIGN(ExtensionInfo);
698};
699
[email protected]a9f39a312010-12-23 22:14:27700struct UnloadedExtensionInfo {
[email protected]814a7bf0f2011-08-13 05:30:59701 extension_misc::UnloadedExtensionReason reason;
[email protected]a9f39a312010-12-23 22:14:27702
703 // Was the extension already disabled?
704 bool already_disabled;
705
706 // The extension being unloaded - this should always be non-NULL.
707 const Extension* extension;
708
[email protected]814a7bf0f2011-08-13 05:30:59709 UnloadedExtensionInfo(
710 const Extension* extension,
711 extension_misc::UnloadedExtensionReason reason);
[email protected]a9f39a312010-12-23 22:14:27712};
713
[email protected]902fd7b2011-07-27 18:42:31714// The details sent for EXTENSION_PERMISSIONS_UPDATED notifications.
715struct UpdatedExtensionPermissionsInfo {
716 enum Reason {
[email protected]f5532472012-02-23 13:00:55717 ADDED, // The permissions were added to the extension.
718 REMOVED, // The permissions were removed from the extension.
[email protected]902fd7b2011-07-27 18:42:31719 };
720
721 Reason reason;
722
723 // The extension who's permissions have changed.
724 const Extension* extension;
725
726 // The permissions that have changed. For Reason::ADDED, this would contain
727 // only the permissions that have added, and for Reason::REMOVED, this would
728 // only contain the removed permissions.
[email protected]c2e66e12012-06-27 06:27:06729 const PermissionSet* permissions;
[email protected]902fd7b2011-07-27 18:42:31730
731 UpdatedExtensionPermissionsInfo(
732 const Extension* extension,
[email protected]c2e66e12012-06-27 06:27:06733 const PermissionSet* permissions,
[email protected]902fd7b2011-07-27 18:42:31734 Reason reason);
735};
736
[email protected]488e6502012-09-07 14:17:34737} // namespace extensions
[email protected]1c321ee2012-05-21 03:02:34738
[email protected]5b1a0e22009-05-26 19:00:58739#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_H_