dom/src/notification/DesktopNotification.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/src/notification/DesktopNotification.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,181 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#ifndef mozilla_dom_DesktopNotification_h
     1.9 +#define mozilla_dom_DesktopNotification_h
    1.10 +
    1.11 +#include "nsIPrincipal.h"
    1.12 +#include "nsIAlertsService.h"
    1.13 +#include "nsIContentPermissionPrompt.h"
    1.14 +
    1.15 +#include "nsIObserver.h"
    1.16 +#include "nsString.h"
    1.17 +#include "nsWeakPtr.h"
    1.18 +#include "nsCycleCollectionParticipant.h"
    1.19 +#include "nsIDOMWindow.h"
    1.20 +#include "nsIScriptObjectPrincipal.h"
    1.21 +
    1.22 +#include "nsIDOMEvent.h"
    1.23 +#include "nsIDocument.h"
    1.24 +
    1.25 +#include "mozilla/Attributes.h"
    1.26 +#include "mozilla/DOMEventTargetHelper.h"
    1.27 +#include "mozilla/ErrorResult.h"
    1.28 +#include "nsWrapperCache.h"
    1.29 +
    1.30 +
    1.31 +namespace mozilla {
    1.32 +namespace dom {
    1.33 +
    1.34 +class AlertServiceObserver;
    1.35 +class DesktopNotification;
    1.36 +
    1.37 +/*
    1.38 + * DesktopNotificationCenter
    1.39 + * Object hangs off of the navigator object and hands out DesktopNotification objects
    1.40 + */
    1.41 +class DesktopNotificationCenter MOZ_FINAL : public nsISupports,
    1.42 +                                            public nsWrapperCache
    1.43 +{
    1.44 +public:
    1.45 +  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
    1.46 +  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DesktopNotificationCenter)
    1.47 +
    1.48 +  DesktopNotificationCenter(nsPIDOMWindow *aWindow)
    1.49 +  {
    1.50 +    MOZ_ASSERT(aWindow);
    1.51 +    mOwner = aWindow;
    1.52 +
    1.53 +    nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(aWindow);
    1.54 +    MOZ_ASSERT(sop);
    1.55 +
    1.56 +    mPrincipal = sop->GetPrincipal();
    1.57 +    MOZ_ASSERT(mPrincipal);
    1.58 +
    1.59 +    SetIsDOMBinding();
    1.60 +  }
    1.61 +
    1.62 +  virtual ~DesktopNotificationCenter()
    1.63 +  {
    1.64 +  }
    1.65 +
    1.66 +  void Shutdown() {
    1.67 +    mOwner = nullptr;
    1.68 +  }
    1.69 +
    1.70 +  nsPIDOMWindow* GetParentObject() const
    1.71 +  {
    1.72 +    return mOwner;
    1.73 +  }
    1.74 +
    1.75 +  virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
    1.76 +
    1.77 +  already_AddRefed<DesktopNotification>
    1.78 +  CreateNotification(const nsAString& title,
    1.79 +                     const nsAString& description,
    1.80 +                     const nsAString& iconURL);
    1.81 +
    1.82 +private:
    1.83 +  nsCOMPtr<nsPIDOMWindow> mOwner;
    1.84 +  nsCOMPtr<nsIPrincipal> mPrincipal;
    1.85 +};
    1.86 +
    1.87 +class DesktopNotificationRequest;
    1.88 +
    1.89 +class DesktopNotification MOZ_FINAL : public DOMEventTargetHelper
    1.90 +{
    1.91 +  friend class DesktopNotificationRequest;
    1.92 +
    1.93 +public:
    1.94 +
    1.95 +  DesktopNotification(const nsAString& aTitle,
    1.96 +                      const nsAString& aDescription,
    1.97 +                      const nsAString& aIconURL,
    1.98 +                      nsPIDOMWindow *aWindow,
    1.99 +                      nsIPrincipal* principal);
   1.100 +
   1.101 +  virtual ~DesktopNotification();
   1.102 +
   1.103 +  void Init();
   1.104 +
   1.105 +  /*
   1.106 +   * PostDesktopNotification
   1.107 +   * Uses alert service to display a notification
   1.108 +   */
   1.109 +  nsresult PostDesktopNotification();
   1.110 +
   1.111 +  nsresult SetAllow(bool aAllow);
   1.112 +
   1.113 +  /*
   1.114 +   * Creates and dispatches a dom event of type aName
   1.115 +   */
   1.116 +  void DispatchNotificationEvent(const nsString& aName);
   1.117 +
   1.118 +  void HandleAlertServiceNotification(const char *aTopic);
   1.119 +
   1.120 +  // WebIDL
   1.121 +
   1.122 +  nsPIDOMWindow* GetParentObject() const
   1.123 +  {
   1.124 +    return GetOwner();
   1.125 +  }
   1.126 +
   1.127 +  virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
   1.128 +
   1.129 +  void Show(ErrorResult& aRv);
   1.130 +
   1.131 +  IMPL_EVENT_HANDLER(click)
   1.132 +  IMPL_EVENT_HANDLER(close)
   1.133 +
   1.134 +protected:
   1.135 +
   1.136 +  nsString mTitle;
   1.137 +  nsString mDescription;
   1.138 +  nsString mIconURL;
   1.139 +
   1.140 +  nsRefPtr<AlertServiceObserver> mObserver;
   1.141 +  nsCOMPtr<nsIPrincipal> mPrincipal;
   1.142 +  bool mAllow;
   1.143 +  bool mShowHasBeenCalled;
   1.144 +
   1.145 +  static uint32_t sCount;
   1.146 +};
   1.147 +
   1.148 +class AlertServiceObserver: public nsIObserver
   1.149 +{
   1.150 + public:
   1.151 +  NS_DECL_ISUPPORTS
   1.152 +
   1.153 +    AlertServiceObserver(DesktopNotification* notification)
   1.154 +    : mNotification(notification) {}
   1.155 +
   1.156 +  virtual ~AlertServiceObserver() {}
   1.157 +
   1.158 +  void Disconnect() { mNotification = nullptr; }
   1.159 +
   1.160 +  NS_IMETHODIMP
   1.161 +  Observe(nsISupports *aSubject,
   1.162 +          const char *aTopic,
   1.163 +          const char16_t *aData)
   1.164 +  {
   1.165 +
   1.166 +    // forward to parent
   1.167 +    if (mNotification) {
   1.168 +#ifdef MOZ_B2G
   1.169 +    if (NS_FAILED(mNotification->CheckInnerWindowCorrectness()))
   1.170 +      return NS_ERROR_NOT_AVAILABLE;
   1.171 +#endif
   1.172 +      mNotification->HandleAlertServiceNotification(aTopic);
   1.173 +    }
   1.174 +    return NS_OK;
   1.175 +  };
   1.176 +
   1.177 + private:
   1.178 +  DesktopNotification* mNotification;
   1.179 +};
   1.180 +
   1.181 +} // namespace dom
   1.182 +} // namespace mozilla
   1.183 +
   1.184 +#endif /* mozilla_dom_DesktopNotification_h */

mercurial