1 // Copyright 2018 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_RENDERER_LOADER_CHILD_URL_LOADER_FACTORY_BUNDLE_H_
6 #define CONTENT_RENDERER_LOADER_CHILD_URL_LOADER_FACTORY_BUNDLE_H_
7 
8 #include <map>
9 #include <memory>
10 #include <vector>
11 
12 #include "base/callback.h"
13 #include "base/optional.h"
14 #include "content/common/content_export.h"
15 #include "content/public/common/transferrable_url_loader.mojom.h"
16 #include "mojo/public/cpp/bindings/pending_receiver.h"
17 #include "mojo/public/cpp/bindings/pending_remote.h"
18 #include "mojo/public/cpp/bindings/remote.h"
19 #include "services/network/public/mojom/url_loader_factory.mojom-forward.h"
20 #include "third_party/blink/public/common/loader/url_loader_factory_bundle.h"
21 
22 namespace content {
23 
24 // Holds the internal state of a ChildURLLoaderFactoryBundle in a form that is
25 // safe to pass across sequences.
26 // |pending_prefetch_loader_factory| is used only by the frames who may send
27 // prefetch requests by <link rel="prefetch"> tags. The loader factory allows
28 // prefetch loading to be done by the browser process (therefore less memory
29 // pressure), and also adds special handling for Signed Exchanges (SXG) when the
30 // flag is enabled. TODO(crbug/803776): deprecate this once SXG specific code is
31 // moved into Network Service unless we see huge memory benefit for doing this.
32 class CONTENT_EXPORT ChildPendingURLLoaderFactoryBundle
33     : public blink::PendingURLLoaderFactoryBundle {
34  public:
35   ChildPendingURLLoaderFactoryBundle();
36   explicit ChildPendingURLLoaderFactoryBundle(
37       std::unique_ptr<PendingURLLoaderFactoryBundle> base_factories);
38   ChildPendingURLLoaderFactoryBundle(
39       mojo::PendingRemote<network::mojom::URLLoaderFactory>
40           pending_default_factory,
41       mojo::PendingRemote<network::mojom::URLLoaderFactory>
42           pending_default_network_factory,
43       SchemeMap pending_scheme_specific_factories,
44       OriginMap pending_isolated_world_factories,
45       mojo::PendingRemote<network::mojom::URLLoaderFactory>
46           direct_network_factory_remote,
47       mojo::PendingRemote<network::mojom::URLLoaderFactory>
48           pending_prefetch_loader_factory,
49       bool bypass_redirect_checks);
50   ~ChildPendingURLLoaderFactoryBundle() override;
51 
52   mojo::PendingRemote<network::mojom::URLLoaderFactory>&
direct_network_factory_remote()53   direct_network_factory_remote() {
54     return direct_network_factory_remote_;
55   }
56   mojo::PendingRemote<network::mojom::URLLoaderFactory>&
pending_prefetch_loader_factory()57   pending_prefetch_loader_factory() {
58     return pending_prefetch_loader_factory_;
59   }
60 
61  protected:
62   // PendingURLLoaderFactoryBundle overrides.
63   scoped_refptr<network::SharedURLLoaderFactory> CreateFactory() override;
64 
65   mojo::PendingRemote<network::mojom::URLLoaderFactory>
66       direct_network_factory_remote_;
67   mojo::PendingRemote<network::mojom::URLLoaderFactory>
68       pending_prefetch_loader_factory_;
69 
70   DISALLOW_COPY_AND_ASSIGN(ChildPendingURLLoaderFactoryBundle);
71 };
72 
73 // This class extends URLLoaderFactoryBundle to support a direct network loader
74 // factory, which bypasses custom overrides such as appcache or service worker.
75 // Besides, it also supports using callbacks to lazily initialize the direct
76 // network loader factory.
77 class CONTENT_EXPORT ChildURLLoaderFactoryBundle
78     : public blink::URLLoaderFactoryBundle {
79  public:
80   using FactoryGetterCallback = base::OnceCallback<
81       mojo::PendingRemote<network::mojom::URLLoaderFactory>()>;
82 
83   ChildURLLoaderFactoryBundle();
84 
85   explicit ChildURLLoaderFactoryBundle(
86       std::unique_ptr<ChildPendingURLLoaderFactoryBundle> pending_factories);
87 
88   ChildURLLoaderFactoryBundle(
89       FactoryGetterCallback direct_network_factory_getter);
90 
91   // URLLoaderFactoryBundle overrides.
92   void CreateLoaderAndStart(
93       mojo::PendingReceiver<network::mojom::URLLoader> loader,
94       int32_t routing_id,
95       int32_t request_id,
96       uint32_t options,
97       const network::ResourceRequest& request,
98       mojo::PendingRemote<network::mojom::URLLoaderClient> client,
99       const net::MutableNetworkTrafficAnnotationTag& traffic_annotation)
100       override;
101   std::unique_ptr<network::PendingSharedURLLoaderFactory> Clone() override;
102 
103   // Does the same as Clone(), but without cloning the appcache_factory_.
104   // This is used for creating a bundle for network fallback loading with
105   // Service Workers (where AppCache must be skipped), and only when
106   // claim() is called.
107   virtual std::unique_ptr<network::PendingSharedURLLoaderFactory>
108   CloneWithoutAppCacheFactory();
109 
110   std::unique_ptr<ChildPendingURLLoaderFactoryBundle> PassInterface();
111 
112   void Update(
113       std::unique_ptr<ChildPendingURLLoaderFactoryBundle> pending_factories);
114   void UpdateSubresourceOverrides(
115       std::vector<mojom::TransferrableURLLoaderPtr>* subresource_overrides);
116   void SetPrefetchLoaderFactory(
117       mojo::PendingRemote<network::mojom::URLLoaderFactory>
118           prefetch_loader_factory);
119 
120   virtual bool IsHostChildURLLoaderFactoryBundle() const;
121 
122  protected:
123   ~ChildURLLoaderFactoryBundle() override;
124 
125   // URLLoaderFactoryBundle overrides.
126   network::mojom::URLLoaderFactory* GetFactory(
127       const network::ResourceRequest& request) override;
128 
129  private:
130   void InitDirectNetworkFactoryIfNecessary();
131   std::unique_ptr<network::PendingSharedURLLoaderFactory> CloneInternal(
132       bool include_appcache);
133 
134   FactoryGetterCallback direct_network_factory_getter_;
135   mojo::Remote<network::mojom::URLLoaderFactory> direct_network_factory_;
136   mojo::Remote<network::mojom::URLLoaderFactory> prefetch_loader_factory_;
137 
138   std::map<GURL, mojom::TransferrableURLLoaderPtr> subresource_overrides_;
139 };
140 
141 }  // namespace content
142 
143 #endif  // CONTENT_RENDERER_LOADER_CHILD_URL_LOADER_FACTORY_BUNDLE_H_
144