blob: 08e4bbb138c1d18dadb410a6afc57f1466973a5a [file] [log] [blame]
[email protected]225c8f52010-02-05 22:23:201// Copyright (c) 2010 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]cc655912009-01-29 23:19:1916#include "base/scoped_ptr.h"
[email protected]4361c7c2010-09-30 21:57:5317#include "base/ref_counted.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]052c92702010-06-25 07:25:5223#include "gfx/size.h"
[email protected]eab9b452009-01-23 20:48:5924#include "googleurl/src/gurl.h"
25
[email protected]3bb84992010-08-26 17:23:4626class DictionaryValue;
[email protected]942690b132010-05-11 06:42:1427class ExtensionAction;
28class ExtensionResource;
[email protected]12802702010-07-09 19:43:0929class SkBitmap;
[email protected]daf66aa2010-08-06 06:24:2830class Version;
[email protected]942690b132010-05-11 06:42:1431
[email protected]f0755532010-06-22 07:27:2532// Represents a Chrome extension.
[email protected]66e4eb32010-10-27 20:37:4133class Extension : public base::RefCountedThreadSafe<Extension> {
[email protected]7713d632008-12-02 07:52:3334 public:
[email protected]d3cfa482009-10-17 13:54:5735 typedef std::map<const std::string, GURL> URLOverrideMap;
[email protected]10fb1992010-10-08 09:00:1736 typedef std::vector<std::string> ScriptingWhitelist;
[email protected]b24d8312009-08-27 06:47:4637
[email protected]631cf822009-05-15 07:01:2538 // What an extension was loaded from.
[email protected]9b217652010-10-08 22:04:2339 // NOTE: These values are stored as integers in the preferences and used
40 // in histograms so don't remove or reorder existing items. Just append
41 // to the end.
[email protected]631cf822009-05-15 07:01:2542 enum Location {
43 INVALID,
[email protected]25b34332009-06-05 21:53:1944 INTERNAL, // A crx file from the internal Extensions directory.
45 EXTERNAL_PREF, // A crx file from an external directory (via prefs).
46 EXTERNAL_REGISTRY, // A crx file from an external directory (via eg the
47 // registry on Windows).
[email protected]1952c7d2010-03-04 23:48:3448 LOAD, // --load-extension.
[email protected]8ef78fd2010-08-19 17:14:3249 COMPONENT, // An integral component of Chrome itself, which
50 // happens to be implemented as an extension. We don't
51 // show these in the management UI.
[email protected]04cb7542010-10-25 10:50:0652 EXTERNAL_PREF_DOWNLOAD, // A crx file from an external directory (via
[email protected]8ef78fd2010-08-19 17:14:3253 // prefs), installed from an update URL.
[email protected]04cb7542010-10-25 10:50:0654 EXTERNAL_POLICY_DOWNLOAD, // A crx file from an external directory (via
55 // admin policies), installed from an update URL.
56
57 NUM_LOCATIONS
[email protected]25b34332009-06-05 21:53:1958 };
59
60 enum State {
[email protected]0c6da502009-08-14 22:32:3961 DISABLED = 0,
[email protected]25b34332009-06-05 21:53:1962 ENABLED,
63 KILLBIT, // Don't install/upgrade (applies to external extensions only).
[email protected]0c6da502009-08-14 22:32:3964
65 NUM_STATES
[email protected]631cf822009-05-15 07:01:2566 };
[email protected]7713d632008-12-02 07:52:3367
[email protected]fbcc40302009-06-12 20:45:4568 enum InstallType {
[email protected]ab6f2b22009-07-28 23:28:3769 INSTALL_ERROR,
[email protected]fbcc40302009-06-12 20:45:4570 DOWNGRADE,
71 REINSTALL,
72 UPGRADE,
73 NEW_INSTALL
74 };
75
[email protected]d2817012009-08-04 06:46:2176 // NOTE: If you change this list, you should also change kIconSizes in the cc
77 // file.
78 enum Icons {
79 EXTENSION_ICON_LARGE = 128,
80 EXTENSION_ICON_MEDIUM = 48,
81 EXTENSION_ICON_SMALL = 32,
[email protected]39382942010-03-23 15:57:0982 EXTENSION_ICON_SMALLISH = 24,
[email protected]d2817012009-08-04 06:46:2183 EXTENSION_ICON_BITTY = 16,
84 };
85
[email protected]9b217652010-10-08 22:04:2386 // Type used for UMA_HISTOGRAM_ENUMERATION about extensions.
87 // Do not change the order of entries or remove entries in this list.
88 enum HistogramType {
89 TYPE_UNKNOWN = 0,
90 TYPE_EXTENSION,
91 TYPE_THEME,
92 TYPE_USER_SCRIPT,
93 TYPE_HOSTED_APP,
94 TYPE_PACKAGED_APP
95 };
96
[email protected]92888082010-10-18 19:24:5797 // An NPAPI plugin included in the extension.
98 struct PluginInfo {
99 FilePath path; // Path to the plugin.
100 bool is_public; // False if only this extension can load this plugin.
101 };
102
[email protected]4361c7c2010-09-30 21:57:53103 // Contains a subset of the extension's data that doesn't change once
104 // initialized, and therefore shareable across threads without locking.
105 struct StaticData : public base::RefCountedThreadSafe<StaticData> {
106 StaticData();
107
[email protected]4361c7c2010-09-30 21:57:53108 // A persistent, globally unique ID. An extension's ID is used in things
109 // like directory structures and URLs, and is expected to not change across
110 // versions. It is generated as a SHA-256 hash of the extension's public
111 // key, or as a hash of the path in the case of unpacked extensions.
112 std::string id;
113
114 // The extension's human-readable name. Name is used for display purpose. It
115 // might be wrapped with unicode bidi control characters so that it is
116 // displayed correctly in RTL context.
117 // NOTE: Name is UTF-8 and may contain non-ascii characters.
118 std::string name;
119
120 // The absolute path to the directory the extension is stored in.
121 FilePath path;
122
123 // Default locale for fall back. Can be empty if extension is not localized.
124 std::string default_locale;
125
126 // If true, a separate process will be used for the extension in incognito
127 // mode.
128 bool incognito_split_mode;
129
130 // Defines the set of URLs in the extension's web content.
131 ExtensionExtent extent;
132
[email protected]11540af2010-10-14 14:38:00133 // The set of host permissions that the extension effectively has access to,
134 // which is a merge of host_permissions_ and all of the match patterns in
135 // any content scripts the extension has. This is used to determine which
136 // URLs have the ability to load an extension's resources via embedded
137 // chrome-extension: URLs (see extension_protocols.cc).
[email protected]4361c7c2010-09-30 21:57:53138 ExtensionExtent effective_host_permissions;
139
140 // The set of module-level APIs this extension can use.
141 std::set<std::string> api_permissions;
142
143 // The icons for the extension.
144 ExtensionIconSet icons;
145
[email protected]92888082010-10-18 19:24:57146 // The base extension url for the extension.
147 GURL extension_url;
148
149 // The location the extension was loaded from.
150 Location location;
151
152 // The extension's version.
153 scoped_ptr<Version> version;
154
155 // An optional longer description of the extension.
156 std::string description;
157
158 // True if the extension was generated from a user script. (We show slightly
159 // different UI if so).
160 bool converted_from_user_script;
161
162 // Paths to the content scripts the extension contains.
163 UserScriptList content_scripts;
164
165 // The extension's page action, if any.
166 scoped_ptr<ExtensionAction> page_action;
167
168 // The extension's browser action, if any.
169 scoped_ptr<ExtensionAction> browser_action;
170
171 // Optional list of NPAPI plugins and associated properties.
172 std::vector<PluginInfo> plugins;
173
174 // Optional URL to a master page of which a single instance should be always
175 // loaded in the background.
176 GURL background_url;
177
178 // Optional URL to a page for setting options/preferences.
179 GURL options_url;
180
181 // Optional URL to a devtools extension page.
182 GURL devtools_url;
183
184 // Optional list of toolstrips and associated properties.
185 std::vector<GURL> toolstrips;
186
187 // The public key used to sign the contents of the crx package.
188 std::string public_key;
189
190 // A map of resource id's to relative file paths.
191 scoped_ptr<DictionaryValue> theme_images;
192
193 // A map of color names to colors.
194 scoped_ptr<DictionaryValue> theme_colors;
195
196 // A map of color names to colors.
197 scoped_ptr<DictionaryValue> theme_tints;
198
199 // A map of display properties.
200 scoped_ptr<DictionaryValue> theme_display_properties;
201
202 // Whether the extension is a theme.
203 bool is_theme;
204
205 // The sites this extension has permission to talk to (using XHR, etc).
206 URLPatternList host_permissions;
207
[email protected]37cd64d2010-10-25 18:17:58208 // The homepage for this extension. Useful if it is not hosted by Google and
209 // therefore does not have a Gallery URL.
210 GURL homepage_url;
211
[email protected]92888082010-10-18 19:24:57212 // URL for fetching an update manifest
213 GURL update_url;
214
215 // A copy of the manifest that this extension was created from.
216 scoped_ptr<DictionaryValue> manifest_value;
217
218 // A map of chrome:// hostnames (newtab, downloads, etc.) to Extension URLs
219 // which override the handling of those URLs.
220 URLOverrideMap chrome_url_overrides;
221
222 // Whether this extension uses app features.
223 bool is_app;
224
225 // The local path inside the extension to use with the launcher.
226 std::string launch_local_path;
227
228 // A web url to use with the launcher. Note that this might be relative or
229 // absolute. If relative, it is relative to web_origin.
230 std::string launch_web_url;
231
232 // The type of container to launch into.
233 extension_misc::LaunchContainer launch_container;
234
235 // The default size of the container when launching. Only respected for
236 // containers like panels and windows.
237 int launch_width;
238 int launch_height;
239
240 // The Omnibox keyword for this extension, or empty if there is none.
241 std::string omnibox_keyword;
242
[email protected]4361c7c2010-09-30 21:57:53243 protected:
244 friend class base::RefCountedThreadSafe<StaticData>;
245 ~StaticData();
246 };
247
[email protected]92888082010-10-18 19:24:57248 // Contains the subset of the extension's (private) data that can be modified
249 // after initialization. This class should only be accessed on the UI thread.
250 struct RuntimeData {
251 // We keep a cache of images loaded from extension resources based on their
252 // path and a string representation of a size that may have been used to
253 // scale it (or the empty string if the image is at its original size).
254 typedef std::pair<FilePath, std::string> ImageCacheKey;
255 typedef std::map<ImageCacheKey, SkBitmap> ImageCache;
256
257 RuntimeData();
258 ~RuntimeData();
259
260 // True if the background page is ready.
261 bool background_page_ready;
262
263 // True while the extension is being upgraded.
264 bool being_upgraded;
265
266 // Cached images for this extension.
267 ImageCache image_cache_;
268 };
269
[email protected]0df165f2010-09-28 16:49:40270 // A permission is defined by its |name| (what is used in the manifest),
271 // and the |message_id| that's used by install/update UI.
272 struct Permission {
273 const char* const name;
274 const int message_id;
275 };
276
[email protected]66e4eb32010-10-27 20:37:41277 static scoped_refptr<Extension> Create(const FilePath& path,
278 Location location,
279 const DictionaryValue& value,
280 bool require_key,
281 std::string* error);
282
[email protected]0df165f2010-09-28 16:49:40283 // The install message id for |permission|. Returns 0 if none exists.
284 static int GetPermissionMessageId(const std::string& permission);
285
[email protected]a2a098d2010-09-29 19:42:55286 // Returns the full list of permission messages that this extension
287 // should display at install time.
[email protected]9adb9692010-10-29 23:14:02288 std::vector<string16> GetPermissionMessages() const;
[email protected]a2a098d2010-09-29 19:42:55289
290 // Returns the distinct hosts that should be displayed in the install UI. This
291 // discards some of the detail that is present in the manifest to make it as
292 // easy as possible to process by users. In particular we disregard the scheme
293 // and path components of URLPatterns and de-dupe the result.
294 static std::vector<std::string> GetDistinctHosts(
295 const URLPatternList& host_patterns);
[email protected]9adb9692010-10-29 23:14:02296 std::vector<std::string> GetDistinctHosts() const;
[email protected]0df165f2010-09-28 16:49:40297
[email protected]c3e3def742009-07-17 07:51:06298 // Icon sizes used by the extension system.
[email protected]d2817012009-08-04 06:46:21299 static const int kIconSizes[];
[email protected]c3e3def742009-07-17 07:51:06300
[email protected]4c4f8192009-10-17 01:03:26301 // Max size (both dimensions) for browser and page actions.
302 static const int kPageActionIconMaxSize;
303 static const int kBrowserActionIconMaxSize;
304
[email protected]35506352009-08-07 18:58:19305 // Each permission is a module that the extension is permitted to use.
[email protected]218990c2010-06-26 01:21:07306 //
[email protected]0df165f2010-09-28 16:49:40307 // NOTE: To add a new permission, define it here, and add an entry to
308 // Extension::kPermissions.
[email protected]9dcf8f12010-09-02 20:39:19309 static const char kBackgroundPermission[];
310 static const char kBookmarkPermission[];
311 static const char kContextMenusPermission[];
312 static const char kCookiePermission[];
313 static const char kExperimentalPermission[];
314 static const char kGeolocationPermission[];
315 static const char kHistoryPermission[];
316 static const char kIdlePermission[];
[email protected]eb255772010-10-04 22:10:46317 static const char kManagementPermission[];
[email protected]9dcf8f12010-09-02 20:39:19318 static const char kNotificationPermission[];
319 static const char kProxyPermission[];
320 static const char kTabPermission[];
321 static const char kUnlimitedStoragePermission[];
[email protected]9dcf8f12010-09-02 20:39:19322 static const char kWebstorePrivatePermission[];
[email protected]aeb53b32009-10-29 07:34:45323
[email protected]0df165f2010-09-28 16:49:40324 static const Permission kPermissions[];
[email protected]35506352009-08-07 18:58:19325 static const size_t kNumPermissions;
[email protected]9dcf8f12010-09-02 20:39:19326 static const char* const kHostedAppPermissionNames[];
[email protected]b746f372010-08-29 21:39:32327 static const size_t kNumHostedAppPermissions;
[email protected]35506352009-08-07 18:58:19328
[email protected]03b612f2010-08-13 21:09:21329 // The old name for the unlimited storage permission, which is deprecated but
330 // still accepted as meaning the same thing as kUnlimitedStoragePermission.
[email protected]9dcf8f12010-09-02 20:39:19331 static const char kOldUnlimitedStoragePermission[];
[email protected]03b612f2010-08-13 21:09:21332
[email protected]b746f372010-08-29 21:39:32333 // Returns true if the string is one of the known hosted app permissions (see
334 // kHostedAppPermissionNames).
335 static bool IsHostedAppPermission(const std::string& permission);
336
[email protected]6014d672008-12-05 00:38:25337 // The name of the manifest inside an extension.
[email protected]99efb7b12009-12-18 02:39:16338 static const FilePath::CharType kManifestFilename[];
[email protected]6014d672008-12-05 00:38:25339
[email protected]300cc58db2009-08-19 20:45:14340 // The name of locale folder inside an extension.
[email protected]99efb7b12009-12-18 02:39:16341 static const FilePath::CharType kLocaleFolder[];
[email protected]300cc58db2009-08-19 20:45:14342
343 // The name of the messages file inside an extension.
[email protected]99efb7b12009-12-18 02:39:16344 static const FilePath::CharType kMessagesFilename[];
[email protected]300cc58db2009-08-19 20:45:14345
[email protected]25b34332009-06-05 21:53:19346#if defined(OS_WIN)
[email protected]9dcf8f12010-09-02 20:39:19347 static const char kExtensionRegistryPath[];
[email protected]25b34332009-06-05 21:53:19348#endif
349
[email protected]37eeb5a2009-02-26 23:36:17350 // The number of bytes in a legal id.
[email protected]fe0e7822009-02-26 23:51:48351 static const size_t kIdSize;
[email protected]37eeb5a2009-02-26 23:36:17352
[email protected]e435d6b72009-07-25 03:15:58353 // The mimetype used for extensions.
354 static const char kMimeType[];
355
[email protected]25b34332009-06-05 21:53:19356 // Checks to see if the extension has a valid ID.
357 static bool IdIsValid(const std::string& id);
358
[email protected]4ead6f72010-10-13 19:54:18359 // Generate an ID for an extension in the given path.
360 static std::string GenerateIdForPath(const FilePath& file_name);
361
[email protected]e435d6b72009-07-25 03:15:58362 // Returns true if the specified file is an extension.
363 static bool IsExtension(const FilePath& file_name);
364
[email protected]25b34332009-06-05 21:53:19365 // Whether the |location| is external or not.
366 static inline bool IsExternalLocation(Location location) {
367 return location == Extension::EXTERNAL_PREF ||
[email protected]8ef78fd2010-08-19 17:14:32368 location == Extension::EXTERNAL_REGISTRY ||
[email protected]04cb7542010-10-25 10:50:06369 location == Extension::EXTERNAL_PREF_DOWNLOAD ||
370 location == Extension::EXTERNAL_POLICY_DOWNLOAD;
371 }
372
373 // Whether extensions with |location| are auto-updatable or not.
374 static inline bool IsAutoUpdateableLocation(Location location) {
375 // Only internal and external extensions can be autoupdated.
376 return location == Extension::INTERNAL ||
377 IsExternalLocation(location);
[email protected]25b34332009-06-05 21:53:19378 }
379
[email protected]9b217652010-10-08 22:04:23380 // See HistogramType definition above.
[email protected]9adb9692010-10-29 23:14:02381 HistogramType GetHistogramType() const;
[email protected]9b217652010-10-08 22:04:23382
[email protected]07c00d992009-03-04 20:27:04383 // Returns an absolute url to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:59384 // |extension_url| argument should be the url() from an Extension object. The
385 // |relative_path| can be untrusted user input. The returned URL will either
386 // be invalid() or a child of |extension_url|.
387 // NOTE: Static so that it can be used from multiple threads.
388 static GURL GetResourceURL(const GURL& extension_url,
389 const std::string& relative_path);
[email protected]cffd7892010-08-26 17:43:28390 GURL GetResourceURL(const std::string& relative_path) const {
[email protected]3cfbd0e2009-03-18 21:26:24391 return GetResourceURL(url(), relative_path);
392 }
[email protected]eab9b452009-01-23 20:48:59393
[email protected]99efb7b12009-12-18 02:39:16394 // Returns an extension resource object. |relative_path| should be UTF8
395 // encoded.
[email protected]9adb9692010-10-29 23:14:02396 ExtensionResource GetResource(const std::string& relative_path) const;
[email protected]99efb7b12009-12-18 02:39:16397
398 // As above, but with |relative_path| following the file system's encoding.
[email protected]9adb9692010-10-29 23:14:02399 ExtensionResource GetResource(const FilePath& relative_path) const;
[email protected]eab9b452009-01-23 20:48:59400
[email protected]a17f9462009-06-09 02:56:41401 // |input| is expected to be the text of an rsa public or private key. It
402 // tolerates the presence or absence of bracking header/footer like this:
403 // -----(BEGIN|END) [RSA PUBLIC/PRIVATE] KEY-----
404 // and may contain newlines.
405 static bool ParsePEMKeyBytes(const std::string& input, std::string* output);
406
407 // Does a simple base64 encoding of |input| into |output|.
408 static bool ProducePEM(const std::string& input, std::string* output);
409
[email protected]84ac7f32009-10-06 06:17:54410 // Generates an extension ID from arbitrary input. The same input string will
411 // always generate the same output ID.
412 static bool GenerateId(const std::string& input, std::string* output);
[email protected]fbcc40302009-06-12 20:45:45413
[email protected]a17f9462009-06-09 02:56:41414 // Expects base64 encoded |input| and formats into |output| including
415 // the appropriate header & footer.
416 static bool FormatPEMForFileOutput(const std::string input,
417 std::string* output, bool is_public);
418
[email protected]2a409532009-08-28 19:39:44419 // Determine whether |new_extension| has increased privileges compared to
420 // |old_extension|.
[email protected]9adb9692010-10-29 23:14:02421 static bool IsPrivilegeIncrease(const Extension* old_extension,
422 const Extension* new_extension);
[email protected]b24d8312009-08-27 06:47:46423
[email protected]c690a9812009-12-17 05:55:32424 // Given an extension and icon size, read it if present and decode it into
[email protected]ae2e0f92010-04-06 20:32:23425 // result. In the browser process, this will DCHECK if not called on the
426 // file thread. To easily load extension images on the UI thread, see
427 // ImageLoadingTracker.
[email protected]9adb9692010-10-29 23:14:02428 static void DecodeIcon(const Extension* extension,
[email protected]c690a9812009-12-17 05:55:32429 Icons icon_size,
430 scoped_ptr<SkBitmap>* result);
431
432 // Given an icon_path and icon size, read it if present and decode it into
[email protected]ae2e0f92010-04-06 20:32:23433 // result. In the browser process, this will DCHECK if not called on the
434 // file thread. To easily load extension images on the UI thread, see
435 // ImageLoadingTracker.
[email protected]c690a9812009-12-17 05:55:32436 static void DecodeIconFromPath(const FilePath& icon_path,
437 Icons icon_size,
438 scoped_ptr<SkBitmap>* result);
439
[email protected]a807bbe2010-04-14 10:51:19440 // Returns the base extension url for a given |extension_id|.
441 static GURL GetBaseURLFromExtensionId(const std::string& extension_id);
442
[email protected]ec5b50d2010-10-09 16:35:18443 // Returns the url prefix for the extension/apps gallery. Can be set via the
[email protected]ce3eb7b62010-09-14 13:57:44444 // --apps-gallery-url switch. The URL returned will not contain a trailing
[email protected]d3071992010-10-08 15:24:07445 // slash. Do not use this as a prefix/extent for the store. Instead see
446 // ExtensionsService::GetWebStoreApp or
447 // ExtensionsService::IsDownloadFromGallery
448 static std::string ChromeStoreLaunchURL();
[email protected]a0cf04a2010-06-23 03:29:55449
[email protected]be7e5cb2010-10-04 12:53:17450 // Helper function that consolidates the check for whether the script can
451 // execute into one location. |page_url| is the page that is the candidate
452 // for running the script, |can_execute_script_everywhere| specifies whether
453 // the extension is on the whitelist, |allowed_pages| is a vector of
454 // URLPatterns, listing what access the extension has, |script| is the script
455 // pointer (if content script) and |error| is an optional parameter, which
456 // will receive the error string listing why access was denied.
457 static bool CanExecuteScriptOnPage(
458 const GURL& page_url,
459 bool can_execute_script_everywhere,
460 const std::vector<URLPattern>* allowed_pages,
461 UserScript* script,
462 std::string* error);
463
464 // Adds an extension to the scripting whitelist. Used for testing only.
[email protected]10fb1992010-10-08 09:00:17465 static void SetScriptingWhitelist(const ScriptingWhitelist& whitelist);
[email protected]be7e5cb2010-10-04 12:53:17466
[email protected]4361c7c2010-09-30 21:57:53467 const StaticData* static_data() const { return static_data_; }
468
469 const FilePath& path() const { return static_data_->path; }
[email protected]92888082010-10-18 19:24:57470 const GURL& url() const { return static_data_->extension_url; }
471 Location location() const { return static_data_->location; }
[email protected]4361c7c2010-09-30 21:57:53472 const std::string& id() const { return static_data_->id; }
[email protected]92888082010-10-18 19:24:57473 const Version* version() const { return static_data_->version.get(); }
[email protected]4a8d3272009-03-10 19:15:08474 // String representation of the version number.
475 const std::string VersionString() const;
[email protected]4361c7c2010-09-30 21:57:53476 const std::string& name() const { return static_data_->name; }
[email protected]92888082010-10-18 19:24:57477 const std::string& public_key() const { return static_data_->public_key; }
478 const std::string& description() const { return static_data_->description; }
[email protected]6657afa62009-11-04 02:15:20479 bool converted_from_user_script() const {
[email protected]92888082010-10-18 19:24:57480 return static_data_->converted_from_user_script;
[email protected]6657afa62009-11-04 02:15:20481 }
[email protected]92888082010-10-18 19:24:57482 const UserScriptList& content_scripts() const {
483 return static_data_->content_scripts;
484 }
485 ExtensionAction* page_action() const {
486 return static_data_->page_action.get();
487 }
488 ExtensionAction* browser_action() const {
489 return static_data_->browser_action.get();
490 }
491 const std::vector<PluginInfo>& plugins() const {
492 return static_data_->plugins;
493 }
494 const GURL& background_url() const { return static_data_->background_url; }
495 const GURL& options_url() const { return static_data_->options_url; }
496 const GURL& devtools_url() const { return static_data_->devtools_url; }
497 const std::vector<GURL>& toolstrips() const {
498 return static_data_->toolstrips;
499 }
[email protected]0df165f2010-09-28 16:49:40500 const std::set<std::string>& api_permissions() const {
[email protected]4361c7c2010-09-30 21:57:53501 return static_data_->api_permissions;
[email protected]35506352009-08-07 18:58:19502 }
[email protected]b30e0dd2010-01-29 23:33:21503 const URLPatternList& host_permissions() const {
[email protected]92888082010-10-18 19:24:57504 return static_data_->host_permissions;
[email protected]c7ad50f2009-09-11 06:28:15505 }
506
[email protected]aeb53b32009-10-29 07:34:45507 // Returns true if the extension has the specified API permission.
[email protected]0df165f2010-09-28 16:49:40508 static bool HasApiPermission(const std::set<std::string>& api_permissions,
[email protected]246c05f2010-09-10 09:12:11509 const std::string& function_name);
[email protected]583d45c12010-08-31 02:48:12510
[email protected]246c05f2010-09-10 09:12:11511 bool HasApiPermission(const std::string& function_name) const {
512 return HasApiPermission(this->api_permissions(), function_name);
[email protected]aeb53b32009-10-29 07:34:45513 }
514
[email protected]4361c7c2010-09-30 21:57:53515 const ExtensionExtent& GetEffectiveHostPermissions() const {
516 return static_data_->effective_host_permissions;
517 }
[email protected]b24d8312009-08-27 06:47:46518
[email protected]fbd17cf2010-04-28 23:52:56519 // Whether or not the extension is allowed permission for a URL pattern from
520 // the manifest. http, https, and chrome://favicon/ is allowed for all
521 // extensions, while component extensions are allowed access to
522 // chrome://resources.
[email protected]a3caa822010-10-26 18:10:00523 bool CanSpecifyHostPermission(const URLPattern& pattern) const;
[email protected]fbd17cf2010-04-28 23:52:56524
[email protected]584b8e3f2010-04-10 00:23:37525 // Whether the extension has access to the given URL.
526 bool HasHostPermission(const GURL& url) const;
527
[email protected]0df165f2010-09-28 16:49:40528 // Whether the extension has effective access to all hosts. This is true if
529 // there is a content script that matches all hosts, if there is a host
530 // permission grants access to all hosts (like <all_urls>) or an api
531 // permission that effectively grants access to all hosts (e.g. proxy,
532 // network, etc.)
533 bool HasEffectiveAccessToAllHosts() const;
[email protected]b24d8312009-08-27 06:47:46534
[email protected]92888082010-10-18 19:24:57535 const GURL& update_url() const { return static_data_->update_url; }
[email protected]807871f2010-09-16 01:04:48536
[email protected]92888082010-10-18 19:24:57537 const ExtensionIconSet& icons() const {
538 return static_data_->icons;
539 }
[email protected]4a8d3272009-03-10 19:15:08540
[email protected]37cd64d2010-10-25 18:17:58541 // Returns the Homepage URL for this extension. If homepage_url was not
542 // specified in the manifest, this returns the Google Gallery URL. For
[email protected]bfa90a3a2010-04-28 15:43:23543 // third-party extensions, this returns a blank GURL.
[email protected]37cd64d2010-10-25 18:17:58544 GURL GetHomepageURL() const;
[email protected]bfa90a3a2010-04-28 15:43:23545
[email protected]25b34332009-06-05 21:53:19546 // Theme-related.
[email protected]92888082010-10-18 19:24:57547 DictionaryValue* GetThemeImages() const {
548 return static_data_->theme_images.get();
[email protected]7895ea22009-06-02 20:53:50549 }
[email protected]92888082010-10-18 19:24:57550 DictionaryValue* GetThemeColors() const {
551 return static_data_->theme_colors.get();
552 }
553 DictionaryValue* GetThemeTints() const {
554 return static_data_->theme_tints.get();
555 }
556 DictionaryValue* GetThemeDisplayProperties() const {
557 return static_data_->theme_display_properties.get();
558 }
559 bool is_theme() const { return static_data_->is_theme; }
[email protected]631cf822009-05-15 07:01:25560
[email protected]facd7a7652009-06-05 23:15:02561 // Returns a list of paths (relative to the extension dir) for images that
562 // the browser might load (like themes and page action icons).
[email protected]9adb9692010-10-29 23:14:02563 std::set<FilePath> GetBrowserImages() const;
[email protected]facd7a7652009-06-05 23:15:02564
[email protected]807871f2010-09-16 01:04:48565 // Get an extension icon as a resource or URL.
[email protected]9adb9692010-10-29 23:14:02566 ExtensionResource GetIconResource(
567 int size, ExtensionIconSet::MatchType match_type) const;
568 GURL GetIconURL(int size, ExtensionIconSet::MatchType match_type) const;
[email protected]f34e79632010-03-17 02:34:08569
[email protected]b6ab96d2009-08-20 18:58:19570 const DictionaryValue* manifest_value() const {
[email protected]92888082010-10-18 19:24:57571 return static_data_->manifest_value.get();
[email protected]b6ab96d2009-08-20 18:58:19572 }
573
[email protected]4361c7c2010-09-30 21:57:53574 const std::string default_locale() const {
575 return static_data_->default_locale;
576 }
[email protected]9428edc2009-11-18 18:02:47577
[email protected]86c008e82009-08-28 20:26:05578 // Chrome URL overrides (see ExtensionOverrideUI).
[email protected]d3cfa482009-10-17 13:54:57579 const URLOverrideMap& GetChromeURLOverrides() const {
[email protected]92888082010-10-18 19:24:57580 return static_data_->chrome_url_overrides;
[email protected]86c008e82009-08-28 20:26:05581 }
582
[email protected]92888082010-10-18 19:24:57583 const std::string omnibox_keyword() const {
584 return static_data_->omnibox_keyword;
585 }
[email protected]56ad3792010-05-28 17:45:33586
[email protected]92888082010-10-18 19:24:57587 bool is_app() const { return static_data_->is_app; }
588 const ExtensionExtent& web_extent() const {
589 return static_data_->extent;
590 }
591 const std::string& launch_local_path() const {
592 return static_data_->launch_local_path;
593 }
594 const std::string& launch_web_url() const {
595 return static_data_->launch_web_url;
[email protected]fe13bf62010-08-26 14:33:19596 }
[email protected]d83a5602010-09-16 00:22:48597 extension_misc::LaunchContainer launch_container() const {
[email protected]92888082010-10-18 19:24:57598 return static_data_->launch_container;
[email protected]d83a5602010-09-16 00:22:48599 }
[email protected]92888082010-10-18 19:24:57600 int launch_width() const { return static_data_->launch_width; }
601 int launch_height() const { return static_data_->launch_height; }
[email protected]4361c7c2010-09-30 21:57:53602 bool incognito_split_mode() const {
603 return static_data_->incognito_split_mode;
604 }
[email protected]867a73e12010-03-19 20:45:46605
606 // Gets the fully resolved absolute launch URL.
607 GURL GetFullLaunchURL() const;
[email protected]ceefd3d2010-03-12 09:10:29608
[email protected]e95ad332009-08-03 19:44:25609 // Whether the background page, if any, is ready. We don't load other
610 // components until then. If there is no background page, we consider it to
611 // be ready.
[email protected]9adb9692010-10-29 23:14:02612 bool GetBackgroundPageReady() const;
613 void SetBackgroundPageReady() const;
[email protected]e95ad332009-08-03 19:44:25614
[email protected]1e8c93f2010-02-08 22:58:31615 // Getter and setter for the flag that specifies whether the extension is
616 // being upgraded.
[email protected]92888082010-10-18 19:24:57617 bool being_upgraded() const { return GetRuntimeData()->being_upgraded; }
[email protected]9adb9692010-10-29 23:14:02618 void set_being_upgraded(bool value) const {
[email protected]92888082010-10-18 19:24:57619 GetRuntimeData()->being_upgraded = value;
620 }
[email protected]1e8c93f2010-02-08 22:58:31621
[email protected]d9ad80f2010-03-30 20:40:18622 // Image cache related methods. These are only valid on the UI thread and
[email protected]052c92702010-06-25 07:25:52623 // not maintained by this class. See ImageLoadingTracker for usage. The
624 // |original_size| parameter should be the size of the image at |source|
625 // before any scaling may have been done to produce the pixels in |image|.
[email protected]d9ad80f2010-03-30 20:40:18626 void SetCachedImage(const ExtensionResource& source,
[email protected]052c92702010-06-25 07:25:52627 const SkBitmap& image,
[email protected]9adb9692010-10-29 23:14:02628 const gfx::Size& original_size) const;
[email protected]052c92702010-06-25 07:25:52629 bool HasCachedImage(const ExtensionResource& source,
[email protected]9adb9692010-10-29 23:14:02630 const gfx::Size& max_size) const;
[email protected]052c92702010-06-25 07:25:52631 SkBitmap GetCachedImage(const ExtensionResource& source,
[email protected]9adb9692010-10-29 23:14:02632 const gfx::Size& max_size) const;
[email protected]4361c7c2010-09-30 21:57:53633 bool is_hosted_app() const { return is_app() && !web_extent().is_empty(); }
634 bool is_packaged_app() const { return is_app() && web_extent().is_empty(); }
635
[email protected]be7e5cb2010-10-04 12:53:17636 // Returns true if this extension is a COMPONENT extension, or if it is
637 // on the whitelist of extensions that can script all pages.
638 bool CanExecuteScriptEverywhere() const;
639
[email protected]4a8d3272009-03-10 19:15:08640 private:
[email protected]66e4eb32010-10-27 20:37:41641 friend class base::RefCountedThreadSafe<Extension>;
642
[email protected]4ead6f72010-10-13 19:54:18643 // Normalize the path for use by the extension. On Windows, this will make
644 // sure the drive letter is uppercase.
645 static FilePath MaybeNormalizePath(const FilePath& path);
646
[email protected]66e4eb32010-10-27 20:37:41647 Extension(const FilePath& path, Location location);
648 ~Extension();
649
650 // Initialize the extension from a parsed manifest.
651 // Usually, the id of an extension is generated by the "key" property of
652 // its manifest, but if |require_key| is |false|, a temporary ID will be
653 // generated based on the path.
654 bool InitFromValue(const DictionaryValue& value, bool require_key,
655 std::string* error);
656
[email protected]052c92702010-06-25 07:25:52657 // Helper function for implementing HasCachedImage/GetCachedImage. A return
658 // value of NULL means there is no matching image cached (we allow caching an
659 // empty SkBitmap).
660 SkBitmap* GetCachedImageImpl(const ExtensionResource& source,
[email protected]9adb9692010-10-29 23:14:02661 const gfx::Size& max_size) const;
[email protected]d9ad80f2010-03-30 20:40:18662
[email protected]3cfbd0e2009-03-18 21:26:24663 // Helper method that loads a UserScript object from a
664 // dictionary in the content_script list of the manifest.
665 bool LoadUserScriptHelper(const DictionaryValue* content_script,
666 int definition_index,
667 std::string* error,
668 UserScript* result);
[email protected]f7f3a5f2009-05-01 22:02:34669
[email protected]6657afa62009-11-04 02:15:20670 // Helper method that loads either the include_globs or exclude_globs list
671 // from an entry in the content_script lists of the manifest.
672 bool LoadGlobsHelper(const DictionaryValue* content_script,
673 int content_script_index,
[email protected]e2194742010-08-12 05:54:34674 const char* globs_property_name,
[email protected]6657afa62009-11-04 02:15:20675 std::string* error,
[email protected]11f4857282009-11-13 19:56:17676 void(UserScript::*add_method)(const std::string& glob),
[email protected]6657afa62009-11-04 02:15:20677 UserScript *instance);
678
[email protected]867a73e12010-03-19 20:45:46679 // Helpers to load various chunks of the manifest.
[email protected]3ba0fd32010-06-19 05:39:10680 bool LoadIsApp(const DictionaryValue* manifest, std::string* error);
[email protected]e2194742010-08-12 05:54:34681 bool LoadExtent(const DictionaryValue* manifest, const char* key,
[email protected]4fdbc1492010-07-01 01:20:59682 ExtensionExtent* extent, const char* list_error,
683 const char* value_error, std::string* error);
[email protected]867a73e12010-03-19 20:45:46684 bool LoadLaunchContainer(const DictionaryValue* manifest, std::string* error);
685 bool LoadLaunchURL(const DictionaryValue* manifest, std::string* error);
[email protected]cbf4d1912010-08-12 18:24:57686 bool EnsureNotHybridApp(const DictionaryValue* manifest, std::string* error);
[email protected]867a73e12010-03-19 20:45:46687
[email protected]5d246db22009-10-27 06:17:57688 // Helper method to load an ExtensionAction from the page_action or
[email protected]92c6f9b92009-10-24 04:35:08689 // browser_action entries in the manifest.
[email protected]5d246db22009-10-27 06:17:57690 ExtensionAction* LoadExtensionActionHelper(
[email protected]92c6f9b92009-10-24 04:35:08691 const DictionaryValue* extension_action, std::string* error);
692
[email protected]4361c7c2010-09-30 21:57:53693 // Calculates the effective host permissions from the permissions and content
694 // script petterns.
695 void InitEffectiveHostPermissions();
696
[email protected]2f6698b2010-10-14 00:58:21697 // Returns true if the extension has more than one "UI surface". For example,
698 // an extension that has a browser action and a page action.
699 bool HasMultipleUISurfaces() const;
700
[email protected]e2eb43112009-05-29 21:19:54701 // Figures out if a source contains keys not associated with themes - we
702 // don't want to allow scripts and such to be bundled with themes.
[email protected]9adb9692010-10-29 23:14:02703 bool ContainsNonThemeKeys(const DictionaryValue& source) const;
[email protected]e2eb43112009-05-29 21:19:54704
[email protected]1952c7d2010-03-04 23:48:34705 // Returns true if the string is one of the known api permissions (see
[email protected]0df165f2010-09-28 16:49:40706 // kPermissions).
[email protected]9adb9692010-10-29 23:14:02707 bool IsAPIPermission(const std::string& permission) const;
[email protected]1952c7d2010-03-04 23:48:34708
[email protected]a2a098d2010-09-29 19:42:55709 // The set of unique API install messages that the extension has.
710 // NOTE: This only includes messages related to permissions declared in the
711 // "permissions" key in the manifest. Permissions implied from other features
712 // of the manifest, like plugins and content scripts are not included.
[email protected]9adb9692010-10-29 23:14:02713 std::set<string16> GetSimplePermissionMessages() const;
[email protected]a2a098d2010-09-29 19:42:55714
715 // The permission message displayed related to the host permissions for
716 // this extension.
[email protected]9adb9692010-10-29 23:14:02717 string16 GetHostPermissionMessage() const;
[email protected]a2a098d2010-09-29 19:42:55718
[email protected]92888082010-10-18 19:24:57719 // Returns a mutable pointer to our runtime data. Can only be called on
720 // the UI thread.
721 RuntimeData* GetRuntimeData() const;
722
[email protected]4361c7c2010-09-30 21:57:53723 // Collection of extension data that doesn't change doesn't change once an
724 // Extension object has been initialized. The mutable version is valid only
725 // until InitFromValue finishes, to ensure we don't accidentally modify it
726 // post-initialization.
727 StaticData* mutable_static_data_;
728 scoped_refptr<const StaticData> static_data_;
[email protected]4a8d3272009-03-10 19:15:08729
[email protected]92888082010-10-18 19:24:57730 // Runtime data.
[email protected]66e4eb32010-10-27 20:37:41731 const RuntimeData runtime_data_;
[email protected]1e8c93f2010-02-08 22:58:31732
[email protected]19118d52010-07-26 22:13:42733 FRIEND_TEST_ALL_PREFIXES(ExtensionTest, LoadPageActionHelper);
[email protected]66e4eb32010-10-27 20:37:41734 FRIEND_TEST_ALL_PREFIXES(ExtensionTest, InitFromValueInvalid);
735 FRIEND_TEST_ALL_PREFIXES(ExtensionTest, InitFromValueValid);
736 FRIEND_TEST_ALL_PREFIXES(ExtensionTest, InitFromValueValidNameInRTL);
[email protected]19118d52010-07-26 22:13:42737 FRIEND_TEST_ALL_PREFIXES(TabStripModelTest, Apps);
[email protected]ae7fe712009-07-02 20:33:58738
[email protected]894bb502009-05-21 22:39:57739 DISALLOW_COPY_AND_ASSIGN(Extension);
[email protected]7713d632008-12-02 07:52:33740};
741
[email protected]9adb9692010-10-29 23:14:02742typedef std::vector< scoped_refptr<const Extension> > ExtensionList;
[email protected]ec5b50d2010-10-09 16:35:18743typedef std::set<std::string> ExtensionIdSet;
[email protected]b1748b1d82009-11-30 20:32:56744
[email protected]c6d474f82009-12-16 21:11:06745// Handy struct to pass core extension info around.
746struct ExtensionInfo {
747 ExtensionInfo(const DictionaryValue* manifest,
748 const std::string& id,
749 const FilePath& path,
[email protected]3bb84992010-08-26 17:23:46750 Extension::Location location);
751 ~ExtensionInfo();
[email protected]c6d474f82009-12-16 21:11:06752
753 scoped_ptr<DictionaryValue> extension_manifest;
754 std::string extension_id;
755 FilePath extension_path;
756 Extension::Location extension_location;
757
758 private:
759 DISALLOW_COPY_AND_ASSIGN(ExtensionInfo);
760};
761
[email protected]211030342010-09-30 18:41:06762// Struct used for the details of the EXTENSION_UNINSTALLED
763// notification.
764struct UninstalledExtensionInfo {
765 explicit UninstalledExtensionInfo(const Extension& extension);
[email protected]9b2331d92010-10-04 23:11:19766 ~UninstalledExtensionInfo();
[email protected]211030342010-09-30 18:41:06767
768 std::string extension_id;
769 std::set<std::string> extension_api_permissions;
[email protected]76c25112010-10-01 22:37:21770 // TODO(akalin): Once we have a unified ExtensionType, replace the
771 // below member variables with a member of that type.
772 bool is_theme;
773 bool is_app;
774 bool converted_from_user_script;
775 GURL update_url;
[email protected]211030342010-09-30 18:41:06776};
777
[email protected]5b1a0e22009-05-26 19:00:58778#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_H_