sbBaseScreenSaverSuppressor.cpp
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2009 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
26 
27 #include <nsIAppStartupNotifier.h>
28 #include <nsICategoryManager.h>
29 #include <nsIGenericFactory.h>
30 #include <nsIObserverService.h>
31 
32 #include <nsServiceManagerUtils.h>
33 
34 #include <prlog.h>
35 
40 #ifdef PR_LOGGING
41 static PRLogModuleInfo* gBaseScreenSaverSuppressor = nsnull;
42 #define TRACE(args) PR_LOG(gBaseScreenSaverSuppressor , PR_LOG_DEBUG, args)
43 #define LOG(args) PR_LOG(gBaseScreenSaverSuppressor , PR_LOG_WARN, args)
44 #ifdef __GNUC__
45 #define __FUNCTION__ __PRETTY_FUNCTION__
46 #endif /* __GNUC__ */
47 #else
48 #define TRACE(args) /* nothing */
49 #define LOG(args) /* nothing */
50 #endif
51 
55 
57 : mSuppress(0)
58 , mUserSetting(PR_FALSE)
59 , mHasUserSetting(PR_FALSE)
60 {
61  #ifdef PR_LOGGING
62  if (!gBaseScreenSaverSuppressor)
63  gBaseScreenSaverSuppressor =
64  PR_NewLogModule("sbBaseScreenSaverSuppressor");
65  #endif
66 }
67 
69 {
70 }
71 
72 /*static*/ nsresult
73 sbBaseScreenSaverSuppressor::RegisterSelf(nsIComponentManager* aCompMgr,
74  nsIFile* aPath,
75  const char* aLoaderStr,
76  const char* aType,
77  const nsModuleComponentInfo *aInfo)
78 {
79  nsresult rv = NS_ERROR_UNEXPECTED;
80  nsCOMPtr<nsICategoryManager> categoryManager =
81  do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
82  NS_ENSURE_SUCCESS(rv, rv);
83 
84  rv = categoryManager->AddCategoryEntry(APPSTARTUP_CATEGORY,
86  "service,"
88  PR_TRUE, PR_TRUE, nsnull);
89  NS_ENSURE_SUCCESS(rv, rv);
90 
91  return NS_OK;
92 }
93 
94 nsresult
96 {
97  nsresult rv = NS_ERROR_UNEXPECTED;
98  nsCOMPtr<nsIObserverService> observerService =
99  do_GetService("@mozilla.org/observer-service;1", &rv);
100  NS_ENSURE_SUCCESS(rv, rv);
101 
102  rv = observerService->AddObserver(this, "xpcom-shutdown", PR_FALSE);
103  NS_ENSURE_SUCCESS(rv, rv);
104 
105  return NS_OK;
106 }
107 
108 
109 //-----------------------------------------------------------------------------
110 // sbIScreenSaverSuppressor
111 //-----------------------------------------------------------------------------
112 
113 NS_IMETHODIMP
114 sbBaseScreenSaverSuppressor::Suppress(PRBool aSuppress, PRInt32 *aSuppressionCount)
115 {
116  TRACE(("[sbBaseScreenSaverSuppressor] - Suppress"));
117 
118  nsresult rv = NS_ERROR_UNEXPECTED;
119  PRBool shouldCallOnSuppress = PR_FALSE;
120 
121  if(aSuppress) {
122  if(mSuppress == 0) {
123  shouldCallOnSuppress = PR_TRUE;
124  }
125 
126  // Yes, this may overflow if you call it more than 4 billion times.
127  // Please don't call this method more than 4 billion times. Thanks.
128  mSuppress++;
129  }
130  else {
131  if(mSuppress > 0) {
132  mSuppress--;
133  }
134  if(mSuppress == 0) {
135  shouldCallOnSuppress = PR_TRUE;
136  }
137  }
138 
139  if(shouldCallOnSuppress) {
140  rv = OnSuppress(aSuppress);
141  NS_ENSURE_SUCCESS(rv, rv);
142 
143  LOG(("[sbBaseScreenSaverSuppressor] - OnSuppress Succeeded - Suppressed? %s",
144  aSuppress ? "true" : "false"));
145  }
146 
147  *aSuppressionCount = mSuppress;
148 
149  return NS_OK;
150 }
151 
152 
153 //-----------------------------------------------------------------------------
154 // nsIObserver
155 //-----------------------------------------------------------------------------
156 
157 NS_IMETHODIMP
158 sbBaseScreenSaverSuppressor::Observe(nsISupports* aSubject,
159  const char* aTopic,
160  const PRUnichar* aData)
161 {
162  if(!strcmp(aTopic, "xpcom-shutdown")) {
163 
164  TRACE(("[sbBaseScreenSaverSuppressor] - Observe (%s)", aTopic));
165 
166  nsresult rv = NS_ERROR_UNEXPECTED;
167  nsCOMPtr<nsIObserverService> observerService =
168  do_GetService("@mozilla.org/observer-service;1", &rv);
169  NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
170  "Failed to get observer service, observer will not removed");
171 
172  if(NS_SUCCEEDED(rv)) {
173  rv = observerService->RemoveObserver(this, "xpcom-shutdown");
174  NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Failed to remove observer");
175  }
176 
177  rv = OnSuppress(PR_FALSE);
178  NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Failed to unsuppress screen saver!");
179  }
180 
181  return NS_OK;
182 }
return NS_OK
_updateCookies aPath
static nsCOMPtr< nsIObserverService > observerService
Definition: UnityProxy.cpp:6
Get/Set Screen Saver Suppression.
#define TRACE(args)
#define SB_BASE_SCREEN_SAVER_SUPPRESSOR_DESC
#define LOG(args)
static NS_METHOD RegisterSelf(nsIComponentManager *aCompMgr, nsIFile *aPath, const char *aLoaderStr, const char *aType, const nsModuleComponentInfo *aInfo)
#define SB_BASE_SCREEN_SAVER_SUPPRESSOR_CONTRACTID
NS_IMPL_ISUPPORTS2(sbBaseScreenSaverSuppressor, sbIScreenSaverSuppressor, nsIObserver) sbBaseScreenSaverSuppressor
virtual nsresult OnSuppress(PRBool aSuppress)=0
_updateTextAndScrollDataForFrame aData