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>
|
|
|
|
|
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"
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2019-06-11 05:07:58 +00:00
|
|
|
|
|
|
|
URLPipeLoader::URLPipeLoader(
|
|
|
|
scoped_refptr<network::SharedURLLoaderFactory> factory,
|
|
|
|
std::unique_ptr<network::ResourceRequest> request,
|
|
|
|
network::mojom::URLLoaderRequest 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,
|
|
|
|
base::DictionaryValue upload_data)
|
2019-06-11 05:07:58 +00:00
|
|
|
: binding_(this, std::move(loader)),
|
|
|
|
client_(std::move(client)),
|
|
|
|
weak_factory_(this) {
|
|
|
|
binding_.set_connection_error_handler(base::BindOnce(
|
|
|
|
&URLPipeLoader::NotifyComplete, base::Unretained(this), net::ERR_FAILED));
|
|
|
|
|
|
|
|
// PostTask since it might destruct.
|
|
|
|
base::SequencedTaskRunnerHandle::Get()->PostTask(
|
|
|
|
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,
|
|
|
|
base::DictionaryValue 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.
|
|
|
|
std::string content_type, data;
|
|
|
|
if (upload_data.GetString("contentType", &content_type) &&
|
|
|
|
upload_data.GetString("data", &data))
|
|
|
|
loader_->AttachStringForUpload(data, content_type);
|
|
|
|
|
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;
|
|
|
|
MojoResult rv = mojo::CreateDataPipe(nullptr, &producer, &consumer);
|
|
|
|
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
|
|
|
|
2019-09-21 14:51:28 +00:00
|
|
|
client_->OnReceiveResponse(response_head.Clone());
|
2019-06-11 05:07:58 +00:00
|
|
|
client_->OnStartLoadingResponseBody(std::move(consumer));
|
|
|
|
}
|
|
|
|
|
|
|
|
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::
|
|
|
|
STRING_STAYS_VALID_UNTIL_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());
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|