electron_api_push_notifications.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (c) 2022 Asana, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/api/electron_api_push_notifications.h"
  5. #include "gin/handle.h"
  6. #include "shell/common/gin_converters/value_converter.h"
  7. #include "shell/common/gin_helper/dictionary.h"
  8. #include "shell/common/node_includes.h"
  9. namespace electron::api {
  10. PushNotifications* g_push_notifications = nullptr;
  11. gin::WrapperInfo PushNotifications::kWrapperInfo = {gin::kEmbedderNativeGin};
  12. PushNotifications::PushNotifications() = default;
  13. PushNotifications::~PushNotifications() {
  14. g_push_notifications = nullptr;
  15. }
  16. // static
  17. PushNotifications* PushNotifications::Get() {
  18. if (!g_push_notifications)
  19. g_push_notifications = new PushNotifications();
  20. return g_push_notifications;
  21. }
  22. // static
  23. gin::Handle<PushNotifications> PushNotifications::Create(v8::Isolate* isolate) {
  24. return gin::CreateHandle(isolate, PushNotifications::Get());
  25. }
  26. // static
  27. gin::ObjectTemplateBuilder PushNotifications::GetObjectTemplateBuilder(
  28. v8::Isolate* isolate) {
  29. auto builder = gin_helper::EventEmitterMixin<
  30. PushNotifications>::GetObjectTemplateBuilder(isolate);
  31. #if BUILDFLAG(IS_MAC)
  32. builder
  33. .SetMethod("registerForAPNSNotifications",
  34. &PushNotifications::RegisterForAPNSNotifications)
  35. .SetMethod("unregisterForAPNSNotifications",
  36. &PushNotifications::UnregisterForAPNSNotifications);
  37. #endif
  38. return builder;
  39. }
  40. const char* PushNotifications::GetTypeName() {
  41. return "PushNotifications";
  42. }
  43. } // namespace electron::api
  44. namespace {
  45. void Initialize(v8::Local<v8::Object> exports,
  46. v8::Local<v8::Value> unused,
  47. v8::Local<v8::Context> context,
  48. void* priv) {
  49. v8::Isolate* isolate = context->GetIsolate();
  50. gin::Dictionary dict(isolate, exports);
  51. dict.Set("pushNotifications",
  52. electron::api::PushNotifications::Create(isolate));
  53. }
  54. } // namespace
  55. NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_push_notifications,
  56. Initialize)