extensions_client.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 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. #ifndef EXTENSIONS_COMMON_EXTENSIONS_CLIENT_H_
  5. #define EXTENSIONS_COMMON_EXTENSIONS_CLIENT_H_
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10. #include "base/strings/string_piece.h"
  11. #include "extensions/common/permissions/api_permission_set.h"
  12. #include "services/network/public/mojom/cors_origin_pattern.mojom-forward.h"
  13. class GURL;
  14. namespace base {
  15. class CommandLine;
  16. class FilePath;
  17. }
  18. namespace extensions {
  19. class APIPermissionSet;
  20. class Extension;
  21. class ExtensionsAPIProvider;
  22. class FeatureProvider;
  23. class JSONFeatureProviderSource;
  24. class PermissionMessageProvider;
  25. class URLPatternSet;
  26. // Sets up global state for the extensions system. Should be Set() once in each
  27. // process. This should be implemented by the client of the extensions system.
  28. class ExtensionsClient {
  29. public:
  30. using ScriptingAllowlist = std::vector<std::string>;
  31. // Return the extensions client.
  32. static ExtensionsClient* Get();
  33. // Initialize the extensions system with this extensions client.
  34. static void Set(ExtensionsClient* client);
  35. ExtensionsClient();
  36. ExtensionsClient(const ExtensionsClient&) = delete;
  37. ExtensionsClient& operator=(const ExtensionsClient&) = delete;
  38. virtual ~ExtensionsClient();
  39. // Create a FeatureProvider for a specific feature type, e.g. "permission".
  40. std::unique_ptr<FeatureProvider> CreateFeatureProvider(
  41. const std::string& name) const;
  42. // Returns the dictionary of the API features json file.
  43. // TODO(devlin): We should find a way to remove this.
  44. std::unique_ptr<JSONFeatureProviderSource> CreateAPIFeatureSource() const;
  45. // Returns true iff a schema named |name| is generated.
  46. bool IsAPISchemaGenerated(const std::string& name) const;
  47. // Gets the generated API schema named |name|.
  48. base::StringPiece GetAPISchema(const std::string& name) const;
  49. // Adds a new API provider.
  50. void AddAPIProvider(std::unique_ptr<ExtensionsAPIProvider> provider);
  51. //////////////////////////////////////////////////////////////////////////////
  52. // Virtual Functions:
  53. // Initializes global state. Not done in the constructor because unit tests
  54. // can create additional ExtensionsClients because the utility thread runs
  55. // in-process.
  56. virtual void Initialize() = 0;
  57. // Initializes web store URLs.
  58. // Default values could be overriden with command line.
  59. virtual void InitializeWebStoreUrls(base::CommandLine* command_line) = 0;
  60. // Returns the global PermissionMessageProvider to use to provide permission
  61. // warning strings.
  62. virtual const PermissionMessageProvider& GetPermissionMessageProvider()
  63. const = 0;
  64. // Returns the application name. For example, "Chromium" or "app_shell".
  65. virtual const std::string GetProductName() = 0;
  66. // Takes the list of all hosts and filters out those with special
  67. // permission strings. Adds the regular hosts to |new_hosts|,
  68. // and adds any additional permissions to |permissions|.
  69. // TODO(sashab): Split this function in two: One to filter out ignored host
  70. // permissions, and one to get permissions for the given hosts.
  71. virtual void FilterHostPermissions(const URLPatternSet& hosts,
  72. URLPatternSet* new_hosts,
  73. PermissionIDSet* permissions) const = 0;
  74. // Replaces the scripting allowlist with |allowlist|. Used in the renderer;
  75. // only used for testing in the browser process.
  76. virtual void SetScriptingAllowlist(const ScriptingAllowlist& allowlist) = 0;
  77. // Return the allowlist of extensions that can run content scripts on
  78. // any origin.
  79. virtual const ScriptingAllowlist& GetScriptingAllowlist() const = 0;
  80. // Get the set of chrome:// hosts that |extension| can have host permissions
  81. // for.
  82. virtual URLPatternSet GetPermittedChromeSchemeHosts(
  83. const Extension* extension,
  84. const APIPermissionSet& api_permissions) const = 0;
  85. // Returns false if content scripts are forbidden from running on |url|.
  86. virtual bool IsScriptableURL(const GURL& url, std::string* error) const = 0;
  87. // Returns the base webstore URL prefix.
  88. virtual const GURL& GetWebstoreBaseURL() const = 0;
  89. // Returns the base webstore URL prefix for the new webstore. This is defined
  90. // separately rather than just changing what GetWebstoreBaseURL returns, as
  91. // during the transition some functionality needs to operate across both the
  92. // old and the new domain.
  93. virtual const GURL& GetNewWebstoreBaseURL() const = 0;
  94. // Returns the URL to use for update manifest queries.
  95. virtual const GURL& GetWebstoreUpdateURL() const = 0;
  96. // Returns a flag indicating whether or not a given URL is a valid
  97. // extension blocklist URL.
  98. virtual bool IsBlocklistUpdateURL(const GURL& url) const = 0;
  99. // Returns the set of file paths corresponding to any images within an
  100. // extension's contents that may be displayed directly within the browser UI
  101. // or WebUI, such as icons or theme images. This set of paths is used by the
  102. // extension unpacker to determine which assets should be transcoded safely
  103. // within the utility sandbox.
  104. //
  105. // The default implementation returns the images used as icons for the
  106. // extension itself, so implementors of ExtensionsClient overriding this may
  107. // want to call the base class version and then add additional paths to that
  108. // result.
  109. virtual std::set<base::FilePath> GetBrowserImagePaths(
  110. const Extension* extension);
  111. // Adds client specific permitted origins to |origin_patterns| for
  112. // cross-origin communication for an extension context.
  113. virtual void AddOriginAccessPermissions(
  114. const Extension& extension,
  115. bool is_extension_active,
  116. std::vector<network::mojom::CorsOriginPatternPtr>* origin_patterns) const;
  117. // Returns the extended error code used by the embedder when an extension
  118. // blocks a request. Returns absl::nullopt if the embedder doesn't define such
  119. // an error code.
  120. virtual absl::optional<int> GetExtensionExtendedErrorCode() const;
  121. private:
  122. // Performs common initialization and calls Initialize() to allow subclasses
  123. // to do any extra initialization.
  124. void DoInitialize();
  125. std::vector<std::unique_ptr<ExtensionsAPIProvider>> api_providers_;
  126. // Whether DoInitialize() has been called.
  127. bool initialize_called_ = false;
  128. };
  129. } // namespace extensions
  130. #endif // EXTENSIONS_COMMON_EXTENSIONS_CLIENT_H_