7e59d784a0
* chore: bump chromium in DEPS to 101.0.4911.0
* chore: bump chromium in DEPS to 101.0.4913.0
* chore: bump chromium in DEPS to 101.0.4915.0
* chore: bump chromium in DEPS to 101.0.4917.0
* chore: bump chromium in DEPS to 101.0.4919.0
* chore: bump chromium in DEPS to 101.0.4921.0
* chore: bump chromium in DEPS to 101.0.4923.0
* chore: bump chromium in DEPS to 101.0.4925.0
* chore: bump chromium in DEPS to 101.0.4927.0
* chore: bump chromium in DEPS to 101.0.4929.0
* chore: update patches
* chore: bump chromium in DEPS to 101.0.4931.0
* chore: update patches
* 3475388: Remove mojo::InterfacePtr<T> and mojo::InterfacePtrInfo<T>
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3475388
Actual fixes in 1824792: Migrate DisplayClient to the new Mojo types | https://chromium-review.googlesource.com/c/chromium/src/+/1824792
* 3503874: Remove base::size(), base::empty(), and base::data().
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3503874
* chore: reconcile patches with main rebase
* chore: bump chromium in DEPS to 101.0.4933.0
* chore: update patches
* 3329593: [Fenced Frame] Ensure to support external protocols in a fenced frame
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3329593
* 3503874: Remove base::size(), base::empty(), and base::data().
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3503874
* 3446451: Use forward decl of ImageSkiaRep in //ui/gfx/image/image_skia.h
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3446451
* 3499818: partition_alloc: Rename AllocFlags to AllocWithFlags
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3499818
* chore: bump chromium in DEPS to 101.0.4935.0
* chore: update patches
* 3463286: partition_alloc: Move PartitionAlloc into the own namespaces (15 of N)
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3463286
* 3506590: Reland "Support ChromeOS external protocol dialog for Fenced Frame navigations"
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3506590
* 3475388: Remove mojo::InterfacePtr<T> and mojo::InterfacePtrInfo<T>
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3475388
Actual fixes in 1880987: Convert URLLoaderReqeust from //content to new Mojo types | https://chromium-review.googlesource.com/c/chromium/src/+/1880987 The change in the roll started causing the legacy types to fail
* chore: missing SkRegion include
* 3499600: Introduce blink::WebCssOrigin
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3499600
* fixup!: 3503874: Remove base::size(), base::empty(), and base::data().
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3503874
* chore: bump chromium in DEPS to 101.0.4937.0
* chore: update patches
* 3500826: [locales] Refactor locales for ios
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3500826
* 3509531: Make some public Blink media files private
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3509531
* 3497377: bluetooth: Add BluetoothDevice.forget()
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3497377
* chore: bump chromium in DEPS to 101.0.4939.0
* chore: bump chromium in DEPS to 101.0.4941.0
* 3514804: Deprecate all existing uses of mojo_base.mojom.{Dictionary,List}Value.
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3514804
* 3502592: Delete PPAPI init/shutdown code in //pdf.
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3502592
* chore: update patches
* fixup! 3502592: Delete PPAPI init/shutdown code in //pdf.
* chore: bump chromium in DEPS to 101.0.4943.0
* chore: fix lint, remove unneeded headers
* fixup! 3475388: Remove mojo::InterfacePtr<T> and mojo::InterfacePtrInfo<T>
* update mojo calls in offscreen patch
* update hunspell filenames
* chore: bump chromium in DEPS to 101.0.4945.0
* chore: update patches
* fix offscreen patch again
* chore: bump chromium in DEPS to 101.0.4947.0
* chore: update patches
* chore: bump chromium in DEPS to 101.0.4949.0
* support unseasoned pdf
* update patches
* chore: update patches
* chore: [IWYU] include missing skia headers
* chore: bump chromium in DEPS to 101.0.4951.0
* chore: update patches
* 3457645: media: Remove IsKeySystemsUpdateNeeded()
https://chromium-review.googlesource.com/c/chromium/src/+/3457645
* chore: bump chromium in DEPS to 102.0.4952.2
* chore: update patches
* 3488672: Add documentId as a parameter in tabs.connect() and tabs.sendMessage().
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3488672
* 3508375: Fix an issue dangerous dialog is not shown for some apk download
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3508375
* chore: bump chromium in DEPS to 102.0.4953.0
* chore: update patches
* 3510189: Harden up drag and drop support across same-process boundaries.
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3510189
* 3526815: Remove hardcoded colors from chrome/browser/ui/views/overlay/.
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3526815
* chore: bump chromium in DEPS to 102.0.4955.0
* build: add af and ur locale to manifests
3498914 [locales] Add af and ur to desktop
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3498914
* fixup! build: add af and ur locale to manifests
* chore: bump chromium in DEPS to 102.0.4957.0
* 3529090: gin: set JS flags before v8 initialization
Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3529090
chore: fix code shear in chromium/gin_enable_disable_v8_platform.patch
* chore: update patches
* 3536433: [network] Rename data_path and http_cache_path from _path to _directory.
Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3536433
* 3111565: Relocated Page.printToPDF implementation to //components
Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3111565
refactor: inject E args to PrintRequestedPages() and ScriptedPrintCallback
TODO: currently passes a placeholder for job_settings. We have other
code paths that inject settings from electron_api_web_contents.cc.
Should those be injected here as well? (CC @codebytere)
* fixup! 3111565: Relocated Page.printToPDF implementation to //components
* fixup! 3111565: Relocated Page.printToPDF implementation to //components
* 3520025: Make "libcxx_abi_unstable" not a gn arg
Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3520025
build: since it is no longer a gn arg, patch it in
* chore: change usages of std::vector with const elements (#33373)
* chore: bump chromium in DEPS to 102.0.4959.0
* chore: update patches
* build: iwyu base/threading/platform_thread.h
* 3525774: Add GPSForCurrentDocument() into PermissionControllerDelegate.
Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3525774
refactor: copy upstream impl of GetPermissionStatusForCurrentDocument into +ElectronPermissionManager
* use gclient_gn_args_from instead of hand-copying
* checkout mac on mac
* chore: update patches
* Revert "checkout mac on mac"
This reverts commit fe9ccf49ec6139868ccf2272c2016cefa3f32769.
* fixup! 3525774: Add GPSForCurrentDocument() into PermissionControllerDelegate.
* fixup! 3457645: media: Remove IsKeySystemsUpdateNeeded()
add nogncheck
* fix: set .eslintrc.json to root to avoid cascade to chromium eslintrc
* Xref: 6dfdf79b8c
Xref: https://reviews.llvm.org/D101458
Upstream added a CMakeLists.txt in an include dir ¯\_(ツ)_/¯ and
so it must be enumerated in filenames.libcxxabi.gni
* 3511268: Remove unused headers from cxx17_backports.h
https://chromium-review.googlesource.com/c/chromium/src/+/3511268
use std::size instead of base::size
* iwyu: SkPaint
3488428: [includes] Fix transitive includes of SkImageEncoder
* chore: [IWYU] include missing skia headers
* fixup! 3511268: Remove unused headers from cxx17_backports.h
* chore: bump chromium in DEPS to 102.0.4961.0
* chore: update patches
* fixup! 3475388: Remove mojo::InterfacePtr<T> and mojo::InterfacePtrInfo<T>
chore: remove unused #include
* fixup! 3510189: Harden up drag and drop support across same-process boundaries. | https://chromium-review.googlesource.com/c/chromium/src/+/3510189
Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: Keeley Hammond <khammond@slack-corp.com>
Co-authored-by: VerteDinde <vertedinde@electronjs.org>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
Co-authored-by: Jeremy Rose <nornagon@nornagon.net>
Co-authored-by: VerteDinde <keeleymhammond@gmail.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
Co-authored-by: David Sanders <dsanders11@ucsbalum.com>
Co-authored-by: Jeremy Rose <jeremya@chromium.org>
405 lines
16 KiB
C++
405 lines
16 KiB
C++
// Copyright (c) 2019 GitHub, Inc.
|
|
// Use of this source code is governed by the MIT license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include "shell/browser/net/asar/asar_url_loader.h"
|
|
|
|
#include <algorithm>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "base/strings/stringprintf.h"
|
|
#include "base/task/post_task.h"
|
|
#include "base/task/thread_pool.h"
|
|
#include "content/public/browser/file_url_loader.h"
|
|
#include "electron/fuses.h"
|
|
#include "mojo/public/cpp/bindings/receiver.h"
|
|
#include "mojo/public/cpp/bindings/remote.h"
|
|
#include "mojo/public/cpp/system/data_pipe_producer.h"
|
|
#include "mojo/public/cpp/system/file_data_source.h"
|
|
#include "net/base/filename_util.h"
|
|
#include "net/base/mime_sniffer.h"
|
|
#include "net/base/mime_util.h"
|
|
#include "net/http/http_byte_range.h"
|
|
#include "net/http/http_util.h"
|
|
#include "services/network/public/mojom/url_response_head.mojom.h"
|
|
#include "shell/browser/net/asar/asar_file_validator.h"
|
|
#include "shell/common/asar/archive.h"
|
|
#include "shell/common/asar/asar_util.h"
|
|
|
|
namespace asar {
|
|
|
|
namespace {
|
|
|
|
net::Error ConvertMojoResultToNetError(MojoResult result) {
|
|
switch (result) {
|
|
case MOJO_RESULT_OK:
|
|
return net::OK;
|
|
case MOJO_RESULT_NOT_FOUND:
|
|
return net::ERR_FILE_NOT_FOUND;
|
|
case MOJO_RESULT_PERMISSION_DENIED:
|
|
return net::ERR_ACCESS_DENIED;
|
|
case MOJO_RESULT_RESOURCE_EXHAUSTED:
|
|
return net::ERR_INSUFFICIENT_RESOURCES;
|
|
case MOJO_RESULT_ABORTED:
|
|
return net::ERR_ABORTED;
|
|
default:
|
|
return net::ERR_FAILED;
|
|
}
|
|
}
|
|
|
|
constexpr size_t kDefaultFileUrlPipeSize = 65536;
|
|
|
|
// Because this makes things simpler.
|
|
static_assert(kDefaultFileUrlPipeSize >= net::kMaxBytesToSniff,
|
|
"Default file data pipe size must be at least as large as a MIME-"
|
|
"type sniffing buffer.");
|
|
|
|
// Modified from the |FileURLLoader| in |file_url_loader_factory.cc|, to serve
|
|
// asar files instead of normal files.
|
|
class AsarURLLoader : public network::mojom::URLLoader {
|
|
public:
|
|
static void CreateAndStart(
|
|
const network::ResourceRequest& request,
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
scoped_refptr<net::HttpResponseHeaders> extra_response_headers) {
|
|
// Owns itself. Will live as long as its URLLoader and URLLoaderClientPtr
|
|
// bindings are alive - essentially until either the client gives up or all
|
|
// file data has been sent to it.
|
|
auto* asar_url_loader = new AsarURLLoader;
|
|
asar_url_loader->Start(request, std::move(loader), std::move(client),
|
|
std::move(extra_response_headers));
|
|
}
|
|
|
|
// network::mojom::URLLoader:
|
|
void FollowRedirect(
|
|
const std::vector<std::string>& removed_headers,
|
|
const net::HttpRequestHeaders& modified_headers,
|
|
const net::HttpRequestHeaders& modified_cors_exempt_headers,
|
|
const absl::optional<GURL>& new_url) override {}
|
|
void SetPriority(net::RequestPriority priority,
|
|
int32_t intra_priority_value) override {}
|
|
void PauseReadingBodyFromNet() override {}
|
|
void ResumeReadingBodyFromNet() override {}
|
|
|
|
// disable copy
|
|
AsarURLLoader(const AsarURLLoader&) = delete;
|
|
AsarURLLoader& operator=(const AsarURLLoader&) = delete;
|
|
|
|
private:
|
|
AsarURLLoader() = default;
|
|
~AsarURLLoader() override = default;
|
|
|
|
void Start(const network::ResourceRequest& request,
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
scoped_refptr<net::HttpResponseHeaders> extra_response_headers) {
|
|
auto head = network::mojom::URLResponseHead::New();
|
|
head->request_start = base::TimeTicks::Now();
|
|
head->response_start = base::TimeTicks::Now();
|
|
head->headers = extra_response_headers;
|
|
|
|
base::FilePath path;
|
|
if (!net::FileURLToFilePath(request.url, &path)) {
|
|
mojo::Remote<network::mojom::URLLoaderClient> client_remote(
|
|
std::move(client));
|
|
client_remote->OnComplete(
|
|
network::URLLoaderCompletionStatus(net::ERR_FAILED));
|
|
MaybeDeleteSelf();
|
|
return;
|
|
}
|
|
|
|
// Determine whether it is an asar file.
|
|
base::FilePath asar_path, relative_path;
|
|
if (!GetAsarArchivePath(path, &asar_path, &relative_path)) {
|
|
content::CreateFileURLLoaderBypassingSecurityChecks(
|
|
request, std::move(loader), std::move(client), nullptr, false,
|
|
extra_response_headers);
|
|
MaybeDeleteSelf();
|
|
return;
|
|
}
|
|
|
|
client_.Bind(std::move(client));
|
|
receiver_.Bind(std::move(loader));
|
|
receiver_.set_disconnect_handler(base::BindOnce(
|
|
&AsarURLLoader::OnConnectionError, base::Unretained(this)));
|
|
|
|
// Parse asar archive.
|
|
std::shared_ptr<Archive> archive = GetOrCreateAsarArchive(asar_path);
|
|
Archive::FileInfo info;
|
|
if (!archive || !archive->GetFileInfo(relative_path, &info)) {
|
|
OnClientComplete(net::ERR_FILE_NOT_FOUND);
|
|
return;
|
|
}
|
|
bool is_verifying_file = info.integrity.has_value();
|
|
|
|
// For unpacked path, read like normal file.
|
|
base::FilePath real_path;
|
|
if (info.unpacked) {
|
|
archive->CopyFileOut(relative_path, &real_path);
|
|
info.offset = 0;
|
|
}
|
|
|
|
mojo::ScopedDataPipeProducerHandle producer_handle;
|
|
mojo::ScopedDataPipeConsumerHandle consumer_handle;
|
|
if (mojo::CreateDataPipe(kDefaultFileUrlPipeSize, producer_handle,
|
|
consumer_handle) != MOJO_RESULT_OK) {
|
|
OnClientComplete(net::ERR_FAILED);
|
|
return;
|
|
}
|
|
|
|
// Note that while the |Archive| already opens a |base::File|, we still need
|
|
// to create a new |base::File| here, as it might be accessed by multiple
|
|
// requests at the same time.
|
|
base::File file(info.unpacked ? real_path : archive->path(),
|
|
base::File::FLAG_OPEN | base::File::FLAG_READ);
|
|
auto file_data_source =
|
|
std::make_unique<mojo::FileDataSource>(file.Duplicate());
|
|
std::unique_ptr<mojo::DataPipeProducer::DataSource> readable_data_source;
|
|
mojo::FileDataSource* file_data_source_raw = file_data_source.get();
|
|
AsarFileValidator* file_validator_raw = nullptr;
|
|
uint32_t block_size = 0;
|
|
if (info.integrity.has_value()) {
|
|
block_size = info.integrity.value().block_size;
|
|
auto asar_validator = std::make_unique<AsarFileValidator>(
|
|
std::move(info.integrity.value()), std::move(file));
|
|
file_validator_raw = asar_validator.get();
|
|
readable_data_source.reset(new mojo::FilteredDataSource(
|
|
std::move(file_data_source), std::move(asar_validator)));
|
|
} else {
|
|
readable_data_source = std::move(file_data_source);
|
|
}
|
|
|
|
std::vector<char> initial_read_buffer(
|
|
std::min(static_cast<uint32_t>(net::kMaxBytesToSniff), info.size));
|
|
auto read_result = readable_data_source.get()->Read(
|
|
info.offset, base::span<char>(initial_read_buffer));
|
|
if (read_result.result != MOJO_RESULT_OK) {
|
|
OnClientComplete(ConvertMojoResultToNetError(read_result.result));
|
|
return;
|
|
}
|
|
|
|
std::string range_header;
|
|
net::HttpByteRange byte_range;
|
|
if (request.headers.GetHeader(net::HttpRequestHeaders::kRange,
|
|
&range_header)) {
|
|
// Handle a simple Range header for a single range.
|
|
std::vector<net::HttpByteRange> ranges;
|
|
bool fail = false;
|
|
if (net::HttpUtil::ParseRangeHeader(range_header, &ranges) &&
|
|
ranges.size() == 1) {
|
|
byte_range = ranges[0];
|
|
if (!byte_range.ComputeBounds(info.size))
|
|
fail = true;
|
|
} else {
|
|
fail = true;
|
|
}
|
|
|
|
if (fail) {
|
|
OnClientComplete(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE);
|
|
return;
|
|
}
|
|
}
|
|
|
|
uint64_t first_byte_to_send = 0;
|
|
uint64_t total_bytes_dropped_from_head = initial_read_buffer.size();
|
|
uint64_t total_bytes_to_send = info.size;
|
|
|
|
if (byte_range.IsValid()) {
|
|
first_byte_to_send = byte_range.first_byte_position();
|
|
total_bytes_to_send =
|
|
byte_range.last_byte_position() - first_byte_to_send + 1;
|
|
}
|
|
|
|
total_bytes_written_ = total_bytes_to_send;
|
|
|
|
head->content_length = base::saturated_cast<int64_t>(total_bytes_to_send);
|
|
|
|
if (first_byte_to_send < read_result.bytes_read) {
|
|
// Write any data we read for MIME sniffing, constraining by range where
|
|
// applicable. This will always fit in the pipe (see assertion near
|
|
// |kDefaultFileUrlPipeSize| definition).
|
|
uint32_t write_size = std::min(
|
|
static_cast<uint32_t>(read_result.bytes_read - first_byte_to_send),
|
|
static_cast<uint32_t>(total_bytes_to_send));
|
|
const uint32_t expected_write_size = write_size;
|
|
MojoResult result =
|
|
producer_handle->WriteData(&initial_read_buffer[first_byte_to_send],
|
|
&write_size, MOJO_WRITE_DATA_FLAG_NONE);
|
|
if (result != MOJO_RESULT_OK || write_size != expected_write_size) {
|
|
OnFileWritten(result);
|
|
return;
|
|
}
|
|
|
|
// Discount the bytes we just sent from the total range.
|
|
first_byte_to_send = read_result.bytes_read;
|
|
total_bytes_to_send -= write_size;
|
|
} else if (is_verifying_file &&
|
|
first_byte_to_send >= static_cast<uint64_t>(block_size)) {
|
|
// If validation is active and the range of bytes the request wants starts
|
|
// beyond the first block we need to read the next 4MB-1KB to validate
|
|
// that block. Then we can skip ahead to the target block in the SetRange
|
|
// call below If we hit this case it is assumed that none of the data read
|
|
// will be needed by the producer
|
|
uint64_t bytes_to_drop = block_size - net::kMaxBytesToSniff;
|
|
total_bytes_dropped_from_head += bytes_to_drop;
|
|
std::vector<char> abandoned_buffer(bytes_to_drop);
|
|
auto abandon_read_result =
|
|
readable_data_source.get()->Read(info.offset + net::kMaxBytesToSniff,
|
|
base::span<char>(abandoned_buffer));
|
|
if (abandon_read_result.result != MOJO_RESULT_OK) {
|
|
OnClientComplete(
|
|
ConvertMojoResultToNetError(abandon_read_result.result));
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (!net::GetMimeTypeFromFile(path, &head->mime_type)) {
|
|
std::string new_type;
|
|
net::SniffMimeType(
|
|
base::StringPiece(initial_read_buffer.data(), read_result.bytes_read),
|
|
request.url, head->mime_type,
|
|
net::ForceSniffFileUrlsForHtml::kDisabled, &new_type);
|
|
head->mime_type.assign(new_type);
|
|
head->did_mime_sniff = true;
|
|
}
|
|
if (head->headers) {
|
|
head->headers->AddHeader(net::HttpRequestHeaders::kContentType,
|
|
head->mime_type.c_str());
|
|
}
|
|
client_->OnReceiveResponse(std::move(head),
|
|
mojo::ScopedDataPipeConsumerHandle());
|
|
client_->OnStartLoadingResponseBody(std::move(consumer_handle));
|
|
|
|
if (total_bytes_to_send == 0) {
|
|
// There's definitely no more data, so we're already done.
|
|
// We provide the range data to the file validator so that
|
|
// it can validate the tiny amount of data we did send
|
|
if (file_validator_raw)
|
|
file_validator_raw->SetRange(info.offset + first_byte_to_send,
|
|
total_bytes_dropped_from_head,
|
|
info.offset + info.size);
|
|
OnFileWritten(MOJO_RESULT_OK);
|
|
return;
|
|
}
|
|
|
|
if (is_verifying_file) {
|
|
int start_block = first_byte_to_send / block_size;
|
|
|
|
// If we're starting from the first block, we might not be starting from
|
|
// where we sniffed. We might be a few KB into a file so we need to read
|
|
// the data in the middle so it gets hashed.
|
|
//
|
|
// If we're starting from a later block we might be starting half-way
|
|
// through the block regardless of what was sniffed. We need to read the
|
|
// data from the start of our initial block up to the start of our actual
|
|
// read point so it gets hashed.
|
|
uint64_t bytes_to_drop =
|
|
start_block == 0 ? first_byte_to_send - net::kMaxBytesToSniff
|
|
: first_byte_to_send - (start_block * block_size);
|
|
if (file_validator_raw)
|
|
file_validator_raw->SetCurrentBlock(start_block);
|
|
|
|
if (bytes_to_drop > 0) {
|
|
uint64_t dropped_bytes_offset =
|
|
info.offset + (start_block * block_size);
|
|
if (start_block == 0)
|
|
dropped_bytes_offset += net::kMaxBytesToSniff;
|
|
total_bytes_dropped_from_head += bytes_to_drop;
|
|
std::vector<char> abandoned_buffer(bytes_to_drop);
|
|
auto abandon_read_result = readable_data_source.get()->Read(
|
|
dropped_bytes_offset, base::span<char>(abandoned_buffer));
|
|
if (abandon_read_result.result != MOJO_RESULT_OK) {
|
|
OnClientComplete(
|
|
ConvertMojoResultToNetError(abandon_read_result.result));
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
// In case of a range request, seek to the appropriate position before
|
|
// sending the remaining bytes asynchronously. Under normal conditions
|
|
// (i.e., no range request) this Seek is effectively a no-op.
|
|
//
|
|
// Note that in Electron we also need to add file offset.
|
|
file_data_source_raw->SetRange(
|
|
first_byte_to_send + info.offset,
|
|
first_byte_to_send + info.offset + total_bytes_to_send);
|
|
if (file_validator_raw)
|
|
file_validator_raw->SetRange(info.offset + first_byte_to_send,
|
|
total_bytes_dropped_from_head,
|
|
info.offset + info.size);
|
|
|
|
data_producer_ =
|
|
std::make_unique<mojo::DataPipeProducer>(std::move(producer_handle));
|
|
data_producer_->Write(
|
|
std::move(readable_data_source),
|
|
base::BindOnce(&AsarURLLoader::OnFileWritten, base::Unretained(this)));
|
|
}
|
|
|
|
void OnConnectionError() {
|
|
receiver_.reset();
|
|
MaybeDeleteSelf();
|
|
}
|
|
|
|
void OnClientComplete(net::Error net_error) {
|
|
client_->OnComplete(network::URLLoaderCompletionStatus(net_error));
|
|
client_.reset();
|
|
MaybeDeleteSelf();
|
|
}
|
|
|
|
void MaybeDeleteSelf() {
|
|
if (!receiver_.is_bound() && !client_.is_bound())
|
|
delete this;
|
|
}
|
|
|
|
void OnFileWritten(MojoResult result) {
|
|
// All the data has been written now. Close the data pipe. The consumer will
|
|
// be notified that there will be no more data to read from now.
|
|
data_producer_.reset();
|
|
|
|
if (result == MOJO_RESULT_OK) {
|
|
network::URLLoaderCompletionStatus status(net::OK);
|
|
status.encoded_data_length = total_bytes_written_;
|
|
status.encoded_body_length = total_bytes_written_;
|
|
status.decoded_body_length = total_bytes_written_;
|
|
client_->OnComplete(status);
|
|
} else {
|
|
client_->OnComplete(network::URLLoaderCompletionStatus(net::ERR_FAILED));
|
|
}
|
|
client_.reset();
|
|
MaybeDeleteSelf();
|
|
}
|
|
|
|
std::unique_ptr<mojo::DataPipeProducer> data_producer_;
|
|
mojo::Receiver<network::mojom::URLLoader> receiver_{this};
|
|
mojo::Remote<network::mojom::URLLoaderClient> client_;
|
|
|
|
// In case of successful loads, this holds the total number of bytes written
|
|
// to the response (this may be smaller than the total size of the file when
|
|
// a byte range was requested).
|
|
// It is used to set some of the URLLoaderCompletionStatus data passed back
|
|
// to the URLLoaderClients (eg SimpleURLLoader).
|
|
size_t total_bytes_written_ = 0;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
void CreateAsarURLLoader(
|
|
const network::ResourceRequest& request,
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
scoped_refptr<net::HttpResponseHeaders> extra_response_headers) {
|
|
auto task_runner = base::ThreadPool::CreateSequencedTaskRunner(
|
|
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
|
|
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN});
|
|
task_runner->PostTask(
|
|
FROM_HERE,
|
|
base::BindOnce(&AsarURLLoader::CreateAndStart, request, std::move(loader),
|
|
std::move(client), std::move(extra_response_headers)));
|
|
}
|
|
|
|
} // namespace asar
|