Connect PPAPI IPC channels for non-SFI mode.
[chromium-blink-merge.git] / extensions / common / extension.h
blob4ea11b4db8c36961acd622fabc52822def7c8f65
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_COMMON_EXTENSION_H_
6 #define EXTENSIONS_COMMON_EXTENSION_H_
8 #include <algorithm>
9 #include <iosfwd>
10 #include <map>
11 #include <set>
12 #include <string>
13 #include <utility>
14 #include <vector>
16 #include "base/containers/hash_tables.h"
17 #include "base/files/file_path.h"
18 #include "base/memory/linked_ptr.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "base/synchronization/lock.h"
22 #include "base/threading/thread_checker.h"
23 #include "extensions/common/extension_resource.h"
24 #include "extensions/common/install_warning.h"
25 #include "extensions/common/manifest.h"
26 #include "extensions/common/permissions/api_permission.h"
27 #include "extensions/common/url_pattern.h"
28 #include "extensions/common/url_pattern_set.h"
29 #include "ui/base/accelerators/accelerator.h"
30 #include "ui/gfx/size.h"
31 #include "url/gurl.h"
33 class ExtensionAction;
34 class SkBitmap;
36 namespace base {
37 class DictionaryValue;
38 class Version;
41 namespace gfx {
42 class ImageSkia;
45 namespace extensions {
46 class PermissionsData;
47 class APIPermissionSet;
48 class ManifestPermissionSet;
49 class PermissionSet;
51 // Represents a Chrome extension.
52 // Once created, an Extension object is immutable, with the exception of its
53 // RuntimeData. This makes it safe to use on any thread, since access to the
54 // RuntimeData is protected by a lock.
55 class Extension : public base::RefCountedThreadSafe<Extension> {
56 public:
57 struct ManifestData;
59 typedef std::map<const std::string, linked_ptr<ManifestData> >
60 ManifestDataMap;
62 enum State {
63 DISABLED = 0,
64 ENABLED,
65 // An external extension that the user uninstalled. We should not reinstall
66 // such extensions on startup.
67 EXTERNAL_EXTENSION_UNINSTALLED,
68 // Special state for component extensions, since they are always loaded by
69 // the component loader, and should never be auto-installed on startup.
70 ENABLED_COMPONENT,
71 NUM_STATES
74 // Used to record the reason an extension was disabled.
75 enum DeprecatedDisableReason {
76 DEPRECATED_DISABLE_UNKNOWN,
77 DEPRECATED_DISABLE_USER_ACTION,
78 DEPRECATED_DISABLE_PERMISSIONS_INCREASE,
79 DEPRECATED_DISABLE_RELOAD,
80 DEPRECATED_DISABLE_LAST, // Not used.
83 enum DisableReason {
84 DISABLE_NONE = 0,
85 DISABLE_USER_ACTION = 1 << 0,
86 DISABLE_PERMISSIONS_INCREASE = 1 << 1,
87 DISABLE_RELOAD = 1 << 2,
88 DISABLE_UNSUPPORTED_REQUIREMENT = 1 << 3,
89 DISABLE_SIDELOAD_WIPEOUT = 1 << 4,
90 DISABLE_UNKNOWN_FROM_SYNC = 1 << 5,
91 DISABLE_PERMISSIONS_CONSENT = 1 << 6, // Unused - abandoned experiment.
92 DISABLE_KNOWN_DISABLED = 1 << 7,
93 DISABLE_NOT_VERIFIED = 1 << 8, // Disabled because we could not verify
94 // the install.
95 DISABLE_GREYLIST = 1 << 9
98 enum InstallType {
99 INSTALL_ERROR,
100 DOWNGRADE,
101 REINSTALL,
102 UPGRADE,
103 NEW_INSTALL
106 // A base class for parsed manifest data that APIs want to store on
107 // the extension. Related to base::SupportsUserData, but with an immutable
108 // thread-safe interface to match Extension.
109 struct ManifestData {
110 virtual ~ManifestData() {}
113 enum InitFromValueFlags {
114 NO_FLAGS = 0,
116 // Usually, the id of an extension is generated by the "key" property of
117 // its manifest, but if |REQUIRE_KEY| is not set, a temporary ID will be
118 // generated based on the path.
119 REQUIRE_KEY = 1 << 0,
121 // Requires the extension to have an up-to-date manifest version.
122 // Typically, we'll support multiple manifest versions during a version
123 // transition. This flag signals that we want to require the most modern
124 // manifest version that Chrome understands.
125 REQUIRE_MODERN_MANIFEST_VERSION = 1 << 1,
127 // |ALLOW_FILE_ACCESS| indicates that the user is allowing this extension
128 // to have file access. If it's not present, then permissions and content
129 // scripts that match file:/// URLs will be filtered out.
130 ALLOW_FILE_ACCESS = 1 << 2,
132 // |FROM_WEBSTORE| indicates that the extension was installed from the
133 // Chrome Web Store.
134 FROM_WEBSTORE = 1 << 3,
136 // |FROM_BOOKMARK| indicates the extension is a bookmark app which has been
137 // generated from a web page. Bookmark apps have no permissions or extent
138 // and launch the web page they are created from when run.
139 FROM_BOOKMARK = 1 << 4,
141 // |FOLLOW_SYMLINKS_ANYWHERE| means that resources can be symlinks to
142 // anywhere in the filesystem, rather than being restricted to the
143 // extension directory.
144 FOLLOW_SYMLINKS_ANYWHERE = 1 << 5,
146 // |ERROR_ON_PRIVATE_KEY| means that private keys inside an
147 // extension should be errors rather than warnings.
148 ERROR_ON_PRIVATE_KEY = 1 << 6,
150 // |WAS_INSTALLED_BY_DEFAULT| installed by default when the profile was
151 // created.
152 WAS_INSTALLED_BY_DEFAULT = 1 << 7,
154 // Unused - was part of an abandoned experiment.
155 REQUIRE_PERMISSIONS_CONSENT = 1 << 8,
157 // |IS_EPHEMERAL| identifies ephemeral apps (experimental), which are not
158 // permanently installed.
159 IS_EPHEMERAL = 1 << 9,
162 static scoped_refptr<Extension> Create(const base::FilePath& path,
163 Manifest::Location location,
164 const base::DictionaryValue& value,
165 int flags,
166 std::string* error);
168 // In a few special circumstances, we want to create an Extension and give it
169 // an explicit id. Most consumers should just use the other Create() method.
170 static scoped_refptr<Extension> Create(const base::FilePath& path,
171 Manifest::Location location,
172 const base::DictionaryValue& value,
173 int flags,
174 const std::string& explicit_id,
175 std::string* error);
177 // Valid schemes for web extent URLPatterns.
178 static const int kValidWebExtentSchemes;
180 // Valid schemes for host permission URLPatterns.
181 static const int kValidHostPermissionSchemes;
183 // The mimetype used for extensions.
184 static const char kMimeType[];
186 // Checks to see if the extension has a valid ID.
187 static bool IdIsValid(const std::string& id);
189 // See Type definition in Manifest.
190 Manifest::Type GetType() const;
192 // Returns an absolute url to a resource inside of an extension. The
193 // |extension_url| argument should be the url() from an Extension object. The
194 // |relative_path| can be untrusted user input. The returned URL will either
195 // be invalid() or a child of |extension_url|.
196 // NOTE: Static so that it can be used from multiple threads.
197 static GURL GetResourceURL(const GURL& extension_url,
198 const std::string& relative_path);
199 GURL GetResourceURL(const std::string& relative_path) const {
200 return GetResourceURL(url(), relative_path);
203 // Returns true if the resource matches a pattern in the pattern_set.
204 bool ResourceMatches(const URLPatternSet& pattern_set,
205 const std::string& resource) const;
207 // Returns an extension resource object. |relative_path| should be UTF8
208 // encoded.
209 ExtensionResource GetResource(const std::string& relative_path) const;
211 // As above, but with |relative_path| following the file system's encoding.
212 ExtensionResource GetResource(const base::FilePath& relative_path) const;
214 // |input| is expected to be the text of an rsa public or private key. It
215 // tolerates the presence or absence of bracking header/footer like this:
216 // -----(BEGIN|END) [RSA PUBLIC/PRIVATE] KEY-----
217 // and may contain newlines.
218 static bool ParsePEMKeyBytes(const std::string& input, std::string* output);
220 // Does a simple base64 encoding of |input| into |output|.
221 static bool ProducePEM(const std::string& input, std::string* output);
223 // Expects base64 encoded |input| and formats into |output| including
224 // the appropriate header & footer.
225 static bool FormatPEMForFileOutput(const std::string& input,
226 std::string* output,
227 bool is_public);
229 // Returns the base extension url for a given |extension_id|.
230 static GURL GetBaseURLFromExtensionId(const std::string& extension_id);
232 // DEPRECATED: These methods have been moved to PermissionsData.
233 // TODO(rdevlin.cronin): remove these once all calls have been updated.
234 bool HasAPIPermission(APIPermission::ID permission) const;
235 bool HasAPIPermission(const std::string& permission_name) const;
236 scoped_refptr<const PermissionSet> GetActivePermissions() const;
238 // Whether context menu should be shown for page and browser actions.
239 bool ShowConfigureContextMenus() const;
241 // Returns true if this extension or app includes areas within |origin|.
242 bool OverlapsWithOrigin(const GURL& origin) const;
244 // Returns true if the extension requires a valid ordinal for sorting, e.g.,
245 // for displaying in a launcher or new tab page.
246 bool RequiresSortOrdinal() const;
248 // Returns true if the extension should be displayed in the app launcher.
249 bool ShouldDisplayInAppLauncher() const;
251 // Returns true if the extension should be displayed in the browser NTP.
252 bool ShouldDisplayInNewTabPage() const;
254 // Returns true if the extension should be displayed in the extension
255 // settings page (i.e. chrome://extensions).
256 bool ShouldDisplayInExtensionSettings() const;
258 // Returns true if the extension should not be shown anywhere. This is
259 // mostly the same as the extension being a component extension, but also
260 // includes non-component apps that are hidden from the app launcher and ntp.
261 bool ShouldNotBeVisible() const;
263 // Get the manifest data associated with the key, or NULL if there is none.
264 // Can only be called after InitValue is finished.
265 ManifestData* GetManifestData(const std::string& key) const;
267 // Sets |data| to be associated with the key. Takes ownership of |data|.
268 // Can only be called before InitValue is finished. Not thread-safe;
269 // all SetManifestData calls should be on only one thread.
270 void SetManifestData(const std::string& key, ManifestData* data);
272 // Accessors:
274 const base::FilePath& path() const { return path_; }
275 const GURL& url() const { return extension_url_; }
276 Manifest::Location location() const;
277 const std::string& id() const;
278 const base::Version* version() const { return version_.get(); }
279 const std::string VersionString() const;
280 const std::string& name() const { return name_; }
281 const std::string& short_name() const { return short_name_; }
282 const std::string& non_localized_name() const { return non_localized_name_; }
283 // Base64-encoded version of the key used to sign this extension.
284 // In pseudocode, returns
285 // base::Base64Encode(RSAPrivateKey(pem_file).ExportPublicKey()).
286 const std::string& public_key() const { return public_key_; }
287 const std::string& description() const { return description_; }
288 int manifest_version() const { return manifest_version_; }
289 bool converted_from_user_script() const {
290 return converted_from_user_script_;
292 PermissionsData* permissions_data() { return permissions_data_.get(); }
293 const PermissionsData* permissions_data() const {
294 return permissions_data_.get();
297 // Appends |new_warning[s]| to install_warnings_.
298 void AddInstallWarning(const InstallWarning& new_warning);
299 void AddInstallWarnings(const std::vector<InstallWarning>& new_warnings);
300 const std::vector<InstallWarning>& install_warnings() const {
301 return install_warnings_;
303 const extensions::Manifest* manifest() const {
304 return manifest_.get();
306 bool wants_file_access() const { return wants_file_access_; }
307 // TODO(rdevlin.cronin): This is needed for ContentScriptsHandler, and should
308 // be moved out as part of crbug.com/159265. This should not be used anywhere
309 // else.
310 void set_wants_file_access(bool wants_file_access) {
311 wants_file_access_ = wants_file_access;
313 int creation_flags() const { return creation_flags_; }
314 bool from_webstore() const { return (creation_flags_ & FROM_WEBSTORE) != 0; }
315 bool from_bookmark() const { return (creation_flags_ & FROM_BOOKMARK) != 0; }
316 bool was_installed_by_default() const {
317 return (creation_flags_ & WAS_INSTALLED_BY_DEFAULT) != 0;
319 bool is_ephemeral() const { return (creation_flags_ & IS_EPHEMERAL) != 0; }
321 // App-related.
322 bool is_app() const;
323 bool is_platform_app() const;
324 bool is_hosted_app() const;
325 bool is_legacy_packaged_app() const;
326 bool is_extension() const;
327 bool can_be_incognito_enabled() const;
328 bool force_incognito_enabled() const;
330 void AddWebExtentPattern(const URLPattern& pattern);
331 const URLPatternSet& web_extent() const { return extent_; }
333 // Theme-related.
334 bool is_theme() const;
336 private:
337 friend class base::RefCountedThreadSafe<Extension>;
339 // Chooses the extension ID for an extension based on a variety of criteria.
340 // The chosen ID will be set in |manifest|.
341 static bool InitExtensionID(extensions::Manifest* manifest,
342 const base::FilePath& path,
343 const std::string& explicit_id,
344 int creation_flags,
345 base::string16* error);
347 Extension(const base::FilePath& path,
348 scoped_ptr<extensions::Manifest> manifest);
349 virtual ~Extension();
351 // Initialize the extension from a parsed manifest.
352 // TODO(aa): Rename to just Init()? There's no Value here anymore.
353 // TODO(aa): It is really weird the way this class essentially contains a copy
354 // of the underlying DictionaryValue in its members. We should decide to
355 // either wrap the DictionaryValue and go with that only, or we should parse
356 // into strong types and discard the value. But doing both is bad.
357 bool InitFromValue(int flags, base::string16* error);
359 // The following are helpers for InitFromValue to load various features of the
360 // extension from the manifest.
362 bool LoadRequiredFeatures(base::string16* error);
363 bool LoadName(base::string16* error);
364 bool LoadVersion(base::string16* error);
366 bool LoadAppFeatures(base::string16* error);
367 bool LoadExtent(const char* key,
368 URLPatternSet* extent,
369 const char* list_error,
370 const char* value_error,
371 base::string16* error);
373 bool LoadSharedFeatures(base::string16* error);
374 bool LoadDescription(base::string16* error);
375 bool LoadManifestVersion(base::string16* error);
376 bool LoadShortName(base::string16* error);
378 bool CheckMinimumChromeVersion(base::string16* error) const;
380 // The extension's human-readable name. Name is used for display purpose. It
381 // might be wrapped with unicode bidi control characters so that it is
382 // displayed correctly in RTL context.
383 // NOTE: Name is UTF-8 and may contain non-ascii characters.
384 std::string name_;
386 // A non-localized version of the extension's name. This is useful for
387 // debug output.
388 std::string non_localized_name_;
390 // A short version of the extension's name. This can be used as an alternative
391 // to the name where there is insufficient space to display the full name. If
392 // an extension has not explicitly specified a short name, the value of this
393 // member variable will be the full name rather than an empty string.
394 std::string short_name_;
396 // The version of this extension's manifest. We increase the manifest
397 // version when making breaking changes to the extension system.
398 // Version 1 was the first manifest version (implied by a lack of a
399 // manifest_version attribute in the extension's manifest). We initialize
400 // this member variable to 0 to distinguish the "uninitialized" case from
401 // the case when we know the manifest version actually is 1.
402 int manifest_version_;
404 // The absolute path to the directory the extension is stored in.
405 base::FilePath path_;
407 // Defines the set of URLs in the extension's web content.
408 URLPatternSet extent_;
410 scoped_ptr<PermissionsData> permissions_data_;
412 // Any warnings that occurred when trying to create/parse the extension.
413 std::vector<InstallWarning> install_warnings_;
415 // The base extension url for the extension.
416 GURL extension_url_;
418 // The extension's version.
419 scoped_ptr<base::Version> version_;
421 // An optional longer description of the extension.
422 std::string description_;
424 // True if the extension was generated from a user script. (We show slightly
425 // different UI if so).
426 bool converted_from_user_script_;
428 // The public key used to sign the contents of the crx package.
429 std::string public_key_;
431 // The manifest from which this extension was created.
432 scoped_ptr<Manifest> manifest_;
434 // Stored parsed manifest data.
435 ManifestDataMap manifest_data_;
437 // Set to true at the end of InitValue when initialization is finished.
438 bool finished_parsing_manifest_;
440 // Ensures that any call to GetManifestData() prior to finishing
441 // initialization happens from the same thread (this can happen when certain
442 // parts of the initialization process need information from previous parts).
443 base::ThreadChecker thread_checker_;
445 // Should this app be shown in the app launcher.
446 bool display_in_launcher_;
448 // Should this app be shown in the browser New Tab Page.
449 bool display_in_new_tab_page_;
451 // Whether the extension has host permissions or user script patterns that
452 // imply access to file:/// scheme URLs (the user may not have actually
453 // granted it that access).
454 bool wants_file_access_;
456 // The flags that were passed to InitFromValue.
457 int creation_flags_;
459 DISALLOW_COPY_AND_ASSIGN(Extension);
462 typedef std::vector<scoped_refptr<const Extension> > ExtensionList;
463 typedef std::set<std::string> ExtensionIdSet;
464 typedef std::vector<std::string> ExtensionIdList;
466 // Handy struct to pass core extension info around.
467 struct ExtensionInfo {
468 ExtensionInfo(const base::DictionaryValue* manifest,
469 const std::string& id,
470 const base::FilePath& path,
471 Manifest::Location location);
472 ~ExtensionInfo();
474 scoped_ptr<base::DictionaryValue> extension_manifest;
475 std::string extension_id;
476 base::FilePath extension_path;
477 Manifest::Location extension_location;
479 private:
480 DISALLOW_COPY_AND_ASSIGN(ExtensionInfo);
483 struct InstalledExtensionInfo {
484 // The extension being installed - this should always be non-NULL.
485 const Extension* extension;
487 // True if the extension is being updated; false if it is being installed.
488 bool is_update;
490 // The name of the extension prior to this update. Will be empty if
491 // |is_update| is false.
492 std::string old_name;
494 InstalledExtensionInfo(const Extension* extension,
495 bool is_update,
496 const std::string& old_name);
499 struct UnloadedExtensionInfo {
500 enum Reason {
501 REASON_DISABLE, // Extension is being disabled.
502 REASON_UPDATE, // Extension is being updated to a newer version.
503 REASON_UNINSTALL, // Extension is being uninstalled.
504 REASON_TERMINATE, // Extension has terminated.
505 REASON_BLACKLIST, // Extension has been blacklisted.
508 Reason reason;
510 // The extension being unloaded - this should always be non-NULL.
511 const Extension* extension;
513 UnloadedExtensionInfo(const Extension* extension, Reason reason);
516 // The details sent for EXTENSION_PERMISSIONS_UPDATED notifications.
517 struct UpdatedExtensionPermissionsInfo {
518 enum Reason {
519 ADDED, // The permissions were added to the extension.
520 REMOVED, // The permissions were removed from the extension.
523 Reason reason;
525 // The extension who's permissions have changed.
526 const Extension* extension;
528 // The permissions that have changed. For Reason::ADDED, this would contain
529 // only the permissions that have added, and for Reason::REMOVED, this would
530 // only contain the removed permissions.
531 const PermissionSet* permissions;
533 UpdatedExtensionPermissionsInfo(
534 const Extension* extension,
535 const PermissionSet* permissions,
536 Reason reason);
539 } // namespace extensions
541 #endif // EXTENSIONS_COMMON_EXTENSION_H_