1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
content / browser / loader / url_loader_factory_utils.h [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_LOADER_URL_LOADER_FACTORY_UTILS_H_
#define CONTENT_BROWSER_LOADER_URL_LOADER_FACTORY_UTILS_H_
#include "base/memory/stack_allocated.h"
#include "content/browser/devtools/devtools_instrumentation.h"
#include "content/common/content_export.h"
#include "content/public/browser/content_browser_client.h"
#include "services/network/public/cpp/url_loader_factory_builder.h"
namespace net {
class IsolationInfo;
}
namespace content {
class StoragePartitionImpl;
namespace url_loader_factory {
using Interceptor = base::RepeatingCallback<
void(int process_id, network::URLLoaderFactoryBuilder& factory_builder)>;
// This method must be called on the UI thread.
CONTENT_EXPORT const Interceptor& GetTestingInterceptor();
// Allows intercepting the URLLoaderFactory creation.
// For every `SetInterceptorForTesting(non-null interceptor)`,
// `SetInterceptorForTesting({})` must be called to ensure restoring the default
// behavior.
// This method must be called either on the UI thread or before threads start.
// This callback is run on the UI thread.
// TODO(crbug.com/40947547): Document when the interception occurs.
CONTENT_EXPORT void SetInterceptorForTesting(const Interceptor& interceptor);
// Only accessed on the IO thread.
// Basically the same as `!!GetTestingInterceptor()`, and introduced to avoid
// possible race conditions between UI/IO threads.
CONTENT_EXPORT bool HasInterceptorOnIOThreadForTesting();
CONTENT_EXPORT void SetHasInterceptorOnIOThreadForTesting(bool has_interceptor);
// A parameter object for `ContentBrowserClient::WillCreateURLLoaderFactory()`.
class CONTENT_EXPORT ContentClientParams final {
STACK_ALLOCATED();
public:
ContentClientParams(BrowserContext* browser_context,
RenderFrameHost* frame,
int render_process_id,
const url::Origin& request_initiator,
const net::IsolationInfo& isolation_info,
ukm::SourceIdObj ukm_source_id,
bool* bypass_redirect_checks = nullptr,
std::optional<int64_t> navigation_id = std::nullopt,
scoped_refptr<base::SequencedTaskRunner>
navigation_response_task_runner = nullptr);
ContentClientParams(const ContentClientParams&) = delete;
ContentClientParams& operator=(const ContentClientParams&) = delete;
ContentClientParams(ContentClientParams&&);
ContentClientParams& operator=(ContentClientParams&&);
~ContentClientParams();
// Invokes `ContentBrowserClient::WillCreateURLLoaderFactory()` with the
// parameters given to this method and the constructor.
void Run(network::URLLoaderFactoryBuilder& factory_builder,
ContentBrowserClient::URLLoaderFactoryType type,
mojo::PendingRemote<network::mojom::TrustedURLLoaderHeaderClient>*
header_client,
bool* disable_secure_dns,
network::mojom::URLLoaderFactoryOverridePtr* factory_override);
private:
raw_ptr<BrowserContext> browser_context_;
raw_ptr<RenderFrameHost> frame_;
int render_process_id_;
raw_ref<const url::Origin> request_initiator_;
raw_ref<const net::IsolationInfo> isolation_info_;
ukm::SourceIdObj ukm_source_id_;
raw_ptr<bool> bypass_redirect_checks_;
std::optional<int64_t> navigation_id_;
scoped_refptr<base::SequencedTaskRunner> navigation_response_task_runner_;
};
// When `kAllow` is used, non-null `header_client`, `disable_secure_dns` and
// `factory_override` (respectively) are passed to
// `ContentBrowserClient::WillCreateURLLoaderFactory()` and
// `devtools_instrumentation`.
// Otherwise (`kDisallow`), nullptr are passed.
enum class HeaderClientOption { kAllow, kDisallow };
enum class DisableSecureDnsOption { kAllow, kDisallow };
enum class FactoryOverrideOption { kAllow, kDisallow };
// Specifies the final destination of the URLLoaderFactory in `Create()`.
class CONTENT_EXPORT TerminalParams final {
STACK_ALLOCATED();
public:
// Connects to the URLLoaderFactory from
// `NetworkContext::CreateURLLoaderFactory(factory_params)`.
// Any options listed in the arguments can be specified.
static TerminalParams ForNetworkContext(
network::mojom::NetworkContext* network_context,
network::mojom::URLLoaderFactoryParamsPtr factory_params,
HeaderClientOption header_client_option = HeaderClientOption::kDisallow,
FactoryOverrideOption factory_override_option =
FactoryOverrideOption::kDisallow,
DisableSecureDnsOption disable_secure_dns_option =
DisableSecureDnsOption::kDisallow);
// Connects to `storage_partition->GetURLLoaderFactoryForBrowserProcess()`
// if possible, or otherwise fallback to
// `storage_partition->GetNetworkContext()->CreateURLLoaderFactory(
// storage_partition->CreateURLLoaderFactoryParams())`.
//
// This is equivalent to
// `TerminalParams::ForNetworkContext(
// storage_partition->GetNetworkContext(),
// storage_partition->CreateURLLoaderFactoryParams(), ...)`
// except that for `ForBrowserProcess()`
// - `GetURLLoaderFactoryForBrowserProcess()` can be used if possible, while
// - `FactoryOverrideOption`/`DisableSecureDnsOption` must be `kDisallow`.
static TerminalParams ForBrowserProcess(
StoragePartitionImpl* storage_partition,
HeaderClientOption header_client_option = HeaderClientOption::kDisallow);
// Connects to the given URLLoaderFactory(-ish) object.
// This should be used only for requests not served by the network service,
// e.g. requests with non-network schemes and requests served by prefetch
// caches. For requests served by the network service, use
// `ForNetworkContext()` or `ForBrowserProcess()` as they should have
// corresponding `NetworkContext`s.
//
// See the `process_id_` comment below for `process_id`.
using URLLoaderFactoryTypes =
absl::variant<mojo::PendingRemote<network::mojom::URLLoaderFactory>,
scoped_refptr<network::SharedURLLoaderFactory>>;
static TerminalParams ForNonNetwork(URLLoaderFactoryTypes url_loader_factory,
int process_id);
TerminalParams(TerminalParams&&);
TerminalParams& operator=(TerminalParams&&);
~TerminalParams();
network::mojom::NetworkContext* network_context() const;
HeaderClientOption header_client_option() const;
FactoryOverrideOption factory_override_option() const;
DisableSecureDnsOption disable_secure_dns_option() const;
StoragePartitionImpl* storage_partition() const;
int process_id() const;
network::mojom::URLLoaderFactoryParamsPtr TakeFactoryParams();
std::optional<URLLoaderFactoryTypes> TakeURLLoaderFactory();
private:
TerminalParams(network::mojom::NetworkContext* network_context,
network::mojom::URLLoaderFactoryParamsPtr factory_params,
HeaderClientOption header_client_option,
FactoryOverrideOption factory_override_option,
DisableSecureDnsOption disable_secure_dns_option,
StoragePartitionImpl* storage_partition,
std::optional<URLLoaderFactoryTypes> url_loader_factory,
int process_id);
raw_ptr<network::mojom::NetworkContext> network_context_;
network::mojom::URLLoaderFactoryParamsPtr factory_params_;
HeaderClientOption header_client_option_;
FactoryOverrideOption factory_override_option_;
DisableSecureDnsOption disable_secure_dns_option_;
raw_ptr<StoragePartitionImpl> storage_partition_;
std::optional<URLLoaderFactoryTypes> url_loader_factory_;
// The process ID plumbed to URLLoaderInterceptor. This can be
// - a renderer process, or
// - `network::mojom::kBrowserProcessId` for browser process.
// TODO(crbug.com/324458368): This is different from
// `ContentClientParams::render_process_id_`. Clarify the meaning of
// `process_id_` here if needed.
int process_id_;
};
// Creates a URLLoaderFactory, intercepted by:
// 1. `ContentBrowserClient::WillCreateURLLoaderFactory()`
// (if `content_client_params` is non-nullopt),
// 2. `devtools_instrumentation` (if `devtools_params` is non-nullopt)
// 3. `GetInterceptor()`
// (see the comments in the .cc file for detailed conditions)
// and then finally routed as specified by `TerminalParams`.
//
// The created URLLoaderFactory is
// - Returned as `scoped_refptr<network::SharedURLLoaderFactory>`,
// - Returned as `mojo::PendingRemote<network::mojom::URLLoaderFactory>`, or
// - Connected to `receiver_to_connect`,
// respectively for the variants below.
//
// Note that the created URLLoaderFactory might NOT support auto-reconnect after
// a crash of Network Service.
[[nodiscard]] CONTENT_EXPORT scoped_refptr<network::SharedURLLoaderFactory>
Create(ContentBrowserClient::URLLoaderFactoryType type,
TerminalParams terminal_params,
std::optional<ContentClientParams> content_client_params = std::nullopt,
std::optional<devtools_instrumentation::WillCreateURLLoaderFactoryParams>
devtools_params = std::nullopt);
[[nodiscard]] CONTENT_EXPORT mojo::PendingRemote<
network::mojom::URLLoaderFactory>
CreatePendingRemote(
ContentBrowserClient::URLLoaderFactoryType type,
TerminalParams terminal_params,
std::optional<ContentClientParams> content_client_params = std::nullopt,
std::optional<devtools_instrumentation::WillCreateURLLoaderFactoryParams>
devtools_params = std::nullopt);
CONTENT_EXPORT void CreateAndConnectToPendingReceiver(
mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver_to_connect,
ContentBrowserClient::URLLoaderFactoryType type,
TerminalParams terminal_params,
std::optional<ContentClientParams> content_client_params = std::nullopt,
std::optional<devtools_instrumentation::WillCreateURLLoaderFactoryParams>
devtools_params = std::nullopt);
} // namespace url_loader_factory
} // namespace content
#endif // CONTENT_BROWSER_LOADER_URL_LOADER_FACTORY_UTILS_H_