dom/time/TimeChangeObserver.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "TimeChangeObserver.h"
michael@0 7 #include "mozilla/Hal.h"
michael@0 8 #include "mozilla/Observer.h"
michael@0 9 #include "mozilla/HalTypes.h"
michael@0 10 #include "nsWeakPtr.h"
michael@0 11 #include "nsTObserverArray.h"
michael@0 12 #include "mozilla/ClearOnShutdown.h"
michael@0 13 #include "mozilla/Services.h"
michael@0 14 #include "mozilla/StaticPtr.h"
michael@0 15 #include "nsPIDOMWindow.h"
michael@0 16 #include "nsContentUtils.h"
michael@0 17 #include "nsIObserverService.h"
michael@0 18 #include "nsIDocument.h"
michael@0 19
michael@0 20 using namespace mozilla;
michael@0 21 using namespace mozilla::hal;
michael@0 22 using namespace mozilla::services;
michael@0 23
michael@0 24 class nsSystemTimeChangeObserver : public SystemClockChangeObserver,
michael@0 25 public SystemTimezoneChangeObserver
michael@0 26 {
michael@0 27 typedef nsTObserverArray<nsWeakPtr> ListenerArray;
michael@0 28 public:
michael@0 29 static nsSystemTimeChangeObserver* GetInstance();
michael@0 30 virtual ~nsSystemTimeChangeObserver();
michael@0 31
michael@0 32 // Implementing hal::SystemClockChangeObserver::Notify()
michael@0 33 void Notify(const int64_t& aClockDeltaMS);
michael@0 34
michael@0 35 // Implementing hal::SystemTimezoneChangeObserver::Notify()
michael@0 36 void Notify(
michael@0 37 const mozilla::hal::SystemTimezoneChangeInformation& aSystemTimezoneChangeInfo);
michael@0 38
michael@0 39 nsresult AddWindowListenerImpl(nsPIDOMWindow* aWindow);
michael@0 40 nsresult RemoveWindowListenerImpl(nsPIDOMWindow* aWindow);
michael@0 41
michael@0 42 private:
michael@0 43 nsSystemTimeChangeObserver() { };
michael@0 44 ListenerArray mWindowListeners;
michael@0 45 void FireMozTimeChangeEvent();
michael@0 46 };
michael@0 47
michael@0 48 StaticAutoPtr<nsSystemTimeChangeObserver> sObserver;
michael@0 49
michael@0 50 nsSystemTimeChangeObserver* nsSystemTimeChangeObserver::GetInstance()
michael@0 51 {
michael@0 52 if (!sObserver) {
michael@0 53 sObserver = new nsSystemTimeChangeObserver();
michael@0 54 ClearOnShutdown(&sObserver);
michael@0 55 }
michael@0 56 return sObserver;
michael@0 57 }
michael@0 58
michael@0 59 nsSystemTimeChangeObserver::~nsSystemTimeChangeObserver()
michael@0 60 {
michael@0 61 UnregisterSystemClockChangeObserver(this);
michael@0 62 UnregisterSystemTimezoneChangeObserver(this);
michael@0 63 }
michael@0 64
michael@0 65 void
michael@0 66 nsSystemTimeChangeObserver::FireMozTimeChangeEvent()
michael@0 67 {
michael@0 68 ListenerArray::ForwardIterator iter(mWindowListeners);
michael@0 69 while (iter.HasMore()) {
michael@0 70 nsWeakPtr weakWindow = iter.GetNext();
michael@0 71 nsCOMPtr<nsPIDOMWindow> innerWindow = do_QueryReferent(weakWindow);
michael@0 72 nsCOMPtr<nsPIDOMWindow> outerWindow;
michael@0 73 nsCOMPtr<nsIDocument> document;
michael@0 74 if (!innerWindow ||
michael@0 75 !(document = innerWindow->GetExtantDoc()) ||
michael@0 76 !(outerWindow = innerWindow->GetOuterWindow())) {
michael@0 77 mWindowListeners.RemoveElement(weakWindow);
michael@0 78 continue;
michael@0 79 }
michael@0 80
michael@0 81 nsContentUtils::DispatchTrustedEvent(document, outerWindow,
michael@0 82 NS_LITERAL_STRING("moztimechange"), /* bubbles = */ true,
michael@0 83 /* canceable = */ false);
michael@0 84 }
michael@0 85 }
michael@0 86
michael@0 87 void
michael@0 88 nsSystemTimeChangeObserver::Notify(const int64_t& aClockDeltaMS)
michael@0 89 {
michael@0 90 // Notify observers that the system clock has been adjusted.
michael@0 91 nsCOMPtr<nsIObserverService> observerService = GetObserverService();
michael@0 92 if (observerService) {
michael@0 93 nsString dataStr;
michael@0 94 dataStr.AppendFloat(static_cast<double>(aClockDeltaMS));
michael@0 95 observerService->NotifyObservers(
michael@0 96 nullptr, "system-clock-change", dataStr.get());
michael@0 97 }
michael@0 98
michael@0 99 FireMozTimeChangeEvent();
michael@0 100 }
michael@0 101
michael@0 102 void
michael@0 103 nsSystemTimeChangeObserver::Notify(
michael@0 104 const SystemTimezoneChangeInformation& aSystemTimezoneChangeInfo)
michael@0 105 {
michael@0 106 FireMozTimeChangeEvent();
michael@0 107 }
michael@0 108
michael@0 109 nsresult
michael@0 110 mozilla::time::AddWindowListener(nsPIDOMWindow* aWindow)
michael@0 111 {
michael@0 112 return nsSystemTimeChangeObserver::GetInstance()->AddWindowListenerImpl(aWindow);
michael@0 113 }
michael@0 114
michael@0 115 nsresult
michael@0 116 nsSystemTimeChangeObserver::AddWindowListenerImpl(nsPIDOMWindow* aWindow)
michael@0 117 {
michael@0 118 if (!aWindow) {
michael@0 119 return NS_ERROR_ILLEGAL_VALUE;
michael@0 120 }
michael@0 121
michael@0 122 if (aWindow->IsOuterWindow()) {
michael@0 123 aWindow = aWindow->GetCurrentInnerWindow();
michael@0 124 if (!aWindow) {
michael@0 125 return NS_ERROR_FAILURE;
michael@0 126 }
michael@0 127 }
michael@0 128
michael@0 129 nsWeakPtr windowWeakRef = do_GetWeakReference(aWindow);
michael@0 130 NS_ASSERTION(windowWeakRef, "nsIDOMWindow implementations shuld support weak ref");
michael@0 131
michael@0 132 if (mWindowListeners.IndexOf(windowWeakRef) !=
michael@0 133 ListenerArray::array_type::NoIndex) {
michael@0 134 return NS_OK;
michael@0 135 }
michael@0 136
michael@0 137 if (mWindowListeners.IsEmpty()) {
michael@0 138 RegisterSystemClockChangeObserver(sObserver);
michael@0 139 RegisterSystemTimezoneChangeObserver(sObserver);
michael@0 140 }
michael@0 141
michael@0 142 mWindowListeners.AppendElement(windowWeakRef);
michael@0 143 return NS_OK;
michael@0 144 }
michael@0 145
michael@0 146 nsresult
michael@0 147 mozilla::time::RemoveWindowListener(nsPIDOMWindow* aWindow)
michael@0 148 {
michael@0 149 if (!sObserver) {
michael@0 150 return NS_OK;
michael@0 151 }
michael@0 152
michael@0 153 return nsSystemTimeChangeObserver::GetInstance()->RemoveWindowListenerImpl(aWindow);
michael@0 154 }
michael@0 155
michael@0 156 nsresult
michael@0 157 nsSystemTimeChangeObserver::RemoveWindowListenerImpl(nsPIDOMWindow* aWindow)
michael@0 158 {
michael@0 159 if (!aWindow) {
michael@0 160 return NS_OK;
michael@0 161 }
michael@0 162
michael@0 163 if (aWindow->IsOuterWindow()) {
michael@0 164 aWindow = aWindow->GetCurrentInnerWindow();
michael@0 165 if (!aWindow) {
michael@0 166 return NS_ERROR_FAILURE;
michael@0 167 }
michael@0 168 }
michael@0 169
michael@0 170 nsWeakPtr windowWeakRef = do_GetWeakReference(aWindow);
michael@0 171 mWindowListeners.RemoveElement(windowWeakRef);
michael@0 172
michael@0 173 if (mWindowListeners.IsEmpty()) {
michael@0 174 UnregisterSystemClockChangeObserver(sObserver);
michael@0 175 UnregisterSystemTimezoneChangeObserver(sObserver);
michael@0 176 }
michael@0 177
michael@0 178 return NS_OK;
michael@0 179 }

mercurial