electron/shell/browser/api/electron_api_push_notifications.cc
Charles Kerr 39cca586f6
refactor: avoid deprecated v8::Context::GetIsolate() calls (pt 1) (#47760)
* refactor: avoid redundant GetIsolate() calls in NodeBindings::CreateEnvironment()

Xref: https://chromium-review.googlesource.com/c/v8/v8/+/6563615

* refactor: use v8::Isolate::GetCurrent() in Initialize() methods

* refactor: add v8::Isolate* arg to NodeBindings::CreateEnvironment()

* fixup! refactor: use v8::Isolate::GetCurrent() in Initialize() methods

* refactor: add v8::Isolate* arg to RendererClientBase::DidCreateScriptContext()

* fixup! refactor: add v8::Isolate* arg to NodeBindings::CreateEnvironment()

* fixup! fixup! refactor: use v8::Isolate::GetCurrent() in Initialize() methods

refactor: prefer JavascriptEnvironment::GetIsolate() in the browser layer
2025-07-21 16:34:38 +02:00

73 lines
2.1 KiB
C++

// Copyright (c) 2022 Asana, 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_push_notifications.h"
#include "gin/handle.h"
#include "shell/common/gin_converters/value_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
namespace electron::api {
PushNotifications* g_push_notifications = nullptr;
gin::DeprecatedWrapperInfo PushNotifications::kWrapperInfo = {
gin::kEmbedderNativeGin};
PushNotifications::PushNotifications() = default;
PushNotifications::~PushNotifications() {
g_push_notifications = nullptr;
}
// static
PushNotifications* PushNotifications::Get() {
if (!g_push_notifications)
g_push_notifications = new PushNotifications();
return g_push_notifications;
}
// static
gin::Handle<PushNotifications> PushNotifications::Create(v8::Isolate* isolate) {
return gin::CreateHandle(isolate, PushNotifications::Get());
}
// static
gin::ObjectTemplateBuilder PushNotifications::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
auto builder = gin_helper::EventEmitterMixin<
PushNotifications>::GetObjectTemplateBuilder(isolate);
#if BUILDFLAG(IS_MAC)
builder
.SetMethod("registerForAPNSNotifications",
&PushNotifications::RegisterForAPNSNotifications)
.SetMethod("unregisterForAPNSNotifications",
&PushNotifications::UnregisterForAPNSNotifications);
#endif
return builder;
}
const char* PushNotifications::GetTypeName() {
return "PushNotifications";
}
} // namespace electron::api
namespace {
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate();
gin::Dictionary dict(isolate, exports);
dict.Set("pushNotifications",
electron::api::PushNotifications::Create(isolate));
}
} // namespace
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_push_notifications,
Initialize)