2019-09-03 22:54:14 +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.
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/api/electron_api_data_pipe_holder.h"
|
2019-09-03 22:54:14 +00:00
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "base/memory/weak_ptr.h"
|
2020-05-19 17:18:12 +00:00
|
|
|
#include "base/no_destructor.h"
|
2019-09-03 22:54:14 +00:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
|
|
|
#include "mojo/public/cpp/system/data_pipe.h"
|
|
|
|
#include "mojo/public/cpp/system/simple_watcher.h"
|
|
|
|
#include "net/base/net_errors.h"
|
2019-11-01 06:10:32 +00:00
|
|
|
#include "shell/common/gin_helper/promise.h"
|
2019-09-03 22:54:14 +00:00
|
|
|
#include "shell/common/key_weak_map.h"
|
|
|
|
|
|
|
|
#include "shell/common/node_includes.h"
|
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Incremental ID.
|
|
|
|
int g_next_id = 0;
|
|
|
|
|
|
|
|
// Map that manages all the DataPipeHolder objects.
|
2020-05-19 17:18:12 +00:00
|
|
|
KeyWeakMap<std::string>& AllDataPipeHolders() {
|
|
|
|
static base::NoDestructor<KeyWeakMap<std::string>> weak_map;
|
|
|
|
return *weak_map.get();
|
|
|
|
}
|
2019-09-03 22:54:14 +00:00
|
|
|
|
|
|
|
// Utility class to read from data pipe.
|
|
|
|
class DataPipeReader {
|
|
|
|
public:
|
2019-11-01 06:10:32 +00:00
|
|
|
DataPipeReader(gin_helper::Promise<v8::Local<v8::Value>> promise,
|
2019-09-18 19:58:00 +00:00
|
|
|
mojo::Remote<network::mojom::DataPipeGetter> data_pipe_getter)
|
2019-09-03 22:54:14 +00:00
|
|
|
: promise_(std::move(promise)),
|
|
|
|
data_pipe_getter_(std::move(data_pipe_getter)),
|
|
|
|
handle_watcher_(FROM_HERE,
|
|
|
|
mojo::SimpleWatcher::ArmingPolicy::MANUAL,
|
|
|
|
base::SequencedTaskRunnerHandle::Get()) {
|
|
|
|
// Get a new data pipe and start.
|
2021-03-04 17:27:05 +00:00
|
|
|
mojo::ScopedDataPipeProducerHandle producer_handle;
|
|
|
|
CHECK_EQ(mojo::CreateDataPipe(nullptr, producer_handle, data_pipe_),
|
|
|
|
MOJO_RESULT_OK);
|
|
|
|
data_pipe_getter_->Read(std::move(producer_handle),
|
2019-09-03 22:54:14 +00:00
|
|
|
base::BindOnce(&DataPipeReader::ReadCallback,
|
|
|
|
weak_factory_.GetWeakPtr()));
|
|
|
|
handle_watcher_.Watch(data_pipe_.get(), MOJO_HANDLE_SIGNAL_READABLE,
|
|
|
|
base::BindRepeating(&DataPipeReader::OnHandleReadable,
|
|
|
|
weak_factory_.GetWeakPtr()));
|
|
|
|
}
|
|
|
|
|
|
|
|
~DataPipeReader() = default;
|
|
|
|
|
2021-11-03 11:41:45 +00:00
|
|
|
// disable copy
|
|
|
|
DataPipeReader(const DataPipeReader&) = delete;
|
|
|
|
DataPipeReader& operator=(const DataPipeReader&) = delete;
|
|
|
|
|
2019-09-03 22:54:14 +00:00
|
|
|
private:
|
|
|
|
// Callback invoked by DataPipeGetter::Read.
|
|
|
|
void ReadCallback(int32_t status, uint64_t size) {
|
|
|
|
if (status != net::OK) {
|
|
|
|
OnFailure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buffer_.resize(size);
|
|
|
|
head_ = &buffer_.front();
|
|
|
|
remaining_size_ = size;
|
|
|
|
handle_watcher_.ArmOrNotify();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called by |handle_watcher_| when data is available or the pipe was closed,
|
|
|
|
// and there's a pending Read() call.
|
|
|
|
void OnHandleReadable(MojoResult result) {
|
|
|
|
if (result != MOJO_RESULT_OK) {
|
|
|
|
OnFailure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read.
|
|
|
|
uint32_t length = remaining_size_;
|
|
|
|
result = data_pipe_->ReadData(head_, &length, MOJO_READ_DATA_FLAG_NONE);
|
|
|
|
if (result == MOJO_RESULT_OK) { // success
|
|
|
|
remaining_size_ -= length;
|
|
|
|
head_ += length;
|
|
|
|
if (remaining_size_ == 0)
|
|
|
|
OnSuccess();
|
|
|
|
} else if (result == MOJO_RESULT_SHOULD_WAIT) { // IO pending
|
|
|
|
handle_watcher_.ArmOrNotify();
|
|
|
|
} else { // error
|
|
|
|
OnFailure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnFailure() {
|
|
|
|
promise_.RejectWithErrorMessage("Could not get blob data");
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnSuccess() {
|
|
|
|
// Pass the buffer to JS.
|
|
|
|
//
|
|
|
|
// Note that the lifetime of the native buffer belongs to us, and we will
|
|
|
|
// free memory when JS buffer gets garbage collected.
|
|
|
|
v8::Locker locker(promise_.isolate());
|
|
|
|
v8::HandleScope handle_scope(promise_.isolate());
|
|
|
|
v8::Local<v8::Value> buffer =
|
|
|
|
node::Buffer::New(promise_.isolate(), &buffer_.front(), buffer_.size(),
|
|
|
|
&DataPipeReader::FreeBuffer, this)
|
|
|
|
.ToLocalChecked();
|
|
|
|
promise_.Resolve(buffer);
|
|
|
|
|
|
|
|
// Destroy data pipe.
|
|
|
|
handle_watcher_.Cancel();
|
|
|
|
data_pipe_.reset();
|
2019-09-18 19:58:00 +00:00
|
|
|
data_pipe_getter_.reset();
|
2019-09-03 22:54:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void FreeBuffer(char* data, void* self) {
|
|
|
|
delete static_cast<DataPipeReader*>(self);
|
|
|
|
}
|
|
|
|
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<v8::Local<v8::Value>> promise_;
|
2019-09-03 22:54:14 +00:00
|
|
|
|
2019-09-18 19:58:00 +00:00
|
|
|
mojo::Remote<network::mojom::DataPipeGetter> data_pipe_getter_;
|
2019-09-03 22:54:14 +00:00
|
|
|
mojo::ScopedDataPipeConsumerHandle data_pipe_;
|
|
|
|
mojo::SimpleWatcher handle_watcher_;
|
|
|
|
|
|
|
|
// Stores read data.
|
|
|
|
std::vector<char> buffer_;
|
|
|
|
|
|
|
|
// The head of buffer.
|
|
|
|
char* head_ = nullptr;
|
|
|
|
|
|
|
|
// Remaining data to read.
|
|
|
|
uint64_t remaining_size_ = 0;
|
|
|
|
|
|
|
|
base::WeakPtrFactory<DataPipeReader> weak_factory_{this};
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
gin::WrapperInfo DataPipeHolder::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
|
|
|
|
DataPipeHolder::DataPipeHolder(const network::DataElement& element)
|
2019-09-18 19:58:00 +00:00
|
|
|
: id_(base::NumberToString(++g_next_id)) {
|
2021-01-25 16:46:00 +00:00
|
|
|
data_pipe_.Bind(
|
|
|
|
element.As<network::DataElementDataPipe>().CloneDataPipeGetter());
|
2019-09-18 19:58:00 +00:00
|
|
|
}
|
2019-09-03 22:54:14 +00:00
|
|
|
|
|
|
|
DataPipeHolder::~DataPipeHolder() = default;
|
|
|
|
|
|
|
|
v8::Local<v8::Promise> DataPipeHolder::ReadAll(v8::Isolate* isolate) {
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<v8::Local<v8::Value>> promise(isolate);
|
2019-09-03 22:54:14 +00:00
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
if (!data_pipe_) {
|
|
|
|
promise.RejectWithErrorMessage("Could not get blob data");
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
new DataPipeReader(std::move(promise), std::move(data_pipe_));
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
gin::Handle<DataPipeHolder> DataPipeHolder::Create(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const network::DataElement& element) {
|
|
|
|
auto handle = gin::CreateHandle(isolate, new DataPipeHolder(element));
|
2020-05-19 17:18:12 +00:00
|
|
|
AllDataPipeHolders().Set(isolate, handle->id(),
|
|
|
|
handle->GetWrapper(isolate).ToLocalChecked());
|
2019-09-03 22:54:14 +00:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
gin::Handle<DataPipeHolder> DataPipeHolder::From(v8::Isolate* isolate,
|
|
|
|
const std::string& id) {
|
2020-05-19 17:18:12 +00:00
|
|
|
v8::MaybeLocal<v8::Object> object = AllDataPipeHolders().Get(isolate, id);
|
2019-09-03 22:54:14 +00:00
|
|
|
if (!object.IsEmpty()) {
|
|
|
|
gin::Handle<DataPipeHolder> handle;
|
|
|
|
if (gin::ConvertFromV8(isolate, object.ToLocalChecked(), &handle))
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
return gin::Handle<DataPipeHolder>();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace electron
|