1 /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 2; -*- */
2 /* vim: set sw=4 ts=8 et tw=80 : */
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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/unused.h"
8 #include "nsIAppShell.h"
9 #include "nsScreenManagerProxy.h"
10 #include "nsServiceManagerUtils.h"
11 #include "nsWidgetsCID.h"
12 #include "ScreenProxy.h"
17 using namespace mozilla::dom
;
19 static NS_DEFINE_CID(kAppShellCID
, NS_APPSHELL_CID
);
21 ScreenProxy::ScreenProxy(nsScreenManagerProxy
* aScreenManager
, ScreenDetails aDetails
)
22 : mScreenManager(aScreenManager
)
24 , mCacheWillInvalidate(false)
26 PopulateByDetails(aDetails
);
30 ScreenProxy::GetId(uint32_t *outId
)
37 ScreenProxy::GetRect(int32_t *outLeft
,
42 if (!EnsureCacheIsValid()) {
43 return NS_ERROR_FAILURE
;
48 *outWidth
= mRect
.width
;
49 *outHeight
= mRect
.height
;
54 ScreenProxy::GetRectDisplayPix(int32_t *outLeft
,
59 if (!EnsureCacheIsValid()) {
60 return NS_ERROR_FAILURE
;
63 *outLeft
= mRectDisplayPix
.x
;
64 *outTop
= mRectDisplayPix
.y
;
65 *outWidth
= mRectDisplayPix
.width
;
66 *outHeight
= mRectDisplayPix
.height
;
71 ScreenProxy::GetAvailRect(int32_t *outLeft
,
76 if (!EnsureCacheIsValid()) {
77 return NS_ERROR_FAILURE
;
80 *outLeft
= mAvailRect
.x
;
81 *outTop
= mAvailRect
.y
;
82 *outWidth
= mAvailRect
.width
;
83 *outHeight
= mAvailRect
.height
;
88 ScreenProxy::GetAvailRectDisplayPix(int32_t *outLeft
,
93 if (!EnsureCacheIsValid()) {
94 return NS_ERROR_FAILURE
;
97 *outLeft
= mAvailRectDisplayPix
.x
;
98 *outTop
= mAvailRectDisplayPix
.y
;
99 *outWidth
= mAvailRectDisplayPix
.width
;
100 *outHeight
= mAvailRectDisplayPix
.height
;
105 ScreenProxy::GetPixelDepth(int32_t *aPixelDepth
)
107 if (!EnsureCacheIsValid()) {
108 return NS_ERROR_FAILURE
;
111 *aPixelDepth
= mPixelDepth
;
116 ScreenProxy::GetColorDepth(int32_t *aColorDepth
)
118 if (!EnsureCacheIsValid()) {
119 return NS_ERROR_FAILURE
;
122 *aColorDepth
= mColorDepth
;
127 ScreenProxy::PopulateByDetails(ScreenDetails aDetails
)
130 mRect
= nsIntRect(aDetails
.rect());
131 mRectDisplayPix
= nsIntRect(aDetails
.rectDisplayPix());
132 mAvailRect
= nsIntRect(aDetails
.availRect());
133 mAvailRectDisplayPix
= nsIntRect(aDetails
.availRectDisplayPix());
134 mPixelDepth
= aDetails
.pixelDepth();
135 mColorDepth
= aDetails
.colorDepth();
136 mContentsScaleFactor
= aDetails
.contentsScaleFactor();
140 ScreenProxy::EnsureCacheIsValid()
146 bool success
= false;
147 // Kick off a synchronous IPC call to the parent to get the
148 // most up-to-date information.
149 ScreenDetails details
;
150 unused
<< mScreenManager
->SendScreenRefresh(mId
, &details
, &success
);
152 NS_WARNING("Updating a ScreenProxy in the child process failed on parent side.");
156 PopulateByDetails(details
);
159 InvalidateCacheOnNextTick();
164 ScreenProxy::InvalidateCacheOnNextTick()
166 if (mCacheWillInvalidate
) {
170 mCacheWillInvalidate
= true;
172 nsCOMPtr
<nsIAppShell
> appShell
= do_GetService(kAppShellCID
);
174 appShell
->RunInStableState(
175 NS_NewRunnableMethod(this, &ScreenProxy::InvalidateCache
)
178 // It's pretty bad news if we can't get the appshell. In that case,
179 // let's just invalidate the cache right away.
185 ScreenProxy::InvalidateCache()
188 mCacheWillInvalidate
= false;
191 } // namespace widget
192 } // namespace mozilla