2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-09-11 10:05:00 +04: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/. */
|
|
|
|
|
|
|
|
#include "TimeChangeObserver.h"
|
2013-11-05 18:16:25 +04:00
|
|
|
#include "mozilla/Hal.h"
|
|
|
|
#include "mozilla/Observer.h"
|
|
|
|
#include "mozilla/HalTypes.h"
|
|
|
|
#include "nsWeakPtr.h"
|
|
|
|
#include "nsTObserverArray.h"
|
2012-09-11 10:05:00 +04:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2012-12-11 19:45:51 +04:00
|
|
|
#include "mozilla/Services.h"
|
2012-09-11 10:05:00 +04:00
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsContentUtils.h"
|
2012-10-23 11:15:43 +04:00
|
|
|
#include "nsIObserverService.h"
|
2013-05-18 00:17:53 +04:00
|
|
|
#include "nsIDocument.h"
|
2012-09-11 10:05:00 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2012-10-23 11:15:43 +04:00
|
|
|
using namespace mozilla::hal;
|
|
|
|
using namespace mozilla::services;
|
2012-09-11 10:05:00 +04:00
|
|
|
|
2013-11-05 18:16:25 +04:00
|
|
|
class nsSystemTimeChangeObserver : public SystemClockChangeObserver,
|
|
|
|
public SystemTimezoneChangeObserver
|
|
|
|
{
|
|
|
|
typedef nsTObserverArray<nsWeakPtr> ListenerArray;
|
|
|
|
public:
|
|
|
|
static nsSystemTimeChangeObserver* GetInstance();
|
|
|
|
virtual ~nsSystemTimeChangeObserver();
|
|
|
|
|
|
|
|
// Implementing hal::SystemClockChangeObserver::Notify()
|
|
|
|
void Notify(const int64_t& aClockDeltaMS);
|
|
|
|
|
|
|
|
// Implementing hal::SystemTimezoneChangeObserver::Notify()
|
|
|
|
void Notify(
|
|
|
|
const mozilla::hal::SystemTimezoneChangeInformation& aSystemTimezoneChangeInfo);
|
|
|
|
|
|
|
|
nsresult AddWindowListenerImpl(nsPIDOMWindow* aWindow);
|
|
|
|
nsresult RemoveWindowListenerImpl(nsPIDOMWindow* aWindow);
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsSystemTimeChangeObserver() { };
|
|
|
|
ListenerArray mWindowListeners;
|
|
|
|
void FireMozTimeChangeEvent();
|
|
|
|
};
|
|
|
|
|
2012-09-11 10:05:00 +04:00
|
|
|
StaticAutoPtr<nsSystemTimeChangeObserver> sObserver;
|
|
|
|
|
|
|
|
nsSystemTimeChangeObserver* nsSystemTimeChangeObserver::GetInstance()
|
|
|
|
{
|
|
|
|
if (!sObserver) {
|
|
|
|
sObserver = new nsSystemTimeChangeObserver();
|
|
|
|
ClearOnShutdown(&sObserver);
|
|
|
|
}
|
|
|
|
return sObserver;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSystemTimeChangeObserver::~nsSystemTimeChangeObserver()
|
|
|
|
{
|
2012-10-23 11:15:43 +04:00
|
|
|
UnregisterSystemClockChangeObserver(this);
|
|
|
|
UnregisterSystemTimezoneChangeObserver(this);
|
2012-09-11 10:05:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-10-23 11:15:43 +04:00
|
|
|
nsSystemTimeChangeObserver::FireMozTimeChangeEvent()
|
2012-09-11 10:05:00 +04:00
|
|
|
{
|
2013-02-16 01:57:18 +04:00
|
|
|
ListenerArray::ForwardIterator iter(mWindowListeners);
|
|
|
|
while (iter.HasMore()) {
|
|
|
|
nsWeakPtr weakWindow = iter.GetNext();
|
2013-02-16 02:03:18 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> innerWindow = do_QueryReferent(weakWindow);
|
|
|
|
nsCOMPtr<nsPIDOMWindow> outerWindow;
|
2013-02-16 01:57:18 +04:00
|
|
|
nsCOMPtr<nsIDocument> document;
|
2013-02-16 02:03:18 +04:00
|
|
|
if (!innerWindow ||
|
|
|
|
!(document = innerWindow->GetExtantDoc()) ||
|
|
|
|
!(outerWindow = innerWindow->GetOuterWindow())) {
|
2013-02-16 01:57:18 +04:00
|
|
|
mWindowListeners.RemoveElement(weakWindow);
|
|
|
|
continue;
|
2012-09-11 10:05:00 +04:00
|
|
|
}
|
|
|
|
|
2013-02-16 02:03:18 +04:00
|
|
|
nsContentUtils::DispatchTrustedEvent(document, outerWindow,
|
2012-09-11 10:05:00 +04:00
|
|
|
NS_LITERAL_STRING("moztimechange"), /* bubbles = */ true,
|
|
|
|
/* canceable = */ false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 11:15:43 +04:00
|
|
|
void
|
|
|
|
nsSystemTimeChangeObserver::Notify(const int64_t& aClockDeltaMS)
|
|
|
|
{
|
|
|
|
// Notify observers that the system clock has been adjusted.
|
|
|
|
nsCOMPtr<nsIObserverService> observerService = GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
nsString dataStr;
|
|
|
|
dataStr.AppendFloat(static_cast<double>(aClockDeltaMS));
|
|
|
|
observerService->NotifyObservers(
|
|
|
|
nullptr, "system-clock-change", dataStr.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
FireMozTimeChangeEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSystemTimeChangeObserver::Notify(
|
|
|
|
const SystemTimezoneChangeInformation& aSystemTimezoneChangeInfo)
|
|
|
|
{
|
|
|
|
FireMozTimeChangeEvent();
|
|
|
|
}
|
|
|
|
|
2012-09-11 10:05:00 +04:00
|
|
|
nsresult
|
2013-11-05 18:16:25 +04:00
|
|
|
mozilla::time::AddWindowListener(nsPIDOMWindow* aWindow)
|
2012-09-11 10:05:00 +04:00
|
|
|
{
|
2013-11-05 18:16:25 +04:00
|
|
|
return nsSystemTimeChangeObserver::GetInstance()->AddWindowListenerImpl(aWindow);
|
2012-09-11 10:05:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-02-16 02:03:18 +04:00
|
|
|
nsSystemTimeChangeObserver::AddWindowListenerImpl(nsPIDOMWindow* aWindow)
|
2012-09-11 10:05:00 +04:00
|
|
|
{
|
|
|
|
if (!aWindow) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
2013-02-16 02:03:18 +04:00
|
|
|
if (aWindow->IsOuterWindow()) {
|
|
|
|
aWindow = aWindow->GetCurrentInnerWindow();
|
|
|
|
if (!aWindow) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-16 01:57:18 +04:00
|
|
|
nsWeakPtr windowWeakRef = do_GetWeakReference(aWindow);
|
2012-10-03 13:38:46 +04:00
|
|
|
NS_ASSERTION(windowWeakRef, "nsIDOMWindow implementations shuld support weak ref");
|
|
|
|
|
|
|
|
if (mWindowListeners.IndexOf(windowWeakRef) !=
|
2013-02-16 01:57:18 +04:00
|
|
|
ListenerArray::array_type::NoIndex) {
|
2012-09-11 10:05:00 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-16 01:57:18 +04:00
|
|
|
if (mWindowListeners.IsEmpty()) {
|
2012-10-23 11:15:43 +04:00
|
|
|
RegisterSystemClockChangeObserver(sObserver);
|
|
|
|
RegisterSystemTimezoneChangeObserver(sObserver);
|
2012-09-11 10:05:00 +04:00
|
|
|
}
|
|
|
|
|
2012-10-03 13:38:46 +04:00
|
|
|
mWindowListeners.AppendElement(windowWeakRef);
|
2012-09-11 10:05:00 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-11-05 18:16:25 +04:00
|
|
|
mozilla::time::RemoveWindowListener(nsPIDOMWindow* aWindow)
|
2012-09-11 10:05:00 +04:00
|
|
|
{
|
|
|
|
if (!sObserver) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:25 +04:00
|
|
|
return nsSystemTimeChangeObserver::GetInstance()->RemoveWindowListenerImpl(aWindow);
|
2012-09-11 10:05:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-02-16 02:03:18 +04:00
|
|
|
nsSystemTimeChangeObserver::RemoveWindowListenerImpl(nsPIDOMWindow* aWindow)
|
2012-09-11 10:05:00 +04:00
|
|
|
{
|
2013-02-16 02:03:18 +04:00
|
|
|
if (!aWindow) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aWindow->IsOuterWindow()) {
|
|
|
|
aWindow = aWindow->GetCurrentInnerWindow();
|
|
|
|
if (!aWindow) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-08 15:10:36 +04:00
|
|
|
nsWeakPtr windowWeakRef = do_GetWeakReference(aWindow);
|
|
|
|
mWindowListeners.RemoveElement(windowWeakRef);
|
2012-09-11 10:05:00 +04:00
|
|
|
|
2013-02-16 01:57:18 +04:00
|
|
|
if (mWindowListeners.IsEmpty()) {
|
2012-10-23 11:15:43 +04:00
|
|
|
UnregisterSystemClockChangeObserver(sObserver);
|
|
|
|
UnregisterSystemTimezoneChangeObserver(sObserver);
|
2012-09-11 10:05:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|