windows_toast_notification.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright (c) 2015 Felix Rieseberg <feriese@microsoft.com> and Jason Poon
  2. // <jason.poon@microsoft.com>. All rights reserved.
  3. // Copyright (c) 2015 Ryan McShane <rmcshane@bandwidth.com> and Brandon Smith
  4. // <bsmith@bandwidth.com>
  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 BRIGHTRAY_BROWSER_WIN_WINDOWS_TOAST_NOTIFICATION_H_
  9. #define BRIGHTRAY_BROWSER_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 <vector>
  15. #include "brightray/browser/notification.h"
  16. using Microsoft::WRL::ClassicCom;
  17. using Microsoft::WRL::ComPtr;
  18. using Microsoft::WRL::Make;
  19. using Microsoft::WRL::RuntimeClass;
  20. using Microsoft::WRL::RuntimeClassFlags;
  21. class ScopedHString;
  22. namespace brightray {
  23. using DesktopToastActivatedEventHandler =
  24. ABI::Windows::Foundation::ITypedEventHandler<
  25. ABI::Windows::UI::Notifications::ToastNotification*,
  26. IInspectable*>;
  27. using DesktopToastDismissedEventHandler =
  28. ABI::Windows::Foundation::ITypedEventHandler<
  29. ABI::Windows::UI::Notifications::ToastNotification*,
  30. ABI::Windows::UI::Notifications::ToastDismissedEventArgs*>;
  31. using DesktopToastFailedEventHandler =
  32. ABI::Windows::Foundation::ITypedEventHandler<
  33. ABI::Windows::UI::Notifications::ToastNotification*,
  34. ABI::Windows::UI::Notifications::ToastFailedEventArgs*>;
  35. class WindowsToastNotification : public Notification {
  36. public:
  37. // Should only be called by NotificationPresenterWin.
  38. static bool Initialize();
  39. WindowsToastNotification(NotificationDelegate* delegate,
  40. NotificationPresenter* presenter);
  41. ~WindowsToastNotification();
  42. protected:
  43. // Notification:
  44. void Show(const NotificationOptions& options) override;
  45. void Dismiss() override;
  46. private:
  47. friend class ToastEventHandler;
  48. bool GetToastXml(
  49. ABI::Windows::UI::Notifications::IToastNotificationManagerStatics*
  50. toastManager,
  51. const std::wstring& title,
  52. const std::wstring& msg,
  53. const std::wstring& icon_path,
  54. const bool silent,
  55. ABI::Windows::Data::Xml::Dom::IXmlDocument** toastXml);
  56. bool SetXmlAudioSilent(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc);
  57. bool SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
  58. const std::wstring& text);
  59. bool SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
  60. const std::wstring& title,
  61. const std::wstring& body);
  62. bool SetXmlImage(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
  63. const std::wstring& icon_path);
  64. bool GetTextNodeList(ScopedHString* tag,
  65. ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
  66. ABI::Windows::Data::Xml::Dom::IXmlNodeList** nodeList,
  67. uint32_t reqLength);
  68. bool AppendTextToXml(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
  69. ABI::Windows::Data::Xml::Dom::IXmlNode* node,
  70. const std::wstring& text);
  71. bool SetupCallbacks(
  72. ABI::Windows::UI::Notifications::IToastNotification* toast);
  73. bool RemoveCallbacks(
  74. ABI::Windows::UI::Notifications::IToastNotification* toast);
  75. static ComPtr<
  76. ABI::Windows::UI::Notifications::IToastNotificationManagerStatics>
  77. toast_manager_;
  78. static ComPtr<ABI::Windows::UI::Notifications::IToastNotifier>
  79. toast_notifier_;
  80. EventRegistrationToken activated_token_;
  81. EventRegistrationToken dismissed_token_;
  82. EventRegistrationToken failed_token_;
  83. ComPtr<ToastEventHandler> event_handler_;
  84. ComPtr<ABI::Windows::UI::Notifications::IToastNotification>
  85. toast_notification_;
  86. DISALLOW_COPY_AND_ASSIGN(WindowsToastNotification);
  87. };
  88. class ToastEventHandler : public RuntimeClass<RuntimeClassFlags<ClassicCom>,
  89. DesktopToastActivatedEventHandler,
  90. DesktopToastDismissedEventHandler,
  91. DesktopToastFailedEventHandler> {
  92. public:
  93. explicit ToastEventHandler(Notification* notification);
  94. ~ToastEventHandler();
  95. IFACEMETHODIMP Invoke(
  96. ABI::Windows::UI::Notifications::IToastNotification* sender,
  97. IInspectable* args);
  98. IFACEMETHODIMP Invoke(
  99. ABI::Windows::UI::Notifications::IToastNotification* sender,
  100. ABI::Windows::UI::Notifications::IToastDismissedEventArgs* e);
  101. IFACEMETHODIMP Invoke(
  102. ABI::Windows::UI::Notifications::IToastNotification* sender,
  103. ABI::Windows::UI::Notifications::IToastFailedEventArgs* e);
  104. private:
  105. base::WeakPtr<Notification> notification_; // weak ref.
  106. DISALLOW_COPY_AND_ASSIGN(ToastEventHandler);
  107. };
  108. } // namespace brightray
  109. #endif // BRIGHTRAY_BROWSER_WIN_WINDOWS_TOAST_NOTIFICATION_H_