dom/src/notification/DesktopNotification.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #ifndef mozilla_dom_DesktopNotification_h
michael@0 6 #define mozilla_dom_DesktopNotification_h
michael@0 7
michael@0 8 #include "nsIPrincipal.h"
michael@0 9 #include "nsIAlertsService.h"
michael@0 10 #include "nsIContentPermissionPrompt.h"
michael@0 11
michael@0 12 #include "nsIObserver.h"
michael@0 13 #include "nsString.h"
michael@0 14 #include "nsWeakPtr.h"
michael@0 15 #include "nsCycleCollectionParticipant.h"
michael@0 16 #include "nsIDOMWindow.h"
michael@0 17 #include "nsIScriptObjectPrincipal.h"
michael@0 18
michael@0 19 #include "nsIDOMEvent.h"
michael@0 20 #include "nsIDocument.h"
michael@0 21
michael@0 22 #include "mozilla/Attributes.h"
michael@0 23 #include "mozilla/DOMEventTargetHelper.h"
michael@0 24 #include "mozilla/ErrorResult.h"
michael@0 25 #include "nsWrapperCache.h"
michael@0 26
michael@0 27
michael@0 28 namespace mozilla {
michael@0 29 namespace dom {
michael@0 30
michael@0 31 class AlertServiceObserver;
michael@0 32 class DesktopNotification;
michael@0 33
michael@0 34 /*
michael@0 35 * DesktopNotificationCenter
michael@0 36 * Object hangs off of the navigator object and hands out DesktopNotification objects
michael@0 37 */
michael@0 38 class DesktopNotificationCenter MOZ_FINAL : public nsISupports,
michael@0 39 public nsWrapperCache
michael@0 40 {
michael@0 41 public:
michael@0 42 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
michael@0 43 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DesktopNotificationCenter)
michael@0 44
michael@0 45 DesktopNotificationCenter(nsPIDOMWindow *aWindow)
michael@0 46 {
michael@0 47 MOZ_ASSERT(aWindow);
michael@0 48 mOwner = aWindow;
michael@0 49
michael@0 50 nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(aWindow);
michael@0 51 MOZ_ASSERT(sop);
michael@0 52
michael@0 53 mPrincipal = sop->GetPrincipal();
michael@0 54 MOZ_ASSERT(mPrincipal);
michael@0 55
michael@0 56 SetIsDOMBinding();
michael@0 57 }
michael@0 58
michael@0 59 virtual ~DesktopNotificationCenter()
michael@0 60 {
michael@0 61 }
michael@0 62
michael@0 63 void Shutdown() {
michael@0 64 mOwner = nullptr;
michael@0 65 }
michael@0 66
michael@0 67 nsPIDOMWindow* GetParentObject() const
michael@0 68 {
michael@0 69 return mOwner;
michael@0 70 }
michael@0 71
michael@0 72 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
michael@0 73
michael@0 74 already_AddRefed<DesktopNotification>
michael@0 75 CreateNotification(const nsAString& title,
michael@0 76 const nsAString& description,
michael@0 77 const nsAString& iconURL);
michael@0 78
michael@0 79 private:
michael@0 80 nsCOMPtr<nsPIDOMWindow> mOwner;
michael@0 81 nsCOMPtr<nsIPrincipal> mPrincipal;
michael@0 82 };
michael@0 83
michael@0 84 class DesktopNotificationRequest;
michael@0 85
michael@0 86 class DesktopNotification MOZ_FINAL : public DOMEventTargetHelper
michael@0 87 {
michael@0 88 friend class DesktopNotificationRequest;
michael@0 89
michael@0 90 public:
michael@0 91
michael@0 92 DesktopNotification(const nsAString& aTitle,
michael@0 93 const nsAString& aDescription,
michael@0 94 const nsAString& aIconURL,
michael@0 95 nsPIDOMWindow *aWindow,
michael@0 96 nsIPrincipal* principal);
michael@0 97
michael@0 98 virtual ~DesktopNotification();
michael@0 99
michael@0 100 void Init();
michael@0 101
michael@0 102 /*
michael@0 103 * PostDesktopNotification
michael@0 104 * Uses alert service to display a notification
michael@0 105 */
michael@0 106 nsresult PostDesktopNotification();
michael@0 107
michael@0 108 nsresult SetAllow(bool aAllow);
michael@0 109
michael@0 110 /*
michael@0 111 * Creates and dispatches a dom event of type aName
michael@0 112 */
michael@0 113 void DispatchNotificationEvent(const nsString& aName);
michael@0 114
michael@0 115 void HandleAlertServiceNotification(const char *aTopic);
michael@0 116
michael@0 117 // WebIDL
michael@0 118
michael@0 119 nsPIDOMWindow* GetParentObject() const
michael@0 120 {
michael@0 121 return GetOwner();
michael@0 122 }
michael@0 123
michael@0 124 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
michael@0 125
michael@0 126 void Show(ErrorResult& aRv);
michael@0 127
michael@0 128 IMPL_EVENT_HANDLER(click)
michael@0 129 IMPL_EVENT_HANDLER(close)
michael@0 130
michael@0 131 protected:
michael@0 132
michael@0 133 nsString mTitle;
michael@0 134 nsString mDescription;
michael@0 135 nsString mIconURL;
michael@0 136
michael@0 137 nsRefPtr<AlertServiceObserver> mObserver;
michael@0 138 nsCOMPtr<nsIPrincipal> mPrincipal;
michael@0 139 bool mAllow;
michael@0 140 bool mShowHasBeenCalled;
michael@0 141
michael@0 142 static uint32_t sCount;
michael@0 143 };
michael@0 144
michael@0 145 class AlertServiceObserver: public nsIObserver
michael@0 146 {
michael@0 147 public:
michael@0 148 NS_DECL_ISUPPORTS
michael@0 149
michael@0 150 AlertServiceObserver(DesktopNotification* notification)
michael@0 151 : mNotification(notification) {}
michael@0 152
michael@0 153 virtual ~AlertServiceObserver() {}
michael@0 154
michael@0 155 void Disconnect() { mNotification = nullptr; }
michael@0 156
michael@0 157 NS_IMETHODIMP
michael@0 158 Observe(nsISupports *aSubject,
michael@0 159 const char *aTopic,
michael@0 160 const char16_t *aData)
michael@0 161 {
michael@0 162
michael@0 163 // forward to parent
michael@0 164 if (mNotification) {
michael@0 165 #ifdef MOZ_B2G
michael@0 166 if (NS_FAILED(mNotification->CheckInnerWindowCorrectness()))
michael@0 167 return NS_ERROR_NOT_AVAILABLE;
michael@0 168 #endif
michael@0 169 mNotification->HandleAlertServiceNotification(aTopic);
michael@0 170 }
michael@0 171 return NS_OK;
michael@0 172 };
michael@0 173
michael@0 174 private:
michael@0 175 DesktopNotification* mNotification;
michael@0 176 };
michael@0 177
michael@0 178 } // namespace dom
michael@0 179 } // namespace mozilla
michael@0 180
michael@0 181 #endif /* mozilla_dom_DesktopNotification_h */

mercurial