Bug 749551 - Alarm API (AlarmHalService). r=cjones
authorGene Lian <clian@mozilla.com>
Tue, 03 Jul 2012 11:25:07 +0200
changeset 100894 23fd9a8d0c9afbd3dc247301304156c3c4949346
parent 100893 1e90805d96aab9f2c29431908b722cfb51107847
child 100895 ed768b821da17bed5d0f932fa3b81b43f29ed9fb
push idunknown
push userunknown
push dateunknown
reviewerscjones
bugs749551
milestone16.0a1
Bug 749551 - Alarm API (AlarmHalService). r=cjones
dom/alarm/AlarmHalService.cpp
dom/alarm/AlarmHalService.h
dom/alarm/Makefile.in
dom/alarm/nsIAlarmHalService.idl
layout/build/nsLayoutModule.cpp
new file mode 100644
--- /dev/null
+++ b/dom/alarm/AlarmHalService.cpp
@@ -0,0 +1,98 @@
+/* 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 "AlarmHalService.h"
+
+namespace mozilla {
+namespace dom {
+namespace alarm {
+
+using namespace hal;
+
+NS_IMPL_ISUPPORTS1(AlarmHalService, nsIAlarmHalService)
+
+void
+AlarmHalService::Init()
+{
+  mAlarmEnabled = RegisterTheOneAlarmObserver(this);
+}
+
+/* virtual */ AlarmHalService::~AlarmHalService() 
+{
+  if (mAlarmEnabled) {
+    UnregisterTheOneAlarmObserver();
+  }
+}
+
+/* static */ nsRefPtr<AlarmHalService> AlarmHalService::sSingleton;
+
+/* static */ already_AddRefed<nsIAlarmHalService>
+AlarmHalService::GetInstance()
+{
+  if (!sSingleton) {
+    sSingleton = new AlarmHalService();
+    sSingleton->Init(); 
+    ClearOnShutdown(&sSingleton);
+  }
+
+  nsCOMPtr<nsIAlarmHalService> service(do_QueryInterface(sSingleton));
+  return service.forget();
+}
+
+NS_IMETHODIMP
+AlarmHalService::SetAlarm(PRInt32 aSeconds, PRInt32 aNanoseconds, bool* aStatus)
+{
+  if (!mAlarmEnabled) {
+    return NS_ERROR_FAILURE;
+  }
+
+  bool status = hal::SetAlarm(aSeconds, aNanoseconds);
+
+  if (status) {
+    *aStatus = status;
+    return NS_OK;
+  } else {
+    return NS_ERROR_FAILURE;
+  }
+}
+
+NS_IMETHODIMP
+AlarmHalService::SetAlarmFiredCb(nsIAlarmFiredCb* aAlarmFiredCb)
+{
+  mAlarmFiredCb = aAlarmFiredCb;
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+AlarmHalService::SetTimezoneChangedCb(nsITimezoneChangedCb* aTimeZoneChangedCb)
+{
+  mTimezoneChangedCb = aTimeZoneChangedCb;
+  return NS_OK;
+}
+
+void
+AlarmHalService::Notify(const mozilla::void_t& aVoid)
+{
+  if (mAlarmFiredCb) {
+    mAlarmFiredCb->OnAlarmFired();
+  }
+}
+
+PRInt32
+AlarmHalService::GetTimezoneOffset(bool aIgnoreDST)
+{
+  PRExplodedTime prTime;
+  PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &prTime);
+
+  PRInt32 offset = prTime.tm_params.tp_gmt_offset;
+  if (!aIgnoreDST) {
+    offset += prTime.tm_params.tp_dst_offset;
+  }
+
+  return -(offset / 60);
+}
+
+} // alarm
+} // dom
+} // mozilla
new file mode 100644
--- /dev/null
+++ b/dom/alarm/AlarmHalService.h
@@ -0,0 +1,56 @@
+/* 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_alarm_AlarmHalService_h
+#define mozilla_dom_alarm_AlarmHalService_h
+
+#include "base/basictypes.h"
+#include "mozilla/ClearOnShutdown.h"
+#include "mozilla/Hal.h"
+#include "mozilla/Services.h"
+#include "nsIAlarmHalService.h"
+#include "nsIObserver.h"
+#include "nsIObserverService.h"
+#include "prtime.h"
+
+namespace mozilla {
+namespace dom {
+namespace alarm {
+
+class AlarmHalService : public nsIAlarmHalService, 
+                        mozilla::hal::AlarmObserver
+{
+public:
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSIALARMHALSERVICE
+
+  void Init();
+  virtual ~AlarmHalService();
+
+  static nsRefPtr<AlarmHalService> sSingleton;
+  static already_AddRefed<nsIAlarmHalService> GetInstance();
+
+  // Implementing hal::AlarmObserver
+  void Notify(const mozilla::void_t& aVoid);
+
+private:
+  bool mAlarmEnabled;
+  nsCOMPtr<nsIAlarmFiredCb> mAlarmFiredCb;
+
+  // TODO The mTimezoneChangedCb would be called 
+  // when a timezone-changed event is detected 
+  // at run-time. To do so, we can register a 
+  // timezone-changed observer, see bug 714358.
+  // We need to adjust the alarm time respect to
+  // the correct timezone where user is located.
+  nsCOMPtr<nsITimezoneChangedCb> mTimezoneChangedCb;
+
+  PRInt32 GetTimezoneOffset(bool aIgnoreDST);
+};
+
+} // namespace alarm
+} // namespace dom
+} // namespace mozilla
+
+#endif // mozilla_dom_alarm_AlarmHalService_h
--- a/dom/alarm/Makefile.in
+++ b/dom/alarm/Makefile.in
@@ -8,26 +8,40 @@ srcdir      = @srcdir@
 VPATH       = @srcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
 MODULE              = dom
 XPIDL_MODULE        = dom_alarm
 LIBRARY_NAME        = domalarm_s
 LIBXUL_LIBRARY      = 1
+FORCE_STATIC_LIB    = 1
+GRE_MODULE          = 1
 
 include $(topsrcdir)/dom/dom-config.mk
 
+EXPORTS_NAMESPACES = mozilla/dom/alarm
+
 EXTRA_COMPONENTS =       \
   AlarmsManager.js       \
   AlarmsManager.manifest \
   $(NULL)
 
 XPIDLSRCS =               \
   nsIDOMAlarmsManager.idl \
+  nsIAlarmHalService.idl  \
+  $(NULL)
+
+EXPORTS_mozilla/dom/alarm = \
+  AlarmHalService.h         \
+  $(NULL)
+
+CPPSRCS =             \
+  AlarmHalService.cpp \
   $(NULL)
 
 ifdef ENABLE_TESTS
 DIRS += test
 endif
 
 include $(topsrcdir)/config/config.mk
+include $(topsrcdir)/ipc/chromium/chromium-config.mk
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/dom/alarm/nsIAlarmHalService.idl
@@ -0,0 +1,33 @@
+/* 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, function, uuid(9f3ed2c0-aed9-11e1-8c3d-5310bd393466)]
+interface nsIAlarmFiredCb : nsISupports 
+{
+  void onAlarmFired();
+};
+
+[scriptable, function, uuid(0ca52e84-ba8f-11e1-87e8-63235527db9e)]
+interface nsITimezoneChangedCb : nsISupports 
+{
+  void onTimezoneChanged(in long aTimezoneOffset);
+};
+
+%{C++
+#define NS_ALARMHALSERVICE_CID { 0x7dafea4c, 0x7163, 0x4b70, { 0x95, 0x4e, 0x5a, 0xd4, 0x09, 0x94, 0x83, 0xd7 } }
+#define ALARMHALSERVICE_CONTRACTID "@mozilla.org/alarmHalService;1"
+%}
+
+[scriptable, builtinclass, uuid(057b1ee4-f696-486d-bd55-205e21e88fab)]
+interface nsIAlarmHalService : nsISupports
+{
+  bool setAlarm(in long aSeconds, in long aNanoseconds);
+  void setAlarmFiredCb(in nsIAlarmFiredCb aAlarmFiredCb);
+  void setTimezoneChangedCb(in nsITimezoneChangedCb aTimezoneChangedCb);
+};
+
+
+
--- a/layout/build/nsLayoutModule.cpp
+++ b/layout/build/nsLayoutModule.cpp
@@ -212,23 +212,28 @@ static void Shutdown();
 #include "nsGeolocation.h"
 #include "nsDeviceSensors.h"
 #include "nsCSPService.h"
 #include "nsISmsService.h"
 #include "nsISmsDatabaseService.h"
 #include "mozilla/dom/sms/SmsRequestManager.h"
 #include "mozilla/dom/sms/SmsServicesFactory.h"
 #include "nsIPowerManagerService.h"
+#include "nsIAlarmHalService.h"
 
 using namespace mozilla::dom::sms;
 
 #include "mozilla/dom/power/PowerManagerService.h"
 
 using mozilla::dom::power::PowerManagerService;
 
+#include "mozilla/dom/alarm/AlarmHalService.h"
+
+using mozilla::dom::alarm::AlarmHalService;
+
 // Transformiix
 /* 5d5d92cd-6bf8-11d9-bf4a-000a95dc234c */
 #define TRANSFORMIIX_NODESET_CID \
 { 0x5d5d92cd, 0x6bf8, 0x11d9, { 0xbf, 0x4a, 0x0, 0x0a, 0x95, 0xdc, 0x23, 0x4c } }
 
 #define TRANSFORMIIX_NODESET_CONTRACTID \
 "@mozilla.org/transformiix-nodeset;1"
 
@@ -268,16 +273,18 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsHapticF
 #endif
 #endif
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(ThirdPartyUtil, Init)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsISmsService, SmsServicesFactory::CreateSmsService)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsISmsDatabaseService, SmsServicesFactory::CreateSmsDatabaseService)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIPowerManagerService,
                                          PowerManagerService::GetInstance)
 NS_GENERIC_FACTORY_CONSTRUCTOR(SmsRequestManager)
+NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIAlarmHalService,
+                                         AlarmHalService::GetInstance)
 
 //-----------------------------------------------------------------------------
 
 // Per bug 209804, it is necessary to observe the "xpcom-shutdown" event and
 // perform shutdown of the layout modules at that time instead of waiting for
 // our module destructor to run.  If we do not do this, then we risk holding
 // references to objects in other component libraries that have already been
 // shutdown (and possibly unloaded if 60709 is ever fixed).
@@ -766,16 +773,17 @@ NS_DEFINE_NAMED_CID(NS_DEVICE_SENSORS_CI
 #if defined(ANDROID) || defined(MOZ_PLATFORM_MAEMO)
 NS_DEFINE_NAMED_CID(NS_HAPTICFEEDBACK_CID);
 #endif
 #endif
 NS_DEFINE_NAMED_CID(SMS_SERVICE_CID);
 NS_DEFINE_NAMED_CID(SMS_DATABASE_SERVICE_CID);
 NS_DEFINE_NAMED_CID(SMS_REQUEST_MANAGER_CID);
 NS_DEFINE_NAMED_CID(NS_POWERMANAGERSERVICE_CID);
+NS_DEFINE_NAMED_CID(NS_ALARMHALSERVICE_CID);
 
 static nsresult
 CreateWindowCommandTableConstructor(nsISupports *aOuter,
                                     REFNSIID aIID, void **aResult)
 {
   nsresult rv;
   nsCOMPtr<nsIControllerCommandTable> commandTable =
       do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv);
@@ -1033,16 +1041,17 @@ static const mozilla::Module::CIDEntry k
 #endif
   { &kTHIRDPARTYUTIL_CID, false, NULL, ThirdPartyUtilConstructor },
   { &kNS_STRUCTUREDCLONECONTAINER_CID, false, NULL, nsStructuredCloneContainerConstructor },
   { &kNS_DOMMUTATIONOBSERVER_CID, false, NULL, nsDOMMutationObserverConstructor },
   { &kSMS_SERVICE_CID, false, NULL, nsISmsServiceConstructor },
   { &kSMS_DATABASE_SERVICE_CID, false, NULL, nsISmsDatabaseServiceConstructor },
   { &kSMS_REQUEST_MANAGER_CID, false, NULL, SmsRequestManagerConstructor },
   { &kNS_POWERMANAGERSERVICE_CID, false, NULL, nsIPowerManagerServiceConstructor },
+  { &kNS_ALARMHALSERVICE_CID, false, NULL, nsIAlarmHalServiceConstructor },
   { NULL }
 };
 
 static const mozilla::Module::ContractIDEntry kLayoutContracts[] = {
   XPCONNECT_CONTRACTS
   { "@mozilla.org/layout/xul-boxobject;1", &kNS_BOXOBJECT_CID },
 #ifdef MOZ_XUL
   { "@mozilla.org/layout/xul-boxobject-listbox;1", &kNS_LISTBOXOBJECT_CID },
@@ -1165,16 +1174,17 @@ static const mozilla::Module::ContractID
 #endif
   { THIRDPARTYUTIL_CONTRACTID, &kTHIRDPARTYUTIL_CID },
   { NS_STRUCTUREDCLONECONTAINER_CONTRACTID, &kNS_STRUCTUREDCLONECONTAINER_CID },
   { NS_DOMMUTATIONOBSERVER_CONTRACTID, &kNS_DOMMUTATIONOBSERVER_CID },
   { SMS_SERVICE_CONTRACTID, &kSMS_SERVICE_CID },
   { SMS_DATABASE_SERVICE_CONTRACTID, &kSMS_DATABASE_SERVICE_CID },
   { SMS_REQUEST_MANAGER_CONTRACTID, &kSMS_REQUEST_MANAGER_CID },
   { POWERMANAGERSERVICE_CONTRACTID, &kNS_POWERMANAGERSERVICE_CID },
+  { ALARMHALSERVICE_CONTRACTID, &kNS_ALARMHALSERVICE_CID },
   { NULL }
 };
 
 static const mozilla::Module::CategoryEntry kLayoutCategories[] = {
   XPCONNECT_CATEGORIES
   { JAVASCRIPT_GLOBAL_CONSTRUCTOR_CATEGORY, "Image", NS_HTMLIMGELEMENT_CONTRACTID },
   { JAVASCRIPT_GLOBAL_CONSTRUCTOR_PROTO_ALIAS_CATEGORY, "Image", "HTMLImageElement" },
   { JAVASCRIPT_GLOBAL_CONSTRUCTOR_CATEGORY, "Option", NS_HTMLOPTIONELEMENT_CONTRACTID },