jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 1 | // Copyright 2017 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 CONTENT_BROWSER_URL_LOADER_FACTORY_GETTER_H_ |
| 6 | #define CONTENT_BROWSER_URL_LOADER_FACTORY_GETTER_H_ |
| 7 | |
Lukasz Anforowicz | 288027b | 2019-01-17 01:51:27 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 10 | #include "base/callback_forward.h" |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 11 | #include "base/macros.h" |
| 12 | #include "base/memory/ref_counted.h" |
| 13 | #include "content/common/content_export.h" |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 14 | #include "content/public/browser/browser_thread.h" |
Julie Jeongeun Kim | b405097b | 2019-10-10 15:34:07 | [diff] [blame] | 15 | #include "mojo/public/cpp/bindings/pending_receiver.h" |
Ken Rockot | 54311e6 | 2018-02-10 19:01:52 | [diff] [blame] | 16 | #include "services/network/public/mojom/url_loader_factory.mojom.h" |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 17 | |
Chong Zhang | b7c8d1ce | 2018-03-13 19:14:11 | [diff] [blame] | 18 | namespace network { |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 19 | class SharedURLLoaderFactoryInfo; |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 20 | class SharedURLLoaderFactory; |
Chong Zhang | b7c8d1ce | 2018-03-13 19:14:11 | [diff] [blame] | 21 | } // namespace network |
| 22 | |
| 23 | namespace content { |
| 24 | |
ananta | 2e65213d | 2017-05-19 04:08:24 | [diff] [blame] | 25 | class StoragePartitionImpl; |
| 26 | |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 27 | // Holds on to URLLoaderFactory for a given StoragePartition and allows code |
| 28 | // running on the IO thread to access them. Note these are the factories used by |
| 29 | // the browser process for frame requests. |
| 30 | class URLLoaderFactoryGetter |
| 31 | : public base::RefCountedThreadSafe<URLLoaderFactoryGetter, |
| 32 | BrowserThread::DeleteOnIOThread> { |
| 33 | public: |
Anantanarayanan Iyengar | aa70bb4 | 2017-08-23 01:38:32 | [diff] [blame] | 34 | CONTENT_EXPORT URLLoaderFactoryGetter(); |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 35 | |
ananta | 2e65213d | 2017-05-19 04:08:24 | [diff] [blame] | 36 | // Initializes this object on the UI thread. The |partition| is used to |
Makoto Shimazu | 4c6e1a0 | 2018-04-25 01:00:46 | [diff] [blame] | 37 | // initialize the URLLoaderFactories for the network service, AppCache, and |
| 38 | // ServiceWorkers, and will be cached to recover from connection error. |
| 39 | // After Initialize(), you can get URLLoaderFactories from this |
John Abd-El-Malek | c134dd8 | 2019-07-31 20:51:44 | [diff] [blame] | 40 | // getter. |
ananta | 2e65213d | 2017-05-19 04:08:24 | [diff] [blame] | 41 | void Initialize(StoragePartitionImpl* partition); |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 42 | |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 43 | // Clear the cached pointer to |StoragePartitionImpl| on the UI thread. Should |
| 44 | // be called when the partition is going away. |
| 45 | void OnStoragePartitionDestroyed(); |
| 46 | |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 47 | // Called on the IO thread to get a shared wrapper to this |
| 48 | // URLLoaderFactoryGetter, which can be used to access the URLLoaderFactory |
| 49 | // to the network service and supports auto-reconnect after crash. |
Chong Zhang | b7c8d1ce | 2018-03-13 19:14:11 | [diff] [blame] | 50 | CONTENT_EXPORT scoped_refptr<network::SharedURLLoaderFactory> |
| 51 | GetNetworkFactory(); |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 52 | |
John Abd-El-Malek | 0d9f17b | 2018-12-05 00:02:38 | [diff] [blame] | 53 | // Like above, except it returns a URLLoaderFactory that has CORB enabled. Use |
| 54 | // this when using the factory for requests on behalf of a renderer. |
| 55 | // TODO(lukasza): https://crbug.com/871827: Ensure that |request_initiator| is |
| 56 | // trustworthy, even when starting requests on behalf of a renderer. |
| 57 | CONTENT_EXPORT scoped_refptr<network::SharedURLLoaderFactory> |
| 58 | GetNetworkFactoryWithCORBEnabled(); |
| 59 | |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 60 | // Called on the UI thread to get an info that holds a reference to this |
| 61 | // URLLoaderFactoryGetter, which can be used to construct a similar |
| 62 | // SharedURLLoaderFactory as returned from |GetNetworkFactory()| on IO thread. |
Chong Zhang | b7c8d1ce | 2018-03-13 19:14:11 | [diff] [blame] | 63 | CONTENT_EXPORT std::unique_ptr<network::SharedURLLoaderFactoryInfo> |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 64 | GetNetworkFactoryInfo(); |
| 65 | |
Matt Falkenhagen | 8cdf348 | 2018-08-31 00:05:04 | [diff] [blame] | 66 | // Called on the IO thread. The factory obtained from here can only be used |
John Abd-El-Malek | 0d9f17b | 2018-12-05 00:02:38 | [diff] [blame] | 67 | // from the browser process. It must NOT be sent to a renderer process. It has |
| 68 | // CORB disabled, so it must NOT be used to make requests on behalf of a |
| 69 | // renderer. |
Matt Falkenhagen | 8cdf348 | 2018-08-31 00:05:04 | [diff] [blame] | 70 | // |
| 71 | // When NetworkService is enabled, this clones the internal factory to the |
| 72 | // network service, which doesn't support auto-reconnect after crash. Useful |
| 73 | // for one-off requests (e.g. a single navigation) to avoid an additional Mojo |
| 74 | // hop. |
| 75 | // |
| 76 | // When NetworkService is disabled, this clones the non-NetworkService direct |
| 77 | // network factory. |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 78 | CONTENT_EXPORT void CloneNetworkFactory( |
Julie Jeongeun Kim | b405097b | 2019-10-10 15:34:07 | [diff] [blame] | 79 | mojo::PendingReceiver<network::mojom::URLLoaderFactory> |
| 80 | network_factory_receiver); |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 81 | |
| 82 | // Overrides the network URLLoaderFactory for subsequent requests. Passing a |
| 83 | // null pointer will restore the default behavior. |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 84 | CONTENT_EXPORT void SetNetworkFactoryForTesting( |
Lukasz Anforowicz | 62bb74b | 2019-02-06 21:10:32 | [diff] [blame] | 85 | network::mojom::URLLoaderFactory* test_factory, |
| 86 | bool is_corb_enabled = false); |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 87 | |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 88 | CONTENT_EXPORT network::mojom::URLLoaderFactoryPtr* |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 89 | original_network_factory_for_testing() { |
| 90 | return &network_factory_; |
| 91 | } |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 92 | |
John Abd-El-Malek | 0d9f17b | 2018-12-05 00:02:38 | [diff] [blame] | 93 | CONTENT_EXPORT network::mojom::URLLoaderFactoryPtr* |
| 94 | original_network_factory__corb_enabled_for_testing() { |
| 95 | return &network_factory_corb_enabled_; |
| 96 | } |
| 97 | |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 98 | // When this global function is set, if GetURLLoaderFactory is called and |
| 99 | // |test_factory_| is null, then the callback will be run. This method must be |
| 100 | // called either on the IO thread or before threads start. This callback is |
| 101 | // run on the IO thread. |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 102 | using GetNetworkFactoryCallback = base::RepeatingCallback<void( |
Matt Falkenhagen | 4df68d2 | 2019-09-09 08:34:15 | [diff] [blame] | 103 | scoped_refptr<URLLoaderFactoryGetter> url_loader_factory_getter)>; |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 104 | CONTENT_EXPORT static void SetGetNetworkFactoryCallbackForTesting( |
| 105 | const GetNetworkFactoryCallback& get_network_factory_callback); |
| 106 | |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 107 | // Call |network_factory_.FlushForTesting()| on IO thread. For test use only. |
| 108 | CONTENT_EXPORT void FlushNetworkInterfaceOnIOThreadForTesting(); |
| 109 | |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 110 | private: |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 111 | class URLLoaderFactoryForIOThreadInfo; |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 112 | class URLLoaderFactoryForIOThread; |
| 113 | |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 114 | friend class base::DeleteHelper<URLLoaderFactoryGetter>; |
| 115 | friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>; |
| 116 | |
| 117 | CONTENT_EXPORT ~URLLoaderFactoryGetter(); |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 118 | void InitializeOnIOThread( |
Marijn Kruisselbrink | 0c87e6e | 2018-06-22 22:57:39 | [diff] [blame] | 119 | network::mojom::URLLoaderFactoryPtrInfo network_factory); |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 120 | |
John Abd-El-Malek | 0d9f17b | 2018-12-05 00:02:38 | [diff] [blame] | 121 | // Moves |network_factory| to |network_factory_| or |
| 122 | // |network_factory_corb_enabled_| depending on |is_corb_enabled| and sets up |
| 123 | // an error handler. |
Matt Menke | 58a77c0 | 2018-08-10 03:49:02 | [diff] [blame] | 124 | void ReinitializeOnIOThread( |
John Abd-El-Malek | 0d9f17b | 2018-12-05 00:02:38 | [diff] [blame] | 125 | network::mojom::URLLoaderFactoryPtr network_factory, |
| 126 | bool is_corb_enabled); |
Matt Menke | 58a77c0 | 2018-08-10 03:49:02 | [diff] [blame] | 127 | |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 128 | // Send |network_factory_request| to cached |StoragePartitionImpl|. |
| 129 | void HandleNetworkFactoryRequestOnUIThread( |
Julie Jeongeun Kim | b405097b | 2019-10-10 15:34:07 | [diff] [blame] | 130 | mojo::PendingReceiver<network::mojom::URLLoaderFactory> |
| 131 | network_factory_receiver, |
John Abd-El-Malek | 0d9f17b | 2018-12-05 00:02:38 | [diff] [blame] | 132 | bool is_corb_enabled); |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 133 | |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 134 | // Called on the IO thread to get the URLLoaderFactory to the network service. |
| 135 | // The pointer shouldn't be cached. |
John Abd-El-Malek | 0d9f17b | 2018-12-05 00:02:38 | [diff] [blame] | 136 | network::mojom::URLLoaderFactory* GetURLLoaderFactory(bool is_corb_enabled); |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 137 | |
Clark DuVall | 16be254 | 2018-07-23 22:42:42 | [diff] [blame] | 138 | // Call |network_factory_.FlushForTesting()|. For test use only. When the |
| 139 | // flush is complete, |callback| will be called. |
| 140 | void FlushNetworkInterfaceForTesting(base::OnceClosure callback); |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 141 | |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 142 | // Only accessed on IO thread. |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 143 | network::mojom::URLLoaderFactoryPtr network_factory_; |
John Abd-El-Malek | 0d9f17b | 2018-12-05 00:02:38 | [diff] [blame] | 144 | network::mojom::URLLoaderFactoryPtr network_factory_corb_enabled_; |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 145 | network::mojom::URLLoaderFactory* test_factory_ = nullptr; |
Lukasz Anforowicz | 62bb74b | 2019-02-06 21:10:32 | [diff] [blame] | 146 | network::mojom::URLLoaderFactory* test_factory_corb_enabled_ = nullptr; |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 147 | |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 148 | // Used to re-create |network_factory_| when connection error happens. Can |
| 149 | // only be accessed on UI thread. Must be cleared by |StoragePartitionImpl| |
| 150 | // when it's going away. |
| 151 | StoragePartitionImpl* partition_ = nullptr; |
| 152 | |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 153 | DISALLOW_COPY_AND_ASSIGN(URLLoaderFactoryGetter); |
| 154 | }; |
| 155 | |
| 156 | } // namespace content |
| 157 | |
| 158 | #endif // CONTENT_BROWSER_URL_LOADER_FACTORY_GETTER_H_ |