2019-06-11 14:07:58 +09: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 13:46:59 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "shell/browser/net/url_pipe_loader.h"
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-11 14:07:58 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include <utility>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2023-02-03 12:43:42 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "base/task/sequenced_task_runner.h"
							 | 
						
					
						
							
								
									
										
										
										
											2019-12-10 16:22:35 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "mojo/public/cpp/bindings/pending_remote.h"
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-24 15:58:51 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "mojo/public/cpp/system/string_data_source.h"
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-11 14:07:58 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "services/network/public/cpp/shared_url_loader_factory.h"
							 | 
						
					
						
							
								
									
										
										
										
											2021-07-26 09:02:16 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "services/network/public/mojom/url_response_head.mojom.h"
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-11 14:07:58 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-19 14:23:04 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								namespace electron {
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-11 14:07:58 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								URLPipeLoader::URLPipeLoader(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    scoped_refptr<network::SharedURLLoaderFactory> factory,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    std::unique_ptr<network::ResourceRequest> request,
							 | 
						
					
						
							
								
									
										
										
										
											2021-03-04 09:27:05 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    mojo::PendingReceiver<network::mojom::URLLoader> loader,
							 | 
						
					
						
							
								
									
										
										
										
											2019-12-10 16:22:35 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    mojo::PendingRemote<network::mojom::URLLoaderClient> client,
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-12 08:37:06 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const net::NetworkTrafficAnnotationTag& annotation,
							 | 
						
					
						
							
								
									
										
										
										
											2022-07-05 08:25:18 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    base::Value::Dict upload_data)
							 | 
						
					
						
							
								
									
										
										
										
											2021-03-04 09:27:05 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    : url_loader_(this, std::move(loader)), client_(std::move(client)) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  url_loader_.set_disconnect_handler(base::BindOnce(
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-11 14:07:58 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      &URLPipeLoader::NotifyComplete, base::Unretained(this), net::ERR_FAILED));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  // PostTask since it might destruct.
							 | 
						
					
						
							
								
									
										
										
										
											2023-02-03 12:43:42 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-11 14:07:58 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      FROM_HERE,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      base::BindOnce(&URLPipeLoader::Start, weak_factory_.GetWeakPtr(), factory,
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-12 08:37:06 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                     std::move(request), annotation, std::move(upload_data)));
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-11 14:07:58 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								URLPipeLoader::~URLPipeLoader() = default;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								void URLPipeLoader::Start(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    scoped_refptr<network::SharedURLLoaderFactory> factory,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    std::unique_ptr<network::ResourceRequest> request,
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-12 08:37:06 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const net::NetworkTrafficAnnotationTag& annotation,
							 | 
						
					
						
							
								
									
										
										
										
											2022-07-05 08:25:18 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    base::Value::Dict upload_data) {
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-11 14:07:58 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  loader_ = network::SimpleURLLoader::Create(std::move(request), annotation);
							 | 
						
					
						
							
								
									
										
										
										
											2021-01-25 02:27:40 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  loader_->SetOnResponseStartedCallback(base::BindOnce(
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-11 14:07:58 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      &URLPipeLoader::OnResponseStarted, weak_factory_.GetWeakPtr()));
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-12 08:37:06 +09: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 08:25:18 -07: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-12 08:37:06 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-11 14:07:58 +09: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 10:51:28 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const network::mojom::URLResponseHead& response_head) {
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-11 14:07:58 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  mojo::ScopedDataPipeProducerHandle producer;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  mojo::ScopedDataPipeConsumerHandle consumer;
							 | 
						
					
						
							
								
									
										
										
										
											2021-03-05 16:42:15 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  MojoResult rv = mojo::CreateDataPipe(nullptr, producer, consumer);
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-11 14:07:58 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  if (rv != MOJO_RESULT_OK) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    NotifyComplete(net::ERR_INSUFFICIENT_RESOURCES);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    return;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-07-24 15:58:51 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  producer_ = std::make_unique<mojo::DataPipeProducer>(std::move(producer));
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-11 14:07:58 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2022-09-07 09:46:37 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  client_->OnReceiveResponse(response_head.Clone(), std::move(consumer),
							 | 
						
					
						
							
								
									
										
										
										
											2024-01-10 23:23:35 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                             std::nullopt);
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-11 14:07:58 +09: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 15:58:51 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      std::make_unique<mojo::StringDataSource>(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          string_piece, mojo::StringDataSource::AsyncWritingMode::
							 | 
						
					
						
							
								
									
										
										
										
											2021-06-13 18:18:52 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                            STRING_MAY_BE_INVALIDATED_BEFORE_COMPLETION),
							 | 
						
					
						
							
								
									
										
										
										
											2019-06-11 14:07:58 +09: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 14:23:04 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}  // namespace electron
							 |