2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-09-09 22:00:14 -07:00
|
|
|
|
2013-04-10 07:20:43 -07:00
|
|
|
#ifndef mozilla_dom_DesktopNotification_h
|
|
|
|
#define mozilla_dom_DesktopNotification_h
|
2010-09-09 22:00:14 -07:00
|
|
|
|
2010-09-13 13:44:53 -07:00
|
|
|
#include "PCOMContentPermissionRequestChild.h"
|
|
|
|
|
2011-10-03 12:11:31 -07:00
|
|
|
#include "nsIPrincipal.h"
|
2010-09-09 22:00:14 -07:00
|
|
|
#include "nsIAlertsService.h"
|
2010-09-13 12:31:53 -07:00
|
|
|
#include "nsIContentPermissionPrompt.h"
|
2010-09-09 22:00:14 -07:00
|
|
|
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsWeakPtr.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
#include "nsDOMEventTargetHelper.h"
|
2012-06-10 16:44:50 -07:00
|
|
|
#include "nsIDOMEvent.h"
|
2011-07-20 12:18:54 -07:00
|
|
|
#include "nsIDocument.h"
|
2010-09-09 22:00:14 -07:00
|
|
|
|
2013-04-10 07:20:43 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
|
|
|
|
|
2013-04-10 07:20:43 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2010-09-09 22:00:14 -07:00
|
|
|
class AlertServiceObserver;
|
2013-04-10 07:20:43 -07:00
|
|
|
class DesktopNotification;
|
2010-09-09 22:00:14 -07:00
|
|
|
|
|
|
|
/*
|
2013-04-10 07:20:43 -07:00
|
|
|
* DesktopNotificationCenter
|
|
|
|
* Object hangs off of the navigator object and hands out DesktopNotification objects
|
2010-09-09 22:00:14 -07:00
|
|
|
*/
|
2013-04-10 07:20:43 -07:00
|
|
|
class DesktopNotificationCenter MOZ_FINAL : public nsISupports,
|
|
|
|
public nsWrapperCache
|
2010-09-09 22:00:14 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-04-10 07:20:43 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DesktopNotificationCenter)
|
2010-09-09 22:00:14 -07:00
|
|
|
|
2013-04-10 07:20:43 -07:00
|
|
|
DesktopNotificationCenter(nsPIDOMWindow *aWindow)
|
2010-09-09 22:00:14 -07:00
|
|
|
{
|
2013-07-22 06:09:51 -07:00
|
|
|
MOZ_ASSERT(aWindow);
|
2010-09-09 22:00:14 -07:00
|
|
|
mOwner = aWindow;
|
|
|
|
|
2013-07-22 06:09:51 -07:00
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(aWindow);
|
|
|
|
MOZ_ASSERT(sop);
|
|
|
|
|
|
|
|
mPrincipal = sop->GetPrincipal();
|
|
|
|
MOZ_ASSERT(mPrincipal);
|
2013-04-10 07:20:43 -07:00
|
|
|
|
|
|
|
SetIsDOMBinding();
|
2010-09-09 22:00:14 -07:00
|
|
|
}
|
|
|
|
|
2013-04-10 07:20:43 -07:00
|
|
|
virtual ~DesktopNotificationCenter()
|
2010-09-09 22:00:14 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-10-18 18:54:00 -07:00
|
|
|
void Shutdown() {
|
2012-07-30 07:20:58 -07:00
|
|
|
mOwner = nullptr;
|
2010-10-18 18:54:00 -07:00
|
|
|
}
|
|
|
|
|
2013-04-10 07:20:43 -07:00
|
|
|
nsPIDOMWindow* GetParentObject() const
|
|
|
|
{
|
|
|
|
return mOwner;
|
|
|
|
}
|
|
|
|
|
2013-04-25 09:29:54 -07:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
|
2013-04-10 07:20:43 -07:00
|
|
|
|
|
|
|
already_AddRefed<DesktopNotification>
|
|
|
|
CreateNotification(const nsAString& title,
|
|
|
|
const nsAString& description,
|
|
|
|
const nsAString& iconURL);
|
|
|
|
|
2010-09-09 22:00:14 -07:00
|
|
|
private:
|
|
|
|
nsCOMPtr<nsPIDOMWindow> mOwner;
|
2012-07-30 07:58:26 -07:00
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
2010-09-09 22:00:14 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-04-10 07:20:43 -07:00
|
|
|
class DesktopNotification MOZ_FINAL : public nsDOMEventTargetHelper
|
2010-09-09 22:00:14 -07:00
|
|
|
{
|
2013-04-10 07:20:43 -07:00
|
|
|
friend class DesktopNotificationRequest;
|
2010-09-09 22:00:14 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2013-04-10 07:20:43 -07:00
|
|
|
DesktopNotification(const nsAString& aTitle,
|
|
|
|
const nsAString& aDescription,
|
|
|
|
const nsAString& aIconURL,
|
2013-04-10 07:20:43 -07:00
|
|
|
nsPIDOMWindow *aWindow,
|
|
|
|
nsIPrincipal* principal);
|
2010-09-09 22:00:14 -07:00
|
|
|
|
2013-04-10 07:20:43 -07:00
|
|
|
virtual ~DesktopNotification();
|
2010-09-09 22:00:14 -07:00
|
|
|
|
2012-11-19 10:27:54 -08:00
|
|
|
void Init();
|
|
|
|
|
2010-09-09 22:00:14 -07:00
|
|
|
/*
|
|
|
|
* PostDesktopNotification
|
|
|
|
* Uses alert service to display a notification
|
|
|
|
*/
|
2012-07-26 15:25:02 -07:00
|
|
|
nsresult PostDesktopNotification();
|
2010-09-09 22:00:14 -07:00
|
|
|
|
2012-07-26 15:25:02 -07:00
|
|
|
nsresult SetAllow(bool aAllow);
|
2011-01-19 09:38:36 -08:00
|
|
|
|
2010-09-09 22:00:14 -07:00
|
|
|
/*
|
|
|
|
* Creates and dispatches a dom event of type aName
|
|
|
|
*/
|
|
|
|
void DispatchNotificationEvent(const nsString& aName);
|
|
|
|
|
|
|
|
void HandleAlertServiceNotification(const char *aTopic);
|
|
|
|
|
2013-04-10 07:20:43 -07:00
|
|
|
// WebIDL
|
|
|
|
|
|
|
|
nsPIDOMWindow* GetParentObject() const
|
|
|
|
{
|
|
|
|
return GetOwner();
|
|
|
|
}
|
|
|
|
|
2013-04-25 09:29:54 -07:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
|
2013-04-10 07:20:43 -07:00
|
|
|
|
|
|
|
void Show(ErrorResult& aRv);
|
|
|
|
|
|
|
|
IMPL_EVENT_HANDLER(click)
|
|
|
|
IMPL_EVENT_HANDLER(close)
|
|
|
|
|
2010-09-09 22:00:14 -07:00
|
|
|
protected:
|
|
|
|
|
|
|
|
nsString mTitle;
|
|
|
|
nsString mDescription;
|
|
|
|
nsString mIconURL;
|
|
|
|
|
|
|
|
nsRefPtr<AlertServiceObserver> mObserver;
|
2012-07-30 07:58:26 -07:00
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mAllow;
|
|
|
|
bool mShowHasBeenCalled;
|
2013-03-18 06:24:53 -07:00
|
|
|
|
|
|
|
static uint32_t sCount;
|
2010-09-09 22:00:14 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Simple Request
|
|
|
|
*/
|
2013-04-10 07:20:43 -07:00
|
|
|
class DesktopNotificationRequest : public nsIContentPermissionRequest,
|
|
|
|
public nsRunnable,
|
|
|
|
public PCOMContentPermissionRequestChild
|
2010-09-13 13:44:53 -07:00
|
|
|
|
2010-09-09 22:00:14 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2010-09-13 12:31:53 -07:00
|
|
|
NS_DECL_NSICONTENTPERMISSIONREQUEST
|
2010-09-09 22:00:14 -07:00
|
|
|
|
2013-04-10 07:20:43 -07:00
|
|
|
DesktopNotificationRequest(DesktopNotification* notification)
|
2010-09-09 22:00:14 -07:00
|
|
|
: mDesktopNotification(notification) {}
|
|
|
|
|
2013-06-05 09:15:48 -07:00
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE
|
2010-09-09 22:00:14 -07:00
|
|
|
{
|
2010-09-13 12:31:53 -07:00
|
|
|
nsCOMPtr<nsIContentPermissionPrompt> prompt =
|
2012-10-31 10:06:42 -07:00
|
|
|
do_CreateInstance(NS_CONTENT_PERMISSION_PROMPT_CONTRACTID);
|
2010-09-09 22:00:14 -07:00
|
|
|
if (prompt) {
|
|
|
|
prompt->Prompt(this);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-10 07:20:43 -07:00
|
|
|
~DesktopNotificationRequest()
|
2010-09-13 13:44:53 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-05 09:15:48 -07:00
|
|
|
virtual bool Recv__delete__(const bool& allow) MOZ_OVERRIDE
|
2010-09-09 22:00:14 -07:00
|
|
|
{
|
2010-09-13 13:44:53 -07:00
|
|
|
if (allow)
|
|
|
|
(void) Allow();
|
|
|
|
else
|
|
|
|
(void) Cancel();
|
|
|
|
return true;
|
2010-09-09 22:00:14 -07:00
|
|
|
}
|
2013-06-05 09:15:48 -07:00
|
|
|
virtual void IPDLRelease() MOZ_OVERRIDE { Release(); }
|
2010-09-09 22:00:14 -07:00
|
|
|
|
2013-04-10 07:20:43 -07:00
|
|
|
nsRefPtr<DesktopNotification> mDesktopNotification;
|
2010-09-09 22:00:14 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class AlertServiceObserver: public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2013-04-10 07:20:43 -07:00
|
|
|
|
|
|
|
AlertServiceObserver(DesktopNotification* notification)
|
2010-09-09 22:00:14 -07:00
|
|
|
: mNotification(notification) {}
|
2013-04-10 07:20:43 -07:00
|
|
|
|
2010-09-09 22:00:14 -07:00
|
|
|
virtual ~AlertServiceObserver() {}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
void Disconnect() { mNotification = nullptr; }
|
2010-09-09 22:00:14 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Observe(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *aData)
|
|
|
|
{
|
2012-12-03 06:42:36 -08:00
|
|
|
|
2010-09-09 22:00:14 -07:00
|
|
|
// forward to parent
|
2012-12-03 06:42:36 -08:00
|
|
|
if (mNotification) {
|
|
|
|
#ifdef MOZ_B2G
|
|
|
|
if (NS_FAILED(mNotification->CheckInnerWindowCorrectness()))
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
#endif
|
2010-09-09 22:00:14 -07:00
|
|
|
mNotification->HandleAlertServiceNotification(aTopic);
|
2012-12-03 06:42:36 -08:00
|
|
|
}
|
2010-09-09 22:00:14 -07:00
|
|
|
return NS_OK;
|
|
|
|
};
|
2012-12-03 06:42:36 -08:00
|
|
|
|
2010-09-09 22:00:14 -07:00
|
|
|
private:
|
2013-04-10 07:20:43 -07:00
|
|
|
DesktopNotification* mNotification;
|
2010-09-09 22:00:14 -07:00
|
|
|
};
|
|
|
|
|
2013-04-10 07:20:43 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_dom_DesktopNotification_h */
|