1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set sw=2 ts=8 et ft=cpp : */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "Hal.h"
8 : #include "mozilla/dom/ContentChild.h"
9 : #include "mozilla/hal_sandbox/PHalChild.h"
10 : #include "mozilla/hal_sandbox/PHalParent.h"
11 : #include "mozilla/dom/TabParent.h"
12 : #include "mozilla/dom/TabChild.h"
13 : #include "mozilla/dom/battery/Types.h"
14 : #include "mozilla/dom/network/Types.h"
15 : #include "mozilla/Observer.h"
16 : #include "mozilla/unused.h"
17 : #include "WindowIdentifier.h"
18 :
19 : using namespace mozilla;
20 : using namespace mozilla::dom;
21 : using namespace mozilla::hal;
22 :
23 : namespace mozilla {
24 : namespace hal_sandbox {
25 :
26 : static PHalChild* sHal;
27 : static PHalChild*
28 0 : Hal()
29 : {
30 0 : if (!sHal) {
31 0 : sHal = ContentChild::GetSingleton()->SendPHalConstructor();
32 : }
33 0 : return sHal;
34 : }
35 :
36 : void
37 0 : Vibrate(const nsTArray<uint32>& pattern, const WindowIdentifier &id)
38 : {
39 0 : HAL_LOG(("Vibrate: Sending to parent process."));
40 :
41 0 : AutoInfallibleTArray<uint32, 8> p(pattern);
42 :
43 0 : WindowIdentifier newID(id);
44 0 : newID.AppendProcessID();
45 0 : Hal()->SendVibrate(p, newID.AsArray(), GetTabChildFrom(newID.GetWindow()));
46 0 : }
47 :
48 : void
49 0 : CancelVibrate(const WindowIdentifier &id)
50 : {
51 0 : HAL_LOG(("CancelVibrate: Sending to parent process."));
52 :
53 0 : WindowIdentifier newID(id);
54 0 : newID.AppendProcessID();
55 0 : Hal()->SendCancelVibrate(newID.AsArray(), GetTabChildFrom(newID.GetWindow()));
56 0 : }
57 :
58 : void
59 0 : EnableBatteryNotifications()
60 : {
61 0 : Hal()->SendEnableBatteryNotifications();
62 0 : }
63 :
64 : void
65 0 : DisableBatteryNotifications()
66 : {
67 0 : Hal()->SendDisableBatteryNotifications();
68 0 : }
69 :
70 : void
71 0 : GetCurrentBatteryInformation(BatteryInformation* aBatteryInfo)
72 : {
73 0 : Hal()->SendGetCurrentBatteryInformation(aBatteryInfo);
74 0 : }
75 :
76 : void
77 0 : EnableNetworkNotifications()
78 : {
79 0 : Hal()->SendEnableNetworkNotifications();
80 0 : }
81 :
82 : void
83 0 : DisableNetworkNotifications()
84 : {
85 0 : Hal()->SendDisableNetworkNotifications();
86 0 : }
87 :
88 : void
89 0 : GetCurrentNetworkInformation(NetworkInformation* aNetworkInfo)
90 : {
91 0 : Hal()->SendGetCurrentNetworkInformation(aNetworkInfo);
92 0 : }
93 :
94 : bool
95 0 : GetScreenEnabled()
96 : {
97 0 : bool enabled = false;
98 0 : Hal()->SendGetScreenEnabled(&enabled);
99 0 : return enabled;
100 : }
101 :
102 : void
103 0 : SetScreenEnabled(bool enabled)
104 : {
105 0 : Hal()->SendSetScreenEnabled(enabled);
106 0 : }
107 :
108 : double
109 0 : GetScreenBrightness()
110 : {
111 0 : double brightness = 0;
112 0 : Hal()->SendGetScreenBrightness(&brightness);
113 0 : return brightness;
114 : }
115 :
116 : void
117 0 : SetScreenBrightness(double brightness)
118 : {
119 0 : Hal()->SendSetScreenBrightness(brightness);
120 0 : }
121 :
122 : bool
123 0 : SetLight(hal::LightType light, const hal::LightConfiguration& aConfig)
124 : {
125 : bool status;
126 0 : Hal()->SendSetLight(light, aConfig, &status);
127 0 : return status;
128 : }
129 :
130 : bool
131 0 : GetLight(hal::LightType light, hal::LightConfiguration* aConfig)
132 : {
133 : bool status;
134 0 : Hal()->SendGetLight(light, aConfig, &status);
135 0 : return status;
136 : }
137 :
138 : void
139 0 : AdjustSystemClock(int32_t aDeltaMilliseconds)
140 : {
141 0 : Hal()->SendAdjustSystemClock(aDeltaMilliseconds);
142 0 : }
143 :
144 : void
145 0 : SetTimezone(const nsCString& aTimezoneSpec)
146 : {
147 0 : Hal()->SendSetTimezone(nsCString(aTimezoneSpec));
148 0 : }
149 :
150 : void
151 0 : Reboot()
152 : {
153 0 : Hal()->SendReboot();
154 0 : }
155 :
156 : void
157 0 : PowerOff()
158 : {
159 0 : Hal()->SendPowerOff();
160 0 : }
161 :
162 : void
163 0 : EnableSensorNotifications(SensorType aSensor) {
164 0 : Hal()->SendEnableSensorNotifications(aSensor);
165 0 : }
166 :
167 : void
168 0 : DisableSensorNotifications(SensorType aSensor) {
169 0 : Hal()->SendDisableSensorNotifications(aSensor);
170 0 : }
171 :
172 : void
173 0 : EnableWakeLockNotifications()
174 : {
175 0 : Hal()->SendEnableWakeLockNotifications();
176 0 : }
177 :
178 : void
179 0 : DisableWakeLockNotifications()
180 : {
181 0 : Hal()->SendDisableWakeLockNotifications();
182 0 : }
183 :
184 : void
185 0 : ModifyWakeLock(const nsAString &aTopic, WakeLockControl aLockAdjust, WakeLockControl aHiddenAdjust)
186 : {
187 0 : Hal()->SendModifyWakeLock(nsString(aTopic), aLockAdjust, aHiddenAdjust);
188 0 : }
189 :
190 : void
191 0 : GetWakeLockInfo(const nsAString &aTopic, WakeLockInformation *aWakeLockInfo)
192 : {
193 0 : Hal()->SendGetWakeLockInfo(nsString(aTopic), aWakeLockInfo);
194 0 : }
195 :
196 : class HalParent : public PHalParent
197 : , public BatteryObserver
198 : , public NetworkObserver
199 : , public ISensorObserver
200 : , public WakeLockObserver
201 0 : {
202 : public:
203 : NS_OVERRIDE virtual bool
204 0 : RecvVibrate(const InfallibleTArray<unsigned int>& pattern,
205 : const InfallibleTArray<uint64> &id,
206 : PBrowserParent *browserParent)
207 : {
208 : // Check whether browserParent is active. We should have already
209 : // checked that the corresponding window is active, but this check
210 : // isn't redundant. A window may be inactive in an active
211 : // browser. And a window is not notified synchronously when it's
212 : // deactivated, so the window may think it's active when the tab
213 : // is actually inactive.
214 0 : TabParent *tabParent = static_cast<TabParent*>(browserParent);
215 0 : if (!tabParent->Active()) {
216 0 : HAL_LOG(("RecvVibrate: Tab is not active. Cancelling."));
217 0 : return true;
218 : }
219 :
220 : // Forward to hal::, not hal_impl::, because we might be a
221 : // subprocess of another sandboxed process. The hal:: entry point
222 : // will do the right thing.
223 : nsCOMPtr<nsIDOMWindow> window =
224 0 : do_QueryInterface(tabParent->GetBrowserDOMWindow());
225 0 : WindowIdentifier newID(id, window);
226 0 : hal::Vibrate(pattern, newID);
227 0 : return true;
228 : }
229 :
230 : NS_OVERRIDE virtual bool
231 0 : RecvCancelVibrate(const InfallibleTArray<uint64> &id,
232 : PBrowserParent *browserParent)
233 : {
234 0 : TabParent *tabParent = static_cast<TabParent*>(browserParent);
235 : nsCOMPtr<nsIDOMWindow> window =
236 0 : do_QueryInterface(tabParent->GetBrowserDOMWindow());
237 0 : WindowIdentifier newID(id, window);
238 0 : hal::CancelVibrate(newID);
239 0 : return true;
240 : }
241 :
242 : NS_OVERRIDE virtual bool
243 0 : RecvEnableBatteryNotifications() {
244 0 : hal::RegisterBatteryObserver(this);
245 0 : return true;
246 : }
247 :
248 : NS_OVERRIDE virtual bool
249 0 : RecvDisableBatteryNotifications() {
250 0 : hal::UnregisterBatteryObserver(this);
251 0 : return true;
252 : }
253 :
254 : NS_OVERRIDE virtual bool
255 0 : RecvGetCurrentBatteryInformation(BatteryInformation* aBatteryInfo) {
256 0 : hal::GetCurrentBatteryInformation(aBatteryInfo);
257 0 : return true;
258 : }
259 :
260 0 : void Notify(const BatteryInformation& aBatteryInfo) {
261 0 : unused << SendNotifyBatteryChange(aBatteryInfo);
262 0 : }
263 :
264 : NS_OVERRIDE virtual bool
265 0 : RecvEnableNetworkNotifications() {
266 0 : hal::RegisterNetworkObserver(this);
267 0 : return true;
268 : }
269 :
270 : NS_OVERRIDE virtual bool
271 0 : RecvDisableNetworkNotifications() {
272 0 : hal::UnregisterNetworkObserver(this);
273 0 : return true;
274 : }
275 :
276 : NS_OVERRIDE virtual bool
277 0 : RecvGetCurrentNetworkInformation(NetworkInformation* aNetworkInfo) {
278 0 : hal::GetCurrentNetworkInformation(aNetworkInfo);
279 0 : return true;
280 : }
281 :
282 0 : void Notify(const NetworkInformation& aNetworkInfo) {
283 0 : unused << SendNotifyNetworkChange(aNetworkInfo);
284 0 : }
285 :
286 : NS_OVERRIDE virtual bool
287 0 : RecvGetScreenEnabled(bool *enabled)
288 : {
289 0 : *enabled = hal::GetScreenEnabled();
290 0 : return true;
291 : }
292 :
293 : NS_OVERRIDE virtual bool
294 0 : RecvSetScreenEnabled(const bool &enabled)
295 : {
296 0 : hal::SetScreenEnabled(enabled);
297 0 : return true;
298 : }
299 :
300 : NS_OVERRIDE virtual bool
301 0 : RecvGetScreenBrightness(double *brightness)
302 : {
303 0 : *brightness = hal::GetScreenBrightness();
304 0 : return true;
305 : }
306 :
307 : NS_OVERRIDE virtual bool
308 0 : RecvSetScreenBrightness(const double &brightness)
309 : {
310 0 : hal::SetScreenBrightness(brightness);
311 0 : return true;
312 : }
313 :
314 : NS_OVERRIDE virtual bool
315 0 : RecvSetLight(const LightType& aLight, const hal::LightConfiguration& aConfig, bool *status)
316 : {
317 0 : *status = hal::SetLight(aLight, aConfig);
318 0 : return true;
319 : }
320 :
321 : NS_OVERRIDE virtual bool
322 0 : RecvGetLight(const LightType& aLight, LightConfiguration* aConfig, bool* status)
323 : {
324 0 : *status = hal::GetLight(aLight, aConfig);
325 0 : return true;
326 : }
327 :
328 : NS_OVERRIDE virtual bool
329 0 : RecvAdjustSystemClock(const int32_t &aDeltaMilliseconds)
330 : {
331 0 : hal::AdjustSystemClock(aDeltaMilliseconds);
332 0 : return true;
333 : }
334 :
335 : NS_OVERRIDE virtual bool
336 0 : RecvSetTimezone(const nsCString& aTimezoneSpec)
337 : {
338 0 : hal::SetTimezone(aTimezoneSpec);
339 0 : return true;
340 : }
341 :
342 : NS_OVERRIDE virtual bool
343 0 : RecvReboot()
344 : {
345 0 : hal::Reboot();
346 0 : return true;
347 : }
348 :
349 : NS_OVERRIDE virtual bool
350 0 : RecvPowerOff()
351 : {
352 0 : hal::PowerOff();
353 0 : return true;
354 : }
355 :
356 : NS_OVERRIDE virtual bool
357 0 : RecvEnableSensorNotifications(const SensorType &aSensor) {
358 0 : hal::RegisterSensorObserver(aSensor, this);
359 0 : return true;
360 : }
361 :
362 : NS_OVERRIDE virtual bool
363 0 : RecvDisableSensorNotifications(const SensorType &aSensor) {
364 0 : hal::UnregisterSensorObserver(aSensor, this);
365 0 : return true;
366 : }
367 :
368 0 : void Notify(const SensorData& aSensorData) {
369 0 : unused << SendNotifySensorChange(aSensorData);
370 0 : }
371 :
372 : NS_OVERRIDE virtual bool
373 0 : RecvModifyWakeLock(const nsString &aTopic,
374 : const WakeLockControl &aLockAdjust,
375 : const WakeLockControl &aHiddenAdjust)
376 : {
377 0 : hal::ModifyWakeLock(aTopic, aLockAdjust, aHiddenAdjust);
378 0 : return true;
379 : }
380 :
381 : NS_OVERRIDE virtual bool
382 0 : RecvEnableWakeLockNotifications()
383 : {
384 0 : hal::RegisterWakeLockObserver(this);
385 0 : return true;
386 : }
387 :
388 : NS_OVERRIDE virtual bool
389 0 : RecvDisableWakeLockNotifications()
390 : {
391 0 : hal::UnregisterWakeLockObserver(this);
392 0 : return true;
393 : }
394 :
395 : NS_OVERRIDE virtual bool
396 0 : RecvGetWakeLockInfo(const nsString &aTopic, WakeLockInformation *aWakeLockInfo)
397 : {
398 0 : hal::GetWakeLockInfo(aTopic, aWakeLockInfo);
399 0 : return true;
400 : }
401 :
402 0 : void Notify(const WakeLockInformation& aWakeLockInfo)
403 : {
404 0 : unused << SendNotifyWakeLockChange(aWakeLockInfo);
405 0 : }
406 : };
407 :
408 0 : class HalChild : public PHalChild {
409 : public:
410 : NS_OVERRIDE virtual bool
411 0 : RecvNotifyBatteryChange(const BatteryInformation& aBatteryInfo) {
412 0 : hal::NotifyBatteryChange(aBatteryInfo);
413 0 : return true;
414 : }
415 :
416 : NS_OVERRIDE virtual bool
417 : RecvNotifySensorChange(const hal::SensorData &aSensorData);
418 :
419 : NS_OVERRIDE virtual bool
420 0 : RecvNotifyNetworkChange(const NetworkInformation& aNetworkInfo) {
421 0 : hal::NotifyNetworkChange(aNetworkInfo);
422 0 : return true;
423 : }
424 :
425 : NS_OVERRIDE virtual bool
426 0 : RecvNotifyWakeLockChange(const WakeLockInformation& aWakeLockInfo) {
427 0 : hal::NotifyWakeLockChange(aWakeLockInfo);
428 0 : return true;
429 : }
430 : };
431 :
432 : bool
433 0 : HalChild::RecvNotifySensorChange(const hal::SensorData &aSensorData) {
434 0 : hal::NotifySensorChange(aSensorData);
435 :
436 0 : return true;
437 : }
438 :
439 0 : PHalChild* CreateHalChild() {
440 0 : return new HalChild();
441 : }
442 :
443 0 : PHalParent* CreateHalParent() {
444 0 : return new HalParent();
445 : }
446 :
447 : } // namespace hal_sandbox
448 : } // namespace mozilla
|