Bug 789973 - B2G system time: adjust system clock after receiving NITZ timestamp (part 1). r=jlebar

This commit is contained in:
Gene Lian 2012-09-28 14:02:28 +08:00
parent 269bc6d50f
commit a80f860d03
8 changed files with 117 additions and 20 deletions

View File

@ -54,7 +54,7 @@
#include "nsJSEnvironment.h"
#include "xpcpublic.h"
#include "nsSandboxFlags.h"
#include "TimeChangeObserver.h"
#include "mozilla/dom/time/TimeChangeObserver.h"
using namespace mozilla::dom;
using namespace mozilla::hal;

View File

@ -9,6 +9,7 @@ VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = dom
LIBRARY_NAME = dom_time_s
XPIDL_MODULE = dom_time
LIBXUL_LIBRARY = 1
@ -17,18 +18,23 @@ FAIL_ON_WARNINGS := 1
include $(topsrcdir)/dom/dom-config.mk
EXPORTS_NAMESPACES = mozilla/dom/time
CPPSRCS = \
TimeManager.cpp \
TimeService.cpp \
TimeChangeObserver.cpp \
$(NULL)
EXPORTS = \
EXPORTS_mozilla/dom/time = \
TimeService.h \
TimeChangeObserver.h \
$(NULL)
XPIDLSRCS = \
nsIDOMNavigatorTime.idl \
nsIDOMTimeManager.idl \
nsITimeService.idl \
$(NULL)
include $(topsrcdir)/config/config.mk

View File

@ -1,15 +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 "jsapi.h"
#include "mozilla/Hal.h"
#include "nsDOMEvent.h"
#include "nsDOMEventTargetHelper.h"
#include "nsIDOMClassInfo.h"
#include "prtime.h"
#include "TimeManager.h"
using namespace mozilla::hal;
#include "jsapi.h"
#include "nsIDOMClassInfo.h"
#include "nsITimeService.h"
#include "TimeManager.h"
DOMCI_DATA(MozTimeManager, mozilla::dom::time::TimeManager)
@ -28,7 +24,6 @@ NS_IMPL_RELEASE(TimeManager)
nsresult
TimeManager::Set(const JS::Value& date, JSContext* ctx) {
double nowMSec = JS_Now() / 1000;
double dateMSec;
if (date.isObject()) {
@ -48,7 +43,10 @@ TimeManager::Set(const JS::Value& date, JSContext* ctx) {
return NS_ERROR_INVALID_ARG;
}
hal::AdjustSystemClock(dateMSec - nowMSec);
nsCOMPtr<nsITimeService> timeService = do_GetService(TIMESERVICE_CONTRACTID);
if (timeService) {
return timeService->Set(dateMSec);
}
return NS_OK;
}

View File

@ -5,19 +5,13 @@
#ifndef mozilla_dom_time_TimeManager_h
#define mozilla_dom_time_TimeManager_h
#include "mozilla/HalTypes.h"
#include "nsIDOMTimeManager.h"
#include "mozilla/Observer.h"
#include "mozilla/Attributes.h"
class nsPIDOMWindow;
namespace mozilla {
typedef Observer<hal::SystemTimeChange> SystemTimeObserver;
namespace dom {
namespace time {
class TimeManager MOZ_FINAL : public nsIDOMMozTimeManager
{
public:

38
dom/time/TimeService.cpp Normal file
View File

@ -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

35
dom/time/TimeService.h Normal file
View File

@ -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

View File

@ -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);
};

View File

@ -241,6 +241,7 @@ static void Shutdown();
#include "mozilla/dom/power/PowerManagerService.h"
#include "mozilla/dom/alarm/AlarmHalService.h"
#include "mozilla/dom/time/TimeService.h"
using namespace mozilla;
using namespace mozilla::dom;
@ -250,7 +251,7 @@ using mozilla::dom::alarm::AlarmHalService;
using mozilla::dom::indexedDB::IndexedDatabaseManager;
using mozilla::dom::power::PowerManagerService;
using mozilla::dom::TCPSocketChild;
using mozilla::dom::time::TimeService;
// Transformiix
/* 5d5d92cd-6bf8-11d9-bf4a-000a95dc234c */
@ -315,6 +316,8 @@ NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIPowerManagerService,
NS_GENERIC_FACTORY_CONSTRUCTOR(SmsRequestManager)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIAlarmHalService,
AlarmHalService::GetInstance)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsITimeService,
TimeService::GetInstance)
//-----------------------------------------------------------------------------
@ -828,6 +831,7 @@ NS_DEFINE_NAMED_CID(NS_POWERMANAGERSERVICE_CID);
NS_DEFINE_NAMED_CID(OSFILECONSTANTSSERVICE_CID);
NS_DEFINE_NAMED_CID(NS_ALARMHALSERVICE_CID);
NS_DEFINE_NAMED_CID(TCPSOCKETCHILD_CID);
NS_DEFINE_NAMED_CID(NS_TIMESERVICE_CID);
static nsresult
CreateWindowCommandTableConstructor(nsISupports *aOuter,
@ -1107,6 +1111,7 @@ static const mozilla::Module::CIDEntry kLayoutCIDs[] = {
{ &kOSFILECONSTANTSSERVICE_CID, true, NULL, OSFileConstantsServiceConstructor },
{ &kNS_ALARMHALSERVICE_CID, false, NULL, nsIAlarmHalServiceConstructor },
{ &kTCPSOCKETCHILD_CID, false, NULL, TCPSocketChildConstructor },
{ &kNS_TIMESERVICE_CID, false, NULL, nsITimeServiceConstructor },
{ NULL }
};
@ -1251,6 +1256,7 @@ static const mozilla::Module::ContractIDEntry kLayoutContracts[] = {
{ OSFILECONSTANTSSERVICE_CONTRACTID, &kOSFILECONSTANTSSERVICE_CID },
{ ALARMHALSERVICE_CONTRACTID, &kNS_ALARMHALSERVICE_CID },
{ "@mozilla.org/tcp-socket-child;1", &kTCPSOCKETCHILD_CID },
{ TIMESERVICE_CONTRACTID, &kNS_TIMESERVICE_CID },
{ NULL }
};