1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/loader/reconnectable_url_loader_factory.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "content/browser/loader/url_loader_factory_utils.h"
#include "services/network/public/cpp/cross_thread_pending_shared_url_loader_factory.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace content {
ReconnectableURLLoaderFactory::ReconnectableURLLoaderFactory(
CreateCallback create_url_loader_factory_callback)
: create_url_loader_factory_callback_(
std::move(create_url_loader_factory_callback)) {}
ReconnectableURLLoaderFactory::~ReconnectableURLLoaderFactory() = default;
void ReconnectableURLLoaderFactory::CreateLoaderAndStart(
mojo::PendingReceiver<network::mojom::URLLoader> receiver,
int32_t request_id,
uint32_t options,
const network::ResourceRequest& url_request,
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation) {
if (network::mojom::URLLoaderFactory* factory = GetURLLoaderFactory()) {
factory->CreateLoaderAndStart(std::move(receiver), request_id, options,
url_request, std::move(client),
traffic_annotation);
}
}
void ReconnectableURLLoaderFactory::Clone(
mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver) {
if (network::mojom::URLLoaderFactory* factory = GetURLLoaderFactory()) {
factory->Clone(std::move(receiver));
}
}
std::unique_ptr<network::PendingSharedURLLoaderFactory>
ReconnectableURLLoaderFactory::Clone() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return std::make_unique<network::CrossThreadPendingSharedURLLoaderFactory>(
this);
}
network::mojom::URLLoaderFactory*
ReconnectableURLLoaderFactory::GetURLLoaderFactory() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Create the URLLoaderFactory as needed, but make sure not to reuse a
// previously created one if the test override has changed.
if (url_loader_factory_ && url_loader_factory_.is_connected() &&
is_test_url_loader_factory_ ==
!!url_loader_factory::GetTestingInterceptor()) {
return url_loader_factory_.get();
}
is_test_url_loader_factory_ = !!url_loader_factory::GetTestingInterceptor();
url_loader_factory_.reset();
mojo::PendingRemote<network::mojom::URLLoaderFactory> url_loader_factory;
create_url_loader_factory_callback_.Run(&url_loader_factory);
if (!url_loader_factory) {
return nullptr;
}
url_loader_factory_.Bind(std::move(url_loader_factory));
return url_loader_factory_.get();
}
void ReconnectableURLLoaderFactory::Reset() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
url_loader_factory_.reset();
}
void ReconnectableURLLoaderFactory::FlushForTesting() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (url_loader_factory_) {
url_loader_factory_.FlushForTesting(); // IN-TEST
}
}
// -----------------------------------------------------------------------------
class ReconnectableURLLoaderFactoryForIOThread::
PendingURLLoaderFactoryForIOThread final
: public network::PendingSharedURLLoaderFactory {
public:
explicit PendingURLLoaderFactoryForIOThread(
scoped_refptr<ReconnectableURLLoaderFactoryForIOThread> factory_getter)
: factory_getter_(std::move(factory_getter)) {
CHECK(factory_getter_);
}
PendingURLLoaderFactoryForIOThread(
const PendingURLLoaderFactoryForIOThread&) = delete;
PendingURLLoaderFactoryForIOThread& operator=(
const PendingURLLoaderFactoryForIOThread&) = delete;
~PendingURLLoaderFactoryForIOThread() override = default;
protected:
// PendingSharedURLLoaderFactory implementation.
scoped_refptr<network::SharedURLLoaderFactory> CreateFactory() override;
const scoped_refptr<ReconnectableURLLoaderFactoryForIOThread> factory_getter_;
};
class ReconnectableURLLoaderFactoryForIOThread::URLLoaderFactoryForIOThread
final : public network::SharedURLLoaderFactory {
public:
explicit URLLoaderFactoryForIOThread(
scoped_refptr<ReconnectableURLLoaderFactoryForIOThread> factory_getter)
: factory_getter_(std::move(factory_getter)) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
CHECK(factory_getter_);
}
URLLoaderFactoryForIOThread(const URLLoaderFactoryForIOThread&) = delete;
URLLoaderFactoryForIOThread& operator=(const URLLoaderFactoryForIOThread&) =
delete;
// mojom::URLLoaderFactory implementation:
void CreateLoaderAndStart(
mojo::PendingReceiver<network::mojom::URLLoader> receiver,
int32_t request_id,
uint32_t options,
const network::ResourceRequest& url_request,
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation)
override {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
factory_getter_->GetURLLoaderFactory()->CreateLoaderAndStart(
std::move(receiver), request_id, options, url_request,
std::move(client), traffic_annotation);
}
void Clone(mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver)
override {
factory_getter_->GetURLLoaderFactory()->Clone(std::move(receiver));
}
// SharedURLLoaderFactory implementation:
std::unique_ptr<network::PendingSharedURLLoaderFactory> Clone() override {
NOTREACHED() << "This isn't supported. If you need a SharedURLLoaderFactory"
" on the UI thread, get it from StoragePartition.";
}
private:
friend class base::RefCounted<URLLoaderFactoryForIOThread>;
~URLLoaderFactoryForIOThread() override = default;
const scoped_refptr<ReconnectableURLLoaderFactoryForIOThread> factory_getter_;
};
scoped_refptr<network::SharedURLLoaderFactory>
ReconnectableURLLoaderFactoryForIOThread::PendingURLLoaderFactoryForIOThread::
CreateFactory() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
return base::MakeRefCounted<URLLoaderFactoryForIOThread>(
std::move(factory_getter_));
}
// -----------------------------------------------------------------------------
ReconnectableURLLoaderFactoryForIOThread::
ReconnectableURLLoaderFactoryForIOThread(
CreateCallback create_url_loader_factory_callback)
: create_url_loader_factory_callback_(
std::move(create_url_loader_factory_callback)) {}
void ReconnectableURLLoaderFactoryForIOThread::Initialize() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// Create a mojo::PendingRemote<URLLoaderFactory> synchronously and push it to
// the IO thread. If the pipe errors out later due to a network service crash,
// the pipe is created on the IO thread, and the request send back to the UI
// thread.
// TODO(mmenke): Is one less thread hop on startup worth the extra complexity
// of two different pipe creation paths?
mojo::PendingRemote<network::mojom::URLLoaderFactory> network_factory;
HandleNetworkFactoryRequestOnUIThread(
network_factory.InitWithNewPipeAndPassReceiver());
GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
&ReconnectableURLLoaderFactoryForIOThread::InitializeOnIOThread, this,
std::move(network_factory)));
}
std::unique_ptr<network::PendingSharedURLLoaderFactory>
ReconnectableURLLoaderFactoryForIOThread::CloneForIOThread() {
return std::make_unique<PendingURLLoaderFactoryForIOThread>(
base::WrapRefCounted(this));
}
network::mojom::URLLoaderFactory*
ReconnectableURLLoaderFactoryForIOThread::GetURLLoaderFactory() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (url_loader_factory_ && url_loader_factory_.is_connected() &&
is_test_url_loader_factory_ ==
url_loader_factory::HasInterceptorOnIOThreadForTesting()) {
return url_loader_factory_.get();
}
is_test_url_loader_factory_ =
url_loader_factory::HasInterceptorOnIOThreadForTesting();
mojo::Remote<network::mojom::URLLoaderFactory> network_factory;
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&ReconnectableURLLoaderFactoryForIOThread::
HandleNetworkFactoryRequestOnUIThread,
this, network_factory.BindNewPipeAndPassReceiver()));
ReinitializeOnIOThread(std::move(network_factory));
return url_loader_factory_.get();
}
void ReconnectableURLLoaderFactoryForIOThread::Reset() {
Initialize();
}
void ReconnectableURLLoaderFactoryForIOThread::FlushForTesting() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::RunLoop run_loop;
GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
&ReconnectableURLLoaderFactoryForIOThread::FlushOnIOThreadForTesting,
this, run_loop.QuitClosure()));
run_loop.Run();
}
void ReconnectableURLLoaderFactoryForIOThread::FlushOnIOThreadForTesting(
base::OnceClosure callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (url_loader_factory_) {
url_loader_factory_.FlushAsyncForTesting(std::move(callback)); // IN-TEST
}
}
ReconnectableURLLoaderFactoryForIOThread::
~ReconnectableURLLoaderFactoryForIOThread() = default;
void ReconnectableURLLoaderFactoryForIOThread::InitializeOnIOThread(
mojo::PendingRemote<network::mojom::URLLoaderFactory> network_factory) {
ReinitializeOnIOThread(mojo::Remote<network::mojom::URLLoaderFactory>(
std::move(network_factory)));
}
void ReconnectableURLLoaderFactoryForIOThread::ReinitializeOnIOThread(
mojo::Remote<network::mojom::URLLoaderFactory> network_factory) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(network_factory.is_bound());
// Set a disconnect handler so that connection errors on the pipes are
// noticed, but the class doesn't actually do anything when the error is
// observed - instead, a new pipe is created in GetURLLoaderFactory() as
// needed. This is to avoid incrementing the reference count of |this| in the
// callback, as that could result in increasing the reference count from 0 to
// 1 while there's a pending task to delete |this|. See
// https://crbug.com/870942 for more details.
network_factory.set_disconnect_handler(base::DoNothing());
url_loader_factory_ = std::move(network_factory);
}
void ReconnectableURLLoaderFactoryForIOThread::
HandleNetworkFactoryRequestOnUIThread(
mojo::PendingReceiver<network::mojom::URLLoaderFactory>
network_factory_receiver) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
mojo::PendingRemote<network::mojom::URLLoaderFactory> factory_remote;
create_url_loader_factory_callback_.Run(&factory_remote);
if (!factory_remote) {
// The underlying URLLoaderFactory has went away while
// `ReconnectableURLLoaderFactoryForIOThread` is still held by consumers.
return;
}
CHECK(mojo::FusePipes(std::move(network_factory_receiver),
std::move(factory_remote)));
}
// -----------------------------------------------------------------------------
ReconnectableURLLoaderFactoryForIOThreadWrapper::
ReconnectableURLLoaderFactoryForIOThreadWrapper(
CreateCallback create_url_loader_factory_callback)
: factory_(base::MakeRefCounted<ReconnectableURLLoaderFactory>(
create_url_loader_factory_callback)),
factory_for_io_thread_(
base::MakeRefCounted<ReconnectableURLLoaderFactoryForIOThread>(
create_url_loader_factory_callback)) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
}
ReconnectableURLLoaderFactoryForIOThreadWrapper::
~ReconnectableURLLoaderFactoryForIOThreadWrapper() = default;
} // namespace content
|