summaryrefslogtreecommitdiff
path: root/dom/time
diff options
context:
space:
mode:
authorwolfbeast <mcwerewolf@gmail.com>2014-05-21 11:38:25 +0200
committerwolfbeast <mcwerewolf@gmail.com>2014-05-21 11:38:25 +0200
commitd25ba7d760b017b038e5aa6c0a605b4a330eb68d (patch)
tree16ec27edc7d5f83986f16236d3a36a2682a0f37e /dom/time
parenta942906574671868daf122284a9c4689e6924f74 (diff)
downloadpalemoon-gre-d25ba7d760b017b038e5aa6c0a605b4a330eb68d.tar.gz
Recommit working copy to repo with proper line endings.
Diffstat (limited to 'dom/time')
-rw-r--r--dom/time/DateCacheCleaner.cpp53
-rw-r--r--dom/time/DateCacheCleaner.h18
-rw-r--r--dom/time/Makefile.in21
-rw-r--r--dom/time/TimeChangeObserver.cpp150
-rw-r--r--dom/time/TimeChangeObserver.h44
-rw-r--r--dom/time/TimeManager.cpp49
-rw-r--r--dom/time/TimeManager.h59
-rw-r--r--dom/time/TimeService.cpp38
-rw-r--r--dom/time/TimeService.h35
-rw-r--r--dom/time/moz.build29
-rw-r--r--dom/time/nsIDOMNavigatorTime.idl11
-rw-r--r--dom/time/nsITimeService.idl20
12 files changed, 527 insertions, 0 deletions
diff --git a/dom/time/DateCacheCleaner.cpp b/dom/time/DateCacheCleaner.cpp
new file mode 100644
index 000000000..f277f422f
--- /dev/null
+++ b/dom/time/DateCacheCleaner.cpp
@@ -0,0 +1,53 @@
+/* -*- 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 "mozilla/Hal.h"
+#include "mozilla/ClearOnShutdown.h"
+#include "DateCacheCleaner.h"
+
+#include "nsContentUtils.h"
+#include "nsCxPusher.h"
+#include "mozilla/StaticPtr.h"
+
+using namespace mozilla::hal;
+
+namespace mozilla {
+namespace dom {
+namespace time {
+
+class DateCacheCleaner : public SystemTimezoneChangeObserver
+{
+public:
+ DateCacheCleaner()
+ {
+ RegisterSystemTimezoneChangeObserver(this);
+ }
+
+ ~DateCacheCleaner()
+ {
+ UnregisterSystemTimezoneChangeObserver(this);
+ }
+ void Notify(const SystemTimezoneChangeInformation& aSystemTimezoneChangeInfo)
+ {
+ mozilla::AutoSafeJSContext cx;
+ JS_ClearDateCaches(cx);
+ }
+
+};
+
+StaticAutoPtr<DateCacheCleaner> sDateCacheCleaner;
+
+void
+InitializeDateCacheCleaner()
+{
+ if (!sDateCacheCleaner) {
+ sDateCacheCleaner = new DateCacheCleaner();
+ ClearOnShutdown(&sDateCacheCleaner);
+ }
+}
+
+} // namespace time
+} // namespace dom
+} // namespace mozilla
diff --git a/dom/time/DateCacheCleaner.h b/dom/time/DateCacheCleaner.h
new file mode 100644
index 000000000..795712f64
--- /dev/null
+++ b/dom/time/DateCacheCleaner.h
@@ -0,0 +1,18 @@
+/* -*- 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/. */
+
+/*
+ * InitializeDateCacheCleaner registers DateCacheCleaner to
+ * SystemTimeChangeObserver. When time zone is changed, DateCacheCleaner calls
+ * JS_ClearDateCaches to update the time zone information.
+ */
+
+namespace mozilla {
+namespace dom {
+namespace time {
+void InitializeDateCacheCleaner();
+} //namespace time
+} //namespace dom
+} //namespace mozilla
diff --git a/dom/time/Makefile.in b/dom/time/Makefile.in
new file mode 100644
index 000000000..07cf3a2e2
--- /dev/null
+++ b/dom/time/Makefile.in
@@ -0,0 +1,21 @@
+# 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/.
+
+DEPTH = ../..
+topsrcdir = @top_srcdir@
+srcdir = @srcdir@
+VPATH = @srcdir@
+
+include $(DEPTH)/config/autoconf.mk
+
+LIBRARY_NAME = dom_time_s
+LIBXUL_LIBRARY = 1
+FORCE_STATIC_LIB = 1
+FAIL_ON_WARNINGS := 1
+
+include $(topsrcdir)/dom/dom-config.mk
+
+include $(topsrcdir)/config/config.mk
+include $(topsrcdir)/ipc/chromium/chromium-config.mk
+include $(topsrcdir)/config/rules.mk
diff --git a/dom/time/TimeChangeObserver.cpp b/dom/time/TimeChangeObserver.cpp
new file mode 100644
index 000000000..f0c4c1a07
--- /dev/null
+++ b/dom/time/TimeChangeObserver.cpp
@@ -0,0 +1,150 @@
+/* -*- 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/Services.h"
+#include "mozilla/StaticPtr.h"
+#include "nsPIDOMWindow.h"
+#include "nsDOMEvent.h"
+#include "nsContentUtils.h"
+#include "nsIObserverService.h"
+#include "nsIDocument.h"
+
+using namespace mozilla;
+using namespace mozilla::hal;
+using namespace mozilla::services;
+
+StaticAutoPtr<nsSystemTimeChangeObserver> sObserver;
+
+nsSystemTimeChangeObserver* nsSystemTimeChangeObserver::GetInstance()
+{
+ if (!sObserver) {
+ sObserver = new nsSystemTimeChangeObserver();
+ ClearOnShutdown(&sObserver);
+ }
+ return sObserver;
+}
+
+nsSystemTimeChangeObserver::~nsSystemTimeChangeObserver()
+{
+ UnregisterSystemClockChangeObserver(this);
+ UnregisterSystemTimezoneChangeObserver(this);
+}
+
+void
+nsSystemTimeChangeObserver::FireMozTimeChangeEvent()
+{
+ ListenerArray::ForwardIterator iter(mWindowListeners);
+ while (iter.HasMore()) {
+ nsWeakPtr weakWindow = iter.GetNext();
+ nsCOMPtr<nsPIDOMWindow> innerWindow = do_QueryReferent(weakWindow);
+ nsCOMPtr<nsPIDOMWindow> outerWindow;
+ nsCOMPtr<nsIDocument> document;
+ if (!innerWindow ||
+ !(document = innerWindow->GetExtantDoc()) ||
+ !(outerWindow = innerWindow->GetOuterWindow())) {
+ mWindowListeners.RemoveElement(weakWindow);
+ continue;
+ }
+
+ nsContentUtils::DispatchTrustedEvent(document, outerWindow,
+ NS_LITERAL_STRING("moztimechange"), /* bubbles = */ true,
+ /* canceable = */ false);
+ }
+}
+
+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();
+}
+
+nsresult
+nsSystemTimeChangeObserver::AddWindowListener(nsPIDOMWindow* aWindow)
+{
+ return GetInstance()->AddWindowListenerImpl(aWindow);
+}
+
+nsresult
+nsSystemTimeChangeObserver::AddWindowListenerImpl(nsPIDOMWindow* aWindow)
+{
+ if (!aWindow) {
+ return NS_ERROR_ILLEGAL_VALUE;
+ }
+
+ if (aWindow->IsOuterWindow()) {
+ aWindow = aWindow->GetCurrentInnerWindow();
+ if (!aWindow) {
+ return NS_ERROR_FAILURE;
+ }
+ }
+
+ nsWeakPtr windowWeakRef = do_GetWeakReference(aWindow);
+ NS_ASSERTION(windowWeakRef, "nsIDOMWindow implementations shuld support weak ref");
+
+ if (mWindowListeners.IndexOf(windowWeakRef) !=
+ ListenerArray::array_type::NoIndex) {
+ return NS_OK;
+ }
+
+ if (mWindowListeners.IsEmpty()) {
+ RegisterSystemClockChangeObserver(sObserver);
+ RegisterSystemTimezoneChangeObserver(sObserver);
+ }
+
+ mWindowListeners.AppendElement(windowWeakRef);
+ return NS_OK;
+}
+
+nsresult
+nsSystemTimeChangeObserver::RemoveWindowListener(nsPIDOMWindow* aWindow)
+{
+ if (!sObserver) {
+ return NS_OK;
+ }
+
+ return GetInstance()->RemoveWindowListenerImpl(aWindow);
+}
+
+nsresult
+nsSystemTimeChangeObserver::RemoveWindowListenerImpl(nsPIDOMWindow* aWindow)
+{
+ if (!aWindow) {
+ return NS_OK;
+ }
+
+ if (aWindow->IsOuterWindow()) {
+ aWindow = aWindow->GetCurrentInnerWindow();
+ if (!aWindow) {
+ return NS_ERROR_FAILURE;
+ }
+ }
+
+ mWindowListeners.RemoveElement(NS_GetWeakReference(aWindow));
+
+ if (mWindowListeners.IsEmpty()) {
+ UnregisterSystemClockChangeObserver(sObserver);
+ UnregisterSystemTimezoneChangeObserver(sObserver);
+ }
+
+ return NS_OK;
+}
diff --git a/dom/time/TimeChangeObserver.h b/dom/time/TimeChangeObserver.h
new file mode 100644
index 000000000..8c96ae8b2
--- /dev/null
+++ b/dom/time/TimeChangeObserver.h
@@ -0,0 +1,44 @@
+/* -*- 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/. */
+
+#ifndef _mozilla_time_change_observer_h_
+#define _mozilla_time_change_observer_h_
+
+#include "mozilla/Hal.h"
+#include "mozilla/Observer.h"
+#include "mozilla/HalTypes.h"
+#include "nsPIDOMWindow.h"
+#include "nsWeakPtr.h"
+#include "nsTObserverArray.h"
+
+typedef mozilla::Observer<int64_t> SystemClockChangeObserver;
+typedef mozilla::Observer<mozilla::hal::SystemTimezoneChangeInformation> SystemTimezoneChangeObserver;
+
+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);
+
+ static nsresult AddWindowListener(nsPIDOMWindow* aWindow);
+ static nsresult RemoveWindowListener(nsPIDOMWindow* aWindow);
+private:
+ nsresult AddWindowListenerImpl(nsPIDOMWindow* aWindow);
+ nsresult RemoveWindowListenerImpl(nsPIDOMWindow* aWindow);
+ nsSystemTimeChangeObserver() { };
+ ListenerArray mWindowListeners;
+ void FireMozTimeChangeEvent();
+};
+
+#endif //_mozilla_time_change_observer_h_
diff --git a/dom/time/TimeManager.cpp b/dom/time/TimeManager.cpp
new file mode 100644
index 000000000..a7a3d24c3
--- /dev/null
+++ b/dom/time/TimeManager.cpp
@@ -0,0 +1,49 @@
+/* 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 "TimeManager.h"
+
+#include "mozilla/dom/MozTimeManagerBinding.h"
+#include "nsContentUtils.h"
+#include "nsITimeService.h"
+#include "nsServiceManagerUtils.h"
+
+namespace mozilla {
+namespace dom {
+namespace time {
+
+NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TimeManager)
+ NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
+ NS_INTERFACE_MAP_ENTRY(nsISupports)
+NS_INTERFACE_MAP_END
+
+NS_IMPL_CYCLE_COLLECTING_ADDREF(TimeManager)
+NS_IMPL_CYCLE_COLLECTING_RELEASE(TimeManager)
+
+NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(TimeManager, mWindow)
+
+JSObject*
+TimeManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
+{
+ return MozTimeManagerBinding::Wrap(aCx, aScope, this);
+}
+
+void
+TimeManager::Set(Date& aDate)
+{
+ Set(aDate.TimeStamp());
+}
+
+void
+TimeManager::Set(double aTime)
+{
+ nsCOMPtr<nsITimeService> timeService = do_GetService(TIMESERVICE_CONTRACTID);
+ if (timeService) {
+ timeService->Set(aTime);
+ }
+}
+
+} // namespace time
+} // namespace dom
+} // namespace mozilla
diff --git a/dom/time/TimeManager.h b/dom/time/TimeManager.h
new file mode 100644
index 000000000..95510e8f2
--- /dev/null
+++ b/dom/time/TimeManager.h
@@ -0,0 +1,59 @@
+/* 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/. */
+
+#ifndef mozilla_dom_time_TimeManager_h
+#define mozilla_dom_time_TimeManager_h
+
+#include "mozilla/Attributes.h"
+#include "nsISupports.h"
+#include "nsPIDOMWindow.h"
+#include "nsWrapperCache.h"
+
+namespace mozilla {
+namespace dom {
+
+class Date;
+
+namespace time {
+
+class TimeManager MOZ_FINAL : public nsISupports
+ , public nsWrapperCache
+{
+public:
+ static bool PrefEnabled()
+ {
+#ifdef MOZ_TIME_MANAGER
+ return true;
+#else
+ return false;
+#endif
+ }
+
+ NS_DECL_CYCLE_COLLECTING_ISUPPORTS
+ NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(TimeManager)
+
+ explicit TimeManager(nsPIDOMWindow* aWindow)
+ : mWindow(aWindow)
+ {
+ SetIsDOMBinding();
+ }
+
+ nsPIDOMWindow* GetParentObject() const
+ {
+ return mWindow;
+ }
+ JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope);
+
+ void Set(Date& aDate);
+ void Set(double aTime);
+
+private:
+ nsCOMPtr<nsPIDOMWindow> mWindow;
+};
+
+} // namespace time
+} // namespace dom
+} // namespace mozilla
+
+#endif //mozilla_dom_time_TimeManager_h
diff --git a/dom/time/TimeService.cpp b/dom/time/TimeService.cpp
new file mode 100644
index 000000000..a9cb973b7
--- /dev/null
+++ b/dom/time/TimeService.cpp
@@ -0,0 +1,38 @@
+/* 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 "base/basictypes.h"
+#include "jsapi.h"
+#include "mozilla/ClearOnShutdown.h"
+#include "mozilla/Hal.h"
+#include "TimeService.h"
+
+namespace mozilla {
+namespace dom {
+namespace time {
+
+NS_IMPL_ISUPPORTS1(TimeService, nsITimeService)
+
+/* static */ StaticRefPtr<TimeService> TimeService::sSingleton;
+
+/* static */ already_AddRefed<TimeService>
+TimeService::GetInstance()
+{
+ if (!sSingleton) {
+ sSingleton = new TimeService();
+ ClearOnShutdown(&sSingleton);
+ }
+ nsRefPtr<TimeService> service = sSingleton.get();
+ return service.forget();
+}
+
+NS_IMETHODIMP
+TimeService::Set(int64_t aTimeInMS) {
+ hal::AdjustSystemClock(aTimeInMS - (JS_Now() / PR_USEC_PER_MSEC));
+ return NS_OK;
+}
+
+} // namespace time
+} // namespace dom
+} // namespace mozilla
diff --git a/dom/time/TimeService.h b/dom/time/TimeService.h
new file mode 100644
index 000000000..56dd746b4
--- /dev/null
+++ b/dom/time/TimeService.h
@@ -0,0 +1,35 @@
+/* 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/. */
+
+#ifndef mozilla_dom_time_TimeService_h
+#define mozilla_dom_time_TimeService_h
+
+#include "mozilla/StaticPtr.h"
+#include "nsITimeService.h"
+
+namespace mozilla {
+namespace dom {
+namespace time {
+
+/**
+ * This class implements a service which lets us modify the system clock time.
+ */
+class TimeService : public nsITimeService
+{
+public:
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSITIMESERVICE
+
+ virtual ~TimeService() {};
+ static already_AddRefed<TimeService> GetInstance();
+
+private:
+ static StaticRefPtr<TimeService> sSingleton;
+};
+
+} // namespace time
+} // namespace dom
+} // namespace mozilla
+
+#endif //mozilla_dom_time_TimeService_h
diff --git a/dom/time/moz.build b/dom/time/moz.build
new file mode 100644
index 000000000..3e7d1cd1f
--- /dev/null
+++ b/dom/time/moz.build
@@ -0,0 +1,29 @@
+# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# 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/.
+
+XPIDL_SOURCES += [
+ 'nsIDOMNavigatorTime.idl',
+ 'nsITimeService.idl',
+]
+
+XPIDL_MODULE = 'dom_time'
+
+MODULE = 'dom'
+
+EXPORTS.mozilla.dom.time += [
+ 'DateCacheCleaner.h',
+ 'TimeChangeObserver.h',
+ 'TimeManager.h',
+ 'TimeService.h',
+]
+
+CPP_SOURCES += [
+ 'DateCacheCleaner.cpp',
+ 'TimeChangeObserver.cpp',
+ 'TimeManager.cpp',
+ 'TimeService.cpp',
+]
+
diff --git a/dom/time/nsIDOMNavigatorTime.idl b/dom/time/nsIDOMNavigatorTime.idl
new file mode 100644
index 000000000..955212c23
--- /dev/null
+++ b/dom/time/nsIDOMNavigatorTime.idl
@@ -0,0 +1,11 @@
+/* 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 "nsISupports.idl"
+
+[scriptable, uuid(88df63f0-814d-4424-b1f9-9184149639e5)]
+interface nsIDOMMozNavigatorTime : nsISupports
+{
+ readonly attribute nsISupports /* MozTimeManager */ mozTime;
+};
diff --git a/dom/time/nsITimeService.idl b/dom/time/nsITimeService.idl
new file mode 100644
index 000000000..2bba628e9
--- /dev/null
+++ b/dom/time/nsITimeService.idl
@@ -0,0 +1,20 @@
+/* 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 "nsISupports.idl"
+
+%{C++
+#define NS_TIMESERVICE_CID { 0x80d6f9cc, 0xf16d, 0x40c3, { 0xa5, 0x2e, 0xc4, 0xe6, 0x56, 0xe3, 0x65, 0xb4 } }
+#define TIMESERVICE_CONTRACTID "@mozilla.org/time/timeservice;1"
+%}
+
+[scriptable, builtinclass, uuid(1fc7fde2-0090-11e2-bdd6-0fea4b9f41f8)]
+interface nsITimeService : nsISupports
+{
+ /* Set the system time.
+ *
+ * The |aTimeInMS| argument is the time in milliseconds since the epoch.
+ */
+ void set(in int64_t aTimeInMS);
+};