xref: /aosp_15_r20/external/cronet/net/docs/life-of-a-url-request.md (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker# Life of a URLRequest
2*6777b538SAndroid Build Coastguard Worker
3*6777b538SAndroid Build Coastguard WorkerThis document gives an overview of the browser's lower-layers for networking.
4*6777b538SAndroid Build Coastguard Worker
5*6777b538SAndroid Build Coastguard WorkerNetworking in the browser ranges from high level Javascript APIs like
6*6777b538SAndroid Build Coastguard Worker`fetch()`, all the way down to writing encrypted bytes on a socket.
7*6777b538SAndroid Build Coastguard Worker
8*6777b538SAndroid Build Coastguard WorkerThis document assumes that requests for URLs are mediated through the browser's
9*6777b538SAndroid Build Coastguard Worker[Network Service](../../services/network/README.md), and focuses on all the
10*6777b538SAndroid Build Coastguard Workerlayers below the Network Service, including key points of integration.
11*6777b538SAndroid Build Coastguard Worker
12*6777b538SAndroid Build Coastguard WorkerIt's particularly targeted at people new to the Chrome network stack, but
13*6777b538SAndroid Build Coastguard Workershould also be useful for team members who may be experts at some parts of the
14*6777b538SAndroid Build Coastguard Workerstack, but are largely unfamiliar with other components. It starts by walking
15*6777b538SAndroid Build Coastguard Workerthrough how a basic request issued by another process works its way through the
16*6777b538SAndroid Build Coastguard Workernetwork stack, and then moves on to discuss how various components plug in.
17*6777b538SAndroid Build Coastguard Worker
18*6777b538SAndroid Build Coastguard WorkerIf you notice any inaccuracies in this document, or feel that things could be
19*6777b538SAndroid Build Coastguard Workerbetter explained, please do not hesitate to submit patches.
20*6777b538SAndroid Build Coastguard Worker
21*6777b538SAndroid Build Coastguard Worker
22*6777b538SAndroid Build Coastguard Worker# Anatomy of the Network Stack
23*6777b538SAndroid Build Coastguard Worker
24*6777b538SAndroid Build Coastguard WorkerThe network stack is located in //net/ in the Chrome repo, and uses the
25*6777b538SAndroid Build Coastguard Workernamespace "net". Whenever a class name in this doc has no namespace, it can
26*6777b538SAndroid Build Coastguard Workergenerally be assumed it's in //net/ and is in the net namespace.
27*6777b538SAndroid Build Coastguard Worker
28*6777b538SAndroid Build Coastguard WorkerThe top-level network stack object is the URLRequestContext. The context has
29*6777b538SAndroid Build Coastguard Workernon-owning pointers to everything needed to create and issue a URLRequest. The
30*6777b538SAndroid Build Coastguard Workercontext must outlive all requests that use it. Creating a context is a rather
31*6777b538SAndroid Build Coastguard Workercomplicated process usually managed by URLRequestContextBuilder.
32*6777b538SAndroid Build Coastguard Worker
33*6777b538SAndroid Build Coastguard WorkerThe primary use of the URLRequestContext is to create URLRequest objects using
34*6777b538SAndroid Build Coastguard WorkerURLRequestContext::CreateRequest(). The URLRequest is the main interface used
35*6777b538SAndroid Build Coastguard Workerby direct consumers of the network stack. It manages loading URLs with the
36*6777b538SAndroid Build Coastguard Workerhttp, https, ws, and wss schemes. URLs for other schemes, such as file,
37*6777b538SAndroid Build Coastguard Workerfilesystem, blob, chrome, and data, are managed completely outside of //net.
38*6777b538SAndroid Build Coastguard WorkerEach URLRequest tracks a single request across all redirects until an error
39*6777b538SAndroid Build Coastguard Workeroccurs, it's canceled, or a final response is received, with a (possibly empty)
40*6777b538SAndroid Build Coastguard Workerbody.
41*6777b538SAndroid Build Coastguard Worker
42*6777b538SAndroid Build Coastguard WorkerThe HttpNetworkSession is another major network stack object. It owns the
43*6777b538SAndroid Build Coastguard WorkerHttpStreamFactory, the socket pools, and the HTTP/2 and QUIC session pools. It
44*6777b538SAndroid Build Coastguard Workeralso has non-owning pointers to the network stack objects that more directly
45*6777b538SAndroid Build Coastguard Workerdeal with sockets.
46*6777b538SAndroid Build Coastguard Worker
47*6777b538SAndroid Build Coastguard WorkerThis document does not mention either of these objects much, but at layers
48*6777b538SAndroid Build Coastguard Workerabove the HttpStreamFactory, objects often grab their dependencies from the
49*6777b538SAndroid Build Coastguard WorkerURLRequestContext, while the HttpStreamFactory and layers below it generally
50*6777b538SAndroid Build Coastguard Workerget their dependencies from the HttpNetworkSession.
51*6777b538SAndroid Build Coastguard Worker
52*6777b538SAndroid Build Coastguard Worker
53*6777b538SAndroid Build Coastguard Worker# How many "Delegates"?
54*6777b538SAndroid Build Coastguard Worker
55*6777b538SAndroid Build Coastguard WorkerA URLRequest informs the consumer of important events for a request using two
56*6777b538SAndroid Build Coastguard Workermain interfaces: the URLRequest::Delegate interface and the NetworkDelegate
57*6777b538SAndroid Build Coastguard Workerinterface.
58*6777b538SAndroid Build Coastguard Worker
59*6777b538SAndroid Build Coastguard WorkerThe URLRequest::Delegate interface consists of a small set of callbacks needed
60*6777b538SAndroid Build Coastguard Workerto let the embedder drive a request forward. The NetworkDelegate is an object
61*6777b538SAndroid Build Coastguard Workerpointed to by the URLRequestContext and shared by all requests, and includes
62*6777b538SAndroid Build Coastguard Workercallbacks corresponding to most of the URLRequest::Delegate's callbacks, as
63*6777b538SAndroid Build Coastguard Workerwell as an assortment of other methods.
64*6777b538SAndroid Build Coastguard Worker
65*6777b538SAndroid Build Coastguard Worker# The Network Service and Mojo
66*6777b538SAndroid Build Coastguard Worker
67*6777b538SAndroid Build Coastguard WorkerThe network service, which lives in //services/network/, wraps //net/ objects,
68*6777b538SAndroid Build Coastguard Workerand provides cross-process network APIs and their implementations for the rest
69*6777b538SAndroid Build Coastguard Workerof Chrome. The network service uses the namespace "network" for all its classes.
70*6777b538SAndroid Build Coastguard WorkerThe Mojo interfaces it provides are in the network::mojom namespace. Mojo is
71*6777b538SAndroid Build Coastguard WorkerChrome's IPC layer. Generally there's a `mojo::Remote<network::mojom::Foo>`
72*6777b538SAndroid Build Coastguard Workerproxy object in the consumer's process which also implements
73*6777b538SAndroid Build Coastguard Workerthe network::mojom::Foo interface. When the proxy object's methods are invoked,
74*6777b538SAndroid Build Coastguard Workerit passes the call and all its arguments over a Mojo IPC channel, using a
75*6777b538SAndroid Build Coastguard Worker`mojo::Receiver<network::mojom::Foo>`, to an implementation of the
76*6777b538SAndroid Build Coastguard Workernetwork::mojom::Foo interface in the network service (the implementation is
77*6777b538SAndroid Build Coastguard Workertypically a class named network::Foo), which may be running in another process,
78*6777b538SAndroid Build Coastguard Workeranother thread in the consumer's process, or even the same thread in the
79*6777b538SAndroid Build Coastguard Workerconsumer's process.
80*6777b538SAndroid Build Coastguard Worker
81*6777b538SAndroid Build Coastguard WorkerThe network::NetworkService object is singleton that is used by Chrome to create
82*6777b538SAndroid Build Coastguard Workerall other network service objects. The primary objects it is used to create are
83*6777b538SAndroid Build Coastguard Workerthe network::NetworkContexts, each of which owns its own mostly independent
84*6777b538SAndroid Build Coastguard WorkerURLRequestContext. Chrome has a number of different NetworkContexts, as there
85*6777b538SAndroid Build Coastguard Workeris often a need to keep cookies, caches, and socket pools separate for different
86*6777b538SAndroid Build Coastguard Workertypes of requests, depending on what's making the request. Here are the main
87*6777b538SAndroid Build Coastguard WorkerNetworkContexts used by Chrome:
88*6777b538SAndroid Build Coastguard Worker
89*6777b538SAndroid Build Coastguard Worker* The system NetworkContext, created and owned by Chrome's
90*6777b538SAndroid Build Coastguard WorkerSystemNetworkContextManager, is used for requests that aren't associated with
91*6777b538SAndroid Build Coastguard Workerparticular user or Profile. It has no on-disk storage, so loses all state, like
92*6777b538SAndroid Build Coastguard Workercookies, after each browser restart. It has no in-memory http cache, either.
93*6777b538SAndroid Build Coastguard WorkerSystemNetworkContextManager also sets up global network service preferences.
94*6777b538SAndroid Build Coastguard Worker* Each Chrome Profile, including incognito Profiles, has its own NetworkContext.
95*6777b538SAndroid Build Coastguard WorkerExcept for incognito and guest profiles, these contexts store information in
96*6777b538SAndroid Build Coastguard Workertheir own on-disk store, which includes cookies and an HTTP cache, among other
97*6777b538SAndroid Build Coastguard Workerthings. Each of these NetworkContexts is owned by a StoragePartition object in
98*6777b538SAndroid Build Coastguard Workerthe browser process, and created by a Profile's ProfileNetworkContextService.
99*6777b538SAndroid Build Coastguard Worker* On platforms that support apps, each Profile has a NetworkContext for each app
100*6777b538SAndroid Build Coastguard Workerinstalled on that Profile. As with the main NetworkContext, these may have
101*6777b538SAndroid Build Coastguard Workeron-disk data, depending on the Profile and the App.
102*6777b538SAndroid Build Coastguard Worker
103*6777b538SAndroid Build Coastguard Worker
104*6777b538SAndroid Build Coastguard Worker# Life of a Simple URLRequest
105*6777b538SAndroid Build Coastguard Worker
106*6777b538SAndroid Build Coastguard WorkerA request for data is dispatched from some process, which results in creating
107*6777b538SAndroid Build Coastguard Workera network::URLLoader in the network service (which, on desktop platform, is
108*6777b538SAndroid Build Coastguard Workertypically in its own process). The URLLoader then creates a URLRequest to
109*6777b538SAndroid Build Coastguard Workerdrive the network request. That job first checks the HTTP cache, and then
110*6777b538SAndroid Build Coastguard Workercreates a network transaction object, if necessary, to actually fetch the data.
111*6777b538SAndroid Build Coastguard WorkerThat transaction tries to reuse a connection if available. If none is available,
112*6777b538SAndroid Build Coastguard Workerit creates a new one. Once it has established a connection, the HTTP request is
113*6777b538SAndroid Build Coastguard Workerdispatched, the response read and parsed, and the result returned back up the
114*6777b538SAndroid Build Coastguard Workerstack and sent over to the caller.
115*6777b538SAndroid Build Coastguard Worker
116*6777b538SAndroid Build Coastguard WorkerOf course, it's not quite that simple :-}.
117*6777b538SAndroid Build Coastguard Worker
118*6777b538SAndroid Build Coastguard WorkerConsider a simple request issued by some process other than the network
119*6777b538SAndroid Build Coastguard Workerservice's process. Suppose it's an HTTP request, the response is uncompressed,
120*6777b538SAndroid Build Coastguard Workerno matching entry in the cache, and there are no idle sockets connected to the
121*6777b538SAndroid Build Coastguard Workerserver in the socket pool.
122*6777b538SAndroid Build Coastguard Worker
123*6777b538SAndroid Build Coastguard WorkerContinuing with a "simple" URLRequest, here's a bit more detail on how things
124*6777b538SAndroid Build Coastguard Workerwork.
125*6777b538SAndroid Build Coastguard Worker
126*6777b538SAndroid Build Coastguard Worker### Request starts in some (non-network) process
127*6777b538SAndroid Build Coastguard Worker
128*6777b538SAndroid Build Coastguard WorkerSummary:
129*6777b538SAndroid Build Coastguard Worker
130*6777b538SAndroid Build Coastguard Worker* In the browser process, the network::mojom::NetworkContext interface is used
131*6777b538SAndroid Build Coastguard Workerto create a network::mojom::URLLoaderFactory.
132*6777b538SAndroid Build Coastguard Worker* A consumer (e.g. the content::ResourceDispatcher for Blink, the
133*6777b538SAndroid Build Coastguard Workercontent::NavigationURLLoaderImpl for frame navigations, or a
134*6777b538SAndroid Build Coastguard Workernetwork::SimpleURLLoader) passes a network::ResourceRequest object and
135*6777b538SAndroid Build Coastguard Workernetwork::mojom::URLLoaderClient Mojo channel to the
136*6777b538SAndroid Build Coastguard Workernetwork::mojom::URLLoaderFactory, and tells it to create and start a
137*6777b538SAndroid Build Coastguard Workernetwork::mojom::URLLoader.
138*6777b538SAndroid Build Coastguard Worker* Mojo sends the network::ResourceRequest over an IPC pipe to a
139*6777b538SAndroid Build Coastguard Workernetwork::URLLoaderFactory in the network process.
140*6777b538SAndroid Build Coastguard Worker
141*6777b538SAndroid Build Coastguard WorkerChrome has a single browser process which handles starting and configuring other
142*6777b538SAndroid Build Coastguard Workerprocesses, tab management, and navigation, among other things, and multiple
143*6777b538SAndroid Build Coastguard Workerchild processes, which are generally sandboxed and have no network access
144*6777b538SAndroid Build Coastguard Workerthemselves, apart from the network service (Which either runs in its own
145*6777b538SAndroid Build Coastguard Workerprocess, or potentially in the browser process to conserve RAM). There are
146*6777b538SAndroid Build Coastguard Workermultiple types of child processes (renderer, GPU, plugin, network, etc). The
147*6777b538SAndroid Build Coastguard Workerrenderer processes are the ones that layout webpages and run HTML.
148*6777b538SAndroid Build Coastguard Worker
149*6777b538SAndroid Build Coastguard WorkerThe browser process creates the top level network::mojom::NetworkContext
150*6777b538SAndroid Build Coastguard Workerobjects. The NetworkContext interface is privileged and can only be accessed
151*6777b538SAndroid Build Coastguard Workerfrom the browser process. The browser process uses it to create
152*6777b538SAndroid Build Coastguard Workernetwork::mojom::URLLoaderFactories, which can then be passed to less
153*6777b538SAndroid Build Coastguard Workerprivileged processes to allow them to load resources using the NetworkContext.
154*6777b538SAndroid Build Coastguard WorkerTo create a URLLoaderFactory, a network::mojom::URLLoaderFactoryParams object
155*6777b538SAndroid Build Coastguard Workeris passed to the NetworkContext to configure fields that other processes are
156*6777b538SAndroid Build Coastguard Workernot trusted to set, for security and privacy reasons.
157*6777b538SAndroid Build Coastguard Worker
158*6777b538SAndroid Build Coastguard WorkerOne such field is the net::IsolationInfo field, which includes:
159*6777b538SAndroid Build Coastguard Worker* A net::NetworkIsolationKey, which is used to enforce the
160*6777b538SAndroid Build Coastguard Worker[privacy sandbox](https://www.chromium.org/Home/chromium-privacy/privacy-sandbox)
161*6777b538SAndroid Build Coastguard Workerin the network stack, separating network resources used by different sites in
162*6777b538SAndroid Build Coastguard Workerorder to protect against tracking a user across sites.
163*6777b538SAndroid Build Coastguard Worker* A net::SiteForCookies, which is used to determine which site to send SameSite
164*6777b538SAndroid Build Coastguard Workercookies for. SameSite cookies prevent cross-site attacks by only being
165*6777b538SAndroid Build Coastguard Workeraccessible when that site is the top-level site.
166*6777b538SAndroid Build Coastguard Worker* How to update these values across redirects.
167*6777b538SAndroid Build Coastguard Worker
168*6777b538SAndroid Build Coastguard WorkerA consumer, either in the browser process or a child process, that wants to
169*6777b538SAndroid Build Coastguard Workermake a network request gets a URLLoaderFactory from the browser process through
170*6777b538SAndroid Build Coastguard Workersome manner, assembles a bunch of parameters in the large
171*6777b538SAndroid Build Coastguard Workernetwork::ResourceRequest object, creates a network::mojom::URLLoaderClient Mojo
172*6777b538SAndroid Build Coastguard Workerchannel for the network::mojom::URLLoader to use to talk back to it, and then
173*6777b538SAndroid Build Coastguard Workerpasses them all to the URLLoaderFactory, which returns a URLLoader object that
174*6777b538SAndroid Build Coastguard Workerit can use to manage the network request.
175*6777b538SAndroid Build Coastguard Worker
176*6777b538SAndroid Build Coastguard Worker### network::URLLoaderFactory sets up the request in the network service
177*6777b538SAndroid Build Coastguard Worker
178*6777b538SAndroid Build Coastguard WorkerSummary:
179*6777b538SAndroid Build Coastguard Worker
180*6777b538SAndroid Build Coastguard Worker* network::URLLoaderFactory creates a network::URLLoader.
181*6777b538SAndroid Build Coastguard Worker* network::URLLoader uses the network::NetworkContext's URLRequestContext to
182*6777b538SAndroid Build Coastguard Workercreate and start a URLRequest.
183*6777b538SAndroid Build Coastguard Worker
184*6777b538SAndroid Build Coastguard WorkerThe network::URLLoaderFactory, along with all NetworkContexts and most of the
185*6777b538SAndroid Build Coastguard Workernetwork stack, lives on a single thread in the network service. It gets a
186*6777b538SAndroid Build Coastguard Workerreconstituted ResourceRequest object from the network::mojom::URLLoaderFactory
187*6777b538SAndroid Build Coastguard WorkerMojo pipe, does some checks to make sure it can service the request, and if so,
188*6777b538SAndroid Build Coastguard Workercreates a URLLoader, passing the request and the NetworkContext associated with
189*6777b538SAndroid Build Coastguard Workerthe URLLoaderFactory.
190*6777b538SAndroid Build Coastguard Worker
191*6777b538SAndroid Build Coastguard WorkerThe URLLoader then calls into the NetworkContext's net::URLRequestContext to
192*6777b538SAndroid Build Coastguard Workercreate the URLRequest. The URLRequestContext has pointers to all the network
193*6777b538SAndroid Build Coastguard Workerstack objects needed to issue the request over the network, such as the cache,
194*6777b538SAndroid Build Coastguard Workercookie store, and host resolver. The URLLoader then calls into the
195*6777b538SAndroid Build Coastguard Workernetwork::ResourceScheduler, which may delay starting the request, based on
196*6777b538SAndroid Build Coastguard Workerpriority and other activity. Eventually, the ResourceScheduler starts the
197*6777b538SAndroid Build Coastguard Workerrequest.
198*6777b538SAndroid Build Coastguard Worker
199*6777b538SAndroid Build Coastguard Worker### Check the cache, request an HttpStream
200*6777b538SAndroid Build Coastguard Worker
201*6777b538SAndroid Build Coastguard WorkerSummary:
202*6777b538SAndroid Build Coastguard Worker
203*6777b538SAndroid Build Coastguard Worker* The URLRequest asks the URLRequestJobFactory to create a URLRequestJob,
204*6777b538SAndroid Build Coastguard Workerand gets a URLRequestHttpJob.
205*6777b538SAndroid Build Coastguard Worker* The URLRequestHttpJob asks the HttpCache to create an HttpTransaction, and
206*6777b538SAndroid Build Coastguard Workergets an HttpCache::Transaction, assuming caching is enabled.
207*6777b538SAndroid Build Coastguard Worker* The HttpCache::Transaction sees there's no cache entry for the request,
208*6777b538SAndroid Build Coastguard Workerand creates an HttpNetworkTransaction.
209*6777b538SAndroid Build Coastguard Worker* The HttpNetworkTransaction calls into the HttpStreamFactory to request an
210*6777b538SAndroid Build Coastguard WorkerHttpStream.
211*6777b538SAndroid Build Coastguard Worker
212*6777b538SAndroid Build Coastguard WorkerThe URLRequest then calls into the URLRequestJobFactory to create a
213*6777b538SAndroid Build Coastguard WorkerURLRequestHttpJob, a subclass of URLRequestJob, and then starts it
214*6777b538SAndroid Build Coastguard Worker(historically, non-network URL schemes were also disptched through the
215*6777b538SAndroid Build Coastguard Workernetwork stack, so there were a variety of job types.) The
216*6777b538SAndroid Build Coastguard WorkerURLRequestHttpJob attaches cookies to the request, if needed. Whether or
217*6777b538SAndroid Build Coastguard Workernot SameSite cookies are attached depends on the IsolationInfo's
218*6777b538SAndroid Build Coastguard WorkerSiteForCookies, the URL, and the URLRequest's request_initiator field.
219*6777b538SAndroid Build Coastguard Worker
220*6777b538SAndroid Build Coastguard WorkerThe URLRequestHttpJob calls into the HttpCache to create an
221*6777b538SAndroid Build Coastguard WorkerHttpCache::Transaction. The cache checks for an entry with the same URL
222*6777b538SAndroid Build Coastguard Workerand NetworkIsolationKey. If there's no matching entry, the
223*6777b538SAndroid Build Coastguard WorkerHttpCache::Transaction will call into the HttpNetworkLayer to create an
224*6777b538SAndroid Build Coastguard WorkerHttpNetworkTransaction, and transparently wrap it. The HttpNetworkTransaction
225*6777b538SAndroid Build Coastguard Workerthen calls into the HttpStreamFactory to request an HttpStream to the server.
226*6777b538SAndroid Build Coastguard Worker
227*6777b538SAndroid Build Coastguard Worker### Create an HttpStream
228*6777b538SAndroid Build Coastguard Worker
229*6777b538SAndroid Build Coastguard WorkerSummary:
230*6777b538SAndroid Build Coastguard Worker
231*6777b538SAndroid Build Coastguard Worker* HttpStreamFactory creates an HttpStreamFactory::Job.
232*6777b538SAndroid Build Coastguard Worker* HttpStreamFactory::Job calls into the TransportClientSocketPool to
233*6777b538SAndroid Build Coastguard Workerpopulate an ClientSocketHandle.
234*6777b538SAndroid Build Coastguard Worker* TransportClientSocketPool has no idle sockets, so it creates a
235*6777b538SAndroid Build Coastguard WorkerTransportConnectJob and starts it.
236*6777b538SAndroid Build Coastguard Worker* TransportConnectJob creates a StreamSocket and establishes a connection.
237*6777b538SAndroid Build Coastguard Worker* TransportClientSocketPool puts the StreamSocket in the ClientSocketHandle,
238*6777b538SAndroid Build Coastguard Workerand calls into HttpStreamFactory::Job.
239*6777b538SAndroid Build Coastguard Worker* HttpStreamFactory::Job creates an HttpBasicStream, which takes
240*6777b538SAndroid Build Coastguard Workerownership of the ClientSocketHandle.
241*6777b538SAndroid Build Coastguard Worker* It returns the HttpBasicStream to the HttpNetworkTransaction.
242*6777b538SAndroid Build Coastguard Worker
243*6777b538SAndroid Build Coastguard WorkerThe HttpStreamFactory::Job creates a ClientSocketHandle to hold a socket,
244*6777b538SAndroid Build Coastguard Workeronce connected, and passes it into the ClientSocketPoolManager. The
245*6777b538SAndroid Build Coastguard WorkerClientSocketPoolManager assembles the TransportSocketParams needed to
246*6777b538SAndroid Build Coastguard Workerestablish the connection and creates a group name ("host:port") used to
247*6777b538SAndroid Build Coastguard Workeridentify sockets that can be used interchangeably.
248*6777b538SAndroid Build Coastguard Worker
249*6777b538SAndroid Build Coastguard WorkerThe ClientSocketPoolManager directs the request to the
250*6777b538SAndroid Build Coastguard WorkerTransportClientSocketPool, since there's no proxy and it's an HTTP request. The
251*6777b538SAndroid Build Coastguard Workerrequest is forwarded to the pool's ClientSocketPoolBase<TransportSocketParams>'s
252*6777b538SAndroid Build Coastguard WorkerClientSocketPoolBaseHelper. If there isn't already an idle connection, and there
253*6777b538SAndroid Build Coastguard Workerare available socket slots, the ClientSocketPoolBaseHelper will create a new
254*6777b538SAndroid Build Coastguard WorkerTransportConnectJob using the aforementioned params object. This Job will do the
255*6777b538SAndroid Build Coastguard Workeractual DNS lookup by calling into the HostResolverImpl, if needed, and then
256*6777b538SAndroid Build Coastguard Workerfinally establishes a connection.
257*6777b538SAndroid Build Coastguard Worker
258*6777b538SAndroid Build Coastguard WorkerOnce the socket is connected, ownership of the socket is passed to the
259*6777b538SAndroid Build Coastguard WorkerClientSocketHandle. The HttpStreamFactory::Job is then informed the
260*6777b538SAndroid Build Coastguard Workerconnection attempt succeeded, and it then creates an HttpBasicStream, which
261*6777b538SAndroid Build Coastguard Workertakes ownership of the ClientSocketHandle. It then passes ownership of the
262*6777b538SAndroid Build Coastguard WorkerHttpBasicStream back to the HttpNetworkTransaction.
263*6777b538SAndroid Build Coastguard Worker
264*6777b538SAndroid Build Coastguard Worker### Send request and read the response headers
265*6777b538SAndroid Build Coastguard Worker
266*6777b538SAndroid Build Coastguard WorkerSummary:
267*6777b538SAndroid Build Coastguard Worker
268*6777b538SAndroid Build Coastguard Worker* HttpNetworkTransaction gives the request headers to the HttpBasicStream,
269*6777b538SAndroid Build Coastguard Workerand tells it to start the request.
270*6777b538SAndroid Build Coastguard Worker* HttpBasicStream sends the request, and waits for the response.
271*6777b538SAndroid Build Coastguard Worker* The HttpBasicStream sends the response headers back to the
272*6777b538SAndroid Build Coastguard WorkerHttpNetworkTransaction.
273*6777b538SAndroid Build Coastguard Worker* The response headers are sent up through the URLRequest, to the
274*6777b538SAndroid Build Coastguard Workernetwork::URLLoader.
275*6777b538SAndroid Build Coastguard Worker* They're then sent to the network::mojom::URLLoaderClient via Mojo.
276*6777b538SAndroid Build Coastguard Worker
277*6777b538SAndroid Build Coastguard WorkerThe HttpNetworkTransaction passes the request headers to the HttpBasicStream,
278*6777b538SAndroid Build Coastguard Workerwhich uses an HttpStreamParser to (finally) format the request headers and body
279*6777b538SAndroid Build Coastguard Worker(if present) and send them to the server.
280*6777b538SAndroid Build Coastguard Worker
281*6777b538SAndroid Build Coastguard WorkerThe HttpStreamParser waits to receive the response and then parses the HTTP/1.x
282*6777b538SAndroid Build Coastguard Workerresponse headers, and then passes them up through both the
283*6777b538SAndroid Build Coastguard WorkerHttpNetworkTransaction and HttpCache::Transaction to the URLRequestHttpJob. The
284*6777b538SAndroid Build Coastguard WorkerURLRequestHttpJob saves any cookies, if needed, and then passes the headers up
285*6777b538SAndroid Build Coastguard Workerto the URLRequest and on to the network::URLLoader, which sends the data over
286*6777b538SAndroid Build Coastguard Workera Mojo pipe to the network::mojom::URLLoaderClient, passed in to the URLLoader
287*6777b538SAndroid Build Coastguard Workerwhen it was created.
288*6777b538SAndroid Build Coastguard Worker
289*6777b538SAndroid Build Coastguard Worker### Response body is read
290*6777b538SAndroid Build Coastguard Worker
291*6777b538SAndroid Build Coastguard WorkerSummary:
292*6777b538SAndroid Build Coastguard Worker
293*6777b538SAndroid Build Coastguard Worker* network::URLLoader creates a raw Mojo data pipe, and passes one end to the
294*6777b538SAndroid Build Coastguard Workernetwork::mojom::URLLoaderClient.
295*6777b538SAndroid Build Coastguard Worker* The URLLoader requests shared memory buffer from the Mojo data pipe.
296*6777b538SAndroid Build Coastguard Worker* The URLLoader tells the URLRequest to write to the memory buffer, and tells
297*6777b538SAndroid Build Coastguard Workerthe pipe when data has been written to the buffer.
298*6777b538SAndroid Build Coastguard Worker* The last two steps repeat until the request is complete.
299*6777b538SAndroid Build Coastguard Worker
300*6777b538SAndroid Build Coastguard WorkerWithout waiting to hear back from the network::mojom::URLLoaderClient, the
301*6777b538SAndroid Build Coastguard Workernetwork::URLLoader allocates a raw mojo data pipe, and passes the client the
302*6777b538SAndroid Build Coastguard Workerread end of the pipe. The URLLoader then grabs an IPC buffer from the pipe,
303*6777b538SAndroid Build Coastguard Workerand passes a 64KB body read request down through the URLRequest all the way
304*6777b538SAndroid Build Coastguard Workerdown to the HttpStreamParser. Once some data is read, possibly less than 64KB,
305*6777b538SAndroid Build Coastguard Workerthe number of bytes read makes its way back to the URLLoader, which then tells
306*6777b538SAndroid Build Coastguard Workerthe Mojo pipe the read was complete, and then requests another buffer from the
307*6777b538SAndroid Build Coastguard Workerpipe, to continue writing data to. The pipe may apply back pressure, to limit
308*6777b538SAndroid Build Coastguard Workerthe amount of unconsumed data that can be in shared memory buffers at once.
309*6777b538SAndroid Build Coastguard WorkerThis process repeats until the response body is completely read.
310*6777b538SAndroid Build Coastguard Worker
311*6777b538SAndroid Build Coastguard Worker### URLRequest is destroyed
312*6777b538SAndroid Build Coastguard Worker
313*6777b538SAndroid Build Coastguard WorkerSummary:
314*6777b538SAndroid Build Coastguard Worker
315*6777b538SAndroid Build Coastguard Worker* When complete, the network::URLLoaderFactory deletes the network::URLLoader,
316*6777b538SAndroid Build Coastguard Workerwhich deletes the URLRequest.
317*6777b538SAndroid Build Coastguard Worker* During destruction, the HttpNetworkTransaction determines if the socket is
318*6777b538SAndroid Build Coastguard Workerreusable, and if so, tells the HttpBasicStream to return it to the socket pool.
319*6777b538SAndroid Build Coastguard Worker
320*6777b538SAndroid Build Coastguard WorkerWhen the URLRequest informs the network::URLLoader the request is complete, the
321*6777b538SAndroid Build Coastguard WorkerURLLoader passes the message along to the network::mojom::URLLoaderClient, over
322*6777b538SAndroid Build Coastguard Workerits Mojo pipe, before telling the URLLoaderFactory to destroy the URLLoader,
323*6777b538SAndroid Build Coastguard Workerwhich results in destroying the URLRequest and closing all Mojo pipes related to
324*6777b538SAndroid Build Coastguard Workerthe request.
325*6777b538SAndroid Build Coastguard Worker
326*6777b538SAndroid Build Coastguard WorkerWhen the HttpNetworkTransaction is being torn down, it figures out if the
327*6777b538SAndroid Build Coastguard Workersocket is reusable. If not, it tells the HttpBasicStream to close the socket.
328*6777b538SAndroid Build Coastguard WorkerEither way, the ClientSocketHandle returns the socket is then returned to the
329*6777b538SAndroid Build Coastguard Workersocket pool, either for reuse or so the socket pool knows it has another free
330*6777b538SAndroid Build Coastguard Workersocket slot.
331*6777b538SAndroid Build Coastguard Worker
332*6777b538SAndroid Build Coastguard Worker### Object Relationships and Ownership
333*6777b538SAndroid Build Coastguard Worker
334*6777b538SAndroid Build Coastguard WorkerA sample of the object relationships involved in the above process is
335*6777b538SAndroid Build Coastguard Workerdiagramed here:
336*6777b538SAndroid Build Coastguard Worker
337*6777b538SAndroid Build Coastguard Worker![Object Relationship Diagram for URLRequest lifetime](url_request.png)
338*6777b538SAndroid Build Coastguard Worker
339*6777b538SAndroid Build Coastguard WorkerThere are a couple of points in the above diagram that do not come
340*6777b538SAndroid Build Coastguard Workerclear visually:
341*6777b538SAndroid Build Coastguard Worker
342*6777b538SAndroid Build Coastguard Worker* The method that generates the filter chain that is hung off the
343*6777b538SAndroid Build Coastguard Worker  URLRequestJob is declared on URLRequestJob, but the only current
344*6777b538SAndroid Build Coastguard Worker  implementation of it is on URLRequestHttpJob, so the generation is
345*6777b538SAndroid Build Coastguard Worker  shown as happening from that class.
346*6777b538SAndroid Build Coastguard Worker* HttpTransactions of different types are layered; i.e. a
347*6777b538SAndroid Build Coastguard Worker  HttpCache::Transaction contains a pointer to an HttpTransaction, but
348*6777b538SAndroid Build Coastguard Worker  that pointed-to HttpTransaction generally is an
349*6777b538SAndroid Build Coastguard Worker  HttpNetworkTransaction.
350*6777b538SAndroid Build Coastguard Worker
351*6777b538SAndroid Build Coastguard Worker# Additional Topics
352*6777b538SAndroid Build Coastguard Worker
353*6777b538SAndroid Build Coastguard Worker## HTTP Cache
354*6777b538SAndroid Build Coastguard Worker
355*6777b538SAndroid Build Coastguard WorkerThe HttpCache::Transaction sits between the URLRequestHttpJob and the
356*6777b538SAndroid Build Coastguard WorkerHttpNetworkTransaction, and implements the HttpTransaction interface, just like
357*6777b538SAndroid Build Coastguard Workerthe HttpNetworkTransaction. The HttpCache::Transaction checks if a request can
358*6777b538SAndroid Build Coastguard Workerbe served out of the cache. If a request needs to be revalidated, it handles
359*6777b538SAndroid Build Coastguard Workersending a conditional revalidation request over the network. It may also break a
360*6777b538SAndroid Build Coastguard Workerrange request into multiple cached and non-cached contiguous chunks, and may
361*6777b538SAndroid Build Coastguard Workerissue multiple network requests for a single range URLRequest.
362*6777b538SAndroid Build Coastguard Worker
363*6777b538SAndroid Build Coastguard WorkerThe HttpCache::Transaction uses one of three disk_cache::Backends to actually
364*6777b538SAndroid Build Coastguard Workerstore the cache's index and files: The in memory backend, the blockfile cache
365*6777b538SAndroid Build Coastguard Workerbackend, and the simple cache backend. The first is used in incognito. The
366*6777b538SAndroid Build Coastguard Workerlatter two are both stored on disk, and are used on different platforms.
367*6777b538SAndroid Build Coastguard Worker
368*6777b538SAndroid Build Coastguard WorkerOne important detail is that it has a read/write lock for each URL. The lock
369*6777b538SAndroid Build Coastguard Workertechnically allows multiple reads at once, but since an HttpCache::Transaction
370*6777b538SAndroid Build Coastguard Workeralways grabs the lock for writing and reading before downgrading it to a read
371*6777b538SAndroid Build Coastguard Workeronly lock, all requests for the same URL are effectively done serially. The
372*6777b538SAndroid Build Coastguard Workerrenderer process merges requests for the same URL in many cases, which mitigates
373*6777b538SAndroid Build Coastguard Workerthis problem to some extent.
374*6777b538SAndroid Build Coastguard Worker
375*6777b538SAndroid Build Coastguard WorkerIt's also worth noting that each renderer process also has its own in-memory
376*6777b538SAndroid Build Coastguard Workercache, which has no relation to the cache implemented in net/, which lives in
377*6777b538SAndroid Build Coastguard Workerthe network service.
378*6777b538SAndroid Build Coastguard Worker
379*6777b538SAndroid Build Coastguard Worker## Cancellation
380*6777b538SAndroid Build Coastguard Worker
381*6777b538SAndroid Build Coastguard WorkerA consumer can cancel a request at any time by deleting the
382*6777b538SAndroid Build Coastguard Workernetwork::mojom::URLLoader pipe used by the request. This will cause the
383*6777b538SAndroid Build Coastguard Workernetwork::URLLoader to destroy itself and its URLRequest.
384*6777b538SAndroid Build Coastguard Worker
385*6777b538SAndroid Build Coastguard WorkerWhen an HttpNetworkTransaction for a cancelled request is being torn down, it
386*6777b538SAndroid Build Coastguard Workerfigures out if the socket the HttpStream owns can potentially be reused, based
387*6777b538SAndroid Build Coastguard Workeron the protocol (HTTP / HTTP/2 / QUIC) and any received headers. If the socket
388*6777b538SAndroid Build Coastguard Workerpotentially can be reused, an HttpResponseBodyDrainer is created to try and
389*6777b538SAndroid Build Coastguard Workerread any remaining body bytes of the HttpStream, if any, before returning the
390*6777b538SAndroid Build Coastguard Workersocket to the SocketPool. If this takes too long, or there's an error, the
391*6777b538SAndroid Build Coastguard Workersocket is closed instead. Since this all happens at the layer below the cache,
392*6777b538SAndroid Build Coastguard Workerany drained bytes are not written to the cache, and as far as the cache layer is
393*6777b538SAndroid Build Coastguard Workerconcerned, it only has a partial response.
394*6777b538SAndroid Build Coastguard Worker
395*6777b538SAndroid Build Coastguard Worker## Redirects
396*6777b538SAndroid Build Coastguard Worker
397*6777b538SAndroid Build Coastguard WorkerThe URLRequestHttpJob checks if headers indicate a redirect when it receives
398*6777b538SAndroid Build Coastguard Workerthem from the next layer down (typically the HttpCache::Transaction). If they
399*6777b538SAndroid Build Coastguard Workerindicate a redirect, it tells the cache the response is complete, ignoring the
400*6777b538SAndroid Build Coastguard Workerbody, so the cache only has the headers. The cache then treats it as a complete
401*6777b538SAndroid Build Coastguard Workerentry, even if the headers indicated there will be a body.
402*6777b538SAndroid Build Coastguard Worker
403*6777b538SAndroid Build Coastguard WorkerThe URLRequestHttpJob then checks with the URLRequest if the redirect should be
404*6777b538SAndroid Build Coastguard Workerfollowed. The URLRequest then informs the network::URLLoader about the redirect,
405*6777b538SAndroid Build Coastguard Workerwhich passes information about the redirect to network::mojom::URLLoaderClient,
406*6777b538SAndroid Build Coastguard Workerin the consumer process. Whatever issued the original request then checks
407*6777b538SAndroid Build Coastguard Workerif the redirect should be followed.
408*6777b538SAndroid Build Coastguard Worker
409*6777b538SAndroid Build Coastguard WorkerIf the redirect should be followed, the URLLoaderClient calls back into the
410*6777b538SAndroid Build Coastguard WorkerURLLoader over the network::mojom::URLLoader Mojo interface, which tells the
411*6777b538SAndroid Build Coastguard WorkerURLRequest to follow the redirect. The URLRequest then creates a new
412*6777b538SAndroid Build Coastguard WorkerURLRequestJob to send the new request. If the URLLoaderClient chooses to
413*6777b538SAndroid Build Coastguard Workercancel the request instead, it can delete the network::mojom::URLLoader
414*6777b538SAndroid Build Coastguard Workerpipe, just like the cancellation case discussed above. In either case, the
415*6777b538SAndroid Build Coastguard Workerold HttpTransaction is destroyed, and the HttpNetworkTransaction attempts to
416*6777b538SAndroid Build Coastguard Workerdrain the socket for reuse, as discussed in the previous section.
417*6777b538SAndroid Build Coastguard Worker
418*6777b538SAndroid Build Coastguard WorkerIn some cases, the consumer may choose to handle a redirect itself, like
419*6777b538SAndroid Build Coastguard Workerpassing off the redirect to a ServiceWorker. In this case, the consumer cancels
420*6777b538SAndroid Build Coastguard Workerthe request and then calls into some other network::mojom::URLLoaderFactory
421*6777b538SAndroid Build Coastguard Workerwith the new URL to continue the request.
422*6777b538SAndroid Build Coastguard Worker
423*6777b538SAndroid Build Coastguard Worker## Filters (gzip, deflate, brotli, etc)
424*6777b538SAndroid Build Coastguard Worker
425*6777b538SAndroid Build Coastguard WorkerWhen the URLRequestHttpJob receives headers, it sends a list of all
426*6777b538SAndroid Build Coastguard WorkerContent-Encoding values to Filter::Factory, which creates a (possibly empty)
427*6777b538SAndroid Build Coastguard Workerchain of filters. As body bytes are received, they're passed through the
428*6777b538SAndroid Build Coastguard Workerfilters at the URLRequestJob layer and the decoded bytes are passed back to the
429*6777b538SAndroid Build Coastguard WorkerURLRequest::Delegate.
430*6777b538SAndroid Build Coastguard Worker
431*6777b538SAndroid Build Coastguard WorkerSince this is done above the cache layer, the cache stores the responses prior
432*6777b538SAndroid Build Coastguard Workerto decompression. As a result, if files aren't compressed over the wire, they
433*6777b538SAndroid Build Coastguard Workeraren't compressed in the cache, either.
434*6777b538SAndroid Build Coastguard Worker
435*6777b538SAndroid Build Coastguard Worker## Socket Pools
436*6777b538SAndroid Build Coastguard Worker
437*6777b538SAndroid Build Coastguard WorkerThe ClientSocketPoolManager is responsible for assembling the parameters needed
438*6777b538SAndroid Build Coastguard Workerto connect a socket, and then sending the request to the right socket pool.
439*6777b538SAndroid Build Coastguard WorkerEach socket request sent to a socket pool comes with a socket params object, a
440*6777b538SAndroid Build Coastguard WorkerClientSocketHandle, and a "group name". The params object contains all the
441*6777b538SAndroid Build Coastguard Workerinformation a ConnectJob needs to create a connection of a given type, and
442*6777b538SAndroid Build Coastguard Workerdifferent types of socket pools take different params types. The
443*6777b538SAndroid Build Coastguard WorkerClientSocketHandle will take temporary ownership of a connected socket and
444*6777b538SAndroid Build Coastguard Workerreturn it to the socket pool when done. All connections with the same group name
445*6777b538SAndroid Build Coastguard Workerin the same pool can be used to service the same connection requests, so it
446*6777b538SAndroid Build Coastguard Workerconsists of host, port, protocol, and whether "privacy mode" is enabled for
447*6777b538SAndroid Build Coastguard Workersockets in the goup.
448*6777b538SAndroid Build Coastguard Worker
449*6777b538SAndroid Build Coastguard WorkerAll socket pool classes derive from the ClientSocketPoolBase<SocketParamType>.
450*6777b538SAndroid Build Coastguard WorkerThe ClientSocketPoolBase handles managing sockets - which requests to create
451*6777b538SAndroid Build Coastguard Workersockets for, which requests get connected sockets first, which sockets belong
452*6777b538SAndroid Build Coastguard Workerto which groups, connection limits per group, keeping track of and closing idle
453*6777b538SAndroid Build Coastguard Workersockets, etc. Each ClientSocketPoolBase subclass has its own ConnectJob type,
454*6777b538SAndroid Build Coastguard Workerwhich establishes a connection using the socket params, before the pool hands
455*6777b538SAndroid Build Coastguard Workerout the connected socket.
456*6777b538SAndroid Build Coastguard Worker
457*6777b538SAndroid Build Coastguard Worker### Socket Pool Layering
458*6777b538SAndroid Build Coastguard Worker
459*6777b538SAndroid Build Coastguard WorkerSome socket pools are layered on top other socket pools. This is done when a
460*6777b538SAndroid Build Coastguard Worker"socket" in a higher layer needs to establish a connection in a lower level
461*6777b538SAndroid Build Coastguard Workerpool and then take ownership of it as part of its connection process. For
462*6777b538SAndroid Build Coastguard Workerexample, each socket in the SSLClientSocketPool is layered on top of a socket
463*6777b538SAndroid Build Coastguard Workerin the TransportClientSocketPool. There are a couple additional complexities
464*6777b538SAndroid Build Coastguard Workerhere.
465*6777b538SAndroid Build Coastguard Worker
466*6777b538SAndroid Build Coastguard WorkerFrom the perspective of the lower layer pool, all of its sockets that a higher
467*6777b538SAndroid Build Coastguard Workerlayer pools owns are actively in use, even when the higher layer pool considers
468*6777b538SAndroid Build Coastguard Workerthem idle. As a result, when a lower layer pool is at its connection limit and
469*6777b538SAndroid Build Coastguard Workerneeds to make a new connection, it will ask any higher layer pools to close an
470*6777b538SAndroid Build Coastguard Workeridle connection if they have one, so it can make a new connection.
471*6777b538SAndroid Build Coastguard Worker
472*6777b538SAndroid Build Coastguard WorkerSince sockets in the higher layer pool are also in a group in the lower layer
473*6777b538SAndroid Build Coastguard Workerpool, they must have their own distinct group name. This is needed so that, for
474*6777b538SAndroid Build Coastguard Workerinstance, SSL and HTTP connections won't be grouped together in the
475*6777b538SAndroid Build Coastguard WorkerTcpClientSocketPool, which the SSLClientSocketPool sits on top of.
476*6777b538SAndroid Build Coastguard Worker
477*6777b538SAndroid Build Coastguard Worker### Socket Pool Class Relationships
478*6777b538SAndroid Build Coastguard Worker
479*6777b538SAndroid Build Coastguard WorkerThe relationships between the important classes in the socket pools is
480*6777b538SAndroid Build Coastguard Workershown diagrammatically for the lowest layer socket pool
481*6777b538SAndroid Build Coastguard Worker(TransportSocketPool) below.
482*6777b538SAndroid Build Coastguard Worker
483*6777b538SAndroid Build Coastguard Worker![Object Relationship Diagram for Socket Pools](pools.png)
484*6777b538SAndroid Build Coastguard Worker
485*6777b538SAndroid Build Coastguard WorkerThe ClientSocketPoolBase is a template class templatized on the class
486*6777b538SAndroid Build Coastguard Workercontaining the parameters for the appropriate type of socket (in this
487*6777b538SAndroid Build Coastguard Workercase TransportSocketParams). It contains a pointer to the
488*6777b538SAndroid Build Coastguard WorkerClientSocketPoolBaseHelper, which contains all the type-independent
489*6777b538SAndroid Build Coastguard Workermachinery of the socket pool.
490*6777b538SAndroid Build Coastguard Worker
491*6777b538SAndroid Build Coastguard WorkerWhen socket pools are initialized, they in turn initialize their
492*6777b538SAndroid Build Coastguard Workertemplatized ClientSocketPoolBase member with an object with which it
493*6777b538SAndroid Build Coastguard Workershould create connect jobs. That object must derive from
494*6777b538SAndroid Build Coastguard WorkerClientSocketPoolBase::ConnectJobFactory templatized by the same type
495*6777b538SAndroid Build Coastguard Workeras the ClientSocketPoolBase. (In the case of the diagram above, that
496*6777b538SAndroid Build Coastguard Workerobject is a TransportConnectJobFactory, which derives from
497*6777b538SAndroid Build Coastguard WorkerClientSocketPoolBase::ConnectJobFactory&lt;TransportSocketParams&gt;.)
498*6777b538SAndroid Build Coastguard WorkerInternally, that object is wrapped in a type-unsafe wrapper
499*6777b538SAndroid Build Coastguard Worker(ClientSocketPoolBase::ConnectJobFactoryAdaptor) so that it can be
500*6777b538SAndroid Build Coastguard Workerpassed to the initialization of the ClientSocketPoolBaseHelper. This
501*6777b538SAndroid Build Coastguard Workerallows the helper to create connect jobs while preserving a type-safe
502*6777b538SAndroid Build Coastguard WorkerAPI to the initialization of the socket pool.
503*6777b538SAndroid Build Coastguard Worker
504*6777b538SAndroid Build Coastguard Worker### SSL
505*6777b538SAndroid Build Coastguard Worker
506*6777b538SAndroid Build Coastguard WorkerWhen an SSL connection is needed, the ClientSocketPoolManager assembles the
507*6777b538SAndroid Build Coastguard Workerparameters needed both to connect the TCP socket and establish an SSL
508*6777b538SAndroid Build Coastguard Workerconnection. It then passes them to the SSLClientSocketPool, which creates
509*6777b538SAndroid Build Coastguard Workeran SSLConnectJob using them. The SSLConnectJob's first step is to call into the
510*6777b538SAndroid Build Coastguard WorkerTransportSocketPool to establish a TCP connection.
511*6777b538SAndroid Build Coastguard Worker
512*6777b538SAndroid Build Coastguard WorkerOnce a connection is established by the lower layered pool, the SSLConnectJob
513*6777b538SAndroid Build Coastguard Workerthen starts SSL negotiation. Once that's done, the SSL socket is passed back to
514*6777b538SAndroid Build Coastguard Workerthe HttpStreamFactory::Job that initiated the request, and things proceed
515*6777b538SAndroid Build Coastguard Workerjust as with HTTP. When complete, the socket is returned to the
516*6777b538SAndroid Build Coastguard WorkerSSLClientSocketPool.
517*6777b538SAndroid Build Coastguard Worker
518*6777b538SAndroid Build Coastguard Worker## Proxies
519*6777b538SAndroid Build Coastguard Worker
520*6777b538SAndroid Build Coastguard WorkerEach proxy has its own completely independent set of socket pools. They have
521*6777b538SAndroid Build Coastguard Workertheir own exclusive TransportSocketPool, their own protocol-specific pool above
522*6777b538SAndroid Build Coastguard Workerit, and their own SSLSocketPool above that. HTTPS proxies also have a second
523*6777b538SAndroid Build Coastguard WorkerSSLSocketPool between the the HttpProxyClientSocketPool and the
524*6777b538SAndroid Build Coastguard WorkerTransportSocketPool, since they can talk SSL to both the proxy and the
525*6777b538SAndroid Build Coastguard Workerdestination server, layered on top of each other.
526*6777b538SAndroid Build Coastguard Worker
527*6777b538SAndroid Build Coastguard WorkerThe first step the HttpStreamFactory::Job performs, just before calling
528*6777b538SAndroid Build Coastguard Workerinto the ClientSocketPoolManager to create a socket, is to pass the URL to the
529*6777b538SAndroid Build Coastguard WorkerProxy service to get an ordered list of proxies (if any) that should be tried
530*6777b538SAndroid Build Coastguard Workerfor that URL. Then when the ClientSocketPoolManager tries to get a socket for
531*6777b538SAndroid Build Coastguard Workerthe Job, it uses that list of proxies to direct the request to the right socket
532*6777b538SAndroid Build Coastguard Workerpool.
533*6777b538SAndroid Build Coastguard Worker
534*6777b538SAndroid Build Coastguard Worker## Alternate Protocols
535*6777b538SAndroid Build Coastguard Worker
536*6777b538SAndroid Build Coastguard Worker### HTTP/2 (Formerly SPDY)
537*6777b538SAndroid Build Coastguard Worker
538*6777b538SAndroid Build Coastguard WorkerHTTP/2 negotation is performed as part of the SSL handshake, so when
539*6777b538SAndroid Build Coastguard WorkerHttpStreamFactory::Job gets a socket, it may have HTTP/2 negotiated over it
540*6777b538SAndroid Build Coastguard Workeras well. When it gets a socket with HTTP/2 negotiated as well, the Job creates a
541*6777b538SAndroid Build Coastguard WorkerSpdySession using the socket and a SpdyHttpStream on top of the SpdySession.
542*6777b538SAndroid Build Coastguard WorkerThe SpdyHttpStream will be passed to the HttpNetworkTransaction, which drives
543*6777b538SAndroid Build Coastguard Workerthe stream as usual.
544*6777b538SAndroid Build Coastguard Worker
545*6777b538SAndroid Build Coastguard WorkerThe SpdySession will be shared with other Jobs connecting to the same server,
546*6777b538SAndroid Build Coastguard Workerand future Jobs will find the SpdySession before they try to create a
547*6777b538SAndroid Build Coastguard Workerconnection. HttpServerProperties also tracks which servers supported HTTP/2 when
548*6777b538SAndroid Build Coastguard Workerwe last talked to them. We only try to establish a single connection to servers
549*6777b538SAndroid Build Coastguard Workerwe think speak HTTP/2 when multiple HttpStreamFactory::Jobs are trying to
550*6777b538SAndroid Build Coastguard Workerconnect to them, to avoid wasting resources.
551*6777b538SAndroid Build Coastguard Worker
552*6777b538SAndroid Build Coastguard Worker### QUIC
553*6777b538SAndroid Build Coastguard Worker
554*6777b538SAndroid Build Coastguard WorkerQUIC works quite a bit differently from HTTP/2. Servers advertise QUIC support
555*6777b538SAndroid Build Coastguard Workerwith an "Alternate-Protocol" HTTP header in their responses.
556*6777b538SAndroid Build Coastguard WorkerHttpServerProperties then tracks servers that have advertised QUIC support.
557*6777b538SAndroid Build Coastguard Worker
558*6777b538SAndroid Build Coastguard WorkerWhen a new request comes in to HttpStreamFactory for a connection to a
559*6777b538SAndroid Build Coastguard Workerserver that has advertised QUIC support in the past, it will create a second
560*6777b538SAndroid Build Coastguard WorkerHttpStreamFactory::Job for QUIC, which returns an QuicHttpStream on success.
561*6777b538SAndroid Build Coastguard WorkerThe two Jobs (one for QUIC, one for all versions of HTTP) will be raced against
562*6777b538SAndroid Build Coastguard Workereach other, and whichever successfully creates an HttpStream first will be used.
563*6777b538SAndroid Build Coastguard Worker
564*6777b538SAndroid Build Coastguard WorkerAs with HTTP/2, once a QUIC connection is established, it will be shared with
565*6777b538SAndroid Build Coastguard Workerother Jobs connecting to the same server, and future Jobs will just reuse the
566*6777b538SAndroid Build Coastguard Workerexisting QUIC session.
567*6777b538SAndroid Build Coastguard Worker
568*6777b538SAndroid Build Coastguard Worker## Prioritization
569*6777b538SAndroid Build Coastguard Worker
570*6777b538SAndroid Build Coastguard WorkerURLRequests are assigned a priority on creation. It only comes into play in
571*6777b538SAndroid Build Coastguard Workera couple places:
572*6777b538SAndroid Build Coastguard Worker
573*6777b538SAndroid Build Coastguard Worker* The ResourceScheduler lives outside net/, and in some cases, delays starting
574*6777b538SAndroid Build Coastguard Workerlow priority requests on a per-tab basis.
575*6777b538SAndroid Build Coastguard Worker* DNS lookups are initiated based on the highest priority request for a lookup.
576*6777b538SAndroid Build Coastguard Worker* Socket pools hand out and create sockets based on prioritization. However,
577*6777b538SAndroid Build Coastguard Workerwhen a socket becomes idle, it will be assigned to the highest priority request
578*6777b538SAndroid Build Coastguard Workerfor the server it's connected to, even if there's a higher priority request to
579*6777b538SAndroid Build Coastguard Workeranother server that's waiting on a free socket slot.
580*6777b538SAndroid Build Coastguard Worker* HTTP/2 and QUIC both support sending priorities over-the-wire.
581*6777b538SAndroid Build Coastguard Worker
582*6777b538SAndroid Build Coastguard WorkerAt the socket pool layer, sockets are only assigned to socket requests once the
583*6777b538SAndroid Build Coastguard Workersocket is connected and SSL is negotiated, if needed. This is done so that if
584*6777b538SAndroid Build Coastguard Workera higher priority request for a group reaches the socket pool before a
585*6777b538SAndroid Build Coastguard Workerconnection is established, the first usable connection goes to the highest
586*6777b538SAndroid Build Coastguard Workerpriority socket request.
587*6777b538SAndroid Build Coastguard Worker
588*6777b538SAndroid Build Coastguard Worker## Non-HTTP Schemes
589*6777b538SAndroid Build Coastguard Worker
590*6777b538SAndroid Build Coastguard WorkerWebSockets requests (wss:// and ws://) start as HTTP requests with an HTTP
591*6777b538SAndroid Build Coastguard Workerupgrade header. Once the handshake completes successfully, the connection
592*6777b538SAndroid Build Coastguard Workeris used as a full-duplex communication channel to the server for WebSocket
593*6777b538SAndroid Build Coastguard Workerframes, rather than to receive an HTTP response body. WebSockets have their
594*6777b538SAndroid Build Coastguard Workerown Mojo interfaces and //net classes, but internally they reuse the full
595*6777b538SAndroid Build Coastguard WorkerURLRequest machinery up to the point headers are received from the server.
596*6777b538SAndroid Build Coastguard WorkerThen the connection is handed off to the WebSocket code to manage.
597*6777b538SAndroid Build Coastguard Worker
598*6777b538SAndroid Build Coastguard WorkerOther schemes typically have their own network::mojom::URLLoaderFactory that
599*6777b538SAndroid Build Coastguard Workeris not part of the network service. Standard schemes like file:// and blob://
600*6777b538SAndroid Build Coastguard Workerare handled by the content layer and its dependencies
601*6777b538SAndroid Build Coastguard Worker(content::FileURLLoaderFactory and storage::BlobURLLoaderFactory, respectively,
602*6777b538SAndroid Build Coastguard Workerfor those two schemes). Chrome-specific schemes, like externalfile:// and
603*6777b538SAndroid Build Coastguard Workerchrome-extension:// are often handled by a URLLoaderFactory in the chrome layer,
604*6777b538SAndroid Build Coastguard Workerthough chrome:// itself is actually handled in //content.
605*6777b538SAndroid Build Coastguard Worker
606*6777b538SAndroid Build Coastguard Workerdata:// URLs are handled a bit differently from other schemes. If a renderer
607*6777b538SAndroid Build Coastguard Workerprocess requests a data:// subresource, the renderer typically decodes it
608*6777b538SAndroid Build Coastguard Workerinternally, as sending it to an out-of-process URLLoader would be inefficient.
609*6777b538SAndroid Build Coastguard WorkerNavigations are a bit different. To navigate to a URL, the browser process
610*6777b538SAndroid Build Coastguard Workercreates a URLLoader and passes it over to a renderer process. So in the
611*6777b538SAndroid Build Coastguard Workercase of a navigation to a data:// URL, a URLLoader is created using a
612*6777b538SAndroid Build Coastguard Workercontent::DataURLLoaderFactory that lives in the browser process, and then a
613*6777b538SAndroid Build Coastguard Workermojo::Remote for the browser-hosted URLLoader is passed to a renderer
614*6777b538SAndroid Build Coastguard Workerproceess.
615*6777b538SAndroid Build Coastguard Worker
616*6777b538SAndroid Build Coastguard Workerabout:blank is similarly often handled in the renderer, though there is a
617*6777b538SAndroid Build Coastguard Workerfactory for that used in the case of navigations as well. Other about: URLs
618*6777b538SAndroid Build Coastguard Workerare mapped to the corresponding Chrome URLs by the navigation code, rather
619*6777b538SAndroid Build Coastguard Workerthan having that logic live in a URLLoaderFactory.
620