2012-09-10 23:05:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "TimeChangeObserver.h"
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsDOMEvent.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
|
|
|
using namespace mozilla::hal;
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
StaticAutoPtr<nsSystemTimeChangeObserver> sObserver;
|
|
|
|
|
|
|
|
nsSystemTimeChangeObserver* nsSystemTimeChangeObserver::GetInstance()
|
|
|
|
{
|
|
|
|
if (!sObserver) {
|
|
|
|
sObserver = new nsSystemTimeChangeObserver();
|
|
|
|
ClearOnShutdown(&sObserver);
|
|
|
|
}
|
|
|
|
return sObserver;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSystemTimeChangeObserver::~nsSystemTimeChangeObserver()
|
|
|
|
{
|
|
|
|
mWindowListeners.Clear();
|
|
|
|
UnregisterSystemTimeChangeObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSystemTimeChangeObserver::Notify(const SystemTimeChange& aReason)
|
|
|
|
{
|
|
|
|
//Copy mWindowListeners and iterate over windowListeners instead because
|
|
|
|
//mWindowListeners may be modified while we loop.
|
|
|
|
nsTArray<nsWeakPtr> windowListeners;
|
2012-09-17 01:35:55 -07:00
|
|
|
for (uint32_t i = 0; i < mWindowListeners.Length(); i++) {
|
2012-09-10 23:05:00 -07:00
|
|
|
windowListeners.AppendElement(mWindowListeners.SafeElementAt(i));
|
|
|
|
}
|
|
|
|
|
2012-09-17 01:35:55 -07:00
|
|
|
for (int32_t i = windowListeners.Length() - 1; i >= 0; i--) {
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(windowListeners[i]);
|
2012-09-10 23:05:00 -07:00
|
|
|
if (!window) {
|
|
|
|
mWindowListeners.RemoveElement(windowListeners[i]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-17 01:35:55 -07:00
|
|
|
nsCOMPtr<nsIDocument> document = window->GetDoc();
|
|
|
|
if (!document) {
|
2012-09-10 23:05:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-17 01:35:55 -07:00
|
|
|
nsContentUtils::DispatchTrustedEvent(document, window,
|
2012-09-10 23:05:00 -07:00
|
|
|
NS_LITERAL_STRING("moztimechange"), /* bubbles = */ true,
|
|
|
|
/* canceable = */ false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSystemTimeChangeObserver::AddWindowListener(nsIDOMWindow* aWindow)
|
|
|
|
{
|
|
|
|
return GetInstance()->AddWindowListenerImpl(aWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSystemTimeChangeObserver::AddWindowListenerImpl(nsIDOMWindow* aWindow)
|
|
|
|
{
|
|
|
|
if (!aWindow) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
2012-10-03 02:38:46 -07:00
|
|
|
nsCOMPtr<nsIWeakReference> windowWeakRef = do_GetWeakReference(aWindow);
|
|
|
|
NS_ASSERTION(windowWeakRef, "nsIDOMWindow implementations shuld support weak ref");
|
|
|
|
|
|
|
|
if (mWindowListeners.IndexOf(windowWeakRef) !=
|
2012-09-10 23:05:00 -07:00
|
|
|
nsTArray<nsIDOMWindow*>::NoIndex) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mWindowListeners.Length() == 0) {
|
|
|
|
RegisterSystemTimeChangeObserver(sObserver);
|
|
|
|
}
|
|
|
|
|
2012-10-03 02:38:46 -07:00
|
|
|
mWindowListeners.AppendElement(windowWeakRef);
|
2012-09-10 23:05:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSystemTimeChangeObserver::RemoveWindowListener(nsIDOMWindow* aWindow)
|
|
|
|
{
|
|
|
|
if (!sObserver) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetInstance()->RemoveWindowListenerImpl(aWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSystemTimeChangeObserver::RemoveWindowListenerImpl(nsIDOMWindow* aWindow)
|
|
|
|
{
|
|
|
|
mWindowListeners.RemoveElement(NS_GetWeakReference(aWindow));
|
|
|
|
|
|
|
|
if (mWindowListeners.Length() == 0) {
|
|
|
|
UnregisterSystemTimeChangeObserver(sObserver);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|