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