blob: 7e584d72077e294d4767c47c6521b98828d93ecb [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]b6b805e92011-04-16 09:24:1416#include "base/memory/linked_ptr.h"
[email protected]3b63f8f42011-03-28 01:54:1517#include "base/memory/ref_counted.h"
18#include "base/memory/scoped_ptr.h"
[email protected]d83a5602010-09-16 00:22:4819#include "chrome/common/extensions/extension_constants.h"
[email protected]807871f2010-09-16 01:04:4820#include "chrome/common/extensions/extension_icon_set.h"
[email protected]0d3e4a22011-06-23 19:02:5221#include "chrome/common/extensions/extension_permission_set.h"
[email protected]42b6f0f82009-09-18 21:07:3922#include "chrome/common/extensions/user_script.h"
[email protected]7197f4992009-03-23 05:05:4923#include "chrome/common/extensions/url_pattern.h"
[email protected]cced75a2011-05-20 08:31:1224#include "chrome/common/extensions/url_pattern_set.h"
[email protected]eab9b452009-01-23 20:48:5925#include "googleurl/src/gurl.h"
[email protected]08397d52011-02-05 01:53:3826#include "ui/gfx/size.h"
[email protected]eab9b452009-01-23 20:48:5927
[email protected]3bb84992010-08-26 17:23:4628class DictionaryValue;
[email protected]942690b132010-05-11 06:42:1429class ExtensionAction;
30class ExtensionResource;
[email protected]e2dffe02011-01-20 20:30:0631class ExtensionSidebarDefaults;
[email protected]b6b805e92011-04-16 09:24:1432class FileBrowserHandler;
33class ListValue;
[email protected]12802702010-07-09 19:43:0934class SkBitmap;
[email protected]daf66aa2010-08-06 06:24:2835class Version;
[email protected]942690b132010-05-11 06:42:1436
[email protected]f0755532010-06-22 07:27:2537// Represents a Chrome extension.
[email protected]66e4eb32010-10-27 20:37:4138class Extension : public base::RefCountedThreadSafe<Extension> {
[email protected]7713d632008-12-02 07:52:3339 public:
[email protected]d3cfa482009-10-17 13:54:5740 typedef std::map<const std::string, GURL> URLOverrideMap;
[email protected]10fb1992010-10-08 09:00:1741 typedef std::vector<std::string> ScriptingWhitelist;
[email protected]b6b805e92011-04-16 09:24:1442 typedef std::vector<linked_ptr<FileBrowserHandler> > FileBrowserHandlerList;
[email protected]b24d8312009-08-27 06:47:4643
[email protected]631cf822009-05-15 07:01:2544 // What an extension was loaded from.
[email protected]9b217652010-10-08 22:04:2345 // NOTE: These values are stored as integers in the preferences and used
46 // in histograms so don't remove or reorder existing items. Just append
47 // to the end.
[email protected]631cf822009-05-15 07:01:2548 enum Location {
49 INVALID,
[email protected]25b34332009-06-05 21:53:1950 INTERNAL, // A crx file from the internal Extensions directory.
51 EXTERNAL_PREF, // A crx file from an external directory (via prefs).
52 EXTERNAL_REGISTRY, // A crx file from an external directory (via eg the
53 // registry on Windows).
[email protected]1952c7d2010-03-04 23:48:3454 LOAD, // --load-extension.
[email protected]8ef78fd2010-08-19 17:14:3255 COMPONENT, // An integral component of Chrome itself, which
56 // happens to be implemented as an extension. We don't
57 // show these in the management UI.
[email protected]4d2913e32010-11-30 00:28:5558 EXTERNAL_PREF_DOWNLOAD, // A crx file from an external directory (via
59 // prefs), installed from an update URL.
60 EXTERNAL_POLICY_DOWNLOAD, // A crx file from an external directory (via
61 // admin policies), installed from an update URL.
[email protected]04cb7542010-10-25 10:50:0662
63 NUM_LOCATIONS
[email protected]25b34332009-06-05 21:53:1964 };
65
66 enum State {
[email protected]0c6da502009-08-14 22:32:3967 DISABLED = 0,
[email protected]25b34332009-06-05 21:53:1968 ENABLED,
[email protected]79c833b52011-04-05 18:31:0169 // An external extension that the user uninstalled. We should not reinstall
70 // such extensions on startup.
71 EXTERNAL_EXTENSION_UNINSTALLED,
[email protected]0c6da502009-08-14 22:32:3972 NUM_STATES
[email protected]631cf822009-05-15 07:01:2573 };
[email protected]7713d632008-12-02 07:52:3374
[email protected]fbcc40302009-06-12 20:45:4575 enum InstallType {
[email protected]ab6f2b22009-07-28 23:28:3776 INSTALL_ERROR,
[email protected]fbcc40302009-06-12 20:45:4577 DOWNGRADE,
78 REINSTALL,
79 UPGRADE,
80 NEW_INSTALL
81 };
82
[email protected]d2817012009-08-04 06:46:2183 // NOTE: If you change this list, you should also change kIconSizes in the cc
84 // file.
85 enum Icons {
86 EXTENSION_ICON_LARGE = 128,
87 EXTENSION_ICON_MEDIUM = 48,
88 EXTENSION_ICON_SMALL = 32,
[email protected]39382942010-03-23 15:57:0989 EXTENSION_ICON_SMALLISH = 24,
[email protected]d2817012009-08-04 06:46:2190 EXTENSION_ICON_BITTY = 16,
91 };
92
[email protected]7fa19f82010-12-21 19:40:0893 // Do not change the order of entries or remove entries in this list
94 // as this is used in UMA_HISTOGRAM_ENUMERATIONs about extensions.
95 enum Type {
[email protected]9b217652010-10-08 22:04:2396 TYPE_UNKNOWN = 0,
97 TYPE_EXTENSION,
98 TYPE_THEME,
99 TYPE_USER_SCRIPT,
100 TYPE_HOSTED_APP,
101 TYPE_PACKAGED_APP
102 };
103
[email protected]92888082010-10-18 19:24:57104 // An NPAPI plugin included in the extension.
105 struct PluginInfo {
106 FilePath path; // Path to the plugin.
107 bool is_public; // False if only this extension can load this plugin.
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]b0820372011-06-03 07:05:27116 enum InputComponentType {
[email protected]d45f7512011-06-21 21:18:27117 INPUT_COMPONENT_TYPE_NONE = -1,
118 INPUT_COMPONENT_TYPE_IME,
[email protected]b0820372011-06-03 07:05:27119 INPUT_COMPONENT_TYPE_VIRTUAL_KEYBOARD,
120 INPUT_COMPONENT_TYPE_COUNT
121 };
122
123 struct InputComponentInfo {
124 // Define out of line constructor/destructor to please Clang.
125 InputComponentInfo();
126 ~InputComponentInfo();
127
128 std::string name;
129 InputComponentType type;
130 std::string id;
131 std::string description;
132 std::string language;
133 std::set<std::string> layouts;
134 std::string shortcut_keycode;
135 bool shortcut_alt;
136 bool shortcut_ctrl;
137 bool shortcut_shift;
138 };
139
[email protected]a4a38c12010-12-23 16:43:56140 struct TtsVoice {
141 std::string voice_name;
142 std::string locale;
143 std::string gender;
144 };
145
[email protected]83048a22011-03-29 00:14:13146 enum InitFromValueFlags {
147 NO_FLAGS = 0,
148
149 // Usually, the id of an extension is generated by the "key" property of
150 // its manifest, but if |REQUIRE_KEY| is not set, a temporary ID will be
151 // generated based on the path.
152 REQUIRE_KEY = 1 << 0,
153
154 // |STRICT_ERROR_CHECKS| enables extra error checking, such as
155 // checks that URL patterns do not contain ports. This error
156 // checking may find an error that a previous version of
157 // Chrome did not flag. To avoid errors in installed extensions
158 // when Chrome is upgraded, strict error checking is only enabled
159 // when loading extensions as a developer would (such as loading
160 // an unpacked extension), or when loading an extension that is
161 // tied to a specific version of Chrome (such as a component
162 // extension). Most callers will set the |STRICT_ERROR_CHECKS| bit when
163 // Extension::ShouldDoStrictErrorChecking(location) returns true.
164 STRICT_ERROR_CHECKS = 1 << 1,
[email protected]3aff9ad2011-04-01 20:26:48165
166 // |ALLOW_FILE_ACCESS| indicates that the user is allowing this extension
167 // to have file access. If it's not present, then permissions and content
168 // scripts that match file:/// URLs will be filtered out.
169 ALLOW_FILE_ACCESS = 1 << 2,
[email protected]83048a22011-03-29 00:14:13170 };
171
[email protected]66e4eb32010-10-27 20:37:41172 static scoped_refptr<Extension> Create(const FilePath& path,
173 Location location,
174 const DictionaryValue& value,
[email protected]83048a22011-03-29 00:14:13175 int flags,
[email protected]66e4eb32010-10-27 20:37:41176 std::string* error);
177
[email protected]4d2913e32010-11-30 00:28:55178 // Return the update url used by gallery/webstore extensions.
179 static GURL GalleryUpdateUrl(bool secure);
180
[email protected]145a317b2011-04-12 16:03:46181 // Given two install sources, return the one which should take priority
182 // over the other. If an extension is installed from two sources A and B,
183 // its install source should be set to GetHigherPriorityLocation(A, B).
184 static Location GetHigherPriorityLocation(Location loc1, Location loc2);
185
[email protected]a2a098d2010-09-29 19:42:55186 // Returns the full list of permission messages that this extension
187 // should display at install time.
[email protected]0d3e4a22011-06-23 19:02:52188 ExtensionPermissionMessages GetPermissionMessages() const;
[email protected]fe2dd7742011-04-19 22:52:49189
190 // Returns the full list of permission messages that this extension
191 // should display at install time. The messages are returned as strings
192 // for convenience.
193 std::vector<string16> GetPermissionMessageStrings() const;
[email protected]a2a098d2010-09-29 19:42:55194
[email protected]c3e3def742009-07-17 07:51:06195 // Icon sizes used by the extension system.
[email protected]d2817012009-08-04 06:46:21196 static const int kIconSizes[];
[email protected]c3e3def742009-07-17 07:51:06197
[email protected]4c4f8192009-10-17 01:03:26198 // Max size (both dimensions) for browser and page actions.
199 static const int kPageActionIconMaxSize;
200 static const int kBrowserActionIconMaxSize;
[email protected]e2dffe02011-01-20 20:30:06201 static const int kSidebarIconMaxSize;
[email protected]4c4f8192009-10-17 01:03:26202
[email protected]8d888c12010-11-30 00:00:25203 // Valid schemes for web extent URLPatterns.
204 static const int kValidWebExtentSchemes;
205
[email protected]f71f7e62010-12-07 03:45:33206 // Valid schemes for host permission URLPatterns.
207 static const int kValidHostPermissionSchemes;
208
[email protected]6014d672008-12-05 00:38:25209 // The name of the manifest inside an extension.
[email protected]99efb7b12009-12-18 02:39:16210 static const FilePath::CharType kManifestFilename[];
[email protected]6014d672008-12-05 00:38:25211
[email protected]300cc58db2009-08-19 20:45:14212 // The name of locale folder inside an extension.
[email protected]99efb7b12009-12-18 02:39:16213 static const FilePath::CharType kLocaleFolder[];
[email protected]300cc58db2009-08-19 20:45:14214
215 // The name of the messages file inside an extension.
[email protected]99efb7b12009-12-18 02:39:16216 static const FilePath::CharType kMessagesFilename[];
[email protected]300cc58db2009-08-19 20:45:14217
[email protected]25b34332009-06-05 21:53:19218#if defined(OS_WIN)
[email protected]9dcf8f12010-09-02 20:39:19219 static const char kExtensionRegistryPath[];
[email protected]25b34332009-06-05 21:53:19220#endif
221
[email protected]37eeb5a2009-02-26 23:36:17222 // The number of bytes in a legal id.
[email protected]fe0e7822009-02-26 23:51:48223 static const size_t kIdSize;
[email protected]37eeb5a2009-02-26 23:36:17224
[email protected]e435d6b72009-07-25 03:15:58225 // The mimetype used for extensions.
226 static const char kMimeType[];
227
[email protected]25b34332009-06-05 21:53:19228 // Checks to see if the extension has a valid ID.
229 static bool IdIsValid(const std::string& id);
230
[email protected]4ead6f72010-10-13 19:54:18231 // Generate an ID for an extension in the given path.
[email protected]28d7479b2011-03-09 21:33:27232 // Used while developing extensions, before they have a key.
[email protected]4ead6f72010-10-13 19:54:18233 static std::string GenerateIdForPath(const FilePath& file_name);
234
[email protected]e435d6b72009-07-25 03:15:58235 // Returns true if the specified file is an extension.
236 static bool IsExtension(const FilePath& file_name);
237
[email protected]25b34332009-06-05 21:53:19238 // Whether the |location| is external or not.
239 static inline bool IsExternalLocation(Location location) {
240 return location == Extension::EXTERNAL_PREF ||
[email protected]8ef78fd2010-08-19 17:14:32241 location == Extension::EXTERNAL_REGISTRY ||
[email protected]04cb7542010-10-25 10:50:06242 location == Extension::EXTERNAL_PREF_DOWNLOAD ||
243 location == Extension::EXTERNAL_POLICY_DOWNLOAD;
244 }
245
246 // Whether extensions with |location| are auto-updatable or not.
247 static inline bool IsAutoUpdateableLocation(Location location) {
248 // Only internal and external extensions can be autoupdated.
249 return location == Extension::INTERNAL ||
250 IsExternalLocation(location);
[email protected]25b34332009-06-05 21:53:19251 }
252
[email protected]95da88c42011-03-31 10:07:33253 // Whether extensions with |location| can be uninstalled or not. Policy
254 // controlled extensions are silently auto-installed and updated, and cannot
255 // be disabled by the user. The same applies for internal components.
256 static inline bool UserMayDisable(Location location) {
257 return location != Extension::EXTERNAL_POLICY_DOWNLOAD &&
258 location != Extension::COMPONENT;
259 }
260
[email protected]542258c2011-03-04 21:25:31261 // Whether extensions with |location| should be loaded with strict
262 // error checking. Strict error checks may flag errors older versions
263 // of chrome did not detect. To avoid breaking installed extensions,
264 // strict checks are disabled unless the location indicates that the
265 // developer is loading the extension, or the extension is a component
266 // of chrome.
267 static inline bool ShouldDoStrictErrorChecking(Location location) {
268 return location == Extension::LOAD ||
269 location == Extension::COMPONENT;
270 }
271
[email protected]3aff9ad2011-04-01 20:26:48272 // Unpacked extensions start off with file access since they are a developer
273 // feature.
274 static inline bool ShouldAlwaysAllowFileAccess(Location location) {
275 return location == Extension::LOAD;
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]c690a9812009-12-17 05:55:32318 // Given an extension and icon size, read it if present and decode it into
[email protected]ae2e0f92010-04-06 20:32:23319 // result. In the browser process, this will DCHECK if not called on the
320 // file thread. To easily load extension images on the UI thread, see
321 // ImageLoadingTracker.
[email protected]9adb9692010-10-29 23:14:02322 static void DecodeIcon(const Extension* extension,
[email protected]c690a9812009-12-17 05:55:32323 Icons icon_size,
324 scoped_ptr<SkBitmap>* result);
325
326 // Given an icon_path 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]c690a9812009-12-17 05:55:32330 static void DecodeIconFromPath(const FilePath& icon_path,
331 Icons icon_size,
332 scoped_ptr<SkBitmap>* result);
333
[email protected]5349ac6d2011-04-05 22:20:17334 // Returns the default extension/app icon (for extensions or apps that don't
335 // have one).
336 static const SkBitmap& GetDefaultIcon(bool is_app);
337
[email protected]a807bbe2010-04-14 10:51:19338 // Returns the base extension url for a given |extension_id|.
339 static GURL GetBaseURLFromExtensionId(const std::string& extension_id);
340
[email protected]ec5b50d2010-10-09 16:35:18341 // Returns the url prefix for the extension/apps gallery. Can be set via the
[email protected]ce3eb7b62010-09-14 13:57:44342 // --apps-gallery-url switch. The URL returned will not contain a trailing
[email protected]d3071992010-10-08 15:24:07343 // slash. Do not use this as a prefix/extent for the store. Instead see
[email protected]eaa7dd182010-12-14 11:09:00344 // ExtensionService::GetWebStoreApp or
345 // ExtensionService::IsDownloadFromGallery
[email protected]d3071992010-10-08 15:24:07346 static std::string ChromeStoreLaunchURL();
[email protected]a0cf04a2010-06-23 03:29:55347
[email protected]be7e5cb2010-10-04 12:53:17348 // Adds an extension to the scripting whitelist. Used for testing only.
[email protected]10fb1992010-10-08 09:00:17349 static void SetScriptingWhitelist(const ScriptingWhitelist& whitelist);
[email protected]2a521c52011-01-26 18:45:21350 static const ScriptingWhitelist* GetScriptingWhitelist();
[email protected]be7e5cb2010-10-04 12:53:17351
[email protected]0d3e4a22011-06-23 19:02:52352 bool HasAPIPermission(ExtensionAPIPermission::ID permission) const;
353 bool HasAPIPermission(const std::string& function_name) const;
[email protected]583d45c12010-08-31 02:48:12354
[email protected]0d3e4a22011-06-23 19:02:52355 const URLPatternSet& GetEffectiveHostPermissions() const;
[email protected]b24d8312009-08-27 06:47:46356
[email protected]fbd17cf2010-04-28 23:52:56357 // Whether or not the extension is allowed permission for a URL pattern from
358 // the manifest. http, https, and chrome://favicon/ is allowed for all
359 // extensions, while component extensions are allowed access to
360 // chrome://resources.
[email protected]a3caa822010-10-26 18:10:00361 bool CanSpecifyHostPermission(const URLPattern& pattern) const;
[email protected]fbd17cf2010-04-28 23:52:56362
[email protected]584b8e3f2010-04-10 00:23:37363 // Whether the extension has access to the given URL.
364 bool HasHostPermission(const GURL& url) const;
365
[email protected]0df165f2010-09-28 16:49:40366 // Whether the extension has effective access to all hosts. This is true if
367 // there is a content script that matches all hosts, if there is a host
368 // permission grants access to all hosts (like <all_urls>) or an api
369 // permission that effectively grants access to all hosts (e.g. proxy,
370 // network, etc.)
371 bool HasEffectiveAccessToAllHosts() const;
[email protected]b24d8312009-08-27 06:47:46372
[email protected]8d888c12010-11-30 00:00:25373 // Whether the extension effectively has all permissions (for example, by
374 // having an NPAPI plugin).
375 bool HasFullPermissions() const;
376
[email protected]5df6a5d2011-01-26 07:39:12377 // Whether context menu should be shown for page and browser actions.
378 bool ShowConfigureContextMenus() const;
379
[email protected]37cd64d2010-10-25 18:17:58380 // Returns the Homepage URL for this extension. If homepage_url was not
381 // specified in the manifest, this returns the Google Gallery URL. For
[email protected]bfa90a3a2010-04-28 15:43:23382 // third-party extensions, this returns a blank GURL.
[email protected]37cd64d2010-10-25 18:17:58383 GURL GetHomepageURL() const;
[email protected]bfa90a3a2010-04-28 15:43:23384
[email protected]facd7a7652009-06-05 23:15:02385 // Returns a list of paths (relative to the extension dir) for images that
386 // the browser might load (like themes and page action icons).
[email protected]9adb9692010-10-29 23:14:02387 std::set<FilePath> GetBrowserImages() const;
[email protected]facd7a7652009-06-05 23:15:02388
[email protected]807871f2010-09-16 01:04:48389 // Get an extension icon as a resource or URL.
[email protected]9adb9692010-10-29 23:14:02390 ExtensionResource GetIconResource(
391 int size, ExtensionIconSet::MatchType match_type) const;
392 GURL GetIconURL(int size, ExtensionIconSet::MatchType match_type) const;
[email protected]f34e79632010-03-17 02:34:08393
[email protected]867a73e12010-03-19 20:45:46394 // Gets the fully resolved absolute launch URL.
395 GURL GetFullLaunchURL() const;
[email protected]2a521c52011-01-26 18:45:21396
[email protected]6f229e82010-11-02 17:47:26397 // Image cache related methods. These are only valid on the UI thread and
398 // not maintained by this class. See ImageLoadingTracker for usage. The
399 // |original_size| parameter should be the size of the image at |source|
400 // before any scaling may have been done to produce the pixels in |image|.
401 void SetCachedImage(const ExtensionResource& source,
402 const SkBitmap& image,
403 const gfx::Size& original_size) const;
404 bool HasCachedImage(const ExtensionResource& source,
405 const gfx::Size& max_size) const;
406 SkBitmap GetCachedImage(const ExtensionResource& source,
407 const gfx::Size& max_size) const;
[email protected]2a521c52011-01-26 18:45:21408
409 // Returns true if this extension can execute script on a page. If a
410 // UserScript object is passed, permission to run that specific script is
411 // checked (using its matches list). Otherwise, permission to execute script
412 // programmatically is checked (using the extension's host permission).
413 //
414 // This method is also aware of certain special pages that extensions are
415 // usually not allowed to run script on.
416 bool CanExecuteScriptOnPage(const GURL& page_url,
[email protected]3aff9ad2011-04-01 20:26:48417 const UserScript* script,
[email protected]2a521c52011-01-26 18:45:21418 std::string* error) const;
419
[email protected]6f229e82010-11-02 17:47:26420 // Returns true if this extension is a COMPONENT extension, or if it is
421 // on the whitelist of extensions that can script all pages.
422 bool CanExecuteScriptEverywhere() const;
423
[email protected]5efbfe012011-02-22 23:07:18424 // Returns true if this extension is allowed to obtain the contents of a
425 // page as an image. Since a page may contain sensitive information, this
426 // is restricted to the extension's host permissions as well as the
427 // extension page itself.
428 bool CanCaptureVisiblePage(const GURL& page_url, std::string* error) const;
429
[email protected]a65882c2010-11-12 15:15:09430 // Returns true if this extension updates itself using the extension
431 // gallery.
432 bool UpdatesFromGallery() const;
433
[email protected]cca147172011-02-17 01:29:29434 // Returns true if this extension or app includes areas within |origin|.
435 bool OverlapsWithOrigin(const GURL& origin) const;
436
[email protected]6f229e82010-11-02 17:47:26437 // Accessors:
438
439 const FilePath& path() const { return path_; }
440 const GURL& url() const { return extension_url_; }
441 Location location() const { return location_; }
442 const std::string& id() const { return id_; }
443 const Version* version() const { return version_.get(); }
444 const std::string VersionString() const;
445 const std::string& name() const { return name_; }
446 const std::string& public_key() const { return public_key_; }
447 const std::string& description() const { return description_; }
448 bool converted_from_user_script() const {
449 return converted_from_user_script_;
450 }
451 const UserScriptList& content_scripts() const { return content_scripts_; }
452 ExtensionAction* page_action() const { return page_action_.get(); }
453 ExtensionAction* browser_action() const { return browser_action_.get(); }
[email protected]e2dffe02011-01-20 20:30:06454 ExtensionSidebarDefaults* sidebar_defaults() const {
455 return sidebar_defaults_.get();
456 }
[email protected]b6b805e92011-04-16 09:24:14457 const FileBrowserHandlerList* file_browser_handlers() const {
458 return file_browser_handlers_.get();
459 }
[email protected]6f229e82010-11-02 17:47:26460 const std::vector<PluginInfo>& plugins() const { return plugins_; }
[email protected]65378f52011-04-08 02:31:23461 const std::vector<NaClModuleInfo>& nacl_modules() const {
462 return nacl_modules_;
463 }
[email protected]b0820372011-06-03 07:05:27464 const std::vector<InputComponentInfo>& input_components() const {
465 return input_components_;
466 }
[email protected]6f229e82010-11-02 17:47:26467 const GURL& background_url() const { return background_url_; }
468 const GURL& options_url() const { return options_url_; }
469 const GURL& devtools_url() const { return devtools_url_; }
470 const std::vector<GURL>& toolstrips() const { return toolstrips_; }
[email protected]0d3e4a22011-06-23 19:02:52471 const ExtensionPermissionSet* permission_set() const {
472 return permission_set_.get();
[email protected]6f229e82010-11-02 17:47:26473 }
[email protected]6f229e82010-11-02 17:47:26474 const GURL& update_url() const { return update_url_; }
475 const ExtensionIconSet& icons() const { return icons_; }
476 const DictionaryValue* manifest_value() const {
477 return manifest_value_.get();
478 }
479 const std::string default_locale() const { return default_locale_; }
480 const URLOverrideMap& GetChromeURLOverrides() const {
481 return chrome_url_overrides_;
482 }
483 const std::string omnibox_keyword() const { return omnibox_keyword_; }
484 bool incognito_split_mode() const { return incognito_split_mode_; }
[email protected]a4a38c12010-12-23 16:43:56485 const std::vector<TtsVoice>& tts_voices() const { return tts_voices_; }
[email protected]6f229e82010-11-02 17:47:26486
[email protected]3aff9ad2011-04-01 20:26:48487 bool wants_file_access() const { return wants_file_access_; }
488
[email protected]7f7b9d932011-04-20 16:13:26489 const std::string& content_security_policy() const {
490 return content_security_policy_;
491 }
492
[email protected]6f229e82010-11-02 17:47:26493 // App-related.
494 bool is_app() const { return is_app_; }
495 bool is_hosted_app() const { return is_app() && !web_extent().is_empty(); }
496 bool is_packaged_app() const { return is_app() && web_extent().is_empty(); }
[email protected]d9696672011-03-15 22:45:09497 bool is_storage_isolated() const { return is_app() && is_storage_isolated_; }
[email protected]cced75a2011-05-20 08:31:12498 const URLPatternSet& web_extent() const { return extent_; }
[email protected]6f229e82010-11-02 17:47:26499 const std::string& launch_local_path() const { return launch_local_path_; }
500 const std::string& launch_web_url() const { return launch_web_url_; }
501 extension_misc::LaunchContainer launch_container() const {
502 return launch_container_;
503 }
504 int launch_width() const { return launch_width_; }
505 int launch_height() const { return launch_height_; }
506
507 // Theme-related.
508 bool is_theme() const { return is_theme_; }
509 DictionaryValue* GetThemeImages() const { return theme_images_.get(); }
510 DictionaryValue* GetThemeColors() const {return theme_colors_.get(); }
511 DictionaryValue* GetThemeTints() const { return theme_tints_.get(); }
512 DictionaryValue* GetThemeDisplayProperties() const {
513 return theme_display_properties_.get();
514 }
515
[email protected]4a8d3272009-03-10 19:15:08516 private:
[email protected]66e4eb32010-10-27 20:37:41517 friend class base::RefCountedThreadSafe<Extension>;
518
[email protected]d7e9a862010-11-03 21:57:49519 // We keep a cache of images loaded from extension resources based on their
520 // path and a string representation of a size that may have been used to
521 // scale it (or the empty string if the image is at its original size).
522 typedef std::pair<FilePath, std::string> ImageCacheKey;
523 typedef std::map<ImageCacheKey, SkBitmap> ImageCache;
524
[email protected]4ead6f72010-10-13 19:54:18525 // Normalize the path for use by the extension. On Windows, this will make
526 // sure the drive letter is uppercase.
527 static FilePath MaybeNormalizePath(const FilePath& path);
528
[email protected]d6a5c78c2010-12-07 05:18:15529 // Returns the distinct hosts that can be displayed in the install UI or be
530 // used for privilege comparisons. This discards some of the detail that is
531 // present in the manifest to make it as easy as possible to process by users.
532 // In particular we disregard the scheme and path components of URLPatterns
533 // and de-dupe the result, which includes filtering out common hosts with
534 // differing RCDs. If |include_rcd| is true, then the de-duped result
535 // will be the first full entry, including its RCD. So if the list was
536 // "*.google.co.uk" and "*.google.com", the returned value would just be
537 // "*.google.co.uk". Keeping the RCD in the result is useful for display
538 // purposes when you want to show the user one sample hostname from the list.
539 // If you need to compare two URLPatternLists for security equality, then set
540 // |include_rcd| to false, which will return a result like "*.google.",
541 // regardless of the order of the patterns.
542 static std::vector<std::string> GetDistinctHosts(
543 const URLPatternList& host_patterns, bool include_rcd);
544
[email protected]66e4eb32010-10-27 20:37:41545 Extension(const FilePath& path, Location location);
546 ~Extension();
547
548 // Initialize the extension from a parsed manifest.
[email protected]83048a22011-03-29 00:14:13549 bool InitFromValue(const DictionaryValue& value, int flags,
550 std::string* error);
[email protected]66e4eb32010-10-27 20:37:41551
[email protected]052c92702010-06-25 07:25:52552 // Helper function for implementing HasCachedImage/GetCachedImage. A return
553 // value of NULL means there is no matching image cached (we allow caching an
554 // empty SkBitmap).
555 SkBitmap* GetCachedImageImpl(const ExtensionResource& source,
[email protected]9adb9692010-10-29 23:14:02556 const gfx::Size& max_size) const;
[email protected]d9ad80f2010-03-30 20:40:18557
[email protected]3cfbd0e2009-03-18 21:26:24558 // Helper method that loads a UserScript object from a
559 // dictionary in the content_script list of the manifest.
560 bool LoadUserScriptHelper(const DictionaryValue* content_script,
561 int definition_index,
[email protected]3aff9ad2011-04-01 20:26:48562 int flags,
[email protected]3cfbd0e2009-03-18 21:26:24563 std::string* error,
564 UserScript* result);
[email protected]f7f3a5f2009-05-01 22:02:34565
[email protected]6657afa62009-11-04 02:15:20566 // Helper method that loads either the include_globs or exclude_globs list
567 // from an entry in the content_script lists of the manifest.
568 bool LoadGlobsHelper(const DictionaryValue* content_script,
569 int content_script_index,
[email protected]e2194742010-08-12 05:54:34570 const char* globs_property_name,
[email protected]6657afa62009-11-04 02:15:20571 std::string* error,
[email protected]11f4857282009-11-13 19:56:17572 void(UserScript::*add_method)(const std::string& glob),
[email protected]6657afa62009-11-04 02:15:20573 UserScript *instance);
574
[email protected]867a73e12010-03-19 20:45:46575 // Helpers to load various chunks of the manifest.
[email protected]3ba0fd32010-06-19 05:39:10576 bool LoadIsApp(const DictionaryValue* manifest, std::string* error);
[email protected]542258c2011-03-04 21:25:31577 bool LoadExtent(const DictionaryValue* manifest,
578 const char* key,
[email protected]cced75a2011-05-20 08:31:12579 URLPatternSet* extent,
[email protected]542258c2011-03-04 21:25:31580 const char* list_error,
581 const char* value_error,
582 URLPattern::ParseOption parse_strictness,
583 std::string* error);
[email protected]867a73e12010-03-19 20:45:46584 bool LoadLaunchContainer(const DictionaryValue* manifest, std::string* error);
585 bool LoadLaunchURL(const DictionaryValue* manifest, std::string* error);
[email protected]d9696672011-03-15 22:45:09586 bool LoadAppIsolation(const DictionaryValue* manifest, std::string* error);
[email protected]cbf4d1912010-08-12 18:24:57587 bool EnsureNotHybridApp(const DictionaryValue* manifest, std::string* error);
[email protected]867a73e12010-03-19 20:45:46588
[email protected]5d246db22009-10-27 06:17:57589 // Helper method to load an ExtensionAction from the page_action or
[email protected]92c6f9b92009-10-24 04:35:08590 // browser_action entries in the manifest.
[email protected]5d246db22009-10-27 06:17:57591 ExtensionAction* LoadExtensionActionHelper(
[email protected]92c6f9b92009-10-24 04:35:08592 const DictionaryValue* extension_action, std::string* error);
593
[email protected]b6b805e92011-04-16 09:24:14594 // Helper method to load an FileBrowserHandlerList from the manifest.
595 FileBrowserHandlerList* LoadFileBrowserHandlers(
596 const ListValue* extension_actions, std::string* error);
597 // Helper method to load an FileBrowserHandler from manifest.
598 FileBrowserHandler* LoadFileBrowserHandler(
599 const DictionaryValue* file_browser_handlers, std::string* error);
600
[email protected]e2dffe02011-01-20 20:30:06601 // Helper method to load an ExtensionSidebarDefaults from the sidebar manifest
602 // entry.
603 ExtensionSidebarDefaults* LoadExtensionSidebarDefaults(
604 const DictionaryValue* sidebar, std::string* error);
605
[email protected]2f6698b2010-10-14 00:58:21606 // Returns true if the extension has more than one "UI surface". For example,
607 // an extension that has a browser action and a page action.
608 bool HasMultipleUISurfaces() const;
609
[email protected]e2eb43112009-05-29 21:19:54610 // Figures out if a source contains keys not associated with themes - we
611 // don't want to allow scripts and such to be bundled with themes.
[email protected]9adb9692010-10-29 23:14:02612 bool ContainsNonThemeKeys(const DictionaryValue& source) const;
[email protected]e2eb43112009-05-29 21:19:54613
[email protected]311e4cc12011-06-07 01:40:40614 // Only allow the experimental API permission if the command line
615 // flag is present.
[email protected]0d3e4a22011-06-23 19:02:52616 bool IsDisallowedExperimentalPermission(
617 ExtensionAPIPermission::ID permission) const;
[email protected]1952c7d2010-03-04 23:48:34618
[email protected]9d4c2c52011-04-07 18:53:10619 // Returns true if this is a component, or we are not attempting to access a
620 // component-private permission.
[email protected]0d3e4a22011-06-23 19:02:52621 bool IsComponentOnlyPermission(const ExtensionAPIPermission* api) const;
[email protected]a2a098d2010-09-29 19:42:55622
[email protected]d7e9a862010-11-03 21:57:49623 // Cached images for this extension. This should only be touched on the UI
624 // thread.
625 mutable ImageCache image_cache_;
[email protected]1e8c93f2010-02-08 22:58:31626
[email protected]6f229e82010-11-02 17:47:26627 // A persistent, globally unique ID. An extension's ID is used in things
628 // like directory structures and URLs, and is expected to not change across
629 // versions. It is generated as a SHA-256 hash of the extension's public
630 // key, or as a hash of the path in the case of unpacked extensions.
631 std::string id_;
632
633 // The extension's human-readable name. Name is used for display purpose. It
634 // might be wrapped with unicode bidi control characters so that it is
635 // displayed correctly in RTL context.
636 // NOTE: Name is UTF-8 and may contain non-ascii characters.
637 std::string name_;
638
639 // The absolute path to the directory the extension is stored in.
640 FilePath path_;
641
642 // Default locale for fall back. Can be empty if extension is not localized.
643 std::string default_locale_;
644
645 // If true, a separate process will be used for the extension in incognito
646 // mode.
647 bool incognito_split_mode_;
648
649 // Defines the set of URLs in the extension's web content.
[email protected]cced75a2011-05-20 08:31:12650 URLPatternSet extent_;
[email protected]6f229e82010-11-02 17:47:26651
[email protected]0d3e4a22011-06-23 19:02:52652 // The set of permissions that the extension effectively has access to.
653 scoped_ptr<ExtensionPermissionSet> permission_set_;
[email protected]6f229e82010-11-02 17:47:26654
655 // The icons for the extension.
656 ExtensionIconSet icons_;
657
658 // The base extension url for the extension.
659 GURL extension_url_;
660
661 // The location the extension was loaded from.
662 Location location_;
663
664 // The extension's version.
665 scoped_ptr<Version> version_;
666
667 // An optional longer description of the extension.
668 std::string description_;
669
670 // True if the extension was generated from a user script. (We show slightly
671 // different UI if so).
672 bool converted_from_user_script_;
673
674 // Paths to the content scripts the extension contains.
675 UserScriptList content_scripts_;
676
677 // The extension's page action, if any.
678 scoped_ptr<ExtensionAction> page_action_;
679
680 // The extension's browser action, if any.
681 scoped_ptr<ExtensionAction> browser_action_;
682
[email protected]b6b805e92011-04-16 09:24:14683 // The extension's file browser actions, if any.
684 scoped_ptr<FileBrowserHandlerList> file_browser_handlers_;
685
[email protected]e2dffe02011-01-20 20:30:06686 // The extension's sidebar, if any.
687 scoped_ptr<ExtensionSidebarDefaults> sidebar_defaults_;
688
[email protected]6f229e82010-11-02 17:47:26689 // Optional list of NPAPI plugins and associated properties.
690 std::vector<PluginInfo> plugins_;
691
[email protected]65378f52011-04-08 02:31:23692 // Optional list of NaCl modules and associated properties.
693 std::vector<NaClModuleInfo> nacl_modules_;
694
[email protected]b0820372011-06-03 07:05:27695 // Optional list of input components and associated properties.
696 std::vector<InputComponentInfo> input_components_;
697
[email protected]6f229e82010-11-02 17:47:26698 // Optional URL to a master page of which a single instance should be always
699 // loaded in the background.
700 GURL background_url_;
701
702 // Optional URL to a page for setting options/preferences.
703 GURL options_url_;
704
705 // Optional URL to a devtools extension page.
706 GURL devtools_url_;
707
708 // Optional list of toolstrips and associated properties.
709 std::vector<GURL> toolstrips_;
710
711 // The public key used to sign the contents of the crx package.
712 std::string public_key_;
713
714 // A map of resource id's to relative file paths.
715 scoped_ptr<DictionaryValue> theme_images_;
716
717 // A map of color names to colors.
718 scoped_ptr<DictionaryValue> theme_colors_;
719
720 // A map of color names to colors.
721 scoped_ptr<DictionaryValue> theme_tints_;
722
723 // A map of display properties.
724 scoped_ptr<DictionaryValue> theme_display_properties_;
725
726 // Whether the extension is a theme.
727 bool is_theme_;
728
[email protected]6f229e82010-11-02 17:47:26729 // The homepage for this extension. Useful if it is not hosted by Google and
730 // therefore does not have a Gallery URL.
731 GURL homepage_url_;
732
733 // URL for fetching an update manifest
734 GURL update_url_;
735
736 // A copy of the manifest that this extension was created from.
737 scoped_ptr<DictionaryValue> manifest_value_;
738
739 // A map of chrome:// hostnames (newtab, downloads, etc.) to Extension URLs
740 // which override the handling of those URLs. (see ExtensionOverrideUI).
741 URLOverrideMap chrome_url_overrides_;
742
743 // Whether this extension uses app features.
744 bool is_app_;
745
[email protected]d9696672011-03-15 22:45:09746 // Whether this extension requests isolated storage.
747 bool is_storage_isolated_;
748
[email protected]6f229e82010-11-02 17:47:26749 // The local path inside the extension to use with the launcher.
750 std::string launch_local_path_;
751
752 // A web url to use with the launcher. Note that this might be relative or
753 // absolute. If relative, it is relative to web_origin.
754 std::string launch_web_url_;
755
[email protected]4e595682011-02-09 17:07:02756 // The window type that an app's manifest specifies to launch into.
757 // This is not always the window type an app will open into, because
758 // users can override the way each app launches. See
759 // ExtensionPrefs::GetLaunchContainer(), which looks at a per-app pref
760 // to decide what container an app will launch in.
[email protected]6f229e82010-11-02 17:47:26761 extension_misc::LaunchContainer launch_container_;
762
763 // The default size of the container when launching. Only respected for
764 // containers like panels and windows.
765 int launch_width_;
766 int launch_height_;
767
768 // The Omnibox keyword for this extension, or empty if there is none.
769 std::string omnibox_keyword_;
770
[email protected]a4a38c12010-12-23 16:43:56771 // List of text-to-speech voices that this extension provides, if any.
772 std::vector<TtsVoice> tts_voices_;
773
[email protected]3aff9ad2011-04-01 20:26:48774 // Whether the extension has host permissions or user script patterns that
775 // imply access to file:/// scheme URLs (the user may not have actually
776 // granted it that access).
777 bool wants_file_access_;
778
[email protected]7f7b9d932011-04-20 16:13:26779 // The Content-Security-Policy for this extension. Extensions can use
780 // Content-Security-Policies to mitigate cross-site scripting and other
781 // vulnerabilities.
782 std::string content_security_policy_;
783
[email protected]eaa7dd182010-12-14 11:09:00784 FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
[email protected]5eb375e92010-11-26 07:50:41785 UpdateExtensionPreservesLocation);
[email protected]19118d52010-07-26 22:13:42786 FRIEND_TEST_ALL_PREFIXES(ExtensionTest, LoadPageActionHelper);
[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.
[email protected]b05fb9ff2011-04-23 00:07:56814//
815// TODO(akalin): Now that sync doesn't need to listen to
816// EXTENSION_UNINSTALLED, everything but |extension_id| can be
817// removed.
[email protected]211030342010-09-30 18:41:06818struct UninstalledExtensionInfo {
819 explicit UninstalledExtensionInfo(const Extension& extension);
[email protected]9b2331d92010-10-04 23:11:19820 ~UninstalledExtensionInfo();
[email protected]211030342010-09-30 18:41:06821
822 std::string extension_id;
823 std::set<std::string> extension_api_permissions;
[email protected]7fa19f82010-12-21 19:40:08824 Extension::Type extension_type;
[email protected]76c25112010-10-01 22:37:21825 GURL update_url;
[email protected]211030342010-09-30 18:41:06826};
827
[email protected]a9f39a312010-12-23 22:14:27828struct UnloadedExtensionInfo {
829 enum Reason {
830 DISABLE, // The extension is being disabled.
831 UPDATE, // The extension is being updated to a newer version.
832 UNINSTALL, // The extension is being uninstalled.
833 };
834
835 Reason reason;
836
837 // Was the extension already disabled?
838 bool already_disabled;
839
840 // The extension being unloaded - this should always be non-NULL.
841 const Extension* extension;
842
843 UnloadedExtensionInfo(const Extension* extension, Reason reason);
844};
845
[email protected]5b1a0e22009-05-26 19:00:58846#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_H_