url_loader_factory_manager.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2018 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_BROWSER_URL_LOADER_FACTORY_MANAGER_H_
  5. #define EXTENSIONS_BROWSER_URL_LOADER_FACTORY_MANAGER_H_
  6. #include "base/types/pass_key.h"
  7. #include "content/public/browser/navigation_handle.h"
  8. #include "extensions/common/extension.h"
  9. #include "mojo/public/cpp/bindings/pending_remote.h"
  10. #include "services/network/public/mojom/network_context.mojom.h"
  11. #include "url/gurl.h"
  12. namespace content {
  13. class BrowserContext;
  14. class RenderFrameHost;
  15. } // namespace content
  16. namespace url {
  17. class Origin;
  18. } // namespace url
  19. namespace extensions {
  20. class ContentScriptTracker;
  21. // This class manages URLLoaderFactory objects that handle network requests that
  22. // require extension-specific permissions (related to relaxed CORB and CORS).
  23. //
  24. // See also https://crbug.com/846346 for motivation for having separate
  25. // URLLoaderFactory objects for content scripts.
  26. class URLLoaderFactoryManager {
  27. public:
  28. // Only static methods.
  29. URLLoaderFactoryManager() = delete;
  30. URLLoaderFactoryManager(const URLLoaderFactoryManager&) = delete;
  31. URLLoaderFactoryManager& operator=(const URLLoaderFactoryManager&) = delete;
  32. // Invoked when `navigation` is ready to commit with the set of `extensions`
  33. // asked to inject content script into the target frame using
  34. // declarations in the extension manifest approach:
  35. // https://developer.chrome.com/docs/extensions/mv2/content_scripts/#declaratively
  36. static void WillInjectContentScriptsWhenNavigationCommits(
  37. base::PassKey<ContentScriptTracker> pass_key,
  38. content::NavigationHandle* navigation,
  39. const std::vector<const Extension*>& extensions);
  40. // Invoked when `extension` asks to inject a content script into `frame`
  41. // (invoked before an IPC with the content script injection request is
  42. // actually sent to the renderer process). This covers injections via
  43. // `chrome.declarativeContent` and `chrome.scripting.executeScript` APIs -
  44. // see:
  45. // https://developer.chrome.com/docs/extensions/mv2/content_scripts/#programmatic
  46. // and
  47. // https://developer.chrome.com/docs/extensions/reference/declarativeContent/#type-RequestContentScript
  48. static void WillProgrammaticallyInjectContentScript(
  49. base::PassKey<ContentScriptTracker> pass_key,
  50. content::RenderFrameHost* frame,
  51. const Extension& extension);
  52. // Creates a URLLoaderFactory that should be used for requests initiated from
  53. // |process| by |origin|.
  54. //
  55. // The behavior of this method depends on the intended consumer of the
  56. // URLLoaderFactory:
  57. // - "web": No changes are made to |factory_params| - an extensions-agnostic,
  58. // default URLLoaderFactory should be used
  59. // - "extension": Extension-specific permissions are set in |factory_params|
  60. // if the factory will be used by an extension frame (e.g. from an extension
  61. // background page).
  62. // - "content script": For most extensions no changes are made to
  63. // |factory_params|, but platform apps might need to set app-specific
  64. // security properties in the URLLoaderFactory used by content scripts.
  65. // The method recognizes the intended consumer based on |origin| ("web" vs
  66. // other cases) and |is_for_isolated_world| ("extension" vs "content script").
  67. //
  68. // The following examples might help understand the difference between
  69. // |origin| and other properties of a factory and/or network request:
  70. //
  71. // | web | extension | content script
  72. // --------------------------------|-----------|-------------|---------------
  73. // network::ResourceRequest: | | |
  74. // - request_initiator | web | extension | web
  75. // - isolated_world_origin | nullopt | nullopt | extension
  76. // | | |
  77. // OverrideFactory...Params: | | |
  78. // - origin | web | extension | extension
  79. // | | |
  80. // URLLoaderFactoryParams: | | |
  81. // - request_initiator_origin_lock | web | extension | web
  82. // - overridden properties? | no | yes | if needed
  83. // - is_corb_enabled | secure- | ext-based | ext-based for
  84. // - ..._access_patterns | -default | | platform apps
  85. static void OverrideURLLoaderFactoryParams(
  86. content::BrowserContext* browser_context,
  87. const url::Origin& origin,
  88. bool is_for_isolated_world,
  89. network::mojom::URLLoaderFactoryParams* factory_params);
  90. };
  91. } // namespace extensions
  92. #endif // EXTENSIONS_BROWSER_URL_LOADER_FACTORY_MANAGER_H_