blob: e1509fe61ebd7240bea0485ef3bd2e5cc261023c [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
John Abd-El-Malek63ff5f32017-12-18 22:14:588#include "base/callback_forward.h"
jamc1905862017-05-16 14:45:309#include "base/macros.h"
10#include "base/memory/ref_counted.h"
11#include "content/common/content_export.h"
jamc1905862017-05-16 14:45:3012#include "content/public/browser/browser_thread.h"
Ken Rockot54311e62018-02-10 19:01:5213#include "services/network/public/mojom/url_loader_factory.mojom.h"
jamc1905862017-05-16 14:45:3014
Chong Zhangb7c8d1ce2018-03-13 19:14:1115namespace network {
Chong Zhang4dd97ebf2018-03-07 04:48:4816class SharedURLLoaderFactoryInfo;
Chong Zhang5271432a2018-03-01 23:31:0217class SharedURLLoaderFactory;
Chong Zhangb7c8d1ce2018-03-13 19:14:1118} // namespace network
19
20namespace content {
21
ananta2e65213d2017-05-19 04:08:2422class StoragePartitionImpl;
23
jamc1905862017-05-16 14:45:3024// Holds on to URLLoaderFactory for a given StoragePartition and allows code
25// running on the IO thread to access them. Note these are the factories used by
26// the browser process for frame requests.
27class URLLoaderFactoryGetter
28 : public base::RefCountedThreadSafe<URLLoaderFactoryGetter,
29 BrowserThread::DeleteOnIOThread> {
30 public:
Anantanarayanan Iyengaraa70bb42017-08-23 01:38:3231 CONTENT_EXPORT URLLoaderFactoryGetter();
jamc1905862017-05-16 14:45:3032
ananta2e65213d2017-05-19 04:08:2433 // Initializes this object on the UI thread. The |partition| is used to
Makoto Shimazu4c6e1a02018-04-25 01:00:4634 // initialize the URLLoaderFactories for the network service, AppCache, and
35 // ServiceWorkers, and will be cached to recover from connection error.
36 // After Initialize(), you can get URLLoaderFactories from this
37 // getter. However, any messages on it will be queued until
38 // HandleFactoryRequests() is called.
ananta2e65213d2017-05-19 04:08:2439 void Initialize(StoragePartitionImpl* partition);
jamc1905862017-05-16 14:45:3040
Makoto Shimazu4c6e1a02018-04-25 01:00:4641 // Called on the UI thread to actually instantiate factories whose pointers
42 // are to be exposed by this getter. This must be called after NetworkContext
43 // is initialized.
44 // TODO(shimazu): Remove this once NetworkService is shipped.
45 void HandleFactoryRequests();
46
Chong Zhangd4c923642018-01-03 21:22:2947 // Clear the cached pointer to |StoragePartitionImpl| on the UI thread. Should
48 // be called when the partition is going away.
49 void OnStoragePartitionDestroyed();
50
Chong Zhang5271432a2018-03-01 23:31:0251 // Called on the IO thread to get a shared wrapper to this
52 // URLLoaderFactoryGetter, which can be used to access the URLLoaderFactory
53 // to the network service and supports auto-reconnect after crash.
Chong Zhangb7c8d1ce2018-03-13 19:14:1154 CONTENT_EXPORT scoped_refptr<network::SharedURLLoaderFactory>
55 GetNetworkFactory();
Chong Zhang5271432a2018-03-01 23:31:0256
Chong Zhang4dd97ebf2018-03-07 04:48:4857 // Called on the UI thread to get an info that holds a reference to this
58 // URLLoaderFactoryGetter, which can be used to construct a similar
59 // SharedURLLoaderFactory as returned from |GetNetworkFactory()| on IO thread.
Chong Zhangb7c8d1ce2018-03-13 19:14:1160 CONTENT_EXPORT std::unique_ptr<network::SharedURLLoaderFactoryInfo>
Chong Zhang4dd97ebf2018-03-07 04:48:4861 GetNetworkFactoryInfo();
62
Chong Zhang5271432a2018-03-01 23:31:0263 // Called on the IO thread. Will clone the internal factory to the network
Chong Zhang70432e32018-03-07 04:43:5264 // service which doesn't support auto-reconnect after crash. Useful for
65 // one-off requests (e.g. A single navigation) to avoid additional mojo hop.
Chong Zhang5271432a2018-03-01 23:31:0266 CONTENT_EXPORT void CloneNetworkFactory(
67 network::mojom::URLLoaderFactoryRequest network_factory_request);
jamc1905862017-05-16 14:45:3068
jam9354af82017-06-03 21:59:4169 // Called on the IO thread to get the URLLoaderFactory to the blob service.
70 // The pointer shouldn't be cached.
John Abd-El-Malekb165dc52018-01-18 17:12:1871 CONTENT_EXPORT network::mojom::URLLoaderFactory* GetBlobFactory();
jam9354af82017-06-03 21:59:4172
jamc1905862017-05-16 14:45:3073 // Overrides the network URLLoaderFactory for subsequent requests. Passing a
74 // null pointer will restore the default behavior.
jamc1905862017-05-16 14:45:3075 CONTENT_EXPORT void SetNetworkFactoryForTesting(
John Abd-El-Malekb165dc52018-01-18 17:12:1876 network::mojom::URLLoaderFactory* test_factory);
John Abd-El-Maleka4bb87f2017-11-27 21:32:1977
John Abd-El-Malekb165dc52018-01-18 17:12:1878 CONTENT_EXPORT network::mojom::URLLoaderFactoryPtr*
John Abd-El-Maleka4bb87f2017-11-27 21:32:1979 original_network_factory_for_testing() {
80 return &network_factory_;
81 }
jamc1905862017-05-16 14:45:3082
Chong Zhang4dd97ebf2018-03-07 04:48:4883 // When this global function is set, if GetURLLoaderFactory is called and
84 // |test_factory_| is null, then the callback will be run. This method must be
85 // called either on the IO thread or before threads start. This callback is
86 // run on the IO thread.
John Abd-El-Malek63ff5f32017-12-18 22:14:5887 using GetNetworkFactoryCallback = base::RepeatingCallback<void(
88 URLLoaderFactoryGetter* url_loader_factory_getter)>;
89 CONTENT_EXPORT static void SetGetNetworkFactoryCallbackForTesting(
90 const GetNetworkFactoryCallback& get_network_factory_callback);
91
Chong Zhangd4c923642018-01-03 21:22:2992 // Call |network_factory_.FlushForTesting()| on IO thread. For test use only.
93 CONTENT_EXPORT void FlushNetworkInterfaceOnIOThreadForTesting();
94
jamc1905862017-05-16 14:45:3095 private:
Chong Zhang4dd97ebf2018-03-07 04:48:4896 class URLLoaderFactoryForIOThreadInfo;
Chong Zhang5271432a2018-03-01 23:31:0297 class URLLoaderFactoryForIOThread;
98
jamc1905862017-05-16 14:45:3099 friend class base::DeleteHelper<URLLoaderFactoryGetter>;
100 friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>;
101
102 CONTENT_EXPORT ~URLLoaderFactoryGetter();
John Abd-El-Malekb165dc52018-01-18 17:12:18103 void InitializeOnIOThread(
104 network::mojom::URLLoaderFactoryPtrInfo network_factory,
105 network::mojom::URLLoaderFactoryPtrInfo blob_factory);
jamc1905862017-05-16 14:45:30106
Chong Zhangd4c923642018-01-03 21:22:29107 // Send |network_factory_request| to cached |StoragePartitionImpl|.
108 void HandleNetworkFactoryRequestOnUIThread(
John Abd-El-Malekb165dc52018-01-18 17:12:18109 network::mojom::URLLoaderFactoryRequest network_factory_request);
Chong Zhangd4c923642018-01-03 21:22:29110
Chong Zhang5271432a2018-03-01 23:31:02111 // Called on the IO thread to get the URLLoaderFactory to the network service.
112 // The pointer shouldn't be cached.
113 network::mojom::URLLoaderFactory* GetURLLoaderFactory();
114
Chong Zhangd4c923642018-01-03 21:22:29115 // Call |network_factory_.FlushForTesting()|. For test use only.
116 void FlushNetworkInterfaceForTesting();
117
Makoto Shimazu4c6e1a02018-04-25 01:00:46118 // Bound with appropriate URLLoaderFactories at HandleFactoryRequests().
119 network::mojom::URLLoaderFactoryRequest pending_network_factory_request_;
120 network::mojom::URLLoaderFactoryRequest pending_blob_factory_request_;
121
jamc1905862017-05-16 14:45:30122 // Only accessed on IO thread.
John Abd-El-Malekb165dc52018-01-18 17:12:18123 network::mojom::URLLoaderFactoryPtr network_factory_;
124 network::mojom::URLLoaderFactoryPtr blob_factory_;
125 network::mojom::URLLoaderFactory* test_factory_ = nullptr;
jamc1905862017-05-16 14:45:30126
Chong Zhangd4c923642018-01-03 21:22:29127 // Used to re-create |network_factory_| when connection error happens. Can
128 // only be accessed on UI thread. Must be cleared by |StoragePartitionImpl|
129 // when it's going away.
130 StoragePartitionImpl* partition_ = nullptr;
131
jamc1905862017-05-16 14:45:30132 DISALLOW_COPY_AND_ASSIGN(URLLoaderFactoryGetter);
133};
134
135} // namespace content
136
137#endif // CONTENT_BROWSER_URL_LOADER_FACTORY_GETTER_H_