blob: 5f189124aaaeb3cd04a9e49d669dc0e9029cd8d5 [file] [log] [blame]
jamc1905862017-05-16 14:45:301// 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 Anforowicz288027b2019-01-17 01:51:278#include <memory>
9
John Abd-El-Malek63ff5f32017-12-18 22:14:5810#include "base/callback_forward.h"
jamc1905862017-05-16 14:45:3011#include "base/macros.h"
12#include "base/memory/ref_counted.h"
13#include "content/common/content_export.h"
jamc1905862017-05-16 14:45:3014#include "content/public/browser/browser_thread.h"
Ken Rockot54311e62018-02-10 19:01:5215#include "services/network/public/mojom/url_loader_factory.mojom.h"
jamc1905862017-05-16 14:45:3016
Chong Zhangb7c8d1ce2018-03-13 19:14:1117namespace network {
Chong Zhang4dd97ebf2018-03-07 04:48:4818class SharedURLLoaderFactoryInfo;
Chong Zhang5271432a2018-03-01 23:31:0219class SharedURLLoaderFactory;
Chong Zhangb7c8d1ce2018-03-13 19:14:1120} // namespace network
21
22namespace content {
23
ananta2e65213d2017-05-19 04:08:2424class StoragePartitionImpl;
25
jamc1905862017-05-16 14:45:3026// 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.
29class URLLoaderFactoryGetter
30 : public base::RefCountedThreadSafe<URLLoaderFactoryGetter,
31 BrowserThread::DeleteOnIOThread> {
32 public:
Anantanarayanan Iyengaraa70bb42017-08-23 01:38:3233 CONTENT_EXPORT URLLoaderFactoryGetter();
jamc1905862017-05-16 14:45:3034
ananta2e65213d2017-05-19 04:08:2435 // Initializes this object on the UI thread. The |partition| is used to
Makoto Shimazu4c6e1a02018-04-25 01:00:4636 // 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.
ananta2e65213d2017-05-19 04:08:2441 void Initialize(StoragePartitionImpl* partition);
jamc1905862017-05-16 14:45:3042
Makoto Shimazu4c6e1a02018-04-25 01:00:4643 // 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 Zhangd4c923642018-01-03 21:22:2949 // 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 Zhang5271432a2018-03-01 23:31:0253 // 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 Zhangb7c8d1ce2018-03-13 19:14:1156 CONTENT_EXPORT scoped_refptr<network::SharedURLLoaderFactory>
57 GetNetworkFactory();
Chong Zhang5271432a2018-03-01 23:31:0258
John Abd-El-Malek0d9f17b2018-12-05 00:02:3859 // 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 Zhang4dd97ebf2018-03-07 04:48:4866 // 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 Zhangb7c8d1ce2018-03-13 19:14:1169 CONTENT_EXPORT std::unique_ptr<network::SharedURLLoaderFactoryInfo>
Chong Zhang4dd97ebf2018-03-07 04:48:4870 GetNetworkFactoryInfo();
71
Matt Falkenhagen8cdf3482018-08-31 00:05:0472 // Called on the IO thread. The factory obtained from here can only be used
John Abd-El-Malek0d9f17b2018-12-05 00:02:3873 // 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 Falkenhagen8cdf3482018-08-31 00:05:0476 //
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 Zhang5271432a2018-03-01 23:31:0284 CONTENT_EXPORT void CloneNetworkFactory(
85 network::mojom::URLLoaderFactoryRequest network_factory_request);
jamc1905862017-05-16 14:45:3086
87 // Overrides the network URLLoaderFactory for subsequent requests. Passing a
88 // null pointer will restore the default behavior.
jamc1905862017-05-16 14:45:3089 CONTENT_EXPORT void SetNetworkFactoryForTesting(
Lukasz Anforowicz288027b2019-01-17 01:51:2790 network::mojom::URLLoaderFactory* test_factory);
John Abd-El-Maleka4bb87f2017-11-27 21:32:1991
John Abd-El-Malekb165dc52018-01-18 17:12:1892 CONTENT_EXPORT network::mojom::URLLoaderFactoryPtr*
John Abd-El-Maleka4bb87f2017-11-27 21:32:1993 original_network_factory_for_testing() {
94 return &network_factory_;
95 }
jamc1905862017-05-16 14:45:3096
John Abd-El-Malek0d9f17b2018-12-05 00:02:3897 CONTENT_EXPORT network::mojom::URLLoaderFactoryPtr*
98 original_network_factory__corb_enabled_for_testing() {
99 return &network_factory_corb_enabled_;
100 }
101
Chong Zhang4dd97ebf2018-03-07 04:48:48102 // 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-Malek63ff5f32017-12-18 22:14:58106 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 Zhangd4c923642018-01-03 21:22:29111 // Call |network_factory_.FlushForTesting()| on IO thread. For test use only.
112 CONTENT_EXPORT void FlushNetworkInterfaceOnIOThreadForTesting();
113
jamc1905862017-05-16 14:45:30114 private:
Chong Zhang4dd97ebf2018-03-07 04:48:48115 class URLLoaderFactoryForIOThreadInfo;
Chong Zhang5271432a2018-03-01 23:31:02116 class URLLoaderFactoryForIOThread;
117
jamc1905862017-05-16 14:45:30118 friend class base::DeleteHelper<URLLoaderFactoryGetter>;
119 friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>;
120
121 CONTENT_EXPORT ~URLLoaderFactoryGetter();
John Abd-El-Malekb165dc52018-01-18 17:12:18122 void InitializeOnIOThread(
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:39123 network::mojom::URLLoaderFactoryPtrInfo network_factory);
jamc1905862017-05-16 14:45:30124
John Abd-El-Malek0d9f17b2018-12-05 00:02:38125 // 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 Menke58a77c02018-08-10 03:49:02128 void ReinitializeOnIOThread(
John Abd-El-Malek0d9f17b2018-12-05 00:02:38129 network::mojom::URLLoaderFactoryPtr network_factory,
130 bool is_corb_enabled);
Matt Menke58a77c02018-08-10 03:49:02131
Chong Zhangd4c923642018-01-03 21:22:29132 // Send |network_factory_request| to cached |StoragePartitionImpl|.
133 void HandleNetworkFactoryRequestOnUIThread(
John Abd-El-Malek0d9f17b2018-12-05 00:02:38134 network::mojom::URLLoaderFactoryRequest network_factory_request,
135 bool is_corb_enabled);
Chong Zhangd4c923642018-01-03 21:22:29136
Chong Zhang5271432a2018-03-01 23:31:02137 // Called on the IO thread to get the URLLoaderFactory to the network service.
138 // The pointer shouldn't be cached.
John Abd-El-Malek0d9f17b2018-12-05 00:02:38139 network::mojom::URLLoaderFactory* GetURLLoaderFactory(bool is_corb_enabled);
Chong Zhang5271432a2018-03-01 23:31:02140
Clark DuVall16be2542018-07-23 22:42:42141 // 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 Zhangd4c923642018-01-03 21:22:29144
Makoto Shimazu4c6e1a02018-04-25 01:00:46145 // Bound with appropriate URLLoaderFactories at HandleFactoryRequests().
146 network::mojom::URLLoaderFactoryRequest pending_network_factory_request_;
Makoto Shimazu4c6e1a02018-04-25 01:00:46147
jamc1905862017-05-16 14:45:30148 // Only accessed on IO thread.
John Abd-El-Malekb165dc52018-01-18 17:12:18149 network::mojom::URLLoaderFactoryPtr network_factory_;
John Abd-El-Malek0d9f17b2018-12-05 00:02:38150 network::mojom::URLLoaderFactoryPtr network_factory_corb_enabled_;
John Abd-El-Malekb165dc52018-01-18 17:12:18151 network::mojom::URLLoaderFactory* test_factory_ = nullptr;
jamc1905862017-05-16 14:45:30152
Chong Zhangd4c923642018-01-03 21:22:29153 // 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
jamc1905862017-05-16 14:45:30158 DISALLOW_COPY_AND_ASSIGN(URLLoaderFactoryGetter);
159};
160
161} // namespace content
162
163#endif // CONTENT_BROWSER_URL_LOADER_FACTORY_GETTER_H_