2019-06-11 05:07:58 +00:00
|
|
|
// Copyright (c) 2019 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/net/url_pipe_loader.h"
|
2019-06-11 05:07:58 +00:00
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
2023-02-03 11:43:42 +00:00
|
|
|
#include "base/task/sequenced_task_runner.h"
|
2019-12-11 00:22:35 +00:00
|
|
|
#include "mojo/public/cpp/bindings/pending_remote.h"
|
2019-07-24 22:58:51 +00:00
|
|
|
#include "mojo/public/cpp/system/string_data_source.h"
|
2019-06-11 05:07:58 +00:00
|
|
|
#include "services/network/public/cpp/shared_url_loader_factory.h"
|
2021-07-26 16:02:16 +00:00
|
|
|
#include "services/network/public/mojom/url_response_head.mojom.h"
|
2019-06-11 05:07:58 +00:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
|
|
|
URLPipeLoader::URLPipeLoader(
|
|
|
|
scoped_refptr<network::SharedURLLoaderFactory> factory,
|
|
|
|
std::unique_ptr<network::ResourceRequest> request,
|
2021-03-04 17:27:05 +00:00
|
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
2019-12-11 00:22:35 +00:00
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
2019-06-11 23:37:06 +00:00
|
|
|
const net::NetworkTrafficAnnotationTag& annotation,
|
2022-07-05 15:25:18 +00:00
|
|
|
base::Value::Dict upload_data)
|
2021-03-04 17:27:05 +00:00
|
|
|
: url_loader_(this, std::move(loader)), client_(std::move(client)) {
|
|
|
|
url_loader_.set_disconnect_handler(base::BindOnce(
|
2019-06-11 05:07:58 +00:00
|
|
|
&URLPipeLoader::NotifyComplete, base::Unretained(this), net::ERR_FAILED));
|
|
|
|
|
|
|
|
// PostTask since it might destruct.
|
2023-02-03 11:43:42 +00:00
|
|
|
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
|
2019-06-11 05:07:58 +00:00
|
|
|
FROM_HERE,
|
|
|
|
base::BindOnce(&URLPipeLoader::Start, weak_factory_.GetWeakPtr(), factory,
|
2019-06-11 23:37:06 +00:00
|
|
|
std::move(request), annotation, std::move(upload_data)));
|
2019-06-11 05:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
URLPipeLoader::~URLPipeLoader() = default;
|
|
|
|
|
|
|
|
void URLPipeLoader::Start(
|
|
|
|
scoped_refptr<network::SharedURLLoaderFactory> factory,
|
|
|
|
std::unique_ptr<network::ResourceRequest> request,
|
2019-06-11 23:37:06 +00:00
|
|
|
const net::NetworkTrafficAnnotationTag& annotation,
|
2022-07-05 15:25:18 +00:00
|
|
|
base::Value::Dict upload_data) {
|
2019-06-11 05:07:58 +00:00
|
|
|
loader_ = network::SimpleURLLoader::Create(std::move(request), annotation);
|
2021-01-25 01:27:40 +00:00
|
|
|
loader_->SetOnResponseStartedCallback(base::BindOnce(
|
2019-06-11 05:07:58 +00:00
|
|
|
&URLPipeLoader::OnResponseStarted, weak_factory_.GetWeakPtr()));
|
2019-06-11 23:37:06 +00:00
|
|
|
|
|
|
|
// TODO(zcbenz): The old protocol API only supports string as upload data,
|
|
|
|
// we should seek to support more types in future.
|
2022-07-05 15:25:18 +00:00
|
|
|
std::string* content_type = upload_data.FindString("contentType");
|
|
|
|
std::string* data = upload_data.FindString("data");
|
|
|
|
if (content_type && data)
|
|
|
|
loader_->AttachStringForUpload(*data, *content_type);
|
2019-06-11 23:37:06 +00:00
|
|
|
|
2019-06-11 05:07:58 +00:00
|
|
|
loader_->DownloadAsStream(factory.get(), this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void URLPipeLoader::NotifyComplete(int result) {
|
|
|
|
client_->OnComplete(network::URLLoaderCompletionStatus(result));
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void URLPipeLoader::OnResponseStarted(
|
|
|
|
const GURL& final_url,
|
2019-09-21 14:51:28 +00:00
|
|
|
const network::mojom::URLResponseHead& response_head) {
|
2019-06-11 05:07:58 +00:00
|
|
|
mojo::ScopedDataPipeProducerHandle producer;
|
|
|
|
mojo::ScopedDataPipeConsumerHandle consumer;
|
2021-03-06 00:42:15 +00:00
|
|
|
MojoResult rv = mojo::CreateDataPipe(nullptr, producer, consumer);
|
2019-06-11 05:07:58 +00:00
|
|
|
if (rv != MOJO_RESULT_OK) {
|
|
|
|
NotifyComplete(net::ERR_INSUFFICIENT_RESOURCES);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-24 22:58:51 +00:00
|
|
|
producer_ = std::make_unique<mojo::DataPipeProducer>(std::move(producer));
|
2019-06-11 05:07:58 +00:00
|
|
|
|
2022-09-07 07:46:37 +00:00
|
|
|
client_->OnReceiveResponse(response_head.Clone(), std::move(consumer),
|
2024-01-10 22:23:35 +00:00
|
|
|
std::nullopt);
|
2019-06-11 05:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void URLPipeLoader::OnWrite(base::OnceClosure resume, MojoResult result) {
|
|
|
|
if (result == MOJO_RESULT_OK)
|
|
|
|
std::move(resume).Run();
|
|
|
|
else
|
|
|
|
NotifyComplete(net::ERR_FAILED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void URLPipeLoader::OnDataReceived(base::StringPiece string_piece,
|
|
|
|
base::OnceClosure resume) {
|
|
|
|
producer_->Write(
|
2019-07-24 22:58:51 +00:00
|
|
|
std::make_unique<mojo::StringDataSource>(
|
|
|
|
string_piece, mojo::StringDataSource::AsyncWritingMode::
|
2021-06-14 01:18:52 +00:00
|
|
|
STRING_MAY_BE_INVALIDATED_BEFORE_COMPLETION),
|
2019-06-11 05:07:58 +00:00
|
|
|
base::BindOnce(&URLPipeLoader::OnWrite, weak_factory_.GetWeakPtr(),
|
|
|
|
std::move(resume)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void URLPipeLoader::OnRetry(base::OnceClosure start_retry) {
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void URLPipeLoader::OnComplete(bool success) {
|
|
|
|
NotifyComplete(loader_->NetError());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace electron
|