windows_toast_notification.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (c) 2015 Felix Rieseberg <[email protected]> and Jason Poon
  2. // <[email protected]>. All rights reserved.
  3. // Copyright (c) 2015 Ryan McShane <[email protected]> and Brandon Smith
  4. // <[email protected]>
  5. // Thanks to both of those folks mentioned above who first thought up a bunch of
  6. // this code
  7. // and released it as MIT to the world.
  8. #ifndef ELECTRON_SHELL_BROWSER_NOTIFICATIONS_WIN_WINDOWS_TOAST_NOTIFICATION_H_
  9. #define ELECTRON_SHELL_BROWSER_NOTIFICATIONS_WIN_WINDOWS_TOAST_NOTIFICATION_H_
  10. #include <windows.h>
  11. #include <windows.ui.notifications.h>
  12. #include <wrl/implements.h>
  13. #include <string>
  14. #include "shell/browser/notifications/notification.h"
  15. using Microsoft::WRL::ClassicCom;
  16. using Microsoft::WRL::ComPtr;
  17. using Microsoft::WRL::Make;
  18. using Microsoft::WRL::RuntimeClass;
  19. using Microsoft::WRL::RuntimeClassFlags;
  20. namespace electron {
  21. class ScopedHString;
  22. using DesktopToastActivatedEventHandler =
  23. ABI::Windows::Foundation::ITypedEventHandler<
  24. ABI::Windows::UI::Notifications::ToastNotification*,
  25. IInspectable*>;
  26. using DesktopToastDismissedEventHandler =
  27. ABI::Windows::Foundation::ITypedEventHandler<
  28. ABI::Windows::UI::Notifications::ToastNotification*,
  29. ABI::Windows::UI::Notifications::ToastDismissedEventArgs*>;
  30. using DesktopToastFailedEventHandler =
  31. ABI::Windows::Foundation::ITypedEventHandler<
  32. ABI::Windows::UI::Notifications::ToastNotification*,
  33. ABI::Windows::UI::Notifications::ToastFailedEventArgs*>;
  34. class WindowsToastNotification : public Notification {
  35. public:
  36. // Should only be called by NotificationPresenterWin.
  37. static bool Initialize();
  38. WindowsToastNotification(NotificationDelegate* delegate,
  39. NotificationPresenter* presenter);
  40. ~WindowsToastNotification() override;
  41. protected:
  42. // Notification:
  43. void Show(const NotificationOptions& options) override;
  44. void Dismiss() override;
  45. void Remove() override;
  46. private:
  47. friend class ToastEventHandler;
  48. HRESULT ShowInternal(const NotificationOptions& options);
  49. HRESULT GetToastXml(
  50. ABI::Windows::UI::Notifications::IToastNotificationManagerStatics*
  51. toastManager,
  52. const std::u16string& title,
  53. const std::u16string& msg,
  54. const std::wstring& icon_path,
  55. const std::u16string& timeout_type,
  56. const bool silent,
  57. ABI::Windows::Data::Xml::Dom::IXmlDocument** toast_xml);
  58. HRESULT SetXmlAudioSilent(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc);
  59. HRESULT SetXmlScenarioReminder(
  60. ABI::Windows::Data::Xml::Dom::IXmlDocument* doc);
  61. HRESULT SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
  62. const std::u16string& text);
  63. HRESULT SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
  64. const std::u16string& title,
  65. const std::u16string& body);
  66. HRESULT SetXmlImage(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
  67. const std::wstring& icon_path);
  68. HRESULT GetTextNodeList(
  69. ScopedHString* tag,
  70. ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
  71. ABI::Windows::Data::Xml::Dom::IXmlNodeList** node_list,
  72. uint32_t req_length);
  73. HRESULT AppendTextToXml(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
  74. ABI::Windows::Data::Xml::Dom::IXmlNode* node,
  75. const std::u16string& text);
  76. HRESULT XmlDocumentFromString(
  77. const wchar_t* xmlString,
  78. ABI::Windows::Data::Xml::Dom::IXmlDocument** doc);
  79. HRESULT SetupCallbacks(
  80. ABI::Windows::UI::Notifications::IToastNotification* toast);
  81. bool RemoveCallbacks(
  82. ABI::Windows::UI::Notifications::IToastNotification* toast);
  83. static ComPtr<
  84. ABI::Windows::UI::Notifications::IToastNotificationManagerStatics>
  85. toast_manager_;
  86. static ComPtr<ABI::Windows::UI::Notifications::IToastNotifier>
  87. toast_notifier_;
  88. EventRegistrationToken activated_token_;
  89. EventRegistrationToken dismissed_token_;
  90. EventRegistrationToken failed_token_;
  91. ComPtr<ToastEventHandler> event_handler_;
  92. ComPtr<ABI::Windows::UI::Notifications::IToastNotification>
  93. toast_notification_;
  94. };
  95. class ToastEventHandler : public RuntimeClass<RuntimeClassFlags<ClassicCom>,
  96. DesktopToastActivatedEventHandler,
  97. DesktopToastDismissedEventHandler,
  98. DesktopToastFailedEventHandler> {
  99. public:
  100. explicit ToastEventHandler(Notification* notification);
  101. ~ToastEventHandler() override;
  102. // disable copy
  103. ToastEventHandler(const ToastEventHandler&) = delete;
  104. ToastEventHandler& operator=(const ToastEventHandler&) = delete;
  105. // DesktopToastActivatedEventHandler
  106. IFACEMETHODIMP Invoke(
  107. ABI::Windows::UI::Notifications::IToastNotification* sender,
  108. IInspectable* args) override;
  109. // DesktopToastDismissedEventHandler
  110. IFACEMETHODIMP Invoke(
  111. ABI::Windows::UI::Notifications::IToastNotification* sender,
  112. ABI::Windows::UI::Notifications::IToastDismissedEventArgs* e) override;
  113. // DesktopToastFailedEventHandler
  114. IFACEMETHODIMP Invoke(
  115. ABI::Windows::UI::Notifications::IToastNotification* sender,
  116. ABI::Windows::UI::Notifications::IToastFailedEventArgs* e) override;
  117. private:
  118. base::WeakPtr<Notification> notification_; // weak ref.
  119. };
  120. } // namespace electron
  121. #endif // ELECTRON_SHELL_BROWSER_NOTIFICATIONS_WIN_WINDOWS_TOAST_NOTIFICATION_H_