1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Kan-Ru Chen <kchen@mozilla.com> (Original Author)
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "mozilla/Hal.h"
39 : #include "mozilla/HalWakeLock.h"
40 : #include "mozilla/ClearOnShutdown.h"
41 : #include "nsIDOMWakeLockListener.h"
42 : #include "nsIDOMWindow.h"
43 : #include "PowerManagerService.h"
44 : #include "WakeLock.h"
45 :
46 : namespace mozilla {
47 : namespace dom {
48 : namespace power {
49 :
50 0 : NS_IMPL_ISUPPORTS1(PowerManagerService, nsIPowerManagerService)
51 :
52 1464 : /* static */ nsRefPtr<PowerManagerService> PowerManagerService::sSingleton;
53 :
54 : /* static */ already_AddRefed<nsIPowerManagerService>
55 0 : PowerManagerService::GetInstance()
56 : {
57 0 : if (!sSingleton) {
58 0 : sSingleton = new PowerManagerService();
59 0 : sSingleton->Init();
60 0 : ClearOnShutdown(&sSingleton);
61 : }
62 :
63 0 : nsCOMPtr<nsIPowerManagerService> service(do_QueryInterface(sSingleton));
64 0 : return service.forget();
65 : }
66 :
67 : void
68 0 : PowerManagerService::Init()
69 : {
70 0 : hal::RegisterWakeLockObserver(this);
71 0 : }
72 :
73 0 : PowerManagerService::~PowerManagerService()
74 : {
75 0 : hal::UnregisterWakeLockObserver(this);
76 0 : }
77 :
78 : void
79 0 : PowerManagerService::ComputeWakeLockState(const hal::WakeLockInformation& aWakeLockInfo,
80 : nsAString &aState)
81 : {
82 0 : hal::WakeLockState state = hal::ComputeWakeLockState(aWakeLockInfo.numLocks(),
83 0 : aWakeLockInfo.numHidden());
84 0 : switch (state) {
85 : case hal::WAKE_LOCK_STATE_UNLOCKED:
86 0 : aState.AssignLiteral("unlocked");
87 0 : break;
88 : case hal::WAKE_LOCK_STATE_HIDDEN:
89 0 : aState.AssignLiteral("locked-background");
90 0 : break;
91 : case hal::WAKE_LOCK_STATE_VISIBLE:
92 0 : aState.AssignLiteral("locked-foreground");
93 0 : break;
94 : }
95 0 : }
96 :
97 : void
98 0 : PowerManagerService::Notify(const hal::WakeLockInformation& aWakeLockInfo)
99 : {
100 0 : nsAutoString state;
101 0 : ComputeWakeLockState(aWakeLockInfo, state);
102 :
103 : /**
104 : * Copy the listeners list before we walk through the callbacks
105 : * because the callbacks may install new listeners. We expect no
106 : * more than one listener per window, so it shouldn't be too long.
107 : */
108 0 : nsAutoTArray<nsCOMPtr<nsIDOMMozWakeLockListener>, 2> listeners(mWakeLockListeners);
109 :
110 0 : for (PRUint32 i = 0; i < listeners.Length(); ++i) {
111 0 : listeners[i]->Callback(aWakeLockInfo.topic(), state);
112 : }
113 0 : }
114 :
115 : NS_IMETHODIMP
116 0 : PowerManagerService::Reboot()
117 : {
118 0 : hal::Reboot();
119 0 : return NS_OK;
120 : }
121 :
122 : NS_IMETHODIMP
123 0 : PowerManagerService::PowerOff()
124 : {
125 0 : hal::PowerOff();
126 0 : return NS_OK;
127 : }
128 :
129 : NS_IMETHODIMP
130 0 : PowerManagerService::AddWakeLockListener(nsIDOMMozWakeLockListener *aListener)
131 : {
132 0 : if (mWakeLockListeners.Contains(aListener))
133 0 : return NS_OK;
134 :
135 0 : mWakeLockListeners.AppendElement(aListener);
136 0 : return NS_OK;
137 : }
138 :
139 : NS_IMETHODIMP
140 0 : PowerManagerService::RemoveWakeLockListener(nsIDOMMozWakeLockListener *aListener)
141 : {
142 0 : mWakeLockListeners.RemoveElement(aListener);
143 0 : return NS_OK;
144 : }
145 :
146 : NS_IMETHODIMP
147 0 : PowerManagerService::GetWakeLockState(const nsAString &aTopic, nsAString &aState)
148 : {
149 0 : hal::WakeLockInformation info;
150 0 : hal::GetWakeLockInfo(aTopic, &info);
151 :
152 0 : ComputeWakeLockState(info, aState);
153 :
154 0 : return NS_OK;
155 : }
156 :
157 : NS_IMETHODIMP
158 0 : PowerManagerService::NewWakeLock(const nsAString &aTopic,
159 : nsIDOMWindow *aWindow,
160 : nsIDOMMozWakeLock **aWakeLock)
161 : {
162 0 : nsRefPtr<WakeLock> wakelock = new WakeLock();
163 0 : nsresult rv = wakelock->Init(aTopic, aWindow);
164 0 : NS_ENSURE_SUCCESS(rv, rv);
165 :
166 : nsCOMPtr<nsIDOMMozWakeLock> wl =
167 0 : do_QueryInterface(NS_ISUPPORTS_CAST(nsIDOMMozWakeLock*, wakelock));
168 0 : wl.forget(aWakeLock);
169 :
170 0 : return NS_OK;
171 : }
172 :
173 : } // power
174 : } // dom
175 4392 : } // mozilla
|