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 "PowerManager.h"
39 : #include "WakeLock.h"
40 : #include "nsContentUtils.h"
41 : #include "nsDOMClassInfoID.h"
42 : #include "nsIDOMWakeLockListener.h"
43 : #include "nsIPowerManagerService.h"
44 : #include "nsIPrincipal.h"
45 : #include "nsPIDOMWindow.h"
46 : #include "nsServiceManagerUtils.h"
47 :
48 : DOMCI_DATA(MozPowerManager, mozilla::dom::power::PowerManager)
49 :
50 : namespace mozilla {
51 : namespace dom {
52 : namespace power {
53 :
54 0 : NS_INTERFACE_MAP_BEGIN(PowerManager)
55 0 : NS_INTERFACE_MAP_ENTRY(nsIDOMMozPowerManager)
56 0 : NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMMozPowerManager)
57 0 : NS_INTERFACE_MAP_ENTRY(nsIDOMMozWakeLockListener)
58 0 : NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozPowerManager)
59 0 : NS_INTERFACE_MAP_END
60 :
61 0 : NS_IMPL_ADDREF(PowerManager)
62 0 : NS_IMPL_RELEASE(PowerManager)
63 :
64 : nsresult
65 0 : PowerManager::Init(nsIDOMWindow *aWindow)
66 : {
67 0 : mWindow = do_GetWeakReference(aWindow);
68 :
69 : nsCOMPtr<nsIPowerManagerService> pmService =
70 0 : do_GetService(POWERMANAGERSERVICE_CONTRACTID);
71 0 : NS_ENSURE_STATE(pmService);
72 :
73 : // Add ourself to the global notification list.
74 0 : pmService->AddWakeLockListener(this);
75 0 : return NS_OK;
76 : }
77 :
78 : nsresult
79 0 : PowerManager::Shutdown()
80 : {
81 : nsCOMPtr<nsIPowerManagerService> pmService =
82 0 : do_GetService(POWERMANAGERSERVICE_CONTRACTID);
83 0 : NS_ENSURE_STATE(pmService);
84 :
85 : // Remove ourself from the global notification list.
86 0 : pmService->RemoveWakeLockListener(this);
87 0 : return NS_OK;
88 : }
89 :
90 : nsresult
91 0 : PowerManager::CheckPermission()
92 : {
93 0 : nsCOMPtr<nsPIDOMWindow> win = do_QueryReferent(mWindow);
94 0 : NS_ENSURE_STATE(win);
95 0 : nsCOMPtr<nsIDocument> doc = do_QueryInterface(win->GetExtantDocument());
96 0 : NS_ENSURE_STATE(doc);
97 :
98 0 : nsCOMPtr<nsIURI> uri;
99 0 : doc->NodePrincipal()->GetURI(getter_AddRefs(uri));
100 :
101 0 : if (!nsContentUtils::URIIsChromeOrInPref(uri, "dom.power.whitelist")) {
102 0 : return NS_ERROR_DOM_SECURITY_ERR;
103 : }
104 :
105 0 : return NS_OK;
106 : }
107 :
108 : NS_IMETHODIMP
109 0 : PowerManager::Reboot()
110 : {
111 0 : nsresult rv = CheckPermission();
112 0 : NS_ENSURE_SUCCESS(rv, rv);
113 :
114 : nsCOMPtr<nsIPowerManagerService> pmService =
115 0 : do_GetService(POWERMANAGERSERVICE_CONTRACTID);
116 0 : NS_ENSURE_STATE(pmService);
117 :
118 0 : pmService->Reboot();
119 :
120 0 : return NS_OK;
121 : }
122 :
123 : NS_IMETHODIMP
124 0 : PowerManager::PowerOff()
125 : {
126 0 : nsresult rv = CheckPermission();
127 0 : NS_ENSURE_SUCCESS(rv, rv);
128 :
129 : nsCOMPtr<nsIPowerManagerService> pmService =
130 0 : do_GetService(POWERMANAGERSERVICE_CONTRACTID);
131 0 : NS_ENSURE_STATE(pmService);
132 :
133 0 : pmService->PowerOff();
134 :
135 0 : return NS_OK;
136 : }
137 :
138 : NS_IMETHODIMP
139 0 : PowerManager::AddWakeLockListener(nsIDOMMozWakeLockListener *aListener)
140 : {
141 0 : nsresult rv = CheckPermission();
142 0 : NS_ENSURE_SUCCESS(rv, rv);
143 :
144 : // already added? bail out.
145 0 : if (mListeners.Contains(aListener))
146 0 : return NS_OK;
147 :
148 0 : mListeners.AppendElement(aListener);
149 0 : return NS_OK;
150 : }
151 :
152 : NS_IMETHODIMP
153 0 : PowerManager::RemoveWakeLockListener(nsIDOMMozWakeLockListener *aListener)
154 : {
155 0 : nsresult rv = CheckPermission();
156 0 : NS_ENSURE_SUCCESS(rv, rv);
157 :
158 0 : mListeners.RemoveElement(aListener);
159 0 : return NS_OK;
160 : }
161 :
162 : NS_IMETHODIMP
163 0 : PowerManager::GetWakeLockState(const nsAString &aTopic, nsAString &aState)
164 : {
165 0 : nsresult rv = CheckPermission();
166 0 : NS_ENSURE_SUCCESS(rv, rv);
167 :
168 : nsCOMPtr<nsIPowerManagerService> pmService =
169 0 : do_GetService(POWERMANAGERSERVICE_CONTRACTID);
170 0 : NS_ENSURE_STATE(pmService);
171 :
172 0 : return pmService->GetWakeLockState(aTopic, aState);
173 : }
174 :
175 : NS_IMETHODIMP
176 0 : PowerManager::Callback(const nsAString &aTopic, const nsAString &aState)
177 : {
178 : /**
179 : * We maintain a local listener list instead of using the global
180 : * list so that when the window is destroyed we don't have to
181 : * cleanup the mess.
182 : * Copy the listeners list before we walk through the callbacks
183 : * because the callbacks may install new listeners. We expect no
184 : * more than one listener per window, so it shouldn't be too long.
185 : */
186 0 : nsAutoTArray<nsCOMPtr<nsIDOMMozWakeLockListener>, 2> listeners(mListeners);
187 0 : for (PRUint32 i = 0; i < listeners.Length(); ++i) {
188 0 : listeners[i]->Callback(aTopic, aState);
189 : }
190 :
191 0 : return NS_OK;
192 : }
193 :
194 : } // power
195 : } // dom
196 : } // mozilla
|