michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_dom_DesktopNotification_h michael@0: #define mozilla_dom_DesktopNotification_h michael@0: michael@0: #include "nsIPrincipal.h" michael@0: #include "nsIAlertsService.h" michael@0: #include "nsIContentPermissionPrompt.h" michael@0: michael@0: #include "nsIObserver.h" michael@0: #include "nsString.h" michael@0: #include "nsWeakPtr.h" michael@0: #include "nsCycleCollectionParticipant.h" michael@0: #include "nsIDOMWindow.h" michael@0: #include "nsIScriptObjectPrincipal.h" michael@0: michael@0: #include "nsIDOMEvent.h" michael@0: #include "nsIDocument.h" michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: #include "mozilla/DOMEventTargetHelper.h" michael@0: #include "mozilla/ErrorResult.h" michael@0: #include "nsWrapperCache.h" michael@0: michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: class AlertServiceObserver; michael@0: class DesktopNotification; michael@0: michael@0: /* michael@0: * DesktopNotificationCenter michael@0: * Object hangs off of the navigator object and hands out DesktopNotification objects michael@0: */ michael@0: class DesktopNotificationCenter MOZ_FINAL : public nsISupports, michael@0: public nsWrapperCache michael@0: { michael@0: public: michael@0: NS_DECL_CYCLE_COLLECTING_ISUPPORTS michael@0: NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DesktopNotificationCenter) michael@0: michael@0: DesktopNotificationCenter(nsPIDOMWindow *aWindow) michael@0: { michael@0: MOZ_ASSERT(aWindow); michael@0: mOwner = aWindow; michael@0: michael@0: nsCOMPtr sop = do_QueryInterface(aWindow); michael@0: MOZ_ASSERT(sop); michael@0: michael@0: mPrincipal = sop->GetPrincipal(); michael@0: MOZ_ASSERT(mPrincipal); michael@0: michael@0: SetIsDOMBinding(); michael@0: } michael@0: michael@0: virtual ~DesktopNotificationCenter() michael@0: { michael@0: } michael@0: michael@0: void Shutdown() { michael@0: mOwner = nullptr; michael@0: } michael@0: michael@0: nsPIDOMWindow* GetParentObject() const michael@0: { michael@0: return mOwner; michael@0: } michael@0: michael@0: virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; michael@0: michael@0: already_AddRefed michael@0: CreateNotification(const nsAString& title, michael@0: const nsAString& description, michael@0: const nsAString& iconURL); michael@0: michael@0: private: michael@0: nsCOMPtr mOwner; michael@0: nsCOMPtr mPrincipal; michael@0: }; michael@0: michael@0: class DesktopNotificationRequest; michael@0: michael@0: class DesktopNotification MOZ_FINAL : public DOMEventTargetHelper michael@0: { michael@0: friend class DesktopNotificationRequest; michael@0: michael@0: public: michael@0: michael@0: DesktopNotification(const nsAString& aTitle, michael@0: const nsAString& aDescription, michael@0: const nsAString& aIconURL, michael@0: nsPIDOMWindow *aWindow, michael@0: nsIPrincipal* principal); michael@0: michael@0: virtual ~DesktopNotification(); michael@0: michael@0: void Init(); michael@0: michael@0: /* michael@0: * PostDesktopNotification michael@0: * Uses alert service to display a notification michael@0: */ michael@0: nsresult PostDesktopNotification(); michael@0: michael@0: nsresult SetAllow(bool aAllow); michael@0: michael@0: /* michael@0: * Creates and dispatches a dom event of type aName michael@0: */ michael@0: void DispatchNotificationEvent(const nsString& aName); michael@0: michael@0: void HandleAlertServiceNotification(const char *aTopic); michael@0: michael@0: // WebIDL michael@0: michael@0: nsPIDOMWindow* GetParentObject() const michael@0: { michael@0: return GetOwner(); michael@0: } michael@0: michael@0: virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; michael@0: michael@0: void Show(ErrorResult& aRv); michael@0: michael@0: IMPL_EVENT_HANDLER(click) michael@0: IMPL_EVENT_HANDLER(close) michael@0: michael@0: protected: michael@0: michael@0: nsString mTitle; michael@0: nsString mDescription; michael@0: nsString mIconURL; michael@0: michael@0: nsRefPtr mObserver; michael@0: nsCOMPtr mPrincipal; michael@0: bool mAllow; michael@0: bool mShowHasBeenCalled; michael@0: michael@0: static uint32_t sCount; michael@0: }; michael@0: michael@0: class AlertServiceObserver: public nsIObserver michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: michael@0: AlertServiceObserver(DesktopNotification* notification) michael@0: : mNotification(notification) {} michael@0: michael@0: virtual ~AlertServiceObserver() {} michael@0: michael@0: void Disconnect() { mNotification = nullptr; } michael@0: michael@0: NS_IMETHODIMP michael@0: Observe(nsISupports *aSubject, michael@0: const char *aTopic, michael@0: const char16_t *aData) michael@0: { michael@0: michael@0: // forward to parent michael@0: if (mNotification) { michael@0: #ifdef MOZ_B2G michael@0: if (NS_FAILED(mNotification->CheckInnerWindowCorrectness())) michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: #endif michael@0: mNotification->HandleAlertServiceNotification(aTopic); michael@0: } michael@0: return NS_OK; michael@0: }; michael@0: michael@0: private: michael@0: DesktopNotification* mNotification; michael@0: }; michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla michael@0: michael@0: #endif /* mozilla_dom_DesktopNotification_h */