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