dom/src/notification/DesktopNotification.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     5 #ifndef mozilla_dom_DesktopNotification_h
     6 #define mozilla_dom_DesktopNotification_h
     8 #include "nsIPrincipal.h"
     9 #include "nsIAlertsService.h"
    10 #include "nsIContentPermissionPrompt.h"
    12 #include "nsIObserver.h"
    13 #include "nsString.h"
    14 #include "nsWeakPtr.h"
    15 #include "nsCycleCollectionParticipant.h"
    16 #include "nsIDOMWindow.h"
    17 #include "nsIScriptObjectPrincipal.h"
    19 #include "nsIDOMEvent.h"
    20 #include "nsIDocument.h"
    22 #include "mozilla/Attributes.h"
    23 #include "mozilla/DOMEventTargetHelper.h"
    24 #include "mozilla/ErrorResult.h"
    25 #include "nsWrapperCache.h"
    28 namespace mozilla {
    29 namespace dom {
    31 class AlertServiceObserver;
    32 class DesktopNotification;
    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)
    45   DesktopNotificationCenter(nsPIDOMWindow *aWindow)
    46   {
    47     MOZ_ASSERT(aWindow);
    48     mOwner = aWindow;
    50     nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(aWindow);
    51     MOZ_ASSERT(sop);
    53     mPrincipal = sop->GetPrincipal();
    54     MOZ_ASSERT(mPrincipal);
    56     SetIsDOMBinding();
    57   }
    59   virtual ~DesktopNotificationCenter()
    60   {
    61   }
    63   void Shutdown() {
    64     mOwner = nullptr;
    65   }
    67   nsPIDOMWindow* GetParentObject() const
    68   {
    69     return mOwner;
    70   }
    72   virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
    74   already_AddRefed<DesktopNotification>
    75   CreateNotification(const nsAString& title,
    76                      const nsAString& description,
    77                      const nsAString& iconURL);
    79 private:
    80   nsCOMPtr<nsPIDOMWindow> mOwner;
    81   nsCOMPtr<nsIPrincipal> mPrincipal;
    82 };
    84 class DesktopNotificationRequest;
    86 class DesktopNotification MOZ_FINAL : public DOMEventTargetHelper
    87 {
    88   friend class DesktopNotificationRequest;
    90 public:
    92   DesktopNotification(const nsAString& aTitle,
    93                       const nsAString& aDescription,
    94                       const nsAString& aIconURL,
    95                       nsPIDOMWindow *aWindow,
    96                       nsIPrincipal* principal);
    98   virtual ~DesktopNotification();
   100   void Init();
   102   /*
   103    * PostDesktopNotification
   104    * Uses alert service to display a notification
   105    */
   106   nsresult PostDesktopNotification();
   108   nsresult SetAllow(bool aAllow);
   110   /*
   111    * Creates and dispatches a dom event of type aName
   112    */
   113   void DispatchNotificationEvent(const nsString& aName);
   115   void HandleAlertServiceNotification(const char *aTopic);
   117   // WebIDL
   119   nsPIDOMWindow* GetParentObject() const
   120   {
   121     return GetOwner();
   122   }
   124   virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
   126   void Show(ErrorResult& aRv);
   128   IMPL_EVENT_HANDLER(click)
   129   IMPL_EVENT_HANDLER(close)
   131 protected:
   133   nsString mTitle;
   134   nsString mDescription;
   135   nsString mIconURL;
   137   nsRefPtr<AlertServiceObserver> mObserver;
   138   nsCOMPtr<nsIPrincipal> mPrincipal;
   139   bool mAllow;
   140   bool mShowHasBeenCalled;
   142   static uint32_t sCount;
   143 };
   145 class AlertServiceObserver: public nsIObserver
   146 {
   147  public:
   148   NS_DECL_ISUPPORTS
   150     AlertServiceObserver(DesktopNotification* notification)
   151     : mNotification(notification) {}
   153   virtual ~AlertServiceObserver() {}
   155   void Disconnect() { mNotification = nullptr; }
   157   NS_IMETHODIMP
   158   Observe(nsISupports *aSubject,
   159           const char *aTopic,
   160           const char16_t *aData)
   161   {
   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   };
   174  private:
   175   DesktopNotification* mNotification;
   176 };
   178 } // namespace dom
   179 } // namespace mozilla
   181 #endif /* mozilla_dom_DesktopNotification_h */

mercurial