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