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
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Travis Bogard <travis@netscape.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either of the GNU General Public License Version 2 or later (the "GPL"),
27 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "mozilla/Hal.h"
40 : #include "nsScreen.h"
41 : #include "nsIDocShell.h"
42 : #include "nsPresContext.h"
43 : #include "nsCOMPtr.h"
44 : #include "nsDOMClassInfoID.h"
45 : #include "nsIInterfaceRequestorUtils.h"
46 : #include "nsIDocShellTreeItem.h"
47 : #include "nsLayoutUtils.h"
48 : #include "nsContentUtils.h"
49 : #include "mozilla/Preferences.h"
50 :
51 : using namespace mozilla;
52 :
53 : /* static */ bool nsScreen::sInitialized = false;
54 : /* static */ bool nsScreen::sAllowScreenEnabledProperty = false;
55 : /* static */ bool nsScreen::sAllowScreenBrightnessProperty = false;
56 :
57 : /* static */ void
58 0 : nsScreen::Initialize()
59 : {
60 0 : MOZ_ASSERT(!sInitialized);
61 0 : sInitialized = true;
62 : Preferences::AddBoolVarCache(&sAllowScreenEnabledProperty,
63 0 : "dom.screenEnabledProperty.enabled");
64 : Preferences::AddBoolVarCache(&sAllowScreenBrightnessProperty,
65 0 : "dom.screenBrightnessProperty.enabled");
66 0 : }
67 :
68 : //
69 : // Screen class implementation
70 : //
71 0 : nsScreen::nsScreen(nsIDocShell* aDocShell)
72 0 : : mDocShell(aDocShell)
73 : {
74 0 : if (!sInitialized) {
75 0 : Initialize();
76 : }
77 0 : }
78 :
79 0 : nsScreen::~nsScreen()
80 : {
81 0 : }
82 :
83 :
84 : DOMCI_DATA(Screen, nsScreen)
85 :
86 : // QueryInterface implementation for nsScreen
87 0 : NS_INTERFACE_MAP_BEGIN(nsScreen)
88 0 : NS_INTERFACE_MAP_ENTRY(nsISupports)
89 0 : NS_INTERFACE_MAP_ENTRY(nsIDOMScreen)
90 0 : NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(Screen)
91 0 : NS_INTERFACE_MAP_END
92 :
93 :
94 0 : NS_IMPL_ADDREF(nsScreen)
95 0 : NS_IMPL_RELEASE(nsScreen)
96 :
97 :
98 : NS_IMETHODIMP
99 0 : nsScreen::SetDocShell(nsIDocShell* aDocShell)
100 : {
101 0 : mDocShell = aDocShell; // Weak Reference
102 0 : return NS_OK;
103 : }
104 :
105 : NS_IMETHODIMP
106 0 : nsScreen::GetTop(PRInt32* aTop)
107 : {
108 0 : nsRect rect;
109 0 : nsresult rv = GetRect(rect);
110 :
111 0 : *aTop = rect.y;
112 :
113 0 : return rv;
114 : }
115 :
116 :
117 : NS_IMETHODIMP
118 0 : nsScreen::GetLeft(PRInt32* aLeft)
119 : {
120 0 : nsRect rect;
121 0 : nsresult rv = GetRect(rect);
122 :
123 0 : *aLeft = rect.x;
124 :
125 0 : return rv;
126 : }
127 :
128 :
129 : NS_IMETHODIMP
130 0 : nsScreen::GetWidth(PRInt32* aWidth)
131 : {
132 0 : nsRect rect;
133 0 : nsresult rv = GetRect(rect);
134 :
135 0 : *aWidth = rect.width;
136 :
137 0 : return rv;
138 : }
139 :
140 : NS_IMETHODIMP
141 0 : nsScreen::GetHeight(PRInt32* aHeight)
142 : {
143 0 : nsRect rect;
144 0 : nsresult rv = GetRect(rect);
145 :
146 0 : *aHeight = rect.height;
147 :
148 0 : return rv;
149 : }
150 :
151 : NS_IMETHODIMP
152 0 : nsScreen::GetPixelDepth(PRInt32* aPixelDepth)
153 : {
154 0 : nsDeviceContext* context = GetDeviceContext();
155 :
156 0 : if (!context) {
157 0 : *aPixelDepth = -1;
158 :
159 0 : return NS_ERROR_FAILURE;
160 : }
161 :
162 : PRUint32 depth;
163 0 : context->GetDepth(depth);
164 :
165 0 : *aPixelDepth = depth;
166 :
167 0 : return NS_OK;
168 : }
169 :
170 : NS_IMETHODIMP
171 0 : nsScreen::GetColorDepth(PRInt32* aColorDepth)
172 : {
173 0 : return GetPixelDepth(aColorDepth);
174 : }
175 :
176 : NS_IMETHODIMP
177 0 : nsScreen::GetAvailWidth(PRInt32* aAvailWidth)
178 : {
179 0 : nsRect rect;
180 0 : nsresult rv = GetAvailRect(rect);
181 :
182 0 : *aAvailWidth = rect.width;
183 :
184 0 : return rv;
185 : }
186 :
187 : NS_IMETHODIMP
188 0 : nsScreen::GetAvailHeight(PRInt32* aAvailHeight)
189 : {
190 0 : nsRect rect;
191 0 : nsresult rv = GetAvailRect(rect);
192 :
193 0 : *aAvailHeight = rect.height;
194 :
195 0 : return rv;
196 : }
197 :
198 : NS_IMETHODIMP
199 0 : nsScreen::GetAvailLeft(PRInt32* aAvailLeft)
200 : {
201 0 : nsRect rect;
202 0 : nsresult rv = GetAvailRect(rect);
203 :
204 0 : *aAvailLeft = rect.x;
205 :
206 0 : return rv;
207 : }
208 :
209 : NS_IMETHODIMP
210 0 : nsScreen::GetAvailTop(PRInt32* aAvailTop)
211 : {
212 0 : nsRect rect;
213 0 : nsresult rv = GetAvailRect(rect);
214 :
215 0 : *aAvailTop = rect.y;
216 :
217 0 : return rv;
218 : }
219 :
220 : nsDeviceContext*
221 0 : nsScreen::GetDeviceContext()
222 : {
223 0 : return nsLayoutUtils::GetDeviceContextForScreenInfo(mDocShell);
224 : }
225 :
226 : nsresult
227 0 : nsScreen::GetRect(nsRect& aRect)
228 : {
229 0 : nsDeviceContext *context = GetDeviceContext();
230 :
231 0 : if (!context) {
232 0 : return NS_ERROR_FAILURE;
233 : }
234 :
235 0 : context->GetRect(aRect);
236 :
237 0 : aRect.x = nsPresContext::AppUnitsToIntCSSPixels(aRect.x);
238 0 : aRect.y = nsPresContext::AppUnitsToIntCSSPixels(aRect.y);
239 0 : aRect.height = nsPresContext::AppUnitsToIntCSSPixels(aRect.height);
240 0 : aRect.width = nsPresContext::AppUnitsToIntCSSPixels(aRect.width);
241 :
242 0 : return NS_OK;
243 : }
244 :
245 : nsresult
246 0 : nsScreen::GetAvailRect(nsRect& aRect)
247 : {
248 0 : nsDeviceContext *context = GetDeviceContext();
249 :
250 0 : if (!context) {
251 0 : return NS_ERROR_FAILURE;
252 : }
253 :
254 0 : context->GetClientRect(aRect);
255 :
256 0 : aRect.x = nsPresContext::AppUnitsToIntCSSPixels(aRect.x);
257 0 : aRect.y = nsPresContext::AppUnitsToIntCSSPixels(aRect.y);
258 0 : aRect.height = nsPresContext::AppUnitsToIntCSSPixels(aRect.height);
259 0 : aRect.width = nsPresContext::AppUnitsToIntCSSPixels(aRect.width);
260 :
261 0 : return NS_OK;
262 : }
263 :
264 : namespace {
265 :
266 0 : bool IsWhiteListed(nsIDocShell *aDocShell) {
267 0 : nsCOMPtr<nsIDocShellTreeItem> ds = do_QueryInterface(aDocShell);
268 0 : if (!ds) {
269 0 : return false;
270 : }
271 :
272 : PRInt32 itemType;
273 0 : ds->GetItemType(&itemType);
274 0 : if (itemType == nsIDocShellTreeItem::typeChrome) {
275 0 : return true;
276 : }
277 :
278 0 : nsCOMPtr<nsIDocument> doc = do_GetInterface(aDocShell);
279 0 : nsIPrincipal *principal = doc->NodePrincipal();
280 :
281 0 : nsCOMPtr<nsIURI> principalURI;
282 0 : principal->GetURI(getter_AddRefs(principalURI));
283 0 : if (nsContentUtils::URIIsChromeOrInPref(principalURI,
284 0 : "dom.mozScreenWhitelist")) {
285 0 : return true;
286 : }
287 :
288 0 : return false;
289 : }
290 :
291 : } // anonymous namespace
292 :
293 : nsresult
294 0 : nsScreen::GetMozEnabled(bool *aEnabled)
295 : {
296 0 : if (!sAllowScreenEnabledProperty || !IsWhiteListed(mDocShell)) {
297 0 : *aEnabled = true;
298 0 : return NS_OK;
299 : }
300 :
301 0 : *aEnabled = hal::GetScreenEnabled();
302 0 : return NS_OK;
303 : }
304 :
305 : nsresult
306 0 : nsScreen::SetMozEnabled(bool aEnabled)
307 : {
308 0 : if (!sAllowScreenEnabledProperty || !IsWhiteListed(mDocShell)) {
309 0 : return NS_OK;
310 : }
311 :
312 : // TODO bug 707589: When the screen's state changes, all visible windows
313 : // should fire a visibility change event.
314 0 : hal::SetScreenEnabled(aEnabled);
315 0 : return NS_OK;
316 : }
317 :
318 : nsresult
319 0 : nsScreen::GetMozBrightness(double *aBrightness)
320 : {
321 0 : if (!sAllowScreenEnabledProperty || !IsWhiteListed(mDocShell)) {
322 0 : *aBrightness = 1;
323 0 : return NS_OK;
324 : }
325 :
326 0 : *aBrightness = hal::GetScreenBrightness();
327 0 : return NS_OK;
328 : }
329 :
330 : nsresult
331 0 : nsScreen::SetMozBrightness(double aBrightness)
332 : {
333 0 : if (!sAllowScreenEnabledProperty || !IsWhiteListed(mDocShell)) {
334 0 : return NS_OK;
335 : }
336 :
337 0 : NS_ENSURE_TRUE(0 <= aBrightness && aBrightness <= 1, NS_ERROR_INVALID_ARG);
338 0 : hal::SetScreenBrightness(aBrightness);
339 0 : return NS_OK;
340 : }
|