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_
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/url_pattern_set.h"
27 #include "ui/base/accelerators/accelerator.h"
28 #include "ui/gfx/size.h"
31 #if !defined(ENABLE_EXTENSIONS)
32 #error "Extensions must be enabled"
36 class DictionaryValue
;
40 namespace extensions
{
42 class PermissionsData
;
43 class PermissionsParser
;
45 // Uniquely identifies an Extension, using 32 characters from the alphabet
46 // 'a'-'p'. An empty string represents "no extension".
48 // Note: If this gets used heavily in files that don't otherwise need to include
49 // extension.h, we should pull it into a dedicated header.
50 typedef std::string ExtensionId
;
52 // Represents a Chrome extension.
53 // Once created, an Extension object is immutable, with the exception of its
54 // RuntimeData. This makes it safe to use on any thread, since access to the
55 // RuntimeData is protected by a lock.
56 class Extension
: public base::RefCountedThreadSafe
<Extension
> {
60 typedef std::map
<const std::string
, linked_ptr
<ManifestData
> >
66 // An external extension that the user uninstalled. We should not reinstall
67 // such extensions on startup.
68 EXTERNAL_EXTENSION_UNINSTALLED
,
69 // DEPRECATED: Special state for component extensions.
70 // Maintained as a placeholder since states may be stored to disk.
71 ENABLED_COMPONENT_DEPRECATED
,
75 // Used to record the reason an extension was disabled.
76 enum DeprecatedDisableReason
{
77 DEPRECATED_DISABLE_UNKNOWN
,
78 DEPRECATED_DISABLE_USER_ACTION
,
79 DEPRECATED_DISABLE_PERMISSIONS_INCREASE
,
80 DEPRECATED_DISABLE_RELOAD
,
81 DEPRECATED_DISABLE_LAST
, // Not used.
84 // Reasons an extension may be disabled. These are used in histograms, so do
85 // not remove/reorder entries - only add at the end just before
86 // DISABLE_REASON_LAST (and update the shift value for it). Also remember to
87 // update the enum listing in tools/metrics/histograms.xml.
90 DISABLE_USER_ACTION
= 1 << 0,
91 DISABLE_PERMISSIONS_INCREASE
= 1 << 1,
92 DISABLE_RELOAD
= 1 << 2,
93 DISABLE_UNSUPPORTED_REQUIREMENT
= 1 << 3,
94 DISABLE_SIDELOAD_WIPEOUT
= 1 << 4,
95 DISABLE_UNKNOWN_FROM_SYNC
= 1 << 5,
96 // DISABLE_PERMISSIONS_CONSENT = 1 << 6, // Deprecated.
97 // DISABLE_KNOWN_DISABLED = 1 << 7, // Deprecated.
98 DISABLE_NOT_VERIFIED
= 1 << 8, // Disabled because we could not verify
100 DISABLE_GREYLIST
= 1 << 9,
101 DISABLE_CORRUPTED
= 1 << 10,
102 DISABLE_REMOTE_INSTALL
= 1 << 11,
103 DISABLE_INACTIVE_EPHEMERAL_APP
= 1 << 12, // Cached ephemeral apps are
104 // disabled to prevent activity.
105 DISABLE_EXTERNAL_EXTENSION
= 1 << 13, // External extensions might be
106 // disabled for user prompting.
107 DISABLE_REASON_LAST
= 1 << 14, // This should always be the last value
110 // A base class for parsed manifest data that APIs want to store on
111 // the extension. Related to base::SupportsUserData, but with an immutable
112 // thread-safe interface to match Extension.
113 struct ManifestData
{
114 virtual ~ManifestData() {}
117 // Do not change the order of entries or remove entries in this list
118 // as this is used in UMA_HISTOGRAM_ENUMERATIONs about extensions.
119 enum InitFromValueFlags
{
122 // Usually, the id of an extension is generated by the "key" property of
123 // its manifest, but if |REQUIRE_KEY| is not set, a temporary ID will be
124 // generated based on the path.
125 REQUIRE_KEY
= 1 << 0,
127 // Requires the extension to have an up-to-date manifest version.
128 // Typically, we'll support multiple manifest versions during a version
129 // transition. This flag signals that we want to require the most modern
130 // manifest version that Chrome understands.
131 REQUIRE_MODERN_MANIFEST_VERSION
= 1 << 1,
133 // |ALLOW_FILE_ACCESS| indicates that the user is allowing this extension
134 // to have file access. If it's not present, then permissions and content
135 // scripts that match file:/// URLs will be filtered out.
136 ALLOW_FILE_ACCESS
= 1 << 2,
138 // |FROM_WEBSTORE| indicates that the extension was installed from the
140 FROM_WEBSTORE
= 1 << 3,
142 // |FROM_BOOKMARK| indicates the extension is a bookmark app which has been
143 // generated from a web page. Bookmark apps have no permissions or extent
144 // and launch the web page they are created from when run.
145 FROM_BOOKMARK
= 1 << 4,
147 // |FOLLOW_SYMLINKS_ANYWHERE| means that resources can be symlinks to
148 // anywhere in the filesystem, rather than being restricted to the
149 // extension directory.
150 FOLLOW_SYMLINKS_ANYWHERE
= 1 << 5,
152 // |ERROR_ON_PRIVATE_KEY| means that private keys inside an
153 // extension should be errors rather than warnings.
154 ERROR_ON_PRIVATE_KEY
= 1 << 6,
156 // |WAS_INSTALLED_BY_DEFAULT| installed by default when the profile was
158 WAS_INSTALLED_BY_DEFAULT
= 1 << 7,
160 // Unused - was part of an abandoned experiment.
161 REQUIRE_PERMISSIONS_CONSENT
= 1 << 8,
163 // Unused - this flag has been moved to ExtensionPrefs.
164 IS_EPHEMERAL
= 1 << 9,
166 // |WAS_INSTALLED_BY_OEM| installed by an OEM (e.g on Chrome OS) and should
167 // be placed in a special OEM folder in the App Launcher. Note: OEM apps are
168 // also installed by Default (i.e. WAS_INSTALLED_BY_DEFAULT is also true).
169 WAS_INSTALLED_BY_OEM
= 1 << 10,
171 // |WAS_INSTALLED_BY_CUSTODIAN| means this extension was installed by the
172 // custodian of a supervised user.
173 WAS_INSTALLED_BY_CUSTODIAN
= 1 << 11,
175 // |MAY_BE_UNTRUSTED| indicates that this extension came from a potentially
176 // unsafe source (e.g., sideloaded from a local CRX file via the Windows
177 // registry). Such extensions may be subjected to additional constraints
178 // before they are fully installed and enabled.
179 MAY_BE_UNTRUSTED
= 1 << 12,
181 // When adding new flags, make sure to update kInitFromValueFlagBits.
184 // This is the highest bit index of the flags defined above.
185 static const int kInitFromValueFlagBits
;
187 static scoped_refptr
<Extension
> Create(const base::FilePath
& path
,
188 Manifest::Location location
,
189 const base::DictionaryValue
& value
,
193 // In a few special circumstances, we want to create an Extension and give it
194 // an explicit id. Most consumers should just use the other Create() method.
195 static scoped_refptr
<Extension
> Create(const base::FilePath
& path
,
196 Manifest::Location location
,
197 const base::DictionaryValue
& value
,
199 const ExtensionId
& explicit_id
,
202 // Valid schemes for web extent URLPatterns.
203 static const int kValidWebExtentSchemes
;
205 // Valid schemes for host permission URLPatterns.
206 static const int kValidHostPermissionSchemes
;
208 // The mimetype used for extensions.
209 static const char kMimeType
[];
211 // See Type definition in Manifest.
212 Manifest::Type
GetType() const;
214 // Returns an absolute url to a resource inside of an extension. The
215 // |extension_url| argument should be the url() from an Extension object. The
216 // |relative_path| can be untrusted user input. The returned URL will either
217 // be invalid() or a child of |extension_url|.
218 // NOTE: Static so that it can be used from multiple threads.
219 static GURL
GetResourceURL(const GURL
& extension_url
,
220 const std::string
& relative_path
);
221 GURL
GetResourceURL(const std::string
& relative_path
) const {
222 return GetResourceURL(url(), relative_path
);
225 // Returns true if the resource matches a pattern in the pattern_set.
226 bool ResourceMatches(const URLPatternSet
& pattern_set
,
227 const std::string
& resource
) const;
229 // Returns an extension resource object. |relative_path| should be UTF8
231 ExtensionResource
GetResource(const std::string
& relative_path
) const;
233 // As above, but with |relative_path| following the file system's encoding.
234 ExtensionResource
GetResource(const base::FilePath
& relative_path
) const;
236 // |input| is expected to be the text of an rsa public or private key. It
237 // tolerates the presence or absence of bracking header/footer like this:
238 // -----(BEGIN|END) [RSA PUBLIC/PRIVATE] KEY-----
239 // and may contain newlines.
240 static bool ParsePEMKeyBytes(const std::string
& input
, std::string
* output
);
242 // Does a simple base64 encoding of |input| into |output|.
243 static bool ProducePEM(const std::string
& input
, std::string
* output
);
245 // Expects base64 encoded |input| and formats into |output| including
246 // the appropriate header & footer.
247 static bool FormatPEMForFileOutput(const std::string
& input
,
251 // Returns the base extension url for a given |extension_id|.
252 static GURL
GetBaseURLFromExtensionId(const ExtensionId
& extension_id
);
254 // Whether context menu should be shown for page and browser actions.
255 bool ShowConfigureContextMenus() const;
257 // Returns true if this extension or app includes areas within |origin|.
258 bool OverlapsWithOrigin(const GURL
& origin
) const;
260 // Returns true if the extension requires a valid ordinal for sorting, e.g.,
261 // for displaying in a launcher or new tab page.
262 bool RequiresSortOrdinal() const;
264 // Returns true if the extension should be displayed in the app launcher.
265 bool ShouldDisplayInAppLauncher() const;
267 // Returns true if the extension should be displayed in the browser NTP.
268 bool ShouldDisplayInNewTabPage() const;
270 // Returns true if the extension should be displayed in the extension
271 // settings page (i.e. chrome://extensions).
272 bool ShouldDisplayInExtensionSettings() const;
274 // Returns true if the extension should not be shown anywhere. This is
275 // mostly the same as the extension being a component extension, but also
276 // includes non-component apps that are hidden from the app launcher and ntp.
277 bool ShouldNotBeVisible() const;
279 // Get the manifest data associated with the key, or NULL if there is none.
280 // Can only be called after InitValue is finished.
281 ManifestData
* GetManifestData(const std::string
& key
) const;
283 // Sets |data| to be associated with the key. Takes ownership of |data|.
284 // Can only be called before InitValue is finished. Not thread-safe;
285 // all SetManifestData calls should be on only one thread.
286 void SetManifestData(const std::string
& key
, ManifestData
* data
);
290 const base::FilePath
& path() const { return path_
; }
291 const GURL
& url() const { return extension_url_
; }
292 Manifest::Location
location() const;
293 const ExtensionId
& id() const;
294 const base::Version
* version() const { return version_
.get(); }
295 const std::string
VersionString() const;
296 const std::string
& name() const { return name_
; }
297 const std::string
& short_name() const { return short_name_
; }
298 const std::string
& non_localized_name() const { return non_localized_name_
; }
299 // Base64-encoded version of the key used to sign this extension.
300 // In pseudocode, returns
301 // base::Base64Encode(RSAPrivateKey(pem_file).ExportPublicKey()).
302 const std::string
& public_key() const { return public_key_
; }
303 const std::string
& description() const { return description_
; }
304 int manifest_version() const { return manifest_version_
; }
305 bool converted_from_user_script() const {
306 return converted_from_user_script_
;
308 PermissionsParser
* permissions_parser() { return permissions_parser_
.get(); }
309 const PermissionsParser
* permissions_parser() const {
310 return permissions_parser_
.get();
313 const PermissionsData
* permissions_data() const {
314 return permissions_data_
.get();
317 // Appends |new_warning[s]| to install_warnings_.
318 void AddInstallWarning(const InstallWarning
& new_warning
);
319 void AddInstallWarnings(const std::vector
<InstallWarning
>& new_warnings
);
320 const std::vector
<InstallWarning
>& install_warnings() const {
321 return install_warnings_
;
323 const extensions::Manifest
* manifest() const {
324 return manifest_
.get();
326 bool wants_file_access() const { return wants_file_access_
; }
327 // TODO(rdevlin.cronin): This is needed for ContentScriptsHandler, and should
328 // be moved out as part of crbug.com/159265. This should not be used anywhere
330 void set_wants_file_access(bool wants_file_access
) {
331 wants_file_access_
= wants_file_access
;
333 int creation_flags() const { return creation_flags_
; }
334 bool from_webstore() const { return (creation_flags_
& FROM_WEBSTORE
) != 0; }
335 bool from_bookmark() const { return (creation_flags_
& FROM_BOOKMARK
) != 0; }
336 bool was_installed_by_default() const {
337 return (creation_flags_
& WAS_INSTALLED_BY_DEFAULT
) != 0;
339 bool was_installed_by_oem() const {
340 return (creation_flags_
& WAS_INSTALLED_BY_OEM
) != 0;
342 bool was_installed_by_custodian() const {
343 return (creation_flags_
& WAS_INSTALLED_BY_CUSTODIAN
) != 0;
346 // Type-related queries.
348 bool is_platform_app() const;
349 bool is_hosted_app() const;
350 bool is_legacy_packaged_app() const;
351 bool is_extension() const;
352 bool is_shared_module() const;
353 bool is_theme() const;
355 bool can_be_incognito_enabled() const;
357 void AddWebExtentPattern(const URLPattern
& pattern
);
358 const URLPatternSet
& web_extent() const { return extent_
; }
361 friend class base::RefCountedThreadSafe
<Extension
>;
363 // Chooses the extension ID for an extension based on a variety of criteria.
364 // The chosen ID will be set in |manifest|.
365 static bool InitExtensionID(extensions::Manifest
* manifest
,
366 const base::FilePath
& path
,
367 const ExtensionId
& explicit_id
,
369 base::string16
* error
);
371 Extension(const base::FilePath
& path
,
372 scoped_ptr
<extensions::Manifest
> manifest
);
373 virtual ~Extension();
375 // Initialize the extension from a parsed manifest.
376 // TODO(aa): Rename to just Init()? There's no Value here anymore.
377 // TODO(aa): It is really weird the way this class essentially contains a copy
378 // of the underlying DictionaryValue in its members. We should decide to
379 // either wrap the DictionaryValue and go with that only, or we should parse
380 // into strong types and discard the value. But doing both is bad.
381 bool InitFromValue(int flags
, base::string16
* error
);
383 // The following are helpers for InitFromValue to load various features of the
384 // extension from the manifest.
386 bool LoadRequiredFeatures(base::string16
* error
);
387 bool LoadName(base::string16
* error
);
388 bool LoadVersion(base::string16
* error
);
390 bool LoadAppFeatures(base::string16
* error
);
391 bool LoadExtent(const char* key
,
392 URLPatternSet
* extent
,
393 const char* list_error
,
394 const char* value_error
,
395 base::string16
* error
);
397 bool LoadSharedFeatures(base::string16
* error
);
398 bool LoadDescription(base::string16
* error
);
399 bool LoadManifestVersion(base::string16
* error
);
400 bool LoadShortName(base::string16
* error
);
402 bool CheckMinimumChromeVersion(base::string16
* error
) const;
404 // The extension's human-readable name. Name is used for display purpose. It
405 // might be wrapped with unicode bidi control characters so that it is
406 // displayed correctly in RTL context.
407 // NOTE: Name is UTF-8 and may contain non-ascii characters.
410 // A non-localized version of the extension's name. This is useful for
412 std::string non_localized_name_
;
414 // A short version of the extension's name. This can be used as an alternative
415 // to the name where there is insufficient space to display the full name. If
416 // an extension has not explicitly specified a short name, the value of this
417 // member variable will be the full name rather than an empty string.
418 std::string short_name_
;
420 // The version of this extension's manifest. We increase the manifest
421 // version when making breaking changes to the extension system.
422 // Version 1 was the first manifest version (implied by a lack of a
423 // manifest_version attribute in the extension's manifest). We initialize
424 // this member variable to 0 to distinguish the "uninitialized" case from
425 // the case when we know the manifest version actually is 1.
426 int manifest_version_
;
428 // The absolute path to the directory the extension is stored in.
429 base::FilePath path_
;
431 // Defines the set of URLs in the extension's web content.
432 URLPatternSet extent_
;
434 // The parser for the manifest's permissions. This is NULL anytime not during
436 // TODO(rdevlin.cronin): This doesn't really belong here.
437 scoped_ptr
<PermissionsParser
> permissions_parser_
;
439 // The active permissions for the extension.
440 scoped_ptr
<PermissionsData
> permissions_data_
;
442 // Any warnings that occurred when trying to create/parse the extension.
443 std::vector
<InstallWarning
> install_warnings_
;
445 // The base extension url for the extension.
448 // The extension's version.
449 scoped_ptr
<base::Version
> version_
;
451 // An optional longer description of the extension.
452 std::string description_
;
454 // True if the extension was generated from a user script. (We show slightly
455 // different UI if so).
456 bool converted_from_user_script_
;
458 // The public key used to sign the contents of the crx package.
459 std::string public_key_
;
461 // The manifest from which this extension was created.
462 scoped_ptr
<Manifest
> manifest_
;
464 // Stored parsed manifest data.
465 ManifestDataMap manifest_data_
;
467 // Set to true at the end of InitValue when initialization is finished.
468 bool finished_parsing_manifest_
;
470 // Ensures that any call to GetManifestData() prior to finishing
471 // initialization happens from the same thread (this can happen when certain
472 // parts of the initialization process need information from previous parts).
473 base::ThreadChecker thread_checker_
;
475 // Should this app be shown in the app launcher.
476 bool display_in_launcher_
;
478 // Should this app be shown in the browser New Tab Page.
479 bool display_in_new_tab_page_
;
481 // Whether the extension has host permissions or user script patterns that
482 // imply access to file:/// scheme URLs (the user may not have actually
483 // granted it that access).
484 bool wants_file_access_
;
486 // The flags that were passed to InitFromValue.
489 DISALLOW_COPY_AND_ASSIGN(Extension
);
492 typedef std::vector
<scoped_refptr
<const Extension
> > ExtensionList
;
493 typedef std::set
<ExtensionId
> ExtensionIdSet
;
494 typedef std::vector
<ExtensionId
> ExtensionIdList
;
496 // Handy struct to pass core extension info around.
497 struct ExtensionInfo
{
498 ExtensionInfo(const base::DictionaryValue
* manifest
,
499 const ExtensionId
& id
,
500 const base::FilePath
& path
,
501 Manifest::Location location
);
504 scoped_ptr
<base::DictionaryValue
> extension_manifest
;
505 ExtensionId extension_id
;
506 base::FilePath extension_path
;
507 Manifest::Location extension_location
;
510 DISALLOW_COPY_AND_ASSIGN(ExtensionInfo
);
513 struct InstalledExtensionInfo
{
514 // The extension being installed - this should always be non-NULL.
515 const Extension
* extension
;
517 // True if the extension is being updated; false if it is being installed.
520 // True if the extension was previously installed ephemerally and is now
521 // a regular installed extension.
524 // The name of the extension prior to this update. Will be empty if
525 // |is_update| is false.
526 std::string old_name
;
528 InstalledExtensionInfo(const Extension
* extension
,
531 const std::string
& old_name
);
534 struct UnloadedExtensionInfo
{
535 // TODO(DHNishi): Move this enum to ExtensionRegistryObserver.
537 REASON_UNDEFINED
, // Undefined state used to initialize variables.
538 REASON_DISABLE
, // Extension is being disabled.
539 REASON_UPDATE
, // Extension is being updated to a newer version.
540 REASON_UNINSTALL
, // Extension is being uninstalled.
541 REASON_TERMINATE
, // Extension has terminated.
542 REASON_BLACKLIST
, // Extension has been blacklisted.
543 REASON_PROFILE_SHUTDOWN
, // Profile is being shut down.
548 // The extension being unloaded - this should always be non-NULL.
549 const Extension
* extension
;
551 UnloadedExtensionInfo(const Extension
* extension
, Reason reason
);
554 // The details sent for EXTENSION_PERMISSIONS_UPDATED notifications.
555 struct UpdatedExtensionPermissionsInfo
{
557 ADDED
, // The permissions were added to the extension.
558 REMOVED
, // The permissions were removed from the extension.
563 // The extension who's permissions have changed.
564 const Extension
* extension
;
566 // The permissions that have changed. For Reason::ADDED, this would contain
567 // only the permissions that have added, and for Reason::REMOVED, this would
568 // only contain the removed permissions.
569 const PermissionSet
* permissions
;
571 UpdatedExtensionPermissionsInfo(
572 const Extension
* extension
,
573 const PermissionSet
* permissions
,
577 } // namespace extensions
579 #endif // EXTENSIONS_COMMON_EXTENSION_H_