blob: 4aa662d825cd7795450aad6866dcc67c8aae0999 [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]7713d632008-12-02 07:52:3333class Extension {
34 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
108 // TODO(mpcomplete): RefCountedThreadSafe does not allow AddRef/Release on
109 // const objects. I think that is a mistake. Until we can fix that, here's
110 // a workaround.
111 void AddRef() const {
112 const_cast<StaticData*>(this)->
113 base::RefCountedThreadSafe<StaticData>::AddRef();
114 }
115 void Release() const {
116 const_cast<StaticData*>(this)->
117 base::RefCountedThreadSafe<StaticData>::Release();
118 }
119
120 // A persistent, globally unique ID. An extension's ID is used in things
121 // like directory structures and URLs, and is expected to not change across
122 // versions. It is generated as a SHA-256 hash of the extension's public
123 // key, or as a hash of the path in the case of unpacked extensions.
124 std::string id;
125
126 // The extension's human-readable name. Name is used for display purpose. It
127 // might be wrapped with unicode bidi control characters so that it is
128 // displayed correctly in RTL context.
129 // NOTE: Name is UTF-8 and may contain non-ascii characters.
130 std::string name;
131
132 // The absolute path to the directory the extension is stored in.
133 FilePath path;
134
135 // Default locale for fall back. Can be empty if extension is not localized.
136 std::string default_locale;
137
138 // If true, a separate process will be used for the extension in incognito
139 // mode.
140 bool incognito_split_mode;
141
142 // Defines the set of URLs in the extension's web content.
143 ExtensionExtent extent;
144
[email protected]11540af2010-10-14 14:38:00145 // The set of host permissions that the extension effectively has access to,
146 // which is a merge of host_permissions_ and all of the match patterns in
147 // any content scripts the extension has. This is used to determine which
148 // URLs have the ability to load an extension's resources via embedded
149 // chrome-extension: URLs (see extension_protocols.cc).
[email protected]4361c7c2010-09-30 21:57:53150 ExtensionExtent effective_host_permissions;
151
152 // The set of module-level APIs this extension can use.
153 std::set<std::string> api_permissions;
154
155 // The icons for the extension.
156 ExtensionIconSet icons;
157
[email protected]92888082010-10-18 19:24:57158 // The base extension url for the extension.
159 GURL extension_url;
160
161 // The location the extension was loaded from.
162 Location location;
163
164 // The extension's version.
165 scoped_ptr<Version> version;
166
167 // An optional longer description of the extension.
168 std::string description;
169
170 // True if the extension was generated from a user script. (We show slightly
171 // different UI if so).
172 bool converted_from_user_script;
173
174 // Paths to the content scripts the extension contains.
175 UserScriptList content_scripts;
176
177 // The extension's page action, if any.
178 scoped_ptr<ExtensionAction> page_action;
179
180 // The extension's browser action, if any.
181 scoped_ptr<ExtensionAction> browser_action;
182
183 // Optional list of NPAPI plugins and associated properties.
184 std::vector<PluginInfo> plugins;
185
186 // Optional URL to a master page of which a single instance should be always
187 // loaded in the background.
188 GURL background_url;
189
190 // Optional URL to a page for setting options/preferences.
191 GURL options_url;
192
193 // Optional URL to a devtools extension page.
194 GURL devtools_url;
195
196 // Optional list of toolstrips and associated properties.
197 std::vector<GURL> toolstrips;
198
199 // The public key used to sign the contents of the crx package.
200 std::string public_key;
201
202 // A map of resource id's to relative file paths.
203 scoped_ptr<DictionaryValue> theme_images;
204
205 // A map of color names to colors.
206 scoped_ptr<DictionaryValue> theme_colors;
207
208 // A map of color names to colors.
209 scoped_ptr<DictionaryValue> theme_tints;
210
211 // A map of display properties.
212 scoped_ptr<DictionaryValue> theme_display_properties;
213
214 // Whether the extension is a theme.
215 bool is_theme;
216
217 // The sites this extension has permission to talk to (using XHR, etc).
218 URLPatternList host_permissions;
219
[email protected]37cd64d2010-10-25 18:17:58220 // The homepage for this extension. Useful if it is not hosted by Google and
221 // therefore does not have a Gallery URL.
222 GURL homepage_url;
223
[email protected]92888082010-10-18 19:24:57224 // URL for fetching an update manifest
225 GURL update_url;
226
227 // A copy of the manifest that this extension was created from.
228 scoped_ptr<DictionaryValue> manifest_value;
229
230 // A map of chrome:// hostnames (newtab, downloads, etc.) to Extension URLs
231 // which override the handling of those URLs.
232 URLOverrideMap chrome_url_overrides;
233
234 // Whether this extension uses app features.
235 bool is_app;
236
237 // The local path inside the extension to use with the launcher.
238 std::string launch_local_path;
239
240 // A web url to use with the launcher. Note that this might be relative or
241 // absolute. If relative, it is relative to web_origin.
242 std::string launch_web_url;
243
244 // The type of container to launch into.
245 extension_misc::LaunchContainer launch_container;
246
247 // The default size of the container when launching. Only respected for
248 // containers like panels and windows.
249 int launch_width;
250 int launch_height;
251
252 // The Omnibox keyword for this extension, or empty if there is none.
253 std::string omnibox_keyword;
254
[email protected]4361c7c2010-09-30 21:57:53255 protected:
256 friend class base::RefCountedThreadSafe<StaticData>;
257 ~StaticData();
258 };
259
[email protected]92888082010-10-18 19:24:57260 // Contains the subset of the extension's (private) data that can be modified
261 // after initialization. This class should only be accessed on the UI thread.
262 struct RuntimeData {
263 // We keep a cache of images loaded from extension resources based on their
264 // path and a string representation of a size that may have been used to
265 // scale it (or the empty string if the image is at its original size).
266 typedef std::pair<FilePath, std::string> ImageCacheKey;
267 typedef std::map<ImageCacheKey, SkBitmap> ImageCache;
268
269 RuntimeData();
270 ~RuntimeData();
271
272 // True if the background page is ready.
273 bool background_page_ready;
274
275 // True while the extension is being upgraded.
276 bool being_upgraded;
277
278 // Cached images for this extension.
279 ImageCache image_cache_;
280 };
281
[email protected]0df165f2010-09-28 16:49:40282 // A permission is defined by its |name| (what is used in the manifest),
283 // and the |message_id| that's used by install/update UI.
284 struct Permission {
285 const char* const name;
286 const int message_id;
287 };
288
289 // The install message id for |permission|. Returns 0 if none exists.
290 static int GetPermissionMessageId(const std::string& permission);
291
[email protected]a2a098d2010-09-29 19:42:55292 // Returns the full list of permission messages that this extension
293 // should display at install time.
294 std::vector<string16> GetPermissionMessages();
295
296 // Returns the distinct hosts that should be displayed in the install UI. This
297 // discards some of the detail that is present in the manifest to make it as
298 // easy as possible to process by users. In particular we disregard the scheme
299 // and path components of URLPatterns and de-dupe the result.
300 static std::vector<std::string> GetDistinctHosts(
301 const URLPatternList& host_patterns);
302 std::vector<std::string> GetDistinctHosts();
[email protected]0df165f2010-09-28 16:49:40303
[email protected]c3e3def742009-07-17 07:51:06304 // Icon sizes used by the extension system.
[email protected]d2817012009-08-04 06:46:21305 static const int kIconSizes[];
[email protected]c3e3def742009-07-17 07:51:06306
[email protected]4c4f8192009-10-17 01:03:26307 // Max size (both dimensions) for browser and page actions.
308 static const int kPageActionIconMaxSize;
309 static const int kBrowserActionIconMaxSize;
310
[email protected]35506352009-08-07 18:58:19311 // Each permission is a module that the extension is permitted to use.
[email protected]218990c2010-06-26 01:21:07312 //
[email protected]0df165f2010-09-28 16:49:40313 // NOTE: To add a new permission, define it here, and add an entry to
314 // Extension::kPermissions.
[email protected]9dcf8f12010-09-02 20:39:19315 static const char kBackgroundPermission[];
316 static const char kBookmarkPermission[];
317 static const char kContextMenusPermission[];
318 static const char kCookiePermission[];
319 static const char kExperimentalPermission[];
320 static const char kGeolocationPermission[];
321 static const char kHistoryPermission[];
322 static const char kIdlePermission[];
[email protected]eb255772010-10-04 22:10:46323 static const char kManagementPermission[];
[email protected]9dcf8f12010-09-02 20:39:19324 static const char kNotificationPermission[];
325 static const char kProxyPermission[];
326 static const char kTabPermission[];
327 static const char kUnlimitedStoragePermission[];
[email protected]9dcf8f12010-09-02 20:39:19328 static const char kWebstorePrivatePermission[];
[email protected]aeb53b32009-10-29 07:34:45329
[email protected]0df165f2010-09-28 16:49:40330 static const Permission kPermissions[];
[email protected]35506352009-08-07 18:58:19331 static const size_t kNumPermissions;
[email protected]9dcf8f12010-09-02 20:39:19332 static const char* const kHostedAppPermissionNames[];
[email protected]b746f372010-08-29 21:39:32333 static const size_t kNumHostedAppPermissions;
[email protected]35506352009-08-07 18:58:19334
[email protected]03b612f2010-08-13 21:09:21335 // The old name for the unlimited storage permission, which is deprecated but
336 // still accepted as meaning the same thing as kUnlimitedStoragePermission.
[email protected]9dcf8f12010-09-02 20:39:19337 static const char kOldUnlimitedStoragePermission[];
[email protected]03b612f2010-08-13 21:09:21338
[email protected]b746f372010-08-29 21:39:32339 // Returns true if the string is one of the known hosted app permissions (see
340 // kHostedAppPermissionNames).
341 static bool IsHostedAppPermission(const std::string& permission);
342
[email protected]6014d672008-12-05 00:38:25343 // The name of the manifest inside an extension.
[email protected]99efb7b12009-12-18 02:39:16344 static const FilePath::CharType kManifestFilename[];
[email protected]6014d672008-12-05 00:38:25345
[email protected]300cc58db2009-08-19 20:45:14346 // The name of locale folder inside an extension.
[email protected]99efb7b12009-12-18 02:39:16347 static const FilePath::CharType kLocaleFolder[];
[email protected]300cc58db2009-08-19 20:45:14348
349 // The name of the messages file inside an extension.
[email protected]99efb7b12009-12-18 02:39:16350 static const FilePath::CharType kMessagesFilename[];
[email protected]300cc58db2009-08-19 20:45:14351
[email protected]25b34332009-06-05 21:53:19352#if defined(OS_WIN)
[email protected]9dcf8f12010-09-02 20:39:19353 static const char kExtensionRegistryPath[];
[email protected]25b34332009-06-05 21:53:19354#endif
355
[email protected]37eeb5a2009-02-26 23:36:17356 // The number of bytes in a legal id.
[email protected]fe0e7822009-02-26 23:51:48357 static const size_t kIdSize;
[email protected]37eeb5a2009-02-26 23:36:17358
[email protected]e435d6b72009-07-25 03:15:58359 // The mimetype used for extensions.
360 static const char kMimeType[];
361
[email protected]631cf822009-05-15 07:01:25362 explicit Extension(const FilePath& path);
[email protected]631cf822009-05-15 07:01:25363 virtual ~Extension();
364
[email protected]25b34332009-06-05 21:53:19365 // Checks to see if the extension has a valid ID.
366 static bool IdIsValid(const std::string& id);
367
[email protected]4ead6f72010-10-13 19:54:18368 // Generate an ID for an extension in the given path.
369 static std::string GenerateIdForPath(const FilePath& file_name);
370
[email protected]e435d6b72009-07-25 03:15:58371 // Returns true if the specified file is an extension.
372 static bool IsExtension(const FilePath& file_name);
373
[email protected]25b34332009-06-05 21:53:19374 // Whether the |location| is external or not.
375 static inline bool IsExternalLocation(Location location) {
376 return location == Extension::EXTERNAL_PREF ||
[email protected]8ef78fd2010-08-19 17:14:32377 location == Extension::EXTERNAL_REGISTRY ||
[email protected]04cb7542010-10-25 10:50:06378 location == Extension::EXTERNAL_PREF_DOWNLOAD ||
379 location == Extension::EXTERNAL_POLICY_DOWNLOAD;
380 }
381
382 // Whether extensions with |location| are auto-updatable or not.
383 static inline bool IsAutoUpdateableLocation(Location location) {
384 // Only internal and external extensions can be autoupdated.
385 return location == Extension::INTERNAL ||
386 IsExternalLocation(location);
[email protected]25b34332009-06-05 21:53:19387 }
388
[email protected]9b217652010-10-08 22:04:23389 // See HistogramType definition above.
390 HistogramType GetHistogramType();
391
[email protected]07c00d992009-03-04 20:27:04392 // Returns an absolute url to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:59393 // |extension_url| argument should be the url() from an Extension object. The
394 // |relative_path| can be untrusted user input. The returned URL will either
395 // be invalid() or a child of |extension_url|.
396 // NOTE: Static so that it can be used from multiple threads.
397 static GURL GetResourceURL(const GURL& extension_url,
398 const std::string& relative_path);
[email protected]cffd7892010-08-26 17:43:28399 GURL GetResourceURL(const std::string& relative_path) const {
[email protected]3cfbd0e2009-03-18 21:26:24400 return GetResourceURL(url(), relative_path);
401 }
[email protected]eab9b452009-01-23 20:48:59402
[email protected]99efb7b12009-12-18 02:39:16403 // Returns an extension resource object. |relative_path| should be UTF8
404 // encoded.
405 ExtensionResource GetResource(const std::string& relative_path);
406
407 // As above, but with |relative_path| following the file system's encoding.
408 ExtensionResource GetResource(const FilePath& relative_path);
[email protected]eab9b452009-01-23 20:48:59409
[email protected]a17f9462009-06-09 02:56:41410 // |input| is expected to be the text of an rsa public or private key. It
411 // tolerates the presence or absence of bracking header/footer like this:
412 // -----(BEGIN|END) [RSA PUBLIC/PRIVATE] KEY-----
413 // and may contain newlines.
414 static bool ParsePEMKeyBytes(const std::string& input, std::string* output);
415
416 // Does a simple base64 encoding of |input| into |output|.
417 static bool ProducePEM(const std::string& input, std::string* output);
418
[email protected]84ac7f32009-10-06 06:17:54419 // Generates an extension ID from arbitrary input. The same input string will
420 // always generate the same output ID.
421 static bool GenerateId(const std::string& input, std::string* output);
[email protected]fbcc40302009-06-12 20:45:45422
[email protected]a17f9462009-06-09 02:56:41423 // Expects base64 encoded |input| and formats into |output| including
424 // the appropriate header & footer.
425 static bool FormatPEMForFileOutput(const std::string input,
426 std::string* output, bool is_public);
427
[email protected]2a409532009-08-28 19:39:44428 // Determine whether |new_extension| has increased privileges compared to
429 // |old_extension|.
430 static bool IsPrivilegeIncrease(Extension* old_extension,
431 Extension* new_extension);
[email protected]b24d8312009-08-27 06:47:46432
[email protected]c690a9812009-12-17 05:55:32433 // Given an extension and icon size, read it if present and decode it into
[email protected]ae2e0f92010-04-06 20:32:23434 // result. In the browser process, this will DCHECK if not called on the
435 // file thread. To easily load extension images on the UI thread, see
436 // ImageLoadingTracker.
[email protected]c690a9812009-12-17 05:55:32437 static void DecodeIcon(Extension* extension,
438 Icons icon_size,
439 scoped_ptr<SkBitmap>* result);
440
441 // Given an icon_path and icon size, read it if present and decode it into
[email protected]ae2e0f92010-04-06 20:32:23442 // result. In the browser process, this will DCHECK if not called on the
443 // file thread. To easily load extension images on the UI thread, see
444 // ImageLoadingTracker.
[email protected]c690a9812009-12-17 05:55:32445 static void DecodeIconFromPath(const FilePath& icon_path,
446 Icons icon_size,
447 scoped_ptr<SkBitmap>* result);
448
[email protected]a807bbe2010-04-14 10:51:19449 // Returns the base extension url for a given |extension_id|.
450 static GURL GetBaseURLFromExtensionId(const std::string& extension_id);
451
[email protected]ec5b50d2010-10-09 16:35:18452 // Returns the url prefix for the extension/apps gallery. Can be set via the
[email protected]ce3eb7b62010-09-14 13:57:44453 // --apps-gallery-url switch. The URL returned will not contain a trailing
[email protected]d3071992010-10-08 15:24:07454 // slash. Do not use this as a prefix/extent for the store. Instead see
455 // ExtensionsService::GetWebStoreApp or
456 // ExtensionsService::IsDownloadFromGallery
457 static std::string ChromeStoreLaunchURL();
[email protected]a0cf04a2010-06-23 03:29:55458
[email protected]be7e5cb2010-10-04 12:53:17459 // Helper function that consolidates the check for whether the script can
460 // execute into one location. |page_url| is the page that is the candidate
461 // for running the script, |can_execute_script_everywhere| specifies whether
462 // the extension is on the whitelist, |allowed_pages| is a vector of
463 // URLPatterns, listing what access the extension has, |script| is the script
464 // pointer (if content script) and |error| is an optional parameter, which
465 // will receive the error string listing why access was denied.
466 static bool CanExecuteScriptOnPage(
467 const GURL& page_url,
468 bool can_execute_script_everywhere,
469 const std::vector<URLPattern>* allowed_pages,
470 UserScript* script,
471 std::string* error);
472
473 // Adds an extension to the scripting whitelist. Used for testing only.
[email protected]10fb1992010-10-08 09:00:17474 static void SetScriptingWhitelist(const ScriptingWhitelist& whitelist);
[email protected]be7e5cb2010-10-04 12:53:17475
[email protected]4a8d3272009-03-10 19:15:08476 // Initialize the extension from a parsed manifest.
[email protected]1952c7d2010-03-04 23:48:34477 // Usually, the id of an extension is generated by the "key" property of
478 // its manifest, but if |require_key| is |false|, a temporary ID will be
479 // generated based on the path.
480 bool InitFromValue(const DictionaryValue& value, bool require_key,
[email protected]5bfb1eb0a2009-04-08 18:33:30481 std::string* error);
[email protected]4a8d3272009-03-10 19:15:08482
[email protected]4361c7c2010-09-30 21:57:53483 const StaticData* static_data() const { return static_data_; }
484
485 const FilePath& path() const { return static_data_->path; }
[email protected]92888082010-10-18 19:24:57486 const GURL& url() const { return static_data_->extension_url; }
487 Location location() const { return static_data_->location; }
488 void set_location(Location location) {
489 mutable_static_data_->location = location;
490 }
491
[email protected]4361c7c2010-09-30 21:57:53492 const std::string& id() const { return static_data_->id; }
[email protected]92888082010-10-18 19:24:57493 const Version* version() const { return static_data_->version.get(); }
[email protected]4a8d3272009-03-10 19:15:08494 // String representation of the version number.
495 const std::string VersionString() const;
[email protected]4361c7c2010-09-30 21:57:53496 const std::string& name() const { return static_data_->name; }
[email protected]92888082010-10-18 19:24:57497 const std::string& public_key() const { return static_data_->public_key; }
498 const std::string& description() const { return static_data_->description; }
[email protected]6657afa62009-11-04 02:15:20499 bool converted_from_user_script() const {
[email protected]92888082010-10-18 19:24:57500 return static_data_->converted_from_user_script;
[email protected]6657afa62009-11-04 02:15:20501 }
[email protected]92888082010-10-18 19:24:57502 const UserScriptList& content_scripts() const {
503 return static_data_->content_scripts;
504 }
505 ExtensionAction* page_action() const {
506 return static_data_->page_action.get();
507 }
508 ExtensionAction* browser_action() const {
509 return static_data_->browser_action.get();
510 }
511 const std::vector<PluginInfo>& plugins() const {
512 return static_data_->plugins;
513 }
514 const GURL& background_url() const { return static_data_->background_url; }
515 const GURL& options_url() const { return static_data_->options_url; }
516 const GURL& devtools_url() const { return static_data_->devtools_url; }
517 const std::vector<GURL>& toolstrips() const {
518 return static_data_->toolstrips;
519 }
[email protected]0df165f2010-09-28 16:49:40520 const std::set<std::string>& api_permissions() const {
[email protected]4361c7c2010-09-30 21:57:53521 return static_data_->api_permissions;
[email protected]35506352009-08-07 18:58:19522 }
[email protected]b30e0dd2010-01-29 23:33:21523 const URLPatternList& host_permissions() const {
[email protected]92888082010-10-18 19:24:57524 return static_data_->host_permissions;
[email protected]c7ad50f2009-09-11 06:28:15525 }
526
[email protected]aeb53b32009-10-29 07:34:45527 // Returns true if the extension has the specified API permission.
[email protected]0df165f2010-09-28 16:49:40528 static bool HasApiPermission(const std::set<std::string>& api_permissions,
[email protected]246c05f2010-09-10 09:12:11529 const std::string& function_name);
[email protected]583d45c12010-08-31 02:48:12530
[email protected]246c05f2010-09-10 09:12:11531 bool HasApiPermission(const std::string& function_name) const {
532 return HasApiPermission(this->api_permissions(), function_name);
[email protected]aeb53b32009-10-29 07:34:45533 }
534
[email protected]4361c7c2010-09-30 21:57:53535 const ExtensionExtent& GetEffectiveHostPermissions() const {
536 return static_data_->effective_host_permissions;
537 }
[email protected]b24d8312009-08-27 06:47:46538
[email protected]fbd17cf2010-04-28 23:52:56539 // Whether or not the extension is allowed permission for a URL pattern from
540 // the manifest. http, https, and chrome://favicon/ is allowed for all
541 // extensions, while component extensions are allowed access to
542 // chrome://resources.
[email protected]be7e5cb2010-10-04 12:53:17543 bool CanSpecifyHostPermission(const URLPattern pattern) const;
[email protected]fbd17cf2010-04-28 23:52:56544
[email protected]584b8e3f2010-04-10 00:23:37545 // Whether the extension has access to the given URL.
546 bool HasHostPermission(const GURL& url) const;
547
[email protected]0df165f2010-09-28 16:49:40548 // Whether the extension has effective access to all hosts. This is true if
549 // there is a content script that matches all hosts, if there is a host
550 // permission grants access to all hosts (like <all_urls>) or an api
551 // permission that effectively grants access to all hosts (e.g. proxy,
552 // network, etc.)
553 bool HasEffectiveAccessToAllHosts() const;
[email protected]b24d8312009-08-27 06:47:46554
[email protected]92888082010-10-18 19:24:57555 const GURL& update_url() const { return static_data_->update_url; }
[email protected]807871f2010-09-16 01:04:48556
[email protected]92888082010-10-18 19:24:57557 const ExtensionIconSet& icons() const {
558 return static_data_->icons;
559 }
[email protected]4a8d3272009-03-10 19:15:08560
[email protected]37cd64d2010-10-25 18:17:58561 // Returns the Homepage URL for this extension. If homepage_url was not
562 // specified in the manifest, this returns the Google Gallery URL. For
[email protected]bfa90a3a2010-04-28 15:43:23563 // third-party extensions, this returns a blank GURL.
[email protected]37cd64d2010-10-25 18:17:58564 GURL GetHomepageURL() const;
[email protected]bfa90a3a2010-04-28 15:43:23565
[email protected]25b34332009-06-05 21:53:19566 // Theme-related.
[email protected]92888082010-10-18 19:24:57567 DictionaryValue* GetThemeImages() const {
568 return static_data_->theme_images.get();
[email protected]7895ea22009-06-02 20:53:50569 }
[email protected]92888082010-10-18 19:24:57570 DictionaryValue* GetThemeColors() const {
571 return static_data_->theme_colors.get();
572 }
573 DictionaryValue* GetThemeTints() const {
574 return static_data_->theme_tints.get();
575 }
576 DictionaryValue* GetThemeDisplayProperties() const {
577 return static_data_->theme_display_properties.get();
578 }
579 bool is_theme() const { return static_data_->is_theme; }
[email protected]631cf822009-05-15 07:01:25580
[email protected]facd7a7652009-06-05 23:15:02581 // Returns a list of paths (relative to the extension dir) for images that
582 // the browser might load (like themes and page action icons).
583 std::set<FilePath> GetBrowserImages();
584
[email protected]807871f2010-09-16 01:04:48585 // Get an extension icon as a resource or URL.
586 ExtensionResource GetIconResource(int size,
587 ExtensionIconSet::MatchType match_type);
588 GURL GetIconURL(int size, ExtensionIconSet::MatchType match_type);
[email protected]f34e79632010-03-17 02:34:08589
[email protected]b6ab96d2009-08-20 18:58:19590 const DictionaryValue* manifest_value() const {
[email protected]92888082010-10-18 19:24:57591 return static_data_->manifest_value.get();
[email protected]b6ab96d2009-08-20 18:58:19592 }
593
[email protected]4361c7c2010-09-30 21:57:53594 const std::string default_locale() const {
595 return static_data_->default_locale;
596 }
[email protected]9428edc2009-11-18 18:02:47597
[email protected]86c008e82009-08-28 20:26:05598 // Chrome URL overrides (see ExtensionOverrideUI).
[email protected]d3cfa482009-10-17 13:54:57599 const URLOverrideMap& GetChromeURLOverrides() const {
[email protected]92888082010-10-18 19:24:57600 return static_data_->chrome_url_overrides;
[email protected]86c008e82009-08-28 20:26:05601 }
602
[email protected]92888082010-10-18 19:24:57603 const std::string omnibox_keyword() const {
604 return static_data_->omnibox_keyword;
605 }
[email protected]56ad3792010-05-28 17:45:33606
[email protected]92888082010-10-18 19:24:57607 bool is_app() const { return static_data_->is_app; }
608 const ExtensionExtent& web_extent() const {
609 return static_data_->extent;
610 }
611 const std::string& launch_local_path() const {
612 return static_data_->launch_local_path;
613 }
614 const std::string& launch_web_url() const {
615 return static_data_->launch_web_url;
[email protected]fe13bf62010-08-26 14:33:19616 }
[email protected]d83a5602010-09-16 00:22:48617 extension_misc::LaunchContainer launch_container() const {
[email protected]92888082010-10-18 19:24:57618 return static_data_->launch_container;
[email protected]d83a5602010-09-16 00:22:48619 }
[email protected]92888082010-10-18 19:24:57620 int launch_width() const { return static_data_->launch_width; }
621 int launch_height() const { return static_data_->launch_height; }
[email protected]4361c7c2010-09-30 21:57:53622 bool incognito_split_mode() const {
623 return static_data_->incognito_split_mode;
624 }
[email protected]867a73e12010-03-19 20:45:46625
626 // Gets the fully resolved absolute launch URL.
627 GURL GetFullLaunchURL() const;
[email protected]ceefd3d2010-03-12 09:10:29628
[email protected]e95ad332009-08-03 19:44:25629 // Whether the background page, if any, is ready. We don't load other
630 // components until then. If there is no background page, we consider it to
631 // be ready.
632 bool GetBackgroundPageReady();
633 void SetBackgroundPageReady();
634
[email protected]1e8c93f2010-02-08 22:58:31635 // Getter and setter for the flag that specifies whether the extension is
636 // being upgraded.
[email protected]92888082010-10-18 19:24:57637 bool being_upgraded() const { return GetRuntimeData()->being_upgraded; }
638 void set_being_upgraded(bool value) {
639 GetRuntimeData()->being_upgraded = value;
640 }
[email protected]1e8c93f2010-02-08 22:58:31641
[email protected]d9ad80f2010-03-30 20:40:18642 // Image cache related methods. These are only valid on the UI thread and
[email protected]052c92702010-06-25 07:25:52643 // not maintained by this class. See ImageLoadingTracker for usage. The
644 // |original_size| parameter should be the size of the image at |source|
645 // before any scaling may have been done to produce the pixels in |image|.
[email protected]d9ad80f2010-03-30 20:40:18646 void SetCachedImage(const ExtensionResource& source,
[email protected]052c92702010-06-25 07:25:52647 const SkBitmap& image,
648 const gfx::Size& original_size);
649 bool HasCachedImage(const ExtensionResource& source,
650 const gfx::Size& max_size);
651 SkBitmap GetCachedImage(const ExtensionResource& source,
652 const gfx::Size& max_size);
[email protected]4361c7c2010-09-30 21:57:53653 bool is_hosted_app() const { return is_app() && !web_extent().is_empty(); }
654 bool is_packaged_app() const { return is_app() && web_extent().is_empty(); }
655
[email protected]be7e5cb2010-10-04 12:53:17656 // Returns true if this extension is a COMPONENT extension, or if it is
657 // on the whitelist of extensions that can script all pages.
658 bool CanExecuteScriptEverywhere() const;
659
[email protected]4a8d3272009-03-10 19:15:08660 private:
[email protected]4ead6f72010-10-13 19:54:18661 // Normalize the path for use by the extension. On Windows, this will make
662 // sure the drive letter is uppercase.
663 static FilePath MaybeNormalizePath(const FilePath& path);
664
[email protected]052c92702010-06-25 07:25:52665 // Helper function for implementing HasCachedImage/GetCachedImage. A return
666 // value of NULL means there is no matching image cached (we allow caching an
667 // empty SkBitmap).
668 SkBitmap* GetCachedImageImpl(const ExtensionResource& source,
669 const gfx::Size& max_size);
[email protected]d9ad80f2010-03-30 20:40:18670
[email protected]3cfbd0e2009-03-18 21:26:24671 // Helper method that loads a UserScript object from a
672 // dictionary in the content_script list of the manifest.
673 bool LoadUserScriptHelper(const DictionaryValue* content_script,
674 int definition_index,
675 std::string* error,
676 UserScript* result);
[email protected]f7f3a5f2009-05-01 22:02:34677
[email protected]6657afa62009-11-04 02:15:20678 // Helper method that loads either the include_globs or exclude_globs list
679 // from an entry in the content_script lists of the manifest.
680 bool LoadGlobsHelper(const DictionaryValue* content_script,
681 int content_script_index,
[email protected]e2194742010-08-12 05:54:34682 const char* globs_property_name,
[email protected]6657afa62009-11-04 02:15:20683 std::string* error,
[email protected]11f4857282009-11-13 19:56:17684 void(UserScript::*add_method)(const std::string& glob),
[email protected]6657afa62009-11-04 02:15:20685 UserScript *instance);
686
[email protected]867a73e12010-03-19 20:45:46687 // Helpers to load various chunks of the manifest.
[email protected]3ba0fd32010-06-19 05:39:10688 bool LoadIsApp(const DictionaryValue* manifest, std::string* error);
[email protected]e2194742010-08-12 05:54:34689 bool LoadExtent(const DictionaryValue* manifest, const char* key,
[email protected]4fdbc1492010-07-01 01:20:59690 ExtensionExtent* extent, const char* list_error,
691 const char* value_error, std::string* error);
[email protected]867a73e12010-03-19 20:45:46692 bool LoadLaunchContainer(const DictionaryValue* manifest, std::string* error);
693 bool LoadLaunchURL(const DictionaryValue* manifest, std::string* error);
[email protected]cbf4d1912010-08-12 18:24:57694 bool EnsureNotHybridApp(const DictionaryValue* manifest, std::string* error);
[email protected]867a73e12010-03-19 20:45:46695
[email protected]5d246db22009-10-27 06:17:57696 // Helper method to load an ExtensionAction from the page_action or
[email protected]92c6f9b92009-10-24 04:35:08697 // browser_action entries in the manifest.
[email protected]5d246db22009-10-27 06:17:57698 ExtensionAction* LoadExtensionActionHelper(
[email protected]92c6f9b92009-10-24 04:35:08699 const DictionaryValue* extension_action, std::string* error);
700
[email protected]4361c7c2010-09-30 21:57:53701 // Calculates the effective host permissions from the permissions and content
702 // script petterns.
703 void InitEffectiveHostPermissions();
704
[email protected]2f6698b2010-10-14 00:58:21705 // Returns true if the extension has more than one "UI surface". For example,
706 // an extension that has a browser action and a page action.
707 bool HasMultipleUISurfaces() const;
708
[email protected]e2eb43112009-05-29 21:19:54709 // Figures out if a source contains keys not associated with themes - we
710 // don't want to allow scripts and such to be bundled with themes.
711 bool ContainsNonThemeKeys(const DictionaryValue& source);
712
[email protected]1952c7d2010-03-04 23:48:34713 // Returns true if the string is one of the known api permissions (see
[email protected]0df165f2010-09-28 16:49:40714 // kPermissions).
[email protected]1952c7d2010-03-04 23:48:34715 bool IsAPIPermission(const std::string& permission);
716
[email protected]a2a098d2010-09-29 19:42:55717 // The set of unique API install messages that the extension has.
718 // NOTE: This only includes messages related to permissions declared in the
719 // "permissions" key in the manifest. Permissions implied from other features
720 // of the manifest, like plugins and content scripts are not included.
721 std::set<string16> GetSimplePermissionMessages();
722
723 // The permission message displayed related to the host permissions for
724 // this extension.
725 string16 GetHostPermissionMessage();
726
[email protected]92888082010-10-18 19:24:57727 // Returns a mutable pointer to our runtime data. Can only be called on
728 // the UI thread.
729 RuntimeData* GetRuntimeData() const;
730
[email protected]4361c7c2010-09-30 21:57:53731 // Collection of extension data that doesn't change doesn't change once an
732 // Extension object has been initialized. The mutable version is valid only
733 // until InitFromValue finishes, to ensure we don't accidentally modify it
734 // post-initialization.
735 StaticData* mutable_static_data_;
736 scoped_refptr<const StaticData> static_data_;
[email protected]4a8d3272009-03-10 19:15:08737
[email protected]92888082010-10-18 19:24:57738 // Runtime data.
739 scoped_ptr<const RuntimeData> runtime_data_;
[email protected]1e8c93f2010-02-08 22:58:31740
[email protected]19118d52010-07-26 22:13:42741 FRIEND_TEST_ALL_PREFIXES(ExtensionTest, LoadPageActionHelper);
742 FRIEND_TEST_ALL_PREFIXES(TabStripModelTest, Apps);
[email protected]ae7fe712009-07-02 20:33:58743
[email protected]894bb502009-05-21 22:39:57744 DISALLOW_COPY_AND_ASSIGN(Extension);
[email protected]7713d632008-12-02 07:52:33745};
746
[email protected]b1748b1d82009-11-30 20:32:56747typedef std::vector<Extension*> ExtensionList;
[email protected]ec5b50d2010-10-09 16:35:18748typedef std::set<std::string> ExtensionIdSet;
[email protected]b1748b1d82009-11-30 20:32:56749
[email protected]c6d474f82009-12-16 21:11:06750// Handy struct to pass core extension info around.
751struct ExtensionInfo {
752 ExtensionInfo(const DictionaryValue* manifest,
753 const std::string& id,
754 const FilePath& path,
[email protected]3bb84992010-08-26 17:23:46755 Extension::Location location);
756 ~ExtensionInfo();
[email protected]c6d474f82009-12-16 21:11:06757
758 scoped_ptr<DictionaryValue> extension_manifest;
759 std::string extension_id;
760 FilePath extension_path;
761 Extension::Location extension_location;
762
763 private:
764 DISALLOW_COPY_AND_ASSIGN(ExtensionInfo);
765};
766
[email protected]211030342010-09-30 18:41:06767// Struct used for the details of the EXTENSION_UNINSTALLED
768// notification.
769struct UninstalledExtensionInfo {
770 explicit UninstalledExtensionInfo(const Extension& extension);
[email protected]9b2331d92010-10-04 23:11:19771 ~UninstalledExtensionInfo();
[email protected]211030342010-09-30 18:41:06772
773 std::string extension_id;
774 std::set<std::string> extension_api_permissions;
[email protected]76c25112010-10-01 22:37:21775 // TODO(akalin): Once we have a unified ExtensionType, replace the
776 // below member variables with a member of that type.
777 bool is_theme;
778 bool is_app;
779 bool converted_from_user_script;
780 GURL update_url;
[email protected]211030342010-09-30 18:41:06781};
782
[email protected]5b1a0e22009-05-26 19:00:58783#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_H_