Bug 1224771 - Close all web notifications when the originating tab is closed. r=wchen

--HG--
extra : commitid : 4AtjO7xJVTM
extra : rebase_source : 9841344b83f54eae0afc84cd2a7a95d38863e062
This commit is contained in:
Kit Cambridge 2015-12-29 15:06:28 -07:00
Родитель 003a0c17cc
Коммит 426ea78303
2 изменённых файлов: 68 добавлений и 1 удалений

Просмотреть файл

@ -981,6 +981,23 @@ Notification::Notification(nsIGlobalObject* aGlobal, const nsAString& aID,
}
}
nsresult
Notification::Init()
{
if (!mWorkerPrivate) {
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
NS_ENSURE_TRUE(obs, NS_ERROR_FAILURE);
nsresult rv = obs->AddObserver(this, DOM_WINDOW_DESTROYED_TOPIC, true);
NS_ENSURE_SUCCESS(rv, rv);
rv = obs->AddObserver(this, DOM_WINDOW_FROZEN_TOPIC, true);
NS_ENSURE_SUCCESS(rv, rv);
}
return NS_OK;
}
void
Notification::SetAlertName()
{
@ -1150,6 +1167,7 @@ Notification::CreateInternal(nsIGlobalObject* aGlobal,
const nsAString& aTitle,
const NotificationOptions& aOptions)
{
nsresult rv;
nsString id;
if (!aID.IsEmpty()) {
id = aID;
@ -1158,7 +1176,7 @@ Notification::CreateInternal(nsIGlobalObject* aGlobal,
do_GetService("@mozilla.org/uuid-generator;1");
NS_ENSURE_TRUE(uuidgen, nullptr);
nsID uuid;
nsresult rv = uuidgen->GenerateUUIDInPlace(&uuid);
rv = uuidgen->GenerateUUIDInPlace(&uuid);
NS_ENSURE_SUCCESS(rv, nullptr);
char buffer[NSID_LENGTH];
@ -1174,6 +1192,8 @@ Notification::CreateInternal(nsIGlobalObject* aGlobal,
aOptions.mTag,
aOptions.mIcon,
aOptions.mMozbehavior);
rv = notification->Init();
NS_ENSURE_SUCCESS(rv, nullptr);
return notification.forget();
}
@ -1202,6 +1222,8 @@ NS_IMPL_ADDREF_INHERITED(Notification, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(Notification, DOMEventTargetHelper)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(Notification)
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
nsIPrincipal*
@ -2722,6 +2744,46 @@ Notification::OpenSettings(nsIPrincipal* aPrincipal)
return NS_OK;
}
NS_IMETHODIMP
Notification::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData)
{
AssertIsOnMainThread();
if (!strcmp(aTopic, DOM_WINDOW_DESTROYED_TOPIC) ||
!strcmp(aTopic, DOM_WINDOW_FROZEN_TOPIC)) {
nsCOMPtr<nsPIDOMWindow> window = GetOwner();
if (SameCOMIdentity(aSubject, window)) {
nsCOMPtr<nsIObserverService> obs =
mozilla::services::GetObserverService();
if (obs) {
obs->RemoveObserver(this, DOM_WINDOW_DESTROYED_TOPIC);
obs->RemoveObserver(this, DOM_WINDOW_FROZEN_TOPIC);
}
uint16_t appStatus = nsIPrincipal::APP_STATUS_NOT_INSTALLED;
uint32_t appId = nsIScriptSecurityManager::UNKNOWN_APP_ID;
nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
nsCOMPtr<nsIPrincipal> nodePrincipal = doc ? doc->NodePrincipal() :
nullptr;
if (nodePrincipal) {
appStatus = nodePrincipal->GetAppStatus();
appId = nodePrincipal->GetAppId();
}
if (appStatus == nsIPrincipal::APP_STATUS_NOT_INSTALLED ||
appId == nsIScriptSecurityManager::NO_APP_ID ||
appId == nsIScriptSecurityManager::UNKNOWN_APP_ID) {
CloseInternal();
}
}
}
return NS_OK;
}
} // namespace dom
} // namespace mozilla

Просмотреть файл

@ -17,6 +17,7 @@
#include "nsCycleCollectionParticipant.h"
#include "nsHashKeys.h"
#include "nsTHashtable.h"
#include "nsWeakReference.h"
#define NOTIFICATIONTELEMETRYSERVICE_CONTRACTID \
"@mozilla.org/notificationTelemetryService;1"
@ -132,6 +133,8 @@ private:
*
*/
class Notification : public DOMEventTargetHelper
, public nsIObserver
, public nsSupportsWeakReference
{
friend class CloseNotificationRunnable;
friend class NotificationTask;
@ -151,6 +154,7 @@ public:
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(Notification, DOMEventTargetHelper)
NS_DECL_NSIOBSERVER
static bool PrefEnabled(JSContext* aCx, JSObject* aObj);
// Returns if Notification.get() is allowed for the current global.
@ -325,6 +329,7 @@ protected:
const nsAString& aTitle,
const NotificationOptions& aOptions);
nsresult Init();
bool IsInPrivateBrowsing();
void ShowInternal();
void CloseInternal();