electron_api_push_notifications_mac.mm 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 <string>
  6. #include <utility>
  7. #include <vector>
  8. #import "shell/browser/mac/electron_application.h"
  9. #include "shell/common/gin_converters/value_converter.h"
  10. #include "shell/common/gin_helper/promise.h"
  11. namespace electron::api {
  12. v8::Local<v8::Promise> PushNotifications::RegisterForAPNSNotifications(
  13. v8::Isolate* isolate) {
  14. gin_helper::Promise<std::string> promise(isolate);
  15. v8::Local<v8::Promise> handle = promise.GetHandle();
  16. [[AtomApplication sharedApplication] registerForRemoteNotifications];
  17. PushNotifications::apns_promise_set_.emplace_back(std::move(promise));
  18. return handle;
  19. }
  20. void PushNotifications::ResolveAPNSPromiseSetWithToken(
  21. const std::string& token_string) {
  22. auto promises = std::move(PushNotifications::apns_promise_set_);
  23. for (auto& promise : promises) {
  24. promise.Resolve(token_string);
  25. }
  26. }
  27. void PushNotifications::RejectAPNSPromiseSetWithError(
  28. const std::string& error_message) {
  29. auto promises = std::move(PushNotifications::apns_promise_set_);
  30. for (auto& promise : promises) {
  31. promise.RejectWithErrorMessage(error_message);
  32. }
  33. }
  34. void PushNotifications::UnregisterForAPNSNotifications() {
  35. [[AtomApplication sharedApplication] unregisterForRemoteNotifications];
  36. }
  37. void PushNotifications::OnDidReceiveAPNSNotification(
  38. const base::Value::Dict& user_info) {
  39. Emit("received-apns-notification", user_info);
  40. }
  41. } // namespace electron::api