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 | |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 57 | // Called on the UI thread to get an info that holds a reference to this |
| 58 | // URLLoaderFactoryGetter, which can be used to construct a similar |
| 59 | // SharedURLLoaderFactory as returned from |GetNetworkFactory()| on IO thread. |
Chong Zhang | b7c8d1ce | 2018-03-13 19:14:11 | [diff] [blame] | 60 | CONTENT_EXPORT std::unique_ptr<network::SharedURLLoaderFactoryInfo> |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 61 | GetNetworkFactoryInfo(); |
| 62 | |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 63 | // Called on the IO thread. Will clone the internal factory to the network |
Chong Zhang | 70432e3 | 2018-03-07 04:43:52 | [diff] [blame] | 64 | // service which doesn't support auto-reconnect after crash. Useful for |
| 65 | // one-off requests (e.g. A single navigation) to avoid additional mojo hop. |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 66 | CONTENT_EXPORT void CloneNetworkFactory( |
| 67 | network::mojom::URLLoaderFactoryRequest network_factory_request); |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 68 | |
jam | 9354af8 | 2017-06-03 21:59:41 | [diff] [blame] | 69 | // Called on the IO thread to get the URLLoaderFactory to the blob service. |
| 70 | // The pointer shouldn't be cached. |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 71 | CONTENT_EXPORT network::mojom::URLLoaderFactory* GetBlobFactory(); |
jam | 9354af8 | 2017-06-03 21:59:41 | [diff] [blame] | 72 | |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 73 | // Overrides the network URLLoaderFactory for subsequent requests. Passing a |
| 74 | // null pointer will restore the default behavior. |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 75 | CONTENT_EXPORT void SetNetworkFactoryForTesting( |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 76 | network::mojom::URLLoaderFactory* test_factory); |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 77 | |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 78 | CONTENT_EXPORT network::mojom::URLLoaderFactoryPtr* |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 79 | original_network_factory_for_testing() { |
| 80 | return &network_factory_; |
| 81 | } |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 82 | |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 83 | // When this global function is set, if GetURLLoaderFactory is called and |
| 84 | // |test_factory_| is null, then the callback will be run. This method must be |
| 85 | // called either on the IO thread or before threads start. This callback is |
| 86 | // run on the IO thread. |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 87 | using GetNetworkFactoryCallback = base::RepeatingCallback<void( |
| 88 | URLLoaderFactoryGetter* url_loader_factory_getter)>; |
| 89 | CONTENT_EXPORT static void SetGetNetworkFactoryCallbackForTesting( |
| 90 | const GetNetworkFactoryCallback& get_network_factory_callback); |
| 91 | |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 92 | // Call |network_factory_.FlushForTesting()| on IO thread. For test use only. |
| 93 | CONTENT_EXPORT void FlushNetworkInterfaceOnIOThreadForTesting(); |
| 94 | |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 95 | private: |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 96 | class URLLoaderFactoryForIOThreadInfo; |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 97 | class URLLoaderFactoryForIOThread; |
| 98 | |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 99 | friend class base::DeleteHelper<URLLoaderFactoryGetter>; |
| 100 | friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>; |
| 101 | |
| 102 | CONTENT_EXPORT ~URLLoaderFactoryGetter(); |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 103 | void InitializeOnIOThread( |
| 104 | network::mojom::URLLoaderFactoryPtrInfo network_factory, |
| 105 | network::mojom::URLLoaderFactoryPtrInfo blob_factory); |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 106 | |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 107 | // Send |network_factory_request| to cached |StoragePartitionImpl|. |
| 108 | void HandleNetworkFactoryRequestOnUIThread( |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 109 | network::mojom::URLLoaderFactoryRequest network_factory_request); |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 110 | |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 111 | // Called on the IO thread to get the URLLoaderFactory to the network service. |
| 112 | // The pointer shouldn't be cached. |
| 113 | network::mojom::URLLoaderFactory* GetURLLoaderFactory(); |
| 114 | |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 115 | // Call |network_factory_.FlushForTesting()|. For test use only. |
| 116 | void FlushNetworkInterfaceForTesting(); |
| 117 | |
Makoto Shimazu | 4c6e1a0 | 2018-04-25 01:00:46 | [diff] [blame^] | 118 | // Bound with appropriate URLLoaderFactories at HandleFactoryRequests(). |
| 119 | network::mojom::URLLoaderFactoryRequest pending_network_factory_request_; |
| 120 | network::mojom::URLLoaderFactoryRequest pending_blob_factory_request_; |
| 121 | |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 122 | // Only accessed on IO thread. |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 123 | network::mojom::URLLoaderFactoryPtr network_factory_; |
| 124 | network::mojom::URLLoaderFactoryPtr blob_factory_; |
| 125 | network::mojom::URLLoaderFactory* test_factory_ = nullptr; |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 126 | |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 127 | // Used to re-create |network_factory_| when connection error happens. Can |
| 128 | // only be accessed on UI thread. Must be cleared by |StoragePartitionImpl| |
| 129 | // when it's going away. |
| 130 | StoragePartitionImpl* partition_ = nullptr; |
| 131 | |
jam | c190586 | 2017-05-16 14:45:30 | [diff] [blame] | 132 | DISALLOW_COPY_AND_ASSIGN(URLLoaderFactoryGetter); |
| 133 | }; |
| 134 | |
| 135 | } // namespace content |
| 136 | |
| 137 | #endif // CONTENT_BROWSER_URL_LOADER_FACTORY_GETTER_H_ |