blob: f7022f6fdb94c888dc3e91e9389fc36177ecf7f0 [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
John Abd-El-Malek63ff5f32017-12-18 22:14:588#include "base/callback_forward.h"
jamc1905862017-05-16 14:45:309#include "base/macros.h"
10#include "base/memory/ref_counted.h"
11#include "content/common/content_export.h"
jamc1905862017-05-16 14:45:3012#include "content/public/browser/browser_thread.h"
Ken Rockot54311e62018-02-10 19:01:5213#include "services/network/public/mojom/url_loader_factory.mojom.h"
jamc1905862017-05-16 14:45:3014
Chong Zhangb7c8d1ce2018-03-13 19:14:1115namespace network {
Chong Zhang4dd97ebf2018-03-07 04:48:4816class SharedURLLoaderFactoryInfo;
Chong Zhang5271432a2018-03-01 23:31:0217class SharedURLLoaderFactory;
Chong Zhangb7c8d1ce2018-03-13 19:14:1118} // namespace network
19
20namespace content {
21
ananta2e65213d2017-05-19 04:08:2422class StoragePartitionImpl;
23
jamc1905862017-05-16 14:45:3024// 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.
27class URLLoaderFactoryGetter
28 : public base::RefCountedThreadSafe<URLLoaderFactoryGetter,
29 BrowserThread::DeleteOnIOThread> {
30 public:
Anantanarayanan Iyengaraa70bb42017-08-23 01:38:3231 CONTENT_EXPORT URLLoaderFactoryGetter();
jamc1905862017-05-16 14:45:3032
ananta2e65213d2017-05-19 04:08:2433 // Initializes this object on the UI thread. The |partition| is used to
Makoto Shimazu4c6e1a02018-04-25 01:00:4634 // 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.
ananta2e65213d2017-05-19 04:08:2439 void Initialize(StoragePartitionImpl* partition);
jamc1905862017-05-16 14:45:3040
Makoto Shimazu4c6e1a02018-04-25 01:00:4641 // 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 Zhangd4c923642018-01-03 21:22:2947 // 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 Zhang5271432a2018-03-01 23:31:0251 // 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 Zhangb7c8d1ce2018-03-13 19:14:1154 CONTENT_EXPORT scoped_refptr<network::SharedURLLoaderFactory>
55 GetNetworkFactory();
Chong Zhang5271432a2018-03-01 23:31:0256
John Abd-El-Malek0d9f17b2018-12-05 00:02:3857 // 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 Zhang4dd97ebf2018-03-07 04:48:4864 // 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 Zhangb7c8d1ce2018-03-13 19:14:1167 CONTENT_EXPORT std::unique_ptr<network::SharedURLLoaderFactoryInfo>
Chong Zhang4dd97ebf2018-03-07 04:48:4868 GetNetworkFactoryInfo();
69
Matt Falkenhagen8cdf3482018-08-31 00:05:0470 // Called on the IO thread. The factory obtained from here can only be used
John Abd-El-Malek0d9f17b2018-12-05 00:02:3871 // 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 Falkenhagen8cdf3482018-08-31 00:05:0474 //
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 Zhang5271432a2018-03-01 23:31:0282 CONTENT_EXPORT void CloneNetworkFactory(
83 network::mojom::URLLoaderFactoryRequest network_factory_request);
jamc1905862017-05-16 14:45:3084
85 // Overrides the network URLLoaderFactory for subsequent requests. Passing a
86 // null pointer will restore the default behavior.
jamc1905862017-05-16 14:45:3087 CONTENT_EXPORT void SetNetworkFactoryForTesting(
John Abd-El-Malek0d9f17b2018-12-05 00:02:3888 network::mojom::URLLoaderFactory* test_factory,
89 bool is_corb_enabled = false);
John Abd-El-Maleka4bb87f2017-11-27 21:32:1990
John Abd-El-Malekb165dc52018-01-18 17:12:1891 CONTENT_EXPORT network::mojom::URLLoaderFactoryPtr*
John Abd-El-Maleka4bb87f2017-11-27 21:32:1992 original_network_factory_for_testing() {
93 return &network_factory_;
94 }
jamc1905862017-05-16 14:45:3095
John Abd-El-Malek0d9f17b2018-12-05 00:02:3896 CONTENT_EXPORT network::mojom::URLLoaderFactoryPtr*
97 original_network_factory__corb_enabled_for_testing() {
98 return &network_factory_corb_enabled_;
99 }
100
Chong Zhang4dd97ebf2018-03-07 04:48:48101 // 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-Malek63ff5f32017-12-18 22:14:58105 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 Zhangd4c923642018-01-03 21:22:29110 // Call |network_factory_.FlushForTesting()| on IO thread. For test use only.
111 CONTENT_EXPORT void FlushNetworkInterfaceOnIOThreadForTesting();
112
jamc1905862017-05-16 14:45:30113 private:
Chong Zhang4dd97ebf2018-03-07 04:48:48114 class URLLoaderFactoryForIOThreadInfo;
Chong Zhang5271432a2018-03-01 23:31:02115 class URLLoaderFactoryForIOThread;
116
jamc1905862017-05-16 14:45:30117 friend class base::DeleteHelper<URLLoaderFactoryGetter>;
118 friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>;
119
120 CONTENT_EXPORT ~URLLoaderFactoryGetter();
John Abd-El-Malekb165dc52018-01-18 17:12:18121 void InitializeOnIOThread(
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:39122 network::mojom::URLLoaderFactoryPtrInfo network_factory);
jamc1905862017-05-16 14:45:30123
John Abd-El-Malek0d9f17b2018-12-05 00:02:38124 // 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 Menke58a77c02018-08-10 03:49:02127 void ReinitializeOnIOThread(
John Abd-El-Malek0d9f17b2018-12-05 00:02:38128 network::mojom::URLLoaderFactoryPtr network_factory,
129 bool is_corb_enabled);
Matt Menke58a77c02018-08-10 03:49:02130
Chong Zhangd4c923642018-01-03 21:22:29131 // Send |network_factory_request| to cached |StoragePartitionImpl|.
132 void HandleNetworkFactoryRequestOnUIThread(
John Abd-El-Malek0d9f17b2018-12-05 00:02:38133 network::mojom::URLLoaderFactoryRequest network_factory_request,
134 bool is_corb_enabled);
Chong Zhangd4c923642018-01-03 21:22:29135
Chong Zhang5271432a2018-03-01 23:31:02136 // Called on the IO thread to get the URLLoaderFactory to the network service.
137 // The pointer shouldn't be cached.
John Abd-El-Malek0d9f17b2018-12-05 00:02:38138 network::mojom::URLLoaderFactory* GetURLLoaderFactory(bool is_corb_enabled);
Chong Zhang5271432a2018-03-01 23:31:02139
Clark DuVall16be2542018-07-23 22:42:42140 // 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 Zhangd4c923642018-01-03 21:22:29143
Makoto Shimazu4c6e1a02018-04-25 01:00:46144 // Bound with appropriate URLLoaderFactories at HandleFactoryRequests().
145 network::mojom::URLLoaderFactoryRequest pending_network_factory_request_;
Makoto Shimazu4c6e1a02018-04-25 01:00:46146
jamc1905862017-05-16 14:45:30147 // Only accessed on IO thread.
John Abd-El-Malekb165dc52018-01-18 17:12:18148 network::mojom::URLLoaderFactoryPtr network_factory_;
John Abd-El-Malek0d9f17b2018-12-05 00:02:38149 network::mojom::URLLoaderFactoryPtr network_factory_corb_enabled_;
John Abd-El-Malekb165dc52018-01-18 17:12:18150 network::mojom::URLLoaderFactory* test_factory_ = nullptr;
John Abd-El-Malek0d9f17b2018-12-05 00:02:38151 network::mojom::URLLoaderFactory* test_factory_corb_enabled_ = 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_