1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is DesktopNotification.
15 : *
16 : * The Initial Developer of the Original Code is Mozilla Foundation
17 : * Portions created by the Initial Developer are Copyright (C) 2010
18 : * the Initial Developer. All Rights Reserved.
19 : *
20 : * Contributor(s):
21 : * Doug Turner <dougt@dougt.org> (Original Author)
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 2 or later (the "GPL"), or
25 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 : * in which case the provisions of the GPL or the LGPL are applicable instead
27 : * of those above. If you wish to allow use of your version of this file only
28 : * under the terms of either the GPL or the LGPL, and not to allow others to
29 : * use your version of this file under the terms of the MPL, indicate your
30 : * decision by deleting the provisions above and replace them with the notice
31 : * and other provisions required by the GPL or the LGPL. If you do not delete
32 : * the provisions above, a recipient may use your version of this file under
33 : * the terms of any one of the MPL, the GPL or the LGPL.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : #ifndef nsDesktopNotification_h
38 : #define nsDesktopNotification_h
39 :
40 : #include "PCOMContentPermissionRequestChild.h"
41 :
42 : #include "nsDOMClassInfoID.h"
43 : #include "nsIPrincipal.h"
44 : #include "nsIJSContextStack.h"
45 :
46 : #include "nsIAlertsService.h"
47 :
48 : #include "nsIDOMDesktopNotification.h"
49 : #include "nsIDOMEventTarget.h"
50 : #include "nsIContentPermissionPrompt.h"
51 :
52 : #include "nsIObserver.h"
53 : #include "nsString.h"
54 : #include "nsWeakPtr.h"
55 : #include "nsCycleCollectionParticipant.h"
56 : #include "nsIDOMWindow.h"
57 : #include "nsThreadUtils.h"
58 :
59 : #include "nsDOMEventTargetHelper.h"
60 : #include "nsIPrivateDOMEvent.h"
61 : #include "nsIDocument.h"
62 :
63 : class AlertServiceObserver;
64 :
65 : /*
66 : * nsDesktopNotificationCenter
67 : * Object hangs off of the navigator object and hands out nsDOMDesktopNotification objects
68 : */
69 : class nsDesktopNotificationCenter : public nsIDOMDesktopNotificationCenter
70 : {
71 : public:
72 : NS_DECL_ISUPPORTS
73 : NS_DECL_NSIDOMDESKTOPNOTIFICATIONCENTER
74 :
75 0 : nsDesktopNotificationCenter(nsPIDOMWindow *aWindow)
76 0 : {
77 0 : mOwner = aWindow;
78 :
79 : // Grab the uri of the document
80 0 : nsCOMPtr<nsIDOMDocument> domdoc;
81 0 : mOwner->GetDocument(getter_AddRefs(domdoc));
82 0 : nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc);
83 0 : doc->NodePrincipal()->GetURI(getter_AddRefs(mURI));
84 0 : }
85 :
86 0 : virtual ~nsDesktopNotificationCenter()
87 0 : {
88 0 : }
89 :
90 0 : void Shutdown() {
91 0 : mOwner = nsnull;
92 0 : }
93 :
94 : private:
95 : nsCOMPtr<nsPIDOMWindow> mOwner;
96 : nsCOMPtr<nsIURI> mURI;
97 : };
98 :
99 :
100 : class nsDOMDesktopNotification : public nsDOMEventTargetHelper,
101 : public nsIDOMDesktopNotification
102 : {
103 : friend class nsDesktopNotificationRequest;
104 :
105 : public:
106 : NS_DECL_ISUPPORTS_INHERITED
107 1464 : NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsDOMDesktopNotification,nsDOMEventTargetHelper)
108 : NS_DECL_NSIDOMDESKTOPNOTIFICATION
109 :
110 : nsDOMDesktopNotification(const nsAString & title,
111 : const nsAString & description,
112 : const nsAString & iconURL,
113 : nsPIDOMWindow *aWindow,
114 : nsIURI* uri);
115 :
116 : virtual ~nsDOMDesktopNotification();
117 :
118 : /*
119 : * PostDesktopNotification
120 : * Uses alert service to display a notification
121 : */
122 : void PostDesktopNotification();
123 :
124 : void SetAllow(bool aAllow);
125 :
126 : /*
127 : * Creates and dispatches a dom event of type aName
128 : */
129 : void DispatchNotificationEvent(const nsString& aName);
130 :
131 : void HandleAlertServiceNotification(const char *aTopic);
132 :
133 : protected:
134 :
135 : nsString mTitle;
136 : nsString mDescription;
137 : nsString mIconURL;
138 :
139 : nsRefPtr<nsDOMEventListenerWrapper> mOnClickCallback;
140 : nsRefPtr<nsDOMEventListenerWrapper> mOnCloseCallback;
141 :
142 : nsRefPtr<AlertServiceObserver> mObserver;
143 : nsCOMPtr<nsIURI> mURI;
144 : bool mAllow;
145 : bool mShowHasBeenCalled;
146 : };
147 :
148 : /*
149 : * Simple Request
150 : */
151 : class nsDesktopNotificationRequest : public nsIContentPermissionRequest,
152 : public nsRunnable,
153 : public PCOMContentPermissionRequestChild
154 :
155 : {
156 : public:
157 : NS_DECL_ISUPPORTS
158 : NS_DECL_NSICONTENTPERMISSIONREQUEST
159 :
160 0 : nsDesktopNotificationRequest(nsDOMDesktopNotification* notification)
161 0 : : mDesktopNotification(notification) {}
162 :
163 0 : NS_IMETHOD Run()
164 : {
165 : nsCOMPtr<nsIContentPermissionPrompt> prompt =
166 0 : do_GetService(NS_CONTENT_PERMISSION_PROMPT_CONTRACTID);
167 0 : if (prompt) {
168 0 : prompt->Prompt(this);
169 : }
170 0 : return NS_OK;
171 : }
172 :
173 0 : ~nsDesktopNotificationRequest()
174 0 : {
175 0 : }
176 :
177 0 : bool Recv__delete__(const bool& allow)
178 : {
179 0 : if (allow)
180 0 : (void) Allow();
181 : else
182 0 : (void) Cancel();
183 0 : return true;
184 : }
185 0 : void IPDLRelease() { Release(); }
186 :
187 : nsRefPtr<nsDOMDesktopNotification> mDesktopNotification;
188 : };
189 :
190 : class AlertServiceObserver: public nsIObserver
191 : {
192 : public:
193 : NS_DECL_ISUPPORTS
194 :
195 0 : AlertServiceObserver(nsDOMDesktopNotification* notification)
196 0 : : mNotification(notification) {}
197 :
198 0 : virtual ~AlertServiceObserver() {}
199 :
200 0 : void Disconnect() { mNotification = nsnull; }
201 :
202 : NS_IMETHODIMP
203 0 : Observe(nsISupports *aSubject,
204 : const char *aTopic,
205 : const PRUnichar *aData)
206 : {
207 : // forward to parent
208 0 : if (mNotification)
209 0 : mNotification->HandleAlertServiceNotification(aTopic);
210 0 : return NS_OK;
211 : };
212 :
213 : private:
214 : nsDOMDesktopNotification* mNotification;
215 : };
216 :
217 : #endif /* nsDesktopNotification_h */
|