dom/alarm/AlarmHalService.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/alarm/AlarmHalService.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,100 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "AlarmHalService.h"
     1.9 +
    1.10 +namespace mozilla {
    1.11 +namespace dom {
    1.12 +namespace alarm {
    1.13 +
    1.14 +using namespace hal;
    1.15 +
    1.16 +NS_IMPL_ISUPPORTS(AlarmHalService, nsIAlarmHalService)
    1.17 +
    1.18 +void
    1.19 +AlarmHalService::Init()
    1.20 +{
    1.21 +  mAlarmEnabled = RegisterTheOneAlarmObserver(this);
    1.22 +  if (!mAlarmEnabled) {
    1.23 +    return;
    1.24 +  }
    1.25 +  RegisterSystemTimezoneChangeObserver(this);
    1.26 +}
    1.27 +
    1.28 +/* virtual */ AlarmHalService::~AlarmHalService() 
    1.29 +{
    1.30 +  if (mAlarmEnabled) {
    1.31 +    UnregisterTheOneAlarmObserver();
    1.32 +    UnregisterSystemTimezoneChangeObserver(this);
    1.33 +  }
    1.34 +}
    1.35 +
    1.36 +/* static */ StaticRefPtr<AlarmHalService> AlarmHalService::sSingleton;
    1.37 +
    1.38 +/* static */ already_AddRefed<AlarmHalService>
    1.39 +AlarmHalService::GetInstance()
    1.40 +{
    1.41 +  if (!sSingleton) {
    1.42 +    sSingleton = new AlarmHalService();
    1.43 +    sSingleton->Init(); 
    1.44 +    ClearOnShutdown(&sSingleton);
    1.45 +  }
    1.46 +
    1.47 +  nsRefPtr<AlarmHalService> service = sSingleton.get();
    1.48 +  return service.forget();
    1.49 +}
    1.50 +
    1.51 +NS_IMETHODIMP
    1.52 +AlarmHalService::SetAlarm(int32_t aSeconds, int32_t aNanoseconds, bool* aStatus)
    1.53 +{
    1.54 +  if (!mAlarmEnabled) {
    1.55 +    return NS_ERROR_FAILURE;
    1.56 +  }
    1.57 +
    1.58 +  bool status = hal::SetAlarm(aSeconds, aNanoseconds);
    1.59 +  if (status) {
    1.60 +    *aStatus = status;
    1.61 +    return NS_OK;
    1.62 +  } else {
    1.63 +    return NS_ERROR_FAILURE;
    1.64 +  }
    1.65 +}
    1.66 +
    1.67 +NS_IMETHODIMP
    1.68 +AlarmHalService::SetAlarmFiredCb(nsIAlarmFiredCb* aAlarmFiredCb)
    1.69 +{
    1.70 +  mAlarmFiredCb = aAlarmFiredCb;
    1.71 +  return NS_OK;
    1.72 +}
    1.73 +
    1.74 +NS_IMETHODIMP
    1.75 +AlarmHalService::SetTimezoneChangedCb(nsITimezoneChangedCb* aTimeZoneChangedCb)
    1.76 +{
    1.77 +  mTimezoneChangedCb = aTimeZoneChangedCb;
    1.78 +  return NS_OK;
    1.79 +}
    1.80 +
    1.81 +void
    1.82 +AlarmHalService::Notify(const void_t& aVoid)
    1.83 +{
    1.84 +  if (!mAlarmFiredCb) {
    1.85 +    return;
    1.86 +  }
    1.87 +  mAlarmFiredCb->OnAlarmFired();
    1.88 +}
    1.89 +
    1.90 +void
    1.91 +AlarmHalService::Notify(
    1.92 +  const SystemTimezoneChangeInformation& aSystemTimezoneChangeInfo)
    1.93 +{
    1.94 +  if (!mTimezoneChangedCb) {
    1.95 +    return;
    1.96 +  }
    1.97 +  mTimezoneChangedCb->OnTimezoneChanged(
    1.98 +    aSystemTimezoneChangeInfo.newTimezoneOffsetMinutes());
    1.99 +}
   1.100 +
   1.101 +} // alarm
   1.102 +} // dom
   1.103 +} // mozilla

mercurial