DesktopNotification.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_DesktopNotification_h
  6. #define mozilla_dom_DesktopNotification_h
  7. #include "nsIPrincipal.h"
  8. #include "nsIAlertsService.h"
  9. #include "nsIContentPermissionPrompt.h"
  10. #include "nsIObserver.h"
  11. #include "nsString.h"
  12. #include "nsWeakPtr.h"
  13. #include "nsCycleCollectionParticipant.h"
  14. #include "nsIDOMWindow.h"
  15. #include "nsIScriptObjectPrincipal.h"
  16. #include "nsIDOMEvent.h"
  17. #include "mozilla/Attributes.h"
  18. #include "mozilla/DOMEventTargetHelper.h"
  19. #include "mozilla/ErrorResult.h"
  20. #include "nsWrapperCache.h"
  21. namespace mozilla {
  22. namespace dom {
  23. class AlertServiceObserver;
  24. class DesktopNotification;
  25. /*
  26. * DesktopNotificationCenter
  27. * Object hangs off of the navigator object and hands out DesktopNotification objects
  28. */
  29. class DesktopNotificationCenter final : public nsISupports,
  30. public nsWrapperCache
  31. {
  32. public:
  33. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  34. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DesktopNotificationCenter)
  35. explicit DesktopNotificationCenter(nsPIDOMWindowInner* aWindow)
  36. {
  37. MOZ_ASSERT(aWindow);
  38. mOwner = aWindow;
  39. nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(aWindow);
  40. MOZ_ASSERT(sop);
  41. mPrincipal = sop->GetPrincipal();
  42. MOZ_ASSERT(mPrincipal);
  43. }
  44. void Shutdown() {
  45. mOwner = nullptr;
  46. }
  47. nsPIDOMWindowInner* GetParentObject() const
  48. {
  49. return mOwner;
  50. }
  51. virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  52. already_AddRefed<DesktopNotification>
  53. CreateNotification(const nsAString& title,
  54. const nsAString& description,
  55. const nsAString& iconURL);
  56. private:
  57. virtual ~DesktopNotificationCenter()
  58. {
  59. }
  60. nsCOMPtr<nsPIDOMWindowInner> mOwner;
  61. nsCOMPtr<nsIPrincipal> mPrincipal;
  62. };
  63. class DesktopNotificationRequest;
  64. class DesktopNotification final : public DOMEventTargetHelper
  65. {
  66. friend class DesktopNotificationRequest;
  67. public:
  68. DesktopNotification(const nsAString& aTitle,
  69. const nsAString& aDescription,
  70. const nsAString& aIconURL,
  71. nsPIDOMWindowInner* aWindow,
  72. nsIPrincipal* principal);
  73. virtual ~DesktopNotification();
  74. void Init();
  75. /*
  76. * PostDesktopNotification
  77. * Uses alert service to display a notification
  78. */
  79. nsresult PostDesktopNotification();
  80. nsresult SetAllow(bool aAllow);
  81. /*
  82. * Creates and dispatches a dom event of type aName
  83. */
  84. void DispatchNotificationEvent(const nsString& aName);
  85. void HandleAlertServiceNotification(const char *aTopic);
  86. // WebIDL
  87. nsPIDOMWindowInner* GetParentObject() const
  88. {
  89. return GetOwner();
  90. }
  91. virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  92. void Show(ErrorResult& aRv);
  93. IMPL_EVENT_HANDLER(click)
  94. IMPL_EVENT_HANDLER(close)
  95. protected:
  96. nsString mTitle;
  97. nsString mDescription;
  98. nsString mIconURL;
  99. RefPtr<AlertServiceObserver> mObserver;
  100. nsCOMPtr<nsIPrincipal> mPrincipal;
  101. bool mAllow;
  102. bool mShowHasBeenCalled;
  103. static uint32_t sCount;
  104. };
  105. class AlertServiceObserver: public nsIObserver
  106. {
  107. public:
  108. NS_DECL_ISUPPORTS
  109. explicit AlertServiceObserver(DesktopNotification* notification)
  110. : mNotification(notification) {}
  111. void Disconnect() { mNotification = nullptr; }
  112. NS_IMETHOD
  113. Observe(nsISupports* aSubject,
  114. const char* aTopic,
  115. const char16_t* aData) override
  116. {
  117. // forward to parent
  118. if (mNotification) {
  119. mNotification->HandleAlertServiceNotification(aTopic);
  120. }
  121. return NS_OK;
  122. };
  123. private:
  124. virtual ~AlertServiceObserver() {}
  125. DesktopNotification* mNotification;
  126. };
  127. } // namespace dom
  128. } // namespace mozilla
  129. #endif /* mozilla_dom_DesktopNotification_h */