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