blob: a8a05f5d6cca9751fe51d06ab41517aafc3f219f [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]4d2913e32010-11-30 00:28:5552 EXTERNAL_PREF_DOWNLOAD, // A crx file from an external directory (via
53 // prefs), installed from an update URL.
54 EXTERNAL_POLICY_DOWNLOAD, // A crx file from an external directory (via
55 // admin policies), installed from an update URL.
[email protected]04cb7542010-10-25 10:50:0656
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]0df165f2010-09-28 16:49:40103 // A permission is defined by its |name| (what is used in the manifest),
104 // and the |message_id| that's used by install/update UI.
105 struct Permission {
106 const char* const name;
107 const int message_id;
108 };
109
[email protected]66e4eb32010-10-27 20:37:41110 static scoped_refptr<Extension> Create(const FilePath& path,
111 Location location,
112 const DictionaryValue& value,
113 bool require_key,
114 std::string* error);
115
[email protected]4d2913e32010-11-30 00:28:55116 // Return the update url used by gallery/webstore extensions.
117 static GURL GalleryUpdateUrl(bool secure);
118
[email protected]0df165f2010-09-28 16:49:40119 // The install message id for |permission|. Returns 0 if none exists.
120 static int GetPermissionMessageId(const std::string& permission);
121
[email protected]a2a098d2010-09-29 19:42:55122 // Returns the full list of permission messages that this extension
123 // should display at install time.
[email protected]9adb9692010-10-29 23:14:02124 std::vector<string16> GetPermissionMessages() const;
[email protected]a2a098d2010-09-29 19:42:55125
126 // Returns the distinct hosts that should be displayed in the install UI. This
127 // discards some of the detail that is present in the manifest to make it as
128 // easy as possible to process by users. In particular we disregard the scheme
129 // and path components of URLPatterns and de-dupe the result.
130 static std::vector<std::string> GetDistinctHosts(
131 const URLPatternList& host_patterns);
[email protected]9adb9692010-10-29 23:14:02132 std::vector<std::string> GetDistinctHosts() const;
[email protected]0df165f2010-09-28 16:49:40133
[email protected]c3e3def742009-07-17 07:51:06134 // Icon sizes used by the extension system.
[email protected]d2817012009-08-04 06:46:21135 static const int kIconSizes[];
[email protected]c3e3def742009-07-17 07:51:06136
[email protected]4c4f8192009-10-17 01:03:26137 // Max size (both dimensions) for browser and page actions.
138 static const int kPageActionIconMaxSize;
139 static const int kBrowserActionIconMaxSize;
140
[email protected]35506352009-08-07 18:58:19141 // Each permission is a module that the extension is permitted to use.
[email protected]218990c2010-06-26 01:21:07142 //
[email protected]0df165f2010-09-28 16:49:40143 // NOTE: To add a new permission, define it here, and add an entry to
144 // Extension::kPermissions.
[email protected]9dcf8f12010-09-02 20:39:19145 static const char kBackgroundPermission[];
146 static const char kBookmarkPermission[];
147 static const char kContextMenusPermission[];
148 static const char kCookiePermission[];
149 static const char kExperimentalPermission[];
150 static const char kGeolocationPermission[];
151 static const char kHistoryPermission[];
152 static const char kIdlePermission[];
[email protected]eb255772010-10-04 22:10:46153 static const char kManagementPermission[];
[email protected]9dcf8f12010-09-02 20:39:19154 static const char kNotificationPermission[];
155 static const char kProxyPermission[];
156 static const char kTabPermission[];
157 static const char kUnlimitedStoragePermission[];
[email protected]9dcf8f12010-09-02 20:39:19158 static const char kWebstorePrivatePermission[];
[email protected]aeb53b32009-10-29 07:34:45159
[email protected]0df165f2010-09-28 16:49:40160 static const Permission kPermissions[];
[email protected]35506352009-08-07 18:58:19161 static const size_t kNumPermissions;
[email protected]9dcf8f12010-09-02 20:39:19162 static const char* const kHostedAppPermissionNames[];
[email protected]b746f372010-08-29 21:39:32163 static const size_t kNumHostedAppPermissions;
[email protected]35506352009-08-07 18:58:19164
[email protected]03b612f2010-08-13 21:09:21165 // The old name for the unlimited storage permission, which is deprecated but
166 // still accepted as meaning the same thing as kUnlimitedStoragePermission.
[email protected]9dcf8f12010-09-02 20:39:19167 static const char kOldUnlimitedStoragePermission[];
[email protected]03b612f2010-08-13 21:09:21168
[email protected]8d888c12010-11-30 00:00:25169 // Valid schemes for web extent URLPatterns.
170 static const int kValidWebExtentSchemes;
171
[email protected]f71f7e62010-12-07 03:45:33172 // Valid schemes for host permission URLPatterns.
173 static const int kValidHostPermissionSchemes;
174
[email protected]b746f372010-08-29 21:39:32175 // Returns true if the string is one of the known hosted app permissions (see
176 // kHostedAppPermissionNames).
177 static bool IsHostedAppPermission(const std::string& permission);
178
[email protected]6014d672008-12-05 00:38:25179 // The name of the manifest inside an extension.
[email protected]99efb7b12009-12-18 02:39:16180 static const FilePath::CharType kManifestFilename[];
[email protected]6014d672008-12-05 00:38:25181
[email protected]300cc58db2009-08-19 20:45:14182 // The name of locale folder inside an extension.
[email protected]99efb7b12009-12-18 02:39:16183 static const FilePath::CharType kLocaleFolder[];
[email protected]300cc58db2009-08-19 20:45:14184
185 // The name of the messages file inside an extension.
[email protected]99efb7b12009-12-18 02:39:16186 static const FilePath::CharType kMessagesFilename[];
[email protected]300cc58db2009-08-19 20:45:14187
[email protected]25b34332009-06-05 21:53:19188#if defined(OS_WIN)
[email protected]9dcf8f12010-09-02 20:39:19189 static const char kExtensionRegistryPath[];
[email protected]25b34332009-06-05 21:53:19190#endif
191
[email protected]37eeb5a2009-02-26 23:36:17192 // The number of bytes in a legal id.
[email protected]fe0e7822009-02-26 23:51:48193 static const size_t kIdSize;
[email protected]37eeb5a2009-02-26 23:36:17194
[email protected]e435d6b72009-07-25 03:15:58195 // The mimetype used for extensions.
196 static const char kMimeType[];
197
[email protected]25b34332009-06-05 21:53:19198 // Checks to see if the extension has a valid ID.
199 static bool IdIsValid(const std::string& id);
200
[email protected]4ead6f72010-10-13 19:54:18201 // Generate an ID for an extension in the given path.
202 static std::string GenerateIdForPath(const FilePath& file_name);
203
[email protected]e435d6b72009-07-25 03:15:58204 // Returns true if the specified file is an extension.
205 static bool IsExtension(const FilePath& file_name);
206
[email protected]25b34332009-06-05 21:53:19207 // Whether the |location| is external or not.
208 static inline bool IsExternalLocation(Location location) {
209 return location == Extension::EXTERNAL_PREF ||
[email protected]8ef78fd2010-08-19 17:14:32210 location == Extension::EXTERNAL_REGISTRY ||
[email protected]04cb7542010-10-25 10:50:06211 location == Extension::EXTERNAL_PREF_DOWNLOAD ||
212 location == Extension::EXTERNAL_POLICY_DOWNLOAD;
213 }
214
215 // Whether extensions with |location| are auto-updatable or not.
216 static inline bool IsAutoUpdateableLocation(Location location) {
217 // Only internal and external extensions can be autoupdated.
218 return location == Extension::INTERNAL ||
219 IsExternalLocation(location);
[email protected]25b34332009-06-05 21:53:19220 }
221
[email protected]9b217652010-10-08 22:04:23222 // See HistogramType definition above.
[email protected]9adb9692010-10-29 23:14:02223 HistogramType GetHistogramType() const;
[email protected]9b217652010-10-08 22:04:23224
[email protected]07c00d992009-03-04 20:27:04225 // Returns an absolute url to a resource inside of an extension. The
[email protected]eab9b452009-01-23 20:48:59226 // |extension_url| argument should be the url() from an Extension object. The
227 // |relative_path| can be untrusted user input. The returned URL will either
228 // be invalid() or a child of |extension_url|.
229 // NOTE: Static so that it can be used from multiple threads.
230 static GURL GetResourceURL(const GURL& extension_url,
231 const std::string& relative_path);
[email protected]cffd7892010-08-26 17:43:28232 GURL GetResourceURL(const std::string& relative_path) const {
[email protected]3cfbd0e2009-03-18 21:26:24233 return GetResourceURL(url(), relative_path);
234 }
[email protected]eab9b452009-01-23 20:48:59235
[email protected]99efb7b12009-12-18 02:39:16236 // Returns an extension resource object. |relative_path| should be UTF8
237 // encoded.
[email protected]9adb9692010-10-29 23:14:02238 ExtensionResource GetResource(const std::string& relative_path) const;
[email protected]99efb7b12009-12-18 02:39:16239
240 // As above, but with |relative_path| following the file system's encoding.
[email protected]9adb9692010-10-29 23:14:02241 ExtensionResource GetResource(const FilePath& relative_path) const;
[email protected]eab9b452009-01-23 20:48:59242
[email protected]a17f9462009-06-09 02:56:41243 // |input| is expected to be the text of an rsa public or private key. It
244 // tolerates the presence or absence of bracking header/footer like this:
245 // -----(BEGIN|END) [RSA PUBLIC/PRIVATE] KEY-----
246 // and may contain newlines.
247 static bool ParsePEMKeyBytes(const std::string& input, std::string* output);
248
249 // Does a simple base64 encoding of |input| into |output|.
250 static bool ProducePEM(const std::string& input, std::string* output);
251
[email protected]84ac7f32009-10-06 06:17:54252 // Generates an extension ID from arbitrary input. The same input string will
253 // always generate the same output ID.
254 static bool GenerateId(const std::string& input, std::string* output);
[email protected]fbcc40302009-06-12 20:45:45255
[email protected]a17f9462009-06-09 02:56:41256 // Expects base64 encoded |input| and formats into |output| including
257 // the appropriate header & footer.
258 static bool FormatPEMForFileOutput(const std::string input,
259 std::string* output, bool is_public);
260
[email protected]2a409532009-08-28 19:39:44261 // Determine whether |new_extension| has increased privileges compared to
[email protected]8d888c12010-11-30 00:00:25262 // its previously granted permissions, specified by |granted_apis|,
263 // |granted_extent| and |granted_full_access|.
264 static bool IsPrivilegeIncrease(const bool granted_full_access,
265 const std::set<std::string>& granted_apis,
266 const ExtensionExtent& granted_extent,
[email protected]9adb9692010-10-29 23:14:02267 const Extension* new_extension);
[email protected]b24d8312009-08-27 06:47:46268
[email protected]c690a9812009-12-17 05:55:32269 // Given an extension and icon size, read it if present and decode it into
[email protected]ae2e0f92010-04-06 20:32:23270 // result. In the browser process, this will DCHECK if not called on the
271 // file thread. To easily load extension images on the UI thread, see
272 // ImageLoadingTracker.
[email protected]9adb9692010-10-29 23:14:02273 static void DecodeIcon(const Extension* extension,
[email protected]c690a9812009-12-17 05:55:32274 Icons icon_size,
275 scoped_ptr<SkBitmap>* result);
276
277 // Given an icon_path and icon size, read it if present and decode it into
[email protected]ae2e0f92010-04-06 20:32:23278 // result. In the browser process, this will DCHECK if not called on the
279 // file thread. To easily load extension images on the UI thread, see
280 // ImageLoadingTracker.
[email protected]c690a9812009-12-17 05:55:32281 static void DecodeIconFromPath(const FilePath& icon_path,
282 Icons icon_size,
283 scoped_ptr<SkBitmap>* result);
284
[email protected]a807bbe2010-04-14 10:51:19285 // Returns the base extension url for a given |extension_id|.
286 static GURL GetBaseURLFromExtensionId(const std::string& extension_id);
287
[email protected]ec5b50d2010-10-09 16:35:18288 // Returns the url prefix for the extension/apps gallery. Can be set via the
[email protected]ce3eb7b62010-09-14 13:57:44289 // --apps-gallery-url switch. The URL returned will not contain a trailing
[email protected]d3071992010-10-08 15:24:07290 // slash. Do not use this as a prefix/extent for the store. Instead see
291 // ExtensionsService::GetWebStoreApp or
292 // ExtensionsService::IsDownloadFromGallery
293 static std::string ChromeStoreLaunchURL();
[email protected]a0cf04a2010-06-23 03:29:55294
[email protected]be7e5cb2010-10-04 12:53:17295 // Helper function that consolidates the check for whether the script can
296 // execute into one location. |page_url| is the page that is the candidate
297 // for running the script, |can_execute_script_everywhere| specifies whether
298 // the extension is on the whitelist, |allowed_pages| is a vector of
299 // URLPatterns, listing what access the extension has, |script| is the script
300 // pointer (if content script) and |error| is an optional parameter, which
301 // will receive the error string listing why access was denied.
302 static bool CanExecuteScriptOnPage(
303 const GURL& page_url,
304 bool can_execute_script_everywhere,
305 const std::vector<URLPattern>* allowed_pages,
306 UserScript* script,
307 std::string* error);
308
309 // Adds an extension to the scripting whitelist. Used for testing only.
[email protected]10fb1992010-10-08 09:00:17310 static void SetScriptingWhitelist(const ScriptingWhitelist& whitelist);
[email protected]be7e5cb2010-10-04 12:53:17311
[email protected]aeb53b32009-10-29 07:34:45312 // Returns true if the extension has the specified API permission.
[email protected]0df165f2010-09-28 16:49:40313 static bool HasApiPermission(const std::set<std::string>& api_permissions,
[email protected]246c05f2010-09-10 09:12:11314 const std::string& function_name);
[email protected]583d45c12010-08-31 02:48:12315
[email protected]8d888c12010-11-30 00:00:25316 // Whether the |effective_host_permissions| and |api_permissions| include
317 // effective access to all hosts. See the non-static version of the method
318 // for more details.
319 static bool HasEffectiveAccessToAllHosts(
320 const ExtensionExtent& effective_host_permissions,
321 const std::set<std::string>& api_permissions);
322
[email protected]246c05f2010-09-10 09:12:11323 bool HasApiPermission(const std::string& function_name) const {
324 return HasApiPermission(this->api_permissions(), function_name);
[email protected]aeb53b32009-10-29 07:34:45325 }
326
[email protected]4361c7c2010-09-30 21:57:53327 const ExtensionExtent& GetEffectiveHostPermissions() const {
[email protected]6f229e82010-11-02 17:47:26328 return effective_host_permissions_;
[email protected]4361c7c2010-09-30 21:57:53329 }
[email protected]b24d8312009-08-27 06:47:46330
[email protected]fbd17cf2010-04-28 23:52:56331 // Whether or not the extension is allowed permission for a URL pattern from
332 // the manifest. http, https, and chrome://favicon/ is allowed for all
333 // extensions, while component extensions are allowed access to
334 // chrome://resources.
[email protected]a3caa822010-10-26 18:10:00335 bool CanSpecifyHostPermission(const URLPattern& pattern) const;
[email protected]fbd17cf2010-04-28 23:52:56336
[email protected]584b8e3f2010-04-10 00:23:37337 // Whether the extension has access to the given URL.
338 bool HasHostPermission(const GURL& url) const;
339
[email protected]0df165f2010-09-28 16:49:40340 // Whether the extension has effective access to all hosts. This is true if
341 // there is a content script that matches all hosts, if there is a host
342 // permission grants access to all hosts (like <all_urls>) or an api
343 // permission that effectively grants access to all hosts (e.g. proxy,
344 // network, etc.)
345 bool HasEffectiveAccessToAllHosts() const;
[email protected]b24d8312009-08-27 06:47:46346
[email protected]8d888c12010-11-30 00:00:25347 // Whether the extension effectively has all permissions (for example, by
348 // having an NPAPI plugin).
349 bool HasFullPermissions() const;
350
[email protected]37cd64d2010-10-25 18:17:58351 // Returns the Homepage URL for this extension. If homepage_url was not
352 // specified in the manifest, this returns the Google Gallery URL. For
[email protected]bfa90a3a2010-04-28 15:43:23353 // third-party extensions, this returns a blank GURL.
[email protected]37cd64d2010-10-25 18:17:58354 GURL GetHomepageURL() const;
[email protected]bfa90a3a2010-04-28 15:43:23355
[email protected]facd7a7652009-06-05 23:15:02356 // Returns a list of paths (relative to the extension dir) for images that
357 // the browser might load (like themes and page action icons).
[email protected]9adb9692010-10-29 23:14:02358 std::set<FilePath> GetBrowserImages() const;
[email protected]facd7a7652009-06-05 23:15:02359
[email protected]807871f2010-09-16 01:04:48360 // Get an extension icon as a resource or URL.
[email protected]9adb9692010-10-29 23:14:02361 ExtensionResource GetIconResource(
362 int size, ExtensionIconSet::MatchType match_type) const;
363 GURL GetIconURL(int size, ExtensionIconSet::MatchType match_type) const;
[email protected]f34e79632010-03-17 02:34:08364
[email protected]867a73e12010-03-19 20:45:46365 // Gets the fully resolved absolute launch URL.
366 GURL GetFullLaunchURL() const;
[email protected]6f229e82010-11-02 17:47:26367 // Image cache related methods. These are only valid on the UI thread and
368 // not maintained by this class. See ImageLoadingTracker for usage. The
369 // |original_size| parameter should be the size of the image at |source|
370 // before any scaling may have been done to produce the pixels in |image|.
371 void SetCachedImage(const ExtensionResource& source,
372 const SkBitmap& image,
373 const gfx::Size& original_size) const;
374 bool HasCachedImage(const ExtensionResource& source,
375 const gfx::Size& max_size) const;
376 SkBitmap GetCachedImage(const ExtensionResource& source,
377 const gfx::Size& max_size) const;
378 // Returns true if this extension is a COMPONENT extension, or if it is
379 // on the whitelist of extensions that can script all pages.
380 bool CanExecuteScriptEverywhere() const;
381
[email protected]a65882c2010-11-12 15:15:09382 // Returns true if this extension updates itself using the extension
383 // gallery.
384 bool UpdatesFromGallery() const;
385
[email protected]6f229e82010-11-02 17:47:26386 // Accessors:
387
388 const FilePath& path() const { return path_; }
389 const GURL& url() const { return extension_url_; }
390 Location location() const { return location_; }
391 const std::string& id() const { return id_; }
392 const Version* version() const { return version_.get(); }
393 const std::string VersionString() const;
394 const std::string& name() const { return name_; }
395 const std::string& public_key() const { return public_key_; }
396 const std::string& description() const { return description_; }
397 bool converted_from_user_script() const {
398 return converted_from_user_script_;
399 }
400 const UserScriptList& content_scripts() const { return content_scripts_; }
401 ExtensionAction* page_action() const { return page_action_.get(); }
402 ExtensionAction* browser_action() const { return browser_action_.get(); }
403 const std::vector<PluginInfo>& plugins() const { return plugins_; }
404 const GURL& background_url() const { return background_url_; }
405 const GURL& options_url() const { return options_url_; }
406 const GURL& devtools_url() const { return devtools_url_; }
407 const std::vector<GURL>& toolstrips() const { return toolstrips_; }
408 const std::set<std::string>& api_permissions() const {
409 return api_permissions_;
410 }
411 const URLPatternList& host_permissions() const { return host_permissions_; }
412 const GURL& update_url() const { return update_url_; }
413 const ExtensionIconSet& icons() const { return icons_; }
414 const DictionaryValue* manifest_value() const {
415 return manifest_value_.get();
416 }
417 const std::string default_locale() const { return default_locale_; }
418 const URLOverrideMap& GetChromeURLOverrides() const {
419 return chrome_url_overrides_;
420 }
421 const std::string omnibox_keyword() const { return omnibox_keyword_; }
422 bool incognito_split_mode() const { return incognito_split_mode_; }
423
424 // App-related.
425 bool is_app() const { return is_app_; }
426 bool is_hosted_app() const { return is_app() && !web_extent().is_empty(); }
427 bool is_packaged_app() const { return is_app() && web_extent().is_empty(); }
428 const ExtensionExtent& web_extent() const { return extent_; }
429 const std::string& launch_local_path() const { return launch_local_path_; }
430 const std::string& launch_web_url() const { return launch_web_url_; }
431 extension_misc::LaunchContainer launch_container() const {
432 return launch_container_;
433 }
434 int launch_width() const { return launch_width_; }
435 int launch_height() const { return launch_height_; }
436
437 // Theme-related.
438 bool is_theme() const { return is_theme_; }
439 DictionaryValue* GetThemeImages() const { return theme_images_.get(); }
440 DictionaryValue* GetThemeColors() const {return theme_colors_.get(); }
441 DictionaryValue* GetThemeTints() const { return theme_tints_.get(); }
442 DictionaryValue* GetThemeDisplayProperties() const {
443 return theme_display_properties_.get();
444 }
445
[email protected]4a8d3272009-03-10 19:15:08446 private:
[email protected]66e4eb32010-10-27 20:37:41447 friend class base::RefCountedThreadSafe<Extension>;
448
[email protected]d7e9a862010-11-03 21:57:49449 // We keep a cache of images loaded from extension resources based on their
450 // path and a string representation of a size that may have been used to
451 // scale it (or the empty string if the image is at its original size).
452 typedef std::pair<FilePath, std::string> ImageCacheKey;
453 typedef std::map<ImageCacheKey, SkBitmap> ImageCache;
454
[email protected]4ead6f72010-10-13 19:54:18455 // Normalize the path for use by the extension. On Windows, this will make
456 // sure the drive letter is uppercase.
457 static FilePath MaybeNormalizePath(const FilePath& path);
458
[email protected]66e4eb32010-10-27 20:37:41459 Extension(const FilePath& path, Location location);
460 ~Extension();
461
462 // Initialize the extension from a parsed manifest.
463 // Usually, the id of an extension is generated by the "key" property of
464 // its manifest, but if |require_key| is |false|, a temporary ID will be
465 // generated based on the path.
466 bool InitFromValue(const DictionaryValue& value, bool require_key,
467 std::string* error);
468
[email protected]052c92702010-06-25 07:25:52469 // Helper function for implementing HasCachedImage/GetCachedImage. A return
470 // value of NULL means there is no matching image cached (we allow caching an
471 // empty SkBitmap).
472 SkBitmap* GetCachedImageImpl(const ExtensionResource& source,
[email protected]9adb9692010-10-29 23:14:02473 const gfx::Size& max_size) const;
[email protected]d9ad80f2010-03-30 20:40:18474
[email protected]3cfbd0e2009-03-18 21:26:24475 // Helper method that loads a UserScript object from a
476 // dictionary in the content_script list of the manifest.
477 bool LoadUserScriptHelper(const DictionaryValue* content_script,
478 int definition_index,
479 std::string* error,
480 UserScript* result);
[email protected]f7f3a5f2009-05-01 22:02:34481
[email protected]6657afa62009-11-04 02:15:20482 // Helper method that loads either the include_globs or exclude_globs list
483 // from an entry in the content_script lists of the manifest.
484 bool LoadGlobsHelper(const DictionaryValue* content_script,
485 int content_script_index,
[email protected]e2194742010-08-12 05:54:34486 const char* globs_property_name,
[email protected]6657afa62009-11-04 02:15:20487 std::string* error,
[email protected]11f4857282009-11-13 19:56:17488 void(UserScript::*add_method)(const std::string& glob),
[email protected]6657afa62009-11-04 02:15:20489 UserScript *instance);
490
[email protected]867a73e12010-03-19 20:45:46491 // Helpers to load various chunks of the manifest.
[email protected]3ba0fd32010-06-19 05:39:10492 bool LoadIsApp(const DictionaryValue* manifest, std::string* error);
[email protected]e2194742010-08-12 05:54:34493 bool LoadExtent(const DictionaryValue* manifest, const char* key,
[email protected]4fdbc1492010-07-01 01:20:59494 ExtensionExtent* extent, const char* list_error,
495 const char* value_error, std::string* error);
[email protected]867a73e12010-03-19 20:45:46496 bool LoadLaunchContainer(const DictionaryValue* manifest, std::string* error);
497 bool LoadLaunchURL(const DictionaryValue* manifest, std::string* error);
[email protected]cbf4d1912010-08-12 18:24:57498 bool EnsureNotHybridApp(const DictionaryValue* manifest, std::string* error);
[email protected]867a73e12010-03-19 20:45:46499
[email protected]5d246db22009-10-27 06:17:57500 // Helper method to load an ExtensionAction from the page_action or
[email protected]92c6f9b92009-10-24 04:35:08501 // browser_action entries in the manifest.
[email protected]5d246db22009-10-27 06:17:57502 ExtensionAction* LoadExtensionActionHelper(
[email protected]92c6f9b92009-10-24 04:35:08503 const DictionaryValue* extension_action, std::string* error);
504
[email protected]4361c7c2010-09-30 21:57:53505 // Calculates the effective host permissions from the permissions and content
506 // script petterns.
507 void InitEffectiveHostPermissions();
508
[email protected]2f6698b2010-10-14 00:58:21509 // Returns true if the extension has more than one "UI surface". For example,
510 // an extension that has a browser action and a page action.
511 bool HasMultipleUISurfaces() const;
512
[email protected]e2eb43112009-05-29 21:19:54513 // Figures out if a source contains keys not associated with themes - we
514 // don't want to allow scripts and such to be bundled with themes.
[email protected]9adb9692010-10-29 23:14:02515 bool ContainsNonThemeKeys(const DictionaryValue& source) const;
[email protected]e2eb43112009-05-29 21:19:54516
[email protected]1952c7d2010-03-04 23:48:34517 // Returns true if the string is one of the known api permissions (see
[email protected]0df165f2010-09-28 16:49:40518 // kPermissions).
[email protected]9adb9692010-10-29 23:14:02519 bool IsAPIPermission(const std::string& permission) const;
[email protected]1952c7d2010-03-04 23:48:34520
[email protected]a2a098d2010-09-29 19:42:55521 // The set of unique API install messages that the extension has.
522 // NOTE: This only includes messages related to permissions declared in the
523 // "permissions" key in the manifest. Permissions implied from other features
524 // of the manifest, like plugins and content scripts are not included.
[email protected]9adb9692010-10-29 23:14:02525 std::set<string16> GetSimplePermissionMessages() const;
[email protected]a2a098d2010-09-29 19:42:55526
527 // The permission message displayed related to the host permissions for
528 // this extension.
[email protected]9adb9692010-10-29 23:14:02529 string16 GetHostPermissionMessage() const;
[email protected]a2a098d2010-09-29 19:42:55530
[email protected]d7e9a862010-11-03 21:57:49531 // Cached images for this extension. This should only be touched on the UI
532 // thread.
533 mutable ImageCache image_cache_;
[email protected]1e8c93f2010-02-08 22:58:31534
[email protected]6f229e82010-11-02 17:47:26535 // A persistent, globally unique ID. An extension's ID is used in things
536 // like directory structures and URLs, and is expected to not change across
537 // versions. It is generated as a SHA-256 hash of the extension's public
538 // key, or as a hash of the path in the case of unpacked extensions.
539 std::string id_;
540
541 // The extension's human-readable name. Name is used for display purpose. It
542 // might be wrapped with unicode bidi control characters so that it is
543 // displayed correctly in RTL context.
544 // NOTE: Name is UTF-8 and may contain non-ascii characters.
545 std::string name_;
546
547 // The absolute path to the directory the extension is stored in.
548 FilePath path_;
549
550 // Default locale for fall back. Can be empty if extension is not localized.
551 std::string default_locale_;
552
553 // If true, a separate process will be used for the extension in incognito
554 // mode.
555 bool incognito_split_mode_;
556
557 // Defines the set of URLs in the extension's web content.
558 ExtensionExtent extent_;
559
560 // The set of host permissions that the extension effectively has access to,
561 // which is a merge of host_permissions_ and all of the match patterns in
562 // any content scripts the extension has. This is used to determine which
563 // URLs have the ability to load an extension's resources via embedded
564 // chrome-extension: URLs (see extension_protocols.cc).
565 ExtensionExtent effective_host_permissions_;
566
567 // The set of module-level APIs this extension can use.
568 std::set<std::string> api_permissions_;
569
570 // The icons for the extension.
571 ExtensionIconSet icons_;
572
573 // The base extension url for the extension.
574 GURL extension_url_;
575
576 // The location the extension was loaded from.
577 Location location_;
578
579 // The extension's version.
580 scoped_ptr<Version> version_;
581
582 // An optional longer description of the extension.
583 std::string description_;
584
585 // True if the extension was generated from a user script. (We show slightly
586 // different UI if so).
587 bool converted_from_user_script_;
588
589 // Paths to the content scripts the extension contains.
590 UserScriptList content_scripts_;
591
592 // The extension's page action, if any.
593 scoped_ptr<ExtensionAction> page_action_;
594
595 // The extension's browser action, if any.
596 scoped_ptr<ExtensionAction> browser_action_;
597
598 // Optional list of NPAPI plugins and associated properties.
599 std::vector<PluginInfo> plugins_;
600
601 // Optional URL to a master page of which a single instance should be always
602 // loaded in the background.
603 GURL background_url_;
604
605 // Optional URL to a page for setting options/preferences.
606 GURL options_url_;
607
608 // Optional URL to a devtools extension page.
609 GURL devtools_url_;
610
611 // Optional list of toolstrips and associated properties.
612 std::vector<GURL> toolstrips_;
613
614 // The public key used to sign the contents of the crx package.
615 std::string public_key_;
616
617 // A map of resource id's to relative file paths.
618 scoped_ptr<DictionaryValue> theme_images_;
619
620 // A map of color names to colors.
621 scoped_ptr<DictionaryValue> theme_colors_;
622
623 // A map of color names to colors.
624 scoped_ptr<DictionaryValue> theme_tints_;
625
626 // A map of display properties.
627 scoped_ptr<DictionaryValue> theme_display_properties_;
628
629 // Whether the extension is a theme.
630 bool is_theme_;
631
632 // The sites this extension has permission to talk to (using XHR, etc).
633 URLPatternList host_permissions_;
634
635 // The homepage for this extension. Useful if it is not hosted by Google and
636 // therefore does not have a Gallery URL.
637 GURL homepage_url_;
638
639 // URL for fetching an update manifest
640 GURL update_url_;
641
642 // A copy of the manifest that this extension was created from.
643 scoped_ptr<DictionaryValue> manifest_value_;
644
645 // A map of chrome:// hostnames (newtab, downloads, etc.) to Extension URLs
646 // which override the handling of those URLs. (see ExtensionOverrideUI).
647 URLOverrideMap chrome_url_overrides_;
648
649 // Whether this extension uses app features.
650 bool is_app_;
651
652 // The local path inside the extension to use with the launcher.
653 std::string launch_local_path_;
654
655 // A web url to use with the launcher. Note that this might be relative or
656 // absolute. If relative, it is relative to web_origin.
657 std::string launch_web_url_;
658
659 // The type of container to launch into.
660 extension_misc::LaunchContainer launch_container_;
661
662 // The default size of the container when launching. Only respected for
663 // containers like panels and windows.
664 int launch_width_;
665 int launch_height_;
666
667 // The Omnibox keyword for this extension, or empty if there is none.
668 std::string omnibox_keyword_;
669
[email protected]5eb375e92010-11-26 07:50:41670 FRIEND_TEST_ALL_PREFIXES(ExtensionsServiceTest,
671 UpdateExtensionPreservesLocation);
[email protected]19118d52010-07-26 22:13:42672 FRIEND_TEST_ALL_PREFIXES(ExtensionTest, LoadPageActionHelper);
[email protected]66e4eb32010-10-27 20:37:41673 FRIEND_TEST_ALL_PREFIXES(ExtensionTest, InitFromValueInvalid);
674 FRIEND_TEST_ALL_PREFIXES(ExtensionTest, InitFromValueValid);
675 FRIEND_TEST_ALL_PREFIXES(ExtensionTest, InitFromValueValidNameInRTL);
[email protected]19118d52010-07-26 22:13:42676 FRIEND_TEST_ALL_PREFIXES(TabStripModelTest, Apps);
[email protected]ae7fe712009-07-02 20:33:58677
[email protected]894bb502009-05-21 22:39:57678 DISALLOW_COPY_AND_ASSIGN(Extension);
[email protected]7713d632008-12-02 07:52:33679};
680
[email protected]9adb9692010-10-29 23:14:02681typedef std::vector< scoped_refptr<const Extension> > ExtensionList;
[email protected]ec5b50d2010-10-09 16:35:18682typedef std::set<std::string> ExtensionIdSet;
[email protected]b1748b1d82009-11-30 20:32:56683
[email protected]c6d474f82009-12-16 21:11:06684// Handy struct to pass core extension info around.
685struct ExtensionInfo {
686 ExtensionInfo(const DictionaryValue* manifest,
687 const std::string& id,
688 const FilePath& path,
[email protected]3bb84992010-08-26 17:23:46689 Extension::Location location);
690 ~ExtensionInfo();
[email protected]c6d474f82009-12-16 21:11:06691
692 scoped_ptr<DictionaryValue> extension_manifest;
693 std::string extension_id;
694 FilePath extension_path;
695 Extension::Location extension_location;
696
697 private:
698 DISALLOW_COPY_AND_ASSIGN(ExtensionInfo);
699};
700
[email protected]211030342010-09-30 18:41:06701// Struct used for the details of the EXTENSION_UNINSTALLED
702// notification.
703struct UninstalledExtensionInfo {
704 explicit UninstalledExtensionInfo(const Extension& extension);
[email protected]9b2331d92010-10-04 23:11:19705 ~UninstalledExtensionInfo();
[email protected]211030342010-09-30 18:41:06706
707 std::string extension_id;
708 std::set<std::string> extension_api_permissions;
[email protected]76c25112010-10-01 22:37:21709 // TODO(akalin): Once we have a unified ExtensionType, replace the
710 // below member variables with a member of that type.
711 bool is_theme;
712 bool is_app;
713 bool converted_from_user_script;
714 GURL update_url;
[email protected]211030342010-09-30 18:41:06715};
716
[email protected]5b1a0e22009-05-26 19:00:58717#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_H_