Bumping manifests a=b2g-bump
[gecko.git] / widget / ScreenProxy.cpp
blobf1673af736fe80ac9792a2effa1dc97a3a6c763d
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"
14 namespace mozilla {
15 namespace widget {
17 using namespace mozilla::dom;
19 static NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
21 ScreenProxy::ScreenProxy(nsScreenManagerProxy* aScreenManager, ScreenDetails aDetails)
22 : mScreenManager(aScreenManager)
23 , mCacheValid(false)
24 , mCacheWillInvalidate(false)
26 PopulateByDetails(aDetails);
29 NS_IMETHODIMP
30 ScreenProxy::GetId(uint32_t *outId)
32 *outId = mId;
33 return NS_OK;
36 NS_IMETHODIMP
37 ScreenProxy::GetRect(int32_t *outLeft,
38 int32_t *outTop,
39 int32_t *outWidth,
40 int32_t *outHeight)
42 if (!EnsureCacheIsValid()) {
43 return NS_ERROR_FAILURE;
46 *outLeft = mRect.x;
47 *outTop = mRect.y;
48 *outWidth = mRect.width;
49 *outHeight = mRect.height;
50 return NS_OK;
53 NS_IMETHODIMP
54 ScreenProxy::GetRectDisplayPix(int32_t *outLeft,
55 int32_t *outTop,
56 int32_t *outWidth,
57 int32_t *outHeight)
59 if (!EnsureCacheIsValid()) {
60 return NS_ERROR_FAILURE;
63 *outLeft = mRectDisplayPix.x;
64 *outTop = mRectDisplayPix.y;
65 *outWidth = mRectDisplayPix.width;
66 *outHeight = mRectDisplayPix.height;
67 return NS_OK;
70 NS_IMETHODIMP
71 ScreenProxy::GetAvailRect(int32_t *outLeft,
72 int32_t *outTop,
73 int32_t *outWidth,
74 int32_t *outHeight)
76 if (!EnsureCacheIsValid()) {
77 return NS_ERROR_FAILURE;
80 *outLeft = mAvailRect.x;
81 *outTop = mAvailRect.y;
82 *outWidth = mAvailRect.width;
83 *outHeight = mAvailRect.height;
84 return NS_OK;
87 NS_IMETHODIMP
88 ScreenProxy::GetAvailRectDisplayPix(int32_t *outLeft,
89 int32_t *outTop,
90 int32_t *outWidth,
91 int32_t *outHeight)
93 if (!EnsureCacheIsValid()) {
94 return NS_ERROR_FAILURE;
97 *outLeft = mAvailRectDisplayPix.x;
98 *outTop = mAvailRectDisplayPix.y;
99 *outWidth = mAvailRectDisplayPix.width;
100 *outHeight = mAvailRectDisplayPix.height;
101 return NS_OK;
104 NS_IMETHODIMP
105 ScreenProxy::GetPixelDepth(int32_t *aPixelDepth)
107 if (!EnsureCacheIsValid()) {
108 return NS_ERROR_FAILURE;
111 *aPixelDepth = mPixelDepth;
112 return NS_OK;
115 NS_IMETHODIMP
116 ScreenProxy::GetColorDepth(int32_t *aColorDepth)
118 if (!EnsureCacheIsValid()) {
119 return NS_ERROR_FAILURE;
122 *aColorDepth = mColorDepth;
123 return NS_OK;
126 void
127 ScreenProxy::PopulateByDetails(ScreenDetails aDetails)
129 mId = aDetails.id();
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();
139 bool
140 ScreenProxy::EnsureCacheIsValid()
142 if (mCacheValid) {
143 return true;
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);
151 if (!success) {
152 NS_WARNING("Updating a ScreenProxy in the child process failed on parent side.");
153 return false;
156 PopulateByDetails(details);
157 mCacheValid = true;
159 InvalidateCacheOnNextTick();
160 return true;
163 void
164 ScreenProxy::InvalidateCacheOnNextTick()
166 if (mCacheWillInvalidate) {
167 return;
170 mCacheWillInvalidate = true;
172 nsCOMPtr<nsIAppShell> appShell = do_GetService(kAppShellCID);
173 if (appShell) {
174 appShell->RunInStableState(
175 NS_NewRunnableMethod(this, &ScreenProxy::InvalidateCache)
177 } else {
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.
180 InvalidateCache();
184 void
185 ScreenProxy::InvalidateCache()
187 mCacheValid = false;
188 mCacheWillInvalidate = false;
191 } // namespace widget
192 } // namespace mozilla