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