chore: remove native_mate (Part 12) (#20869)

* refactor: move mate::Event to gin

* refactor: move mate::Locker to gin

* refactor: convert contextBridge to gin

* refactor: convert contentTracing to gin

* refactor: remove callback converter of native_mate

* refactor: remove file_dialog_converter and native_window_converter from native_mate

* refactor: convert webFrame to gin

* refactor: move blink_converter to gin

* refactor: remove net_converter from native_mate

* refactor: remove event_emitter_caller_deprecated

* refactor: remove gurl_converter from native_mate

* refactor: remove file_path and string16_converter from native_mate

* refactor: remove image_converter from native_mate

* refactor: move value_converter to gin
This commit is contained in:
Cheng Zhao 2019-10-31 16:56:00 +09:00 committed by GitHub
parent 6781d5e3c8
commit 3ae3233e65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
96 changed files with 622 additions and 1711 deletions

View file

@ -69,7 +69,7 @@ void CallTranslater(v8::Local<v8::External> external,
struct DeleteOnUIThread {
template <typename T>
static void Destruct(const T* x) {
if (mate::Locker::IsBrowserProcess() &&
if (gin_helper::Locker::IsBrowserProcess() &&
!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
content::BrowserThread::DeleteSoon(content::BrowserThread::UI, FROM_HERE,
x);

View file

@ -9,9 +9,9 @@
#include <vector>
#include "base/bind.h"
#include "shell/common/api/locker.h"
#include "shell/common/gin_converters/std_converter.h"
#include "shell/common/gin_helper/function_template.h"
#include "shell/common/gin_helper/locker.h"
// Implements safe convertions between JS functions and base::Callback.
@ -43,7 +43,7 @@ struct V8FunctionInvoker<v8::Local<v8::Value>(ArgTypes...)> {
static v8::Local<v8::Value> Go(v8::Isolate* isolate,
const SafeV8Function& function,
ArgTypes... raw) {
mate::Locker locker(isolate);
gin_helper::Locker locker(isolate);
v8::EscapableHandleScope handle_scope(isolate);
if (!function.IsAlive())
return v8::Null(isolate);
@ -68,7 +68,7 @@ struct V8FunctionInvoker<void(ArgTypes...)> {
static void Go(v8::Isolate* isolate,
const SafeV8Function& function,
ArgTypes... raw) {
mate::Locker locker(isolate);
gin_helper::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
if (!function.IsAlive())
return;
@ -91,7 +91,7 @@ struct V8FunctionInvoker<ReturnType(ArgTypes...)> {
static ReturnType Go(v8::Isolate* isolate,
const SafeV8Function& function,
ArgTypes... raw) {
mate::Locker locker(isolate);
gin_helper::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
ReturnType ret = ReturnType();
if (!function.IsAlive())

View file

@ -108,6 +108,26 @@ class Dictionary : public gin::Dictionary {
return !result.IsNothing() && result.FromJust();
}
// Note: If we plan to add more Set methods, consider adding an option instead
// of copying code.
template <typename T>
bool SetReadOnlyNonConfigurable(base::StringPiece key, T val) {
v8::Local<v8::Value> v8_value;
if (!gin::TryConvertToV8(isolate(), val, &v8_value))
return false;
v8::Maybe<bool> result = GetHandle()->DefineOwnProperty(
isolate()->GetCurrentContext(), gin::StringToV8(isolate(), key),
v8_value,
static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
return !result.IsNothing() && result.FromJust();
}
bool Has(base::StringPiece key) const {
v8::Maybe<bool> result = GetHandle()->Has(isolate()->GetCurrentContext(),
gin::StringToV8(isolate(), key));
return !result.IsNothing() && result.FromJust();
}
bool Delete(base::StringPiece key) {
v8::Maybe<bool> result = GetHandle()->Delete(
isolate()->GetCurrentContext(), gin::StringToV8(isolate(), key));

View file

@ -68,11 +68,10 @@ v8::Local<v8::Object> CreateNativeEvent(
v8::Isolate* isolate,
v8::Local<v8::Object> sender,
content::RenderFrameHost* frame,
base::Optional<electron::mojom::ElectronBrowser::MessageSyncCallback>
callback) {
electron::mojom::ElectronBrowser::MessageSyncCallback callback) {
v8::Local<v8::Object> event;
if (frame && callback) {
mate::Handle<mate::Event> native_event = mate::Event::Create(isolate);
gin::Handle<Event> native_event = Event::Create(isolate);
native_event->SetCallback(std::move(callback));
event = v8::Local<v8::Object>::Cast(native_event.ToV8());
} else {

View file

@ -8,7 +8,6 @@
#include <utility>
#include <vector>
#include "base/optional.h"
#include "content/public/browser/browser_thread.h"
#include "electron/shell/common/api/api.mojom.h"
#include "native_mate/wrappable.h"
@ -31,8 +30,7 @@ v8::Local<v8::Object> CreateNativeEvent(
v8::Isolate* isolate,
v8::Local<v8::Object> sender,
content::RenderFrameHost* frame,
base::Optional<electron::mojom::ElectronBrowser::MessageSyncCallback>
callback);
electron::mojom::ElectronBrowser::MessageSyncCallback callback);
} // namespace internal
@ -83,11 +81,10 @@ class EventEmitter : public mate::Wrappable<T> {
// this.emit(name, new Event(sender, message), args...);
template <typename... Args>
bool EmitWithSender(
base::StringPiece name,
content::RenderFrameHost* sender,
base::Optional<electron::mojom::ElectronBrowser::InvokeCallback> callback,
Args&&... args) {
bool EmitWithSender(base::StringPiece name,
content::RenderFrameHost* sender,
electron::mojom::ElectronBrowser::InvokeCallback callback,
Args&&... args) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());

View file

@ -4,7 +4,7 @@
#include "shell/common/gin_helper/event_emitter_caller.h"
#include "shell/common/api/locker.h"
#include "shell/common/gin_helper/locker.h"
#include "shell/common/node_includes.h"
namespace gin_helper {

View file

@ -0,0 +1,16 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE.chromium file.
#include "shell/common/gin_helper/locker.h"
namespace gin_helper {
Locker::Locker(v8::Isolate* isolate) {
if (IsBrowserProcess())
locker_ = std::make_unique<v8::Locker>(isolate);
}
Locker::~Locker() = default;
} // namespace gin_helper

View file

@ -0,0 +1,36 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE.chromium file.
#ifndef SHELL_COMMON_GIN_HELPER_LOCKER_H_
#define SHELL_COMMON_GIN_HELPER_LOCKER_H_
#include <memory>
#include "base/macros.h"
#include "v8/include/v8.h"
namespace gin_helper {
// Only lock when lockers are used in current thread.
class Locker {
public:
explicit Locker(v8::Isolate* isolate);
~Locker();
// Returns whether current process is browser process, currently we detect it
// by checking whether current has used V8 Lock, but it might be a bad idea.
static inline bool IsBrowserProcess() { return v8::Locker::IsActive(); }
private:
void* operator new(size_t size);
void operator delete(void*, size_t);
std::unique_ptr<v8::Locker> locker_;
DISALLOW_COPY_AND_ASSIGN(Locker);
};
} // namespace gin_helper
#endif // SHELL_COMMON_GIN_HELPER_LOCKER_H_

View file

@ -0,0 +1,32 @@
// Copyright 2014 Cheng Zhao. All rights reserved.
// Use of this source code is governed by MIT license that can be found in the
// LICENSE file.
#include "shell/common/gin_helper/persistent_dictionary.h"
namespace gin_helper {
PersistentDictionary::PersistentDictionary() = default;
PersistentDictionary::PersistentDictionary(v8::Isolate* isolate,
v8::Local<v8::Object> object)
: isolate_(isolate), handle_(isolate, object) {}
PersistentDictionary::PersistentDictionary(const PersistentDictionary& other)
: isolate_(other.isolate_),
handle_(isolate_, v8::Local<v8::Object>::New(isolate_, other.handle_)) {}
PersistentDictionary::~PersistentDictionary() = default;
PersistentDictionary& PersistentDictionary::operator=(
const PersistentDictionary& other) {
isolate_ = other.isolate_;
handle_.Reset(isolate_, v8::Local<v8::Object>::New(isolate_, other.handle_));
return *this;
}
v8::Local<v8::Object> PersistentDictionary::GetHandle() const {
return v8::Local<v8::Object>::New(isolate_, handle_);
}
} // namespace gin_helper

View file

@ -0,0 +1,64 @@
// Copyright 2014 Cheng Zhao. All rights reserved.
// Use of this source code is governed by MIT license that can be found in the
// LICENSE file.
#ifndef SHELL_COMMON_GIN_HELPER_PERSISTENT_DICTIONARY_H_
#define SHELL_COMMON_GIN_HELPER_PERSISTENT_DICTIONARY_H_
#include "shell/common/gin_helper/dictionary.h"
namespace gin_helper {
// Like Dictionary, but stores object in persistent handle so you can keep it
// safely on heap.
//
// TODO(zcbenz): The only user of this class is AtomTouchBar, we should migrate
// away from this class.
class PersistentDictionary {
public:
PersistentDictionary();
PersistentDictionary(v8::Isolate* isolate, v8::Local<v8::Object> object);
PersistentDictionary(const PersistentDictionary& other);
~PersistentDictionary();
PersistentDictionary& operator=(const PersistentDictionary& other);
v8::Local<v8::Object> GetHandle() const;
template <typename K, typename V>
bool Get(const K& key, V* out) const {
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
v8::Local<v8::Value> v8_key = gin::ConvertToV8(isolate_, key);
v8::Local<v8::Value> value;
v8::Maybe<bool> result = GetHandle()->Has(context, v8_key);
if (result.IsJust() && result.FromJust() &&
GetHandle()->Get(context, v8_key).ToLocal(&value))
return gin::ConvertFromV8(isolate_, value, out);
return false;
}
private:
v8::Isolate* isolate_ = nullptr;
v8::Global<v8::Object> handle_;
};
} // namespace gin_helper
namespace gin {
template <>
struct Converter<gin_helper::PersistentDictionary> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
gin_helper::PersistentDictionary* out) {
if (!val->IsObject())
return false;
*out = gin_helper::PersistentDictionary(isolate,
v8::Local<v8::Object>::Cast(val));
return true;
}
};
} // namespace gin
#endif // SHELL_COMMON_GIN_HELPER_PERSISTENT_DICTIONARY_H_

View file

@ -9,7 +9,7 @@
#include "base/bind.h"
#include "base/supports_user_data.h"
#include "shell/browser/atom_browser_main_parts.h"
#include "shell/common/api/locker.h"
#include "shell/common/gin_helper/locker.h"
namespace gin_helper {
@ -33,7 +33,7 @@ class IDUserData : public base::SupportsUserData::Data {
TrackableObjectBase::TrackableObjectBase() : weak_factory_(this) {
// TODO(zcbenz): Make TrackedObject work in renderer process.
DCHECK(mate::Locker::IsBrowserProcess())
DCHECK(gin_helper::Locker::IsBrowserProcess())
<< "This class only works for browser process";
electron::AtomBrowserMainParts::Get()->RegisterDestructionCallback(