sbDistHelperEnvWriter.cpp
Go to the documentation of this file.
1 /* vim: set sw=2 */
2 /*
3  *=BEGIN SONGBIRD GPL
4  *
5  * This file is part of the Songbird web player.
6  *
7  * Copyright(c) 2005-2010 POTI, Inc.
8  * http://www.songbirdnest.com
9  *
10  * This file may be licensed under the terms of of the
11  * GNU General Public License Version 2 (the ``GPL'').
12  *
13  * Software distributed under the License is distributed
14  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
15  * express or implied. See the GPL for the specific language
16  * governing rights and limitations.
17  *
18  * You should have received a copy of the GPL along with this
19  * program. If not, go to http://www.gnu.org/licenses/gpl.html
20  * or write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  *=END SONGBIRD GPL
24  */
25 
26 /* class definition includes */
27 #include "sbDistHelperEnvWriter.h"
28 
29 /* Mozilla interface includes */
30 #include <nsICategoryManager.h>
31 #include <nsIFile.h>
32 #include <nsIObserverService.h>
33 #include <nsIProperties.h>
34 
35 /* Songbird interface includes */
36 
37 /* Mozilla header includes */
38 #include <nsCOMPtr.h>
39 #include <nsDirectoryServiceDefs.h>
40 #include <nsNetUtil.h>
41 #include <nsServiceManagerUtils.h>
42 #include <nsStringAPI.h>
43 #include <nsXPCOMCID.h>
44 #include <prio.h>
45 
46 /* Songbird header includes */
47 
48 /* Other includes */
49 #if defined(XP_WIN)
50  #include <windows.h>
51 #elif defined(XP_MACOSX)
52  #include <crt_externs.h>
53 #else
54  extern char **environ;
55 #endif
56 
57 /* Misc preprocessor macros */
58 #define NS_APPSTARTUP_CATEGORY "app-startup"
59 #define XRE_UPDATE_ROOT_DIR "UpdRootD"
60 #define TOPIC_UPDATE_STATUS "update-service-pre-update-status"
61 
62 #ifdef __GNUC__
63 # define __FUNCTION__ __PRETTY_FUNCTION__
64 #endif /* __GNUC__ */
65 
70 #ifdef PR_LOGGING
71 static PRLogModuleInfo* gDistHelperEnvWriterLog = nsnull;
72 #define TRACE(args) PR_LOG(gDistHelperEnvWriterLog , PR_LOG_DEBUG, args)
73 #define LOG(args) PR_LOG(gDistHelperEnvWriterLog , PR_LOG_WARN, args)
74 #else
75 #define TRACE(args) /* nothing */
76 #define LOG(args) /* nothing */
77 #endif
78 
80 
82 {
83  #ifdef PR_LOGGING
84  if (!gDistHelperEnvWriterLog)
85  gDistHelperEnvWriterLog = PR_NewLogModule("sbDistHelperEnvWriter");
86  #endif
87 }
88 
89 sbDistHelperEnvWriter::~sbDistHelperEnvWriter()
90 {
91 }
92 
93 /* static */ NS_METHOD
94 sbDistHelperEnvWriter::RegisterSelf(nsIComponentManager* aCompMgr,
95  nsIFile* aPath,
96  const char* aLoaderStr,
97  const char* aType,
98  const nsModuleComponentInfo* aInfo)
99 {
100  nsresult rv = NS_ERROR_UNEXPECTED;
101  nsCOMPtr<nsICategoryManager> categoryManager =
102  do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
103  NS_ENSURE_SUCCESS(rv, rv);
104 
105  char* previousEntry;
106  rv = categoryManager->AddCategoryEntry(NS_APPSTARTUP_CATEGORY,
107  "sbDistHelperEnvWriter",
109  PR_TRUE, PR_TRUE, &previousEntry);
110  NS_ENSURE_SUCCESS(rv, rv);
111  if (previousEntry) {
112  NS_Free(previousEntry);
113  }
114 
115  return NS_OK;
116 }
117 
118 /* static */ NS_METHOD
119 sbDistHelperEnvWriter::UnregisterSelf(nsIComponentManager *aCompMgr,
120  nsIFile *aPath,
121  const char *aLoaderStr,
122  const nsModuleComponentInfo *aInfo)
123 {
124  nsresult rv;
125 
126  // Get the category manager.
127  nsCOMPtr<nsICategoryManager> categoryManager =
128  do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
129  NS_ENSURE_SUCCESS(rv, rv);
130 
131  // Delete self from the app-startup category.
132  rv = categoryManager->DeleteCategoryEntry(NS_APPSTARTUP_CATEGORY,
133  "sbDistHelperEnvWriter",
134  PR_TRUE);
135  NS_ENSURE_SUCCESS(rv, rv);
136 
137  return NS_OK;
138 }
139 
140 nsresult
142 {
143  nsresult rv;
144  TRACE(("%s", __FUNCTION__));
145  NS_ENSURE_ARG_POINTER(aUpdateDir);
146 
147  nsCOMPtr<nsIFile> outFile;
148  rv = aUpdateDir->Clone(getter_AddRefs(outFile));
149  NS_ENSURE_SUCCESS(rv, rv);
150  rv = outFile->Append(NS_LITERAL_STRING("disthelper.env"));
151  NS_ENSURE_SUCCESS(rv, rv);
152 
153  nsCOMPtr<nsIOutputStream> outStream;
154  rv = NS_NewLocalFileOutputStream(getter_AddRefs(outStream),
155  outFile);
156  NS_ENSURE_SUCCESS(rv, rv);
157  #if XP_WIN
158  // Windows needs to go through the raw Win32 calls, otherwise we get nasty
159  // linker errors because we're linking against the CRT DLL.
160  const WCHAR ENV_PREFIX[] = L"DISTHELPER_";
161  LPWSTR env = GetEnvironmentStringsW();
162  while (*env) {
163  if (!wcsncmp(ENV_PREFIX, env, NS_ARRAY_LENGTH(ENV_PREFIX) - 1)) {
164  nsCAutoString envString;
165  CopyUTF16toUTF8(nsDependentString(env), envString);
166  TRACE(("env var: %s", envString.get()));
167  envString.Append('\n');
168  PRUint32 bytesWritten;
169  rv = outStream->Write(envString.get(),
170  envString.Length(),
171  &bytesWritten);
172  NS_ENSURE_SUCCESS(rv, rv);
173  }
174  env += wcslen(env) + 1;
175  }
176  #else
177  const char ENV_PREFIX[] = "DISTHELPER_";
178  char** env;
179  #if defined(XP_MACOSX)
180  // Darwin/OSX is similar to Windows in needing to use a call to get environ
181  env = *_NSGetEnviron();
182  #else
183  env = environ;
184  #endif
185  for (; *env; ++env) {
186  if (!strncmp(ENV_PREFIX, *env, NS_ARRAY_LENGTH(ENV_PREFIX) - 1)) {
187  nsCAutoString envString(*env);
188  TRACE(("env var: %s", envString.get()));
189  envString.Append('\n');
190  PRUint32 bytesWritten;
191  rv = outStream->Write(envString.get(),
192  envString.Length(),
193  &bytesWritten);
194  NS_ENSURE_SUCCESS(rv, rv);
195  }
196  }
197  #endif
198  rv = outStream->Close();
199  NS_ENSURE_SUCCESS(rv, rv);
200 
201  return NS_OK;
202 }
203 
204 /***** nsIObserver *****/
205 /* void observe (in nsISupports aSubject, in string aTopic, in wstring aData); */
206 NS_IMETHODIMP
207 sbDistHelperEnvWriter::Observe(nsISupports *aSubject,
208  const char *aTopic,
209  const PRUnichar *aData)
210 {
211  nsresult rv;
212  TRACE(("%s: %s", __FUNCTION__, aTopic));
213 
214  if (!strcmp(aTopic, NS_APPSTARTUP_CATEGORY)) {
215  nsCOMPtr<nsIObserverService> obsSvc =
216  do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
217  NS_ENSURE_SUCCESS(rv, rv);
218  rv = obsSvc->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
219  NS_ENSURE_SUCCESS(rv, rv);
220  rv = obsSvc->AddObserver(this, TOPIC_UPDATE_STATUS, false);
221  NS_ENSURE_SUCCESS(rv, rv);
222  }
223  else if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
224  nsCOMPtr<nsIObserverService> obsSvc =
225  do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
226  rv = obsSvc->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
227  NS_ENSURE_SUCCESS(rv, rv);
228  rv = obsSvc->RemoveObserver(this, TOPIC_UPDATE_STATUS);
229  NS_ENSURE_SUCCESS(rv, rv);
230  }
231  else if (!strcmp(aTopic, TOPIC_UPDATE_STATUS)) {
232  if (NS_LITERAL_STRING("pending").Equals(aData)) {
233  nsCOMPtr<nsIFile> updateDir = do_QueryInterface(aSubject);
234  NS_ENSURE_TRUE(updateDir, NS_ERROR_FAILURE);
235  rv = OnUpdatePending(updateDir);
236  NS_ENSURE_SUCCESS(rv, rv);
237  }
238  }
239 
240  return NS_OK;
241 }
return NS_OK
_updateCookies aPath
nsresult OnUpdatePending(nsIFile *aUpdateDir)
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
static NS_METHOD RegisterSelf(nsIComponentManager *aCompMgr, nsIFile *aPath, const char *aLoaderStr, const char *aType, const nsModuleComponentInfo *aInfo)
#define SB_DISTHELPER_ENV_WRITER_CONTRACTID
char ** environ
#define NS_APPSTARTUP_CATEGORY
#define TRACE(args)
static NS_METHOD UnregisterSelf(nsIComponentManager *aCompMgr, nsIFile *aPath, const char *aLoaderStr, const nsModuleComponentInfo *aInfo)
#define TOPIC_UPDATE_STATUS
_updateTextAndScrollDataForFrame aData