blob: fcf2e7075594dbd654e7e5b71b9dc883d5d37072 [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
Lukasz Anforowicz288027b2019-01-17 01:51:278#include <memory>
9
John Abd-El-Malek63ff5f32017-12-18 22:14:5810#include "base/callback_forward.h"
jamc1905862017-05-16 14:45:3011#include "base/macros.h"
12#include "base/memory/ref_counted.h"
13#include "content/common/content_export.h"
jamc1905862017-05-16 14:45:3014#include "content/public/browser/browser_thread.h"
Julie Jeongeun Kimb405097b2019-10-10 15:34:0715#include "mojo/public/cpp/bindings/pending_receiver.h"
Ken Rockot54311e62018-02-10 19:01:5216#include "services/network/public/mojom/url_loader_factory.mojom.h"
jamc1905862017-05-16 14:45:3017
Chong Zhangb7c8d1ce2018-03-13 19:14:1118namespace network {
Chong Zhang4dd97ebf2018-03-07 04:48:4819class SharedURLLoaderFactoryInfo;
Chong Zhang5271432a2018-03-01 23:31:0220class SharedURLLoaderFactory;
Chong Zhangb7c8d1ce2018-03-13 19:14:1121} // namespace network
22
23namespace content {
24
ananta2e65213d2017-05-19 04:08:2425class StoragePartitionImpl;
26
jamc1905862017-05-16 14:45:3027// Holds on to URLLoaderFactory for a given StoragePartition and allows code
28// running on the IO thread to access them. Note these are the factories used by
29// the browser process for frame requests.
30class URLLoaderFactoryGetter
31 : public base::RefCountedThreadSafe<URLLoaderFactoryGetter,
32 BrowserThread::DeleteOnIOThread> {
33 public:
Anantanarayanan Iyengaraa70bb42017-08-23 01:38:3234 CONTENT_EXPORT URLLoaderFactoryGetter();
jamc1905862017-05-16 14:45:3035
ananta2e65213d2017-05-19 04:08:2436 // Initializes this object on the UI thread. The |partition| is used to
Makoto Shimazu4c6e1a02018-04-25 01:00:4637 // initialize the URLLoaderFactories for the network service, AppCache, and
38 // ServiceWorkers, and will be cached to recover from connection error.
39 // After Initialize(), you can get URLLoaderFactories from this
John Abd-El-Malekc134dd82019-07-31 20:51:4440 // getter.
ananta2e65213d2017-05-19 04:08:2441 void Initialize(StoragePartitionImpl* partition);
jamc1905862017-05-16 14:45:3042
Chong Zhangd4c923642018-01-03 21:22:2943 // Clear the cached pointer to |StoragePartitionImpl| on the UI thread. Should
44 // be called when the partition is going away.
45 void OnStoragePartitionDestroyed();
46
Chong Zhang5271432a2018-03-01 23:31:0247 // Called on the IO thread to get a shared wrapper to this
48 // URLLoaderFactoryGetter, which can be used to access the URLLoaderFactory
49 // to the network service and supports auto-reconnect after crash.
Chong Zhangb7c8d1ce2018-03-13 19:14:1150 CONTENT_EXPORT scoped_refptr<network::SharedURLLoaderFactory>
51 GetNetworkFactory();
Chong Zhang5271432a2018-03-01 23:31:0252
John Abd-El-Malek0d9f17b2018-12-05 00:02:3853 // Like above, except it returns a URLLoaderFactory that has CORB enabled. Use
54 // this when using the factory for requests on behalf of a renderer.
55 // TODO(lukasza): https://crbug.com/871827: Ensure that |request_initiator| is
56 // trustworthy, even when starting requests on behalf of a renderer.
57 CONTENT_EXPORT scoped_refptr<network::SharedURLLoaderFactory>
58 GetNetworkFactoryWithCORBEnabled();
59
Chong Zhang4dd97ebf2018-03-07 04:48:4860 // Called on the UI thread to get an info that holds a reference to this
61 // URLLoaderFactoryGetter, which can be used to construct a similar
62 // SharedURLLoaderFactory as returned from |GetNetworkFactory()| on IO thread.
Chong Zhangb7c8d1ce2018-03-13 19:14:1163 CONTENT_EXPORT std::unique_ptr<network::SharedURLLoaderFactoryInfo>
Chong Zhang4dd97ebf2018-03-07 04:48:4864 GetNetworkFactoryInfo();
65
Matt Falkenhagen8cdf3482018-08-31 00:05:0466 // Called on the IO thread. The factory obtained from here can only be used
John Abd-El-Malek0d9f17b2018-12-05 00:02:3867 // from the browser process. It must NOT be sent to a renderer process. It has
68 // CORB disabled, so it must NOT be used to make requests on behalf of a
69 // renderer.
Matt Falkenhagen8cdf3482018-08-31 00:05:0470 //
71 // When NetworkService is enabled, this clones the internal factory to the
72 // network service, which doesn't support auto-reconnect after crash. Useful
73 // for one-off requests (e.g. a single navigation) to avoid an additional Mojo
74 // hop.
75 //
76 // When NetworkService is disabled, this clones the non-NetworkService direct
77 // network factory.
Chong Zhang5271432a2018-03-01 23:31:0278 CONTENT_EXPORT void CloneNetworkFactory(
Julie Jeongeun Kimb405097b2019-10-10 15:34:0779 mojo::PendingReceiver<network::mojom::URLLoaderFactory>
80 network_factory_receiver);
jamc1905862017-05-16 14:45:3081
82 // Overrides the network URLLoaderFactory for subsequent requests. Passing a
83 // null pointer will restore the default behavior.
jamc1905862017-05-16 14:45:3084 CONTENT_EXPORT void SetNetworkFactoryForTesting(
Lukasz Anforowicz62bb74b2019-02-06 21:10:3285 network::mojom::URLLoaderFactory* test_factory,
86 bool is_corb_enabled = false);
John Abd-El-Maleka4bb87f2017-11-27 21:32:1987
John Abd-El-Malekb165dc52018-01-18 17:12:1888 CONTENT_EXPORT network::mojom::URLLoaderFactoryPtr*
John Abd-El-Maleka4bb87f2017-11-27 21:32:1989 original_network_factory_for_testing() {
90 return &network_factory_;
91 }
jamc1905862017-05-16 14:45:3092
John Abd-El-Malek0d9f17b2018-12-05 00:02:3893 CONTENT_EXPORT network::mojom::URLLoaderFactoryPtr*
94 original_network_factory__corb_enabled_for_testing() {
95 return &network_factory_corb_enabled_;
96 }
97
Chong Zhang4dd97ebf2018-03-07 04:48:4898 // When this global function is set, if GetURLLoaderFactory is called and
99 // |test_factory_| is null, then the callback will be run. This method must be
100 // called either on the IO thread or before threads start. This callback is
101 // run on the IO thread.
John Abd-El-Malek63ff5f32017-12-18 22:14:58102 using GetNetworkFactoryCallback = base::RepeatingCallback<void(
Matt Falkenhagen4df68d22019-09-09 08:34:15103 scoped_refptr<URLLoaderFactoryGetter> url_loader_factory_getter)>;
John Abd-El-Malek63ff5f32017-12-18 22:14:58104 CONTENT_EXPORT static void SetGetNetworkFactoryCallbackForTesting(
105 const GetNetworkFactoryCallback& get_network_factory_callback);
106
Chong Zhangd4c923642018-01-03 21:22:29107 // Call |network_factory_.FlushForTesting()| on IO thread. For test use only.
108 CONTENT_EXPORT void FlushNetworkInterfaceOnIOThreadForTesting();
109
jamc1905862017-05-16 14:45:30110 private:
Chong Zhang4dd97ebf2018-03-07 04:48:48111 class URLLoaderFactoryForIOThreadInfo;
Chong Zhang5271432a2018-03-01 23:31:02112 class URLLoaderFactoryForIOThread;
113
jamc1905862017-05-16 14:45:30114 friend class base::DeleteHelper<URLLoaderFactoryGetter>;
115 friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>;
116
117 CONTENT_EXPORT ~URLLoaderFactoryGetter();
John Abd-El-Malekb165dc52018-01-18 17:12:18118 void InitializeOnIOThread(
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:39119 network::mojom::URLLoaderFactoryPtrInfo network_factory);
jamc1905862017-05-16 14:45:30120
John Abd-El-Malek0d9f17b2018-12-05 00:02:38121 // Moves |network_factory| to |network_factory_| or
122 // |network_factory_corb_enabled_| depending on |is_corb_enabled| and sets up
123 // an error handler.
Matt Menke58a77c02018-08-10 03:49:02124 void ReinitializeOnIOThread(
John Abd-El-Malek0d9f17b2018-12-05 00:02:38125 network::mojom::URLLoaderFactoryPtr network_factory,
126 bool is_corb_enabled);
Matt Menke58a77c02018-08-10 03:49:02127
Chong Zhangd4c923642018-01-03 21:22:29128 // Send |network_factory_request| to cached |StoragePartitionImpl|.
129 void HandleNetworkFactoryRequestOnUIThread(
Julie Jeongeun Kimb405097b2019-10-10 15:34:07130 mojo::PendingReceiver<network::mojom::URLLoaderFactory>
131 network_factory_receiver,
John Abd-El-Malek0d9f17b2018-12-05 00:02:38132 bool is_corb_enabled);
Chong Zhangd4c923642018-01-03 21:22:29133
Chong Zhang5271432a2018-03-01 23:31:02134 // Called on the IO thread to get the URLLoaderFactory to the network service.
135 // The pointer shouldn't be cached.
John Abd-El-Malek0d9f17b2018-12-05 00:02:38136 network::mojom::URLLoaderFactory* GetURLLoaderFactory(bool is_corb_enabled);
Chong Zhang5271432a2018-03-01 23:31:02137
Clark DuVall16be2542018-07-23 22:42:42138 // Call |network_factory_.FlushForTesting()|. For test use only. When the
139 // flush is complete, |callback| will be called.
140 void FlushNetworkInterfaceForTesting(base::OnceClosure callback);
Chong Zhangd4c923642018-01-03 21:22:29141
jamc1905862017-05-16 14:45:30142 // Only accessed on IO thread.
John Abd-El-Malekb165dc52018-01-18 17:12:18143 network::mojom::URLLoaderFactoryPtr network_factory_;
John Abd-El-Malek0d9f17b2018-12-05 00:02:38144 network::mojom::URLLoaderFactoryPtr network_factory_corb_enabled_;
John Abd-El-Malekb165dc52018-01-18 17:12:18145 network::mojom::URLLoaderFactory* test_factory_ = nullptr;
Lukasz Anforowicz62bb74b2019-02-06 21:10:32146 network::mojom::URLLoaderFactory* test_factory_corb_enabled_ = nullptr;
jamc1905862017-05-16 14:45:30147
Chong Zhangd4c923642018-01-03 21:22:29148 // Used to re-create |network_factory_| when connection error happens. Can
149 // only be accessed on UI thread. Must be cleared by |StoragePartitionImpl|
150 // when it's going away.
151 StoragePartitionImpl* partition_ = nullptr;
152
jamc1905862017-05-16 14:45:30153 DISALLOW_COPY_AND_ASSIGN(URLLoaderFactoryGetter);
154};
155
156} // namespace content
157
158#endif // CONTENT_BROWSER_URL_LOADER_FACTORY_GETTER_H_