|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "AlarmHalService.h" |
|
6 |
|
7 namespace mozilla { |
|
8 namespace dom { |
|
9 namespace alarm { |
|
10 |
|
11 using namespace hal; |
|
12 |
|
13 NS_IMPL_ISUPPORTS(AlarmHalService, nsIAlarmHalService) |
|
14 |
|
15 void |
|
16 AlarmHalService::Init() |
|
17 { |
|
18 mAlarmEnabled = RegisterTheOneAlarmObserver(this); |
|
19 if (!mAlarmEnabled) { |
|
20 return; |
|
21 } |
|
22 RegisterSystemTimezoneChangeObserver(this); |
|
23 } |
|
24 |
|
25 /* virtual */ AlarmHalService::~AlarmHalService() |
|
26 { |
|
27 if (mAlarmEnabled) { |
|
28 UnregisterTheOneAlarmObserver(); |
|
29 UnregisterSystemTimezoneChangeObserver(this); |
|
30 } |
|
31 } |
|
32 |
|
33 /* static */ StaticRefPtr<AlarmHalService> AlarmHalService::sSingleton; |
|
34 |
|
35 /* static */ already_AddRefed<AlarmHalService> |
|
36 AlarmHalService::GetInstance() |
|
37 { |
|
38 if (!sSingleton) { |
|
39 sSingleton = new AlarmHalService(); |
|
40 sSingleton->Init(); |
|
41 ClearOnShutdown(&sSingleton); |
|
42 } |
|
43 |
|
44 nsRefPtr<AlarmHalService> service = sSingleton.get(); |
|
45 return service.forget(); |
|
46 } |
|
47 |
|
48 NS_IMETHODIMP |
|
49 AlarmHalService::SetAlarm(int32_t aSeconds, int32_t aNanoseconds, bool* aStatus) |
|
50 { |
|
51 if (!mAlarmEnabled) { |
|
52 return NS_ERROR_FAILURE; |
|
53 } |
|
54 |
|
55 bool status = hal::SetAlarm(aSeconds, aNanoseconds); |
|
56 if (status) { |
|
57 *aStatus = status; |
|
58 return NS_OK; |
|
59 } else { |
|
60 return NS_ERROR_FAILURE; |
|
61 } |
|
62 } |
|
63 |
|
64 NS_IMETHODIMP |
|
65 AlarmHalService::SetAlarmFiredCb(nsIAlarmFiredCb* aAlarmFiredCb) |
|
66 { |
|
67 mAlarmFiredCb = aAlarmFiredCb; |
|
68 return NS_OK; |
|
69 } |
|
70 |
|
71 NS_IMETHODIMP |
|
72 AlarmHalService::SetTimezoneChangedCb(nsITimezoneChangedCb* aTimeZoneChangedCb) |
|
73 { |
|
74 mTimezoneChangedCb = aTimeZoneChangedCb; |
|
75 return NS_OK; |
|
76 } |
|
77 |
|
78 void |
|
79 AlarmHalService::Notify(const void_t& aVoid) |
|
80 { |
|
81 if (!mAlarmFiredCb) { |
|
82 return; |
|
83 } |
|
84 mAlarmFiredCb->OnAlarmFired(); |
|
85 } |
|
86 |
|
87 void |
|
88 AlarmHalService::Notify( |
|
89 const SystemTimezoneChangeInformation& aSystemTimezoneChangeInfo) |
|
90 { |
|
91 if (!mTimezoneChangedCb) { |
|
92 return; |
|
93 } |
|
94 mTimezoneChangedCb->OnTimezoneChanged( |
|
95 aSystemTimezoneChangeInfo.newTimezoneOffsetMinutes()); |
|
96 } |
|
97 |
|
98 } // alarm |
|
99 } // dom |
|
100 } // mozilla |