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