mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
* chore: bump chromium in DEPS to 89.0.4386.0 * update patches * 2596667: Reland "[printing] Mojofy PrintHostMsg_CheckForCancel" https://chromium-review.googlesource.com/c/chromium/src/+/2596667 (cherry picked from commit70fb0fe694) * fixup for 2596667: Reland "[printing] Mojofy PrintHostMsg_CheckForCancel * 2596880: Refactor frame detach helpers to return a bool indicating success. Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2596880 * 2596880: Refactor frame detach helpers to return a bool indicating success. https://chromium-review.googlesource.com/c/chromium/src/+/2596880 * 2615203: [Code health] Convert DispatchCallback to OnceCallback Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2615203 * 2587618: Refactor contrast tracking in NativeTheme Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2587618 * 2596289: Use ContextMenuDataMediaType Mojo enum instead of the native one Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2596289 * 2587049: Move blink.mojom.DragOperation to //ui/base/dragdrop Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2587049 * 2585255: Move web_isolated_world_ids.h from public/platform to renderer/platform/bindings in blink Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2585255 * 2602887: Convert CheckForUpdates callback to OnceCallback Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2602887 * 2601061: Convert ChromeManagementAPIDelegate to OnceCallback Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2601061 * 2600391: Convert MessagingDelegate to OnceCallback Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2600391 * 2583590: webui: Remove UseWebUIBindingsForURL Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2583590 * 2562002: Deprecate ResourceType in ExtensionsBrowserClient Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2562002 * chore: handle AXPropertyFilter::SCRIPT in accessibility_ui Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2563923 * chore: add DISPLAY_CAPTURE permission to converter Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2551098 * fix: add utility class for linting * 2614784: Remove SchemeRegistry APIs for handling local and secure schemes Ref; https://chromium-review.googlesource.com/c/chromium/src/+/2614784 * chore: bump chromium in DEPS to 89.0.4387.0 * 2618392: Rename PAGE to ENTRY in NAVIGATION_TYPE values. Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2618392 * 2616421: Factor out DnsConfigService::Watcher logic Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2616421 * chore: update Chromium patch indices * 2617866: Componentize OptimizationHintsComponentInstaller for reuse on iOS Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2617866 * refactor: refactor x11 event handling, xeventobserver is now x11:eventobserver Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2577887 Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2585750 * 2595524: [XProto] Use x11 namespace instead of gfx for //ui/gfx/x Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2595524 * build: change ui namespace to x11 Ref:c38f8571a8:ui/gfx/x/xproto_util.h;dlc=ba9145d0c7f2b10e869e2ba482ca05b75ca35812 * 2600437: [XProto] Remove old property setters/getters from x11_util Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2600437 * chore: remove deleted headers * 2402398: Adding ViewsAXTreeManager output to chrome://accessibility Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2402398 * chore: revert to old version of circleci for mac builds Ref: https://gn.googlesource.com/gn/+/0d67e272bdb8145f87d238bc0b2cb8bf80ccec90 * fix: remove outdated --experimental-wasm-bigint flag * chore: bump chromium in DEPS to 89.0.4389.0 * update patches * 2592803: [Extensions] Use policy-enforced update URL in ManagementApi Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2592803 * 2599731: Make network::DataElement a union with absl::variant Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2599731 * 2614784: Remove SchemeRegistry APIs for handling local and secure schemes https://chromium-review.googlesource.com/c/chromium/src/+/2614784 * 2624711: Inline and remove peekLast() https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2624711 * chore: add patch to fix blink prefs fetching during frame swap Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com> * update patches * fix: do not make invalid SKImageRep in FrameSubscriber Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2572896 * Revert "2616421: Factor out DnsConfigService::Watcher logic" This reverts commit7214406ecb. * 2616421: Factor out DnsConfigService::Watcher logic https://chromium-review.googlesource.com/c/chromium/src/+/2616421 * chore: bump chromium in DEPS to 89.0.4388.2 * chore: update patches * 2581958: [api] Dehandlify FunctionTemplate initialization Ref: https://chromium-review.googlesource.com/c/v8/v8/+/2581958 * Revert "2592803: [Extensions] Use policy-enforced update URL in ManagementApi" This reverts commit00f4041cc9. * 2572896: Prevent making invalid SkImageReps Ref: https://chromium-review.googlesource.com/c/chromium/src/+/2572896 * fix: sandbox exit event to preload test * fix: restore clipboard_win patch * ci: force delete user app data directories * chore: clean up typing on webview-spec * docs: undocument display-capture ses.permission option Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> Co-authored-by: VerteDinde <khammond@slack-corp.com> Co-authored-by: VerteDinde <keeleymhammond@gmail.com> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com>
192 lines
5.7 KiB
C++
192 lines
5.7 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/api/electron_api_data_pipe_holder.h"
|
|
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "base/memory/weak_ptr.h"
|
|
#include "base/no_destructor.h"
|
|
#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"
|
|
#include "shell/common/gin_helper/promise.h"
|
|
#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.
|
|
KeyWeakMap<std::string>& AllDataPipeHolders() {
|
|
static base::NoDestructor<KeyWeakMap<std::string>> weak_map;
|
|
return *weak_map.get();
|
|
}
|
|
|
|
// Utility class to read from data pipe.
|
|
class DataPipeReader {
|
|
public:
|
|
DataPipeReader(gin_helper::Promise<v8::Local<v8::Value>> promise,
|
|
mojo::Remote<network::mojom::DataPipeGetter> data_pipe_getter)
|
|
: 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.
|
|
mojo::DataPipe data_pipe;
|
|
data_pipe_getter_->Read(std::move(data_pipe.producer_handle),
|
|
base::BindOnce(&DataPipeReader::ReadCallback,
|
|
weak_factory_.GetWeakPtr()));
|
|
data_pipe_ = std::move(data_pipe.consumer_handle);
|
|
handle_watcher_.Watch(data_pipe_.get(), MOJO_HANDLE_SIGNAL_READABLE,
|
|
base::BindRepeating(&DataPipeReader::OnHandleReadable,
|
|
weak_factory_.GetWeakPtr()));
|
|
}
|
|
|
|
~DataPipeReader() = default;
|
|
|
|
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();
|
|
data_pipe_getter_.reset();
|
|
}
|
|
|
|
static void FreeBuffer(char* data, void* self) {
|
|
delete static_cast<DataPipeReader*>(self);
|
|
}
|
|
|
|
gin_helper::Promise<v8::Local<v8::Value>> promise_;
|
|
|
|
mojo::Remote<network::mojom::DataPipeGetter> data_pipe_getter_;
|
|
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};
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(DataPipeReader);
|
|
};
|
|
|
|
} // namespace
|
|
|
|
gin::WrapperInfo DataPipeHolder::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
DataPipeHolder::DataPipeHolder(const network::DataElement& element)
|
|
: id_(base::NumberToString(++g_next_id)) {
|
|
data_pipe_.Bind(
|
|
element.As<network::DataElementDataPipe>().CloneDataPipeGetter());
|
|
}
|
|
|
|
DataPipeHolder::~DataPipeHolder() = default;
|
|
|
|
v8::Local<v8::Promise> DataPipeHolder::ReadAll(v8::Isolate* isolate) {
|
|
gin_helper::Promise<v8::Local<v8::Value>> promise(isolate);
|
|
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));
|
|
AllDataPipeHolders().Set(isolate, handle->id(),
|
|
handle->GetWrapper(isolate).ToLocalChecked());
|
|
return handle;
|
|
}
|
|
|
|
// static
|
|
gin::Handle<DataPipeHolder> DataPipeHolder::From(v8::Isolate* isolate,
|
|
const std::string& id) {
|
|
v8::MaybeLocal<v8::Object> object = AllDataPipeHolders().Get(isolate, id);
|
|
if (!object.IsEmpty()) {
|
|
gin::Handle<DataPipeHolder> handle;
|
|
if (gin::ConvertFromV8(isolate, object.ToLocalChecked(), &handle))
|
|
return handle;
|
|
}
|
|
return gin::Handle<DataPipeHolder>();
|
|
}
|
|
|
|
} // namespace api
|
|
|
|
} // namespace electron
|