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 SERVICES_NETWORK_URL_LOADER_FACTORY_H_
6 #define SERVICES_NETWORK_URL_LOADER_FACTORY_H_
7 
8 #include <memory>
9 #include <set>
10 
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "mojo/public/cpp/bindings/pending_receiver.h"
14 #include "mojo/public/cpp/bindings/pending_remote.h"
15 #include "mojo/public/cpp/bindings/remote.h"
16 #include "net/traffic_annotation/network_traffic_annotation.h"
17 #include "services/network/public/mojom/cross_origin_embedder_policy.mojom.h"
18 #include "services/network/public/mojom/network_context.mojom.h"
19 #include "services/network/public/mojom/url_loader_factory.mojom.h"
20 
21 namespace network {
22 
23 class NetworkContext;
24 class ResourceSchedulerClient;
25 class URLLoader;
26 
27 namespace cors {
28 class CorsURLLoaderFactory;
29 }  // namespace cors
30 
31 // This class is an implementation of mojom::URLLoaderFactory that
32 // creates a mojom::URLLoader.
33 // A URLLoaderFactory has a pointer to ResourceSchedulerClient. A
34 // ResourceSchedulerClient is associated with cloned
35 // NetworkServiceURLLoaderFactories. Roughly one URLLoaderFactory
36 // is created for one frame in render process, so it means ResourceScheduler
37 // works on each frame.
38 // A URLLoaderFactory can be created with null ResourceSchedulerClient, in which
39 // case requests constructed by the factory will not be throttled.
40 // The CORS related part is implemented in CorsURLLoader[Factory] until
41 // kOutOfBlinkCors and kNetworkService is fully enabled. Note that
42 // NetworkContext::CreateURLLoaderFactory returns a CorsURLLoaderFactory,
43 // instead of a URLLoaderFactory.
44 class URLLoaderFactory : public mojom::URLLoaderFactory {
45  public:
46   // NOTE: |context| must outlive this instance.
47   URLLoaderFactory(
48       NetworkContext* context,
49       mojom::URLLoaderFactoryParamsPtr params,
50       scoped_refptr<ResourceSchedulerClient> resource_scheduler_client,
51       cors::CorsURLLoaderFactory* cors_url_loader_factory);
52 
53   ~URLLoaderFactory() override;
54 
55   // mojom::URLLoaderFactory implementation.
56   void CreateLoaderAndStart(mojo::PendingReceiver<mojom::URLLoader> receiver,
57                             int32_t routing_id,
58                             int32_t request_id,
59                             uint32_t options,
60                             const ResourceRequest& url_request,
61                             mojo::PendingRemote<mojom::URLLoaderClient> client,
62                             const net::MutableNetworkTrafficAnnotationTag&
63                                 traffic_annotation) override;
64   void Clone(mojo::PendingReceiver<mojom::URLLoaderFactory> receiver) override;
65 
66   static constexpr int kMaxKeepaliveConnections = 2048;
67   static constexpr int kMaxKeepaliveConnectionsPerTopLevelFrame = 256;
68   static constexpr int kMaxTotalKeepaliveRequestSize = 512 * 1024;
69 
70  private:
71   // The NetworkContext that indirectly owns |this|.
72   NetworkContext* const context_;
73   mojom::URLLoaderFactoryParamsPtr params_;
74   scoped_refptr<ResourceSchedulerClient> resource_scheduler_client_;
75   mojo::Remote<mojom::TrustedURLLoaderHeaderClient> header_client_;
76   mojo::Remote<mojom::CrossOriginEmbedderPolicyReporter> coep_reporter_;
77 
78   // |cors_url_loader_factory_| owns this.
79   cors::CorsURLLoaderFactory* cors_url_loader_factory_;
80 
81   DISALLOW_COPY_AND_ASSIGN(URLLoaderFactory);
82 };
83 
84 }  // namespace network
85 
86 #endif  // SERVICES_NETWORK_URL_LOADER_FACTORY_H_
87