Skip to content

Commit

Permalink
Bug 801096 - [settings] Set-Automatically for time should take effect…
Browse files Browse the repository at this point in the history
… *immediately* instead of waiting on next NITZ coming (part 1). r=jlebar
  • Loading branch information
airpingu committed Oct 23, 2012
1 parent b74e20d commit e6f31e3
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 124 deletions.
32 changes: 10 additions & 22 deletions dom/alarm/AlarmHalService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ AlarmHalService::Init()
if (!mAlarmEnabled) {
return;
}
RegisterSystemTimeChangeObserver(this);
RegisterSystemTimezoneChangeObserver(this);
}

/* virtual */ AlarmHalService::~AlarmHalService()
{
if (mAlarmEnabled) {
UnregisterTheOneAlarmObserver();
UnregisterSystemTimeChangeObserver(this);
UnregisterSystemTimezoneChangeObserver(this);
}
}

/* static */ StaticRefPtr<AlarmHalService> AlarmHalService::sSingleton;

/* static */ already_AddRefed<nsIAlarmHalService>
/* static */ already_AddRefed<AlarmHalService>
AlarmHalService::GetInstance()
{
if (!sSingleton) {
Expand All @@ -41,7 +41,7 @@ AlarmHalService::GetInstance()
ClearOnShutdown(&sSingleton);
}

nsCOMPtr<nsIAlarmHalService> service(do_QueryInterface(sSingleton));
nsRefPtr<AlarmHalService> service = sSingleton.get();
return service.forget();
}

Expand Down Expand Up @@ -76,7 +76,7 @@ AlarmHalService::SetTimezoneChangedCb(nsITimezoneChangedCb* aTimeZoneChangedCb)
}

void
AlarmHalService::Notify(const mozilla::void_t& aVoid)
AlarmHalService::Notify(const void_t& aVoid)
{
if (!mAlarmFiredCb) {
return;
Expand All @@ -85,26 +85,14 @@ AlarmHalService::Notify(const mozilla::void_t& aVoid)
}

void
AlarmHalService::Notify(const SystemTimeChange& aReason)
AlarmHalService::Notify(
const SystemTimezoneChangeInformation& aSystemTimezoneChangeInfo)
{
if (aReason != SYS_TIME_CHANGE_TZ || !mTimezoneChangedCb) {
if (!mTimezoneChangedCb) {
return;
}
mTimezoneChangedCb->OnTimezoneChanged(GetTimezoneOffset(false));
}

int32_t
AlarmHalService::GetTimezoneOffset(bool aIgnoreDST)
{
PRExplodedTime prTime;
PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &prTime);

int32_t offset = prTime.tm_params.tp_gmt_offset;
if (!aIgnoreDST) {
offset += prTime.tm_params.tp_dst_offset;
}

return -(offset / 60);
mTimezoneChangedCb->OnTimezoneChanged(
aSystemTimezoneChangeInfo.newTimezoneOffsetMinutes());
}

} // alarm
Expand Down
18 changes: 8 additions & 10 deletions dom/alarm/AlarmHalService.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
#include "nsIAlarmHalService.h"
#include "nsIObserver.h"
#include "nsIObserverService.h"
#include "prtime.h"

namespace mozilla {
namespace dom {
namespace alarm {

using namespace hal;

typedef Observer<void_t> AlarmObserver;
typedef Observer<hal::SystemTimezoneChangeInformation> SystemTimezoneChangeObserver;

class AlarmHalService : public nsIAlarmHalService,
public AlarmObserver,
public SystemTimeObserver
public SystemTimezoneChangeObserver
{
public:
NS_DECL_ISUPPORTS
Expand All @@ -32,22 +32,20 @@ class AlarmHalService : public nsIAlarmHalService,
void Init();
virtual ~AlarmHalService();

static already_AddRefed<nsIAlarmHalService> GetInstance();
static already_AddRefed<AlarmHalService> GetInstance();

// Implementing hal::AlarmObserver
void Notify(const mozilla::void_t& aVoid);
void Notify(const void_t& aVoid);

// Implementing hal::SystemTimeObserver
void Notify(const SystemTimeChange& aReason);
// Implementing hal::SystemTimezoneChangeObserver
void Notify(const hal::SystemTimezoneChangeInformation& aSystemTimezoneChangeInfo);

private:
bool mAlarmEnabled;
static StaticRefPtr<AlarmHalService> sSingleton;

nsCOMPtr<nsIAlarmFiredCb> mAlarmFiredCb;
nsCOMPtr<nsITimezoneChangedCb> mTimezoneChangedCb;

int32_t GetTimezoneOffset(bool aIgnoreDST);
};

} // namespace alarm
Expand Down
12 changes: 4 additions & 8 deletions dom/time/DateCacheCleaner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,20 @@ namespace mozilla {
namespace dom {
namespace time {

class DateCacheCleaner : public SystemTimeChangeObserver
class DateCacheCleaner : public SystemTimezoneChangeObserver
{
public:
DateCacheCleaner()
{
RegisterSystemTimeChangeObserver(this);
RegisterSystemTimezoneChangeObserver(this);
}

~DateCacheCleaner()
{
UnregisterSystemTimeChangeObserver(this);
UnregisterSystemTimezoneChangeObserver(this);
}
void Notify(const SystemTimeChange& aReason)
void Notify(const SystemTimezoneChangeInformation& aSystemTimezoneChangeInfo)
{
if (aReason == SYS_TIME_CHANGE_CLOCK) {
return;
}

nsCOMPtr<nsIThreadJSContextStack> stack =
do_GetService("@mozilla.org/js/xpc/ContextStack;1");
if (!stack) {
Expand Down
37 changes: 32 additions & 5 deletions dom/time/TimeChangeObserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
#include "nsPIDOMWindow.h"
#include "nsDOMEvent.h"
#include "nsContentUtils.h"
#include "nsIObserverService.h"

using namespace mozilla::hal;
using namespace mozilla;
using namespace mozilla::hal;
using namespace mozilla::services;

StaticAutoPtr<nsSystemTimeChangeObserver> sObserver;

Expand All @@ -27,11 +29,12 @@ nsSystemTimeChangeObserver* nsSystemTimeChangeObserver::GetInstance()
nsSystemTimeChangeObserver::~nsSystemTimeChangeObserver()
{
mWindowListeners.Clear();
UnregisterSystemTimeChangeObserver(this);
UnregisterSystemClockChangeObserver(this);
UnregisterSystemTimezoneChangeObserver(this);
}

void
nsSystemTimeChangeObserver::Notify(const SystemTimeChange& aReason)
nsSystemTimeChangeObserver::FireMozTimeChangeEvent()
{
//Copy mWindowListeners and iterate over windowListeners instead because
//mWindowListeners may be modified while we loop.
Expand All @@ -58,6 +61,28 @@ nsSystemTimeChangeObserver::Notify(const SystemTimeChange& aReason)
}
}

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(nsIDOMWindow* aWindow)
{
Expand All @@ -80,7 +105,8 @@ nsSystemTimeChangeObserver::AddWindowListenerImpl(nsIDOMWindow* aWindow)
}

if (mWindowListeners.Length() == 0) {
RegisterSystemTimeChangeObserver(sObserver);
RegisterSystemClockChangeObserver(sObserver);
RegisterSystemTimezoneChangeObserver(sObserver);
}

mWindowListeners.AppendElement(windowWeakRef);
Expand All @@ -103,7 +129,8 @@ nsSystemTimeChangeObserver::RemoveWindowListenerImpl(nsIDOMWindow* aWindow)
mWindowListeners.RemoveElement(NS_GetWeakReference(aWindow));

if (mWindowListeners.Length() == 0) {
UnregisterSystemTimeChangeObserver(sObserver);
UnregisterSystemClockChangeObserver(sObserver);
UnregisterSystemTimezoneChangeObserver(sObserver);
}

return NS_OK;
Expand Down
16 changes: 13 additions & 3 deletions dom/time/TimeChangeObserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,31 @@
#include "nsPIDOMWindow.h"
#include "nsWeakPtr.h"

typedef mozilla::Observer<mozilla::hal::SystemTimeChange> SystemTimeChangeObserver;
typedef mozilla::Observer<int64_t> SystemClockChangeObserver;
typedef mozilla::Observer<mozilla::hal::SystemTimezoneChangeInformation> SystemTimezoneChangeObserver;

class nsSystemTimeChangeObserver : public SystemTimeChangeObserver
class nsSystemTimeChangeObserver : public SystemClockChangeObserver,
public SystemTimezoneChangeObserver
{
public:
static nsSystemTimeChangeObserver* GetInstance();
virtual ~nsSystemTimeChangeObserver();
void Notify(const mozilla::hal::SystemTimeChange& aReason);

// Implementing hal::SystemClockChangeObserver::Notify()
void Notify(const int64_t& aClockDeltaMS);

// Implementing hal::SystemTimezoneChangeObserver::Notify()
void Notify(
const mozilla::hal::SystemTimezoneChangeInformation& aSystemTimezoneChangeInfo);

static nsresult AddWindowListener(nsIDOMWindow* aWindow);
static nsresult RemoveWindowListener(nsIDOMWindow* aWindow);
private:
nsresult AddWindowListenerImpl(nsIDOMWindow* aWindow);
nsresult RemoveWindowListenerImpl(nsIDOMWindow* aWindow);
nsSystemTimeChangeObserver() { };
nsTArray<nsWeakPtr> mWindowListeners;
void FireMozTimeChangeEvent();
};

#endif //_mozilla_time_change_observer_h_
Loading

0 comments on commit e6f31e3

Please sign in to comment.