VirtualBox

source: vbox/trunk/src/VBox/Main/glue/initterm.cpp@ 2981

Last change on this file since 2981 was 2981, checked in by vboxsync, 17 years ago

InnoTek -> innotek: all the headers and comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 KB
Line 
1/** @file
2 * MS COM / XPCOM Abstraction Layer - Initialization and Termination.
3 */
4
5/*
6 * Copyright (C) 2006-2007 innotek GmbH
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License as published by the Free Software Foundation,
12 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
13 * distribution. VirtualBox OSE is distributed in the hope that it will
14 * be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * If you received this file as part of a commercial VirtualBox
17 * distribution, then only the terms of your commercial VirtualBox
18 * license agreement apply instead of the previous paragraph.
19 */
20
21#if !defined (VBOX_WITH_XPCOM)
22
23#include <objbase.h>
24
25#else /* !defined (VBOX_WITH_XPCOM) */
26
27#include <stdlib.h>
28
29#include <nsXPCOMGlue.h>
30#include <nsIComponentRegistrar.h>
31#include <nsIServiceManager.h>
32#include <nsCOMPtr.h>
33#include <nsEventQueueUtils.h>
34#include <nsEmbedString.h>
35
36#include <nsILocalFile.h>
37#include <nsIDirectoryService.h>
38#include <nsDirectoryServiceDefs.h>
39
40#endif /* !defined (VBOX_WITH_XPCOM) */
41
42#include <iprt/param.h>
43#include <iprt/path.h>
44#include <iprt/string.h>
45#include <iprt/env.h>
46
47#include <VBox/err.h>
48
49#include "VBox/com/com.h"
50#include "VBox/com/assert.h"
51
52
53namespace com
54{
55
56#if defined (VBOX_WITH_XPCOM)
57
58class DirectoryServiceProvider : public nsIDirectoryServiceProvider
59{
60public:
61
62 NS_DECL_ISUPPORTS
63
64 DirectoryServiceProvider()
65 : mCompRegLocation (NULL), mXPTIDatLocation (NULL)
66 {}
67
68 virtual ~DirectoryServiceProvider();
69
70 HRESULT init (const char *aCompRegLocation,
71 const char *aXPTIDatLocation);
72
73 NS_DECL_NSIDIRECTORYSERVICEPROVIDER
74
75private:
76
77 char *mCompRegLocation;
78 char *mXPTIDatLocation;
79};
80
81NS_IMPL_ISUPPORTS1 (DirectoryServiceProvider, nsIDirectoryServiceProvider)
82
83DirectoryServiceProvider::~DirectoryServiceProvider()
84{
85 if (mCompRegLocation)
86 {
87 RTStrFree (mCompRegLocation);
88 mCompRegLocation = NULL;
89 }
90 if (mXPTIDatLocation)
91 {
92 RTStrFree (mXPTIDatLocation);
93 mXPTIDatLocation = NULL;
94 }
95}
96
97/**
98 * @param aCompRegLocation Path to compreg.dat, in Utf8.
99 * @param aXPTIDatLocation Path to xpti.data, in Utf8.
100 */
101HRESULT
102DirectoryServiceProvider::init (const char *aCompRegLocation,
103 const char *aXPTIDatLocation)
104{
105 AssertReturn (aCompRegLocation, NS_ERROR_INVALID_ARG);
106 AssertReturn (aXPTIDatLocation, NS_ERROR_INVALID_ARG);
107
108 int vrc = RTStrUtf8ToCurrentCP (&mCompRegLocation, aCompRegLocation);
109 if (RT_SUCCESS (vrc))
110 vrc = RTStrUtf8ToCurrentCP (&mXPTIDatLocation, aXPTIDatLocation);
111
112 return RT_SUCCESS (vrc) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
113}
114
115NS_IMETHODIMP
116DirectoryServiceProvider::GetFile (const char *aProp,
117 PRBool *aPersistent,
118 nsIFile **aRetval)
119{
120 nsCOMPtr <nsILocalFile> localFile;
121 nsresult rv = NS_ERROR_FAILURE;
122
123 *aRetval = nsnull;
124 *aPersistent = PR_TRUE;
125
126 const char *fileLocation = NULL;
127
128 if (strcmp (aProp, NS_XPCOM_COMPONENT_REGISTRY_FILE) == 0)
129 fileLocation = mCompRegLocation;
130 else if (strcmp (aProp, NS_XPCOM_XPTI_REGISTRY_FILE) == 0)
131 fileLocation = mXPTIDatLocation;
132 else
133 return NS_ERROR_FAILURE;
134
135 rv = NS_NewNativeLocalFile (nsEmbedCString (fileLocation),
136 PR_TRUE, getter_AddRefs (localFile));
137 if (NS_FAILED(rv))
138 return rv;
139
140 return localFile->QueryInterface (NS_GET_IID (nsIFile),
141 (void **) aRetval);
142}
143
144#endif /* defined (VBOX_WITH_XPCOM) */
145
146HRESULT Initialize()
147{
148 HRESULT rc = E_FAIL;
149
150#if !defined (VBOX_WITH_XPCOM)
151
152 rc = CoInitializeEx (NULL, COINIT_MULTITHREADED |
153 COINIT_DISABLE_OLE1DDE |
154 COINIT_SPEED_OVER_MEMORY);
155
156#else /* !defined (VBOX_WITH_XPCOM) */
157
158 /* Set VBOX_XPCOM_HOME if not present */
159 if (!RTEnvExist ("VBOX_XPCOM_HOME"))
160 {
161 /* get the executable path */
162 char pathProgram [RTPATH_MAX];
163 int vrc = RTPathProgram (pathProgram, sizeof (pathProgram));
164 if (RT_SUCCESS (vrc))
165 {
166 char *pathProgramCP = NULL;
167 vrc = RTStrUtf8ToCurrentCP (&pathProgramCP, pathProgram);
168 if (RT_SUCCESS (vrc))
169 {
170 vrc = RTEnvSet ("VBOX_XPCOM_HOME", pathProgramCP);
171 RTStrFree (pathProgramCP);
172 }
173 }
174 AssertRC (vrc);
175 }
176
177 nsCOMPtr <nsIEventQueue> eventQ;
178 rc = NS_GetMainEventQ (getter_AddRefs (eventQ));
179 if (rc == NS_ERROR_NOT_INITIALIZED)
180 {
181 XPCOMGlueStartup (nsnull);
182
183 nsCOMPtr <DirectoryServiceProvider> dsProv;
184
185 /* prepare paths for registry files */
186 Utf8Str homeDir;
187 int vrc = GetVBoxUserHomeDirectory (homeDir);
188 if (RT_SUCCESS (vrc))
189 {
190 Utf8Str compReg = Utf8StrFmt ("%s%c%s", homeDir.raw(),
191 RTPATH_DELIMITER, "compreg.dat");
192 Utf8Str xptiDat = Utf8StrFmt ("%s%c%s", homeDir.raw(),
193 RTPATH_DELIMITER, "xpti.dat");
194 dsProv = new DirectoryServiceProvider();
195 if (dsProv)
196 rc = dsProv->init (compReg, xptiDat);
197 else
198 rc = NS_ERROR_OUT_OF_MEMORY;
199 }
200 else
201 rc = NS_ERROR_FAILURE;
202
203 /* get the path to the executable */
204 nsCOMPtr <nsIFile> appDir;
205 {
206 char path [RTPATH_MAX];
207 char *appDirCP = NULL;
208#if defined (DEBUG)
209 const char *env = RTEnvGet ("VIRTUALBOX_APP_HOME");
210 if (env)
211 {
212 char *appDirUtf8 = NULL;
213 vrc = RTStrCurrentCPToUtf8 (&appDirUtf8, env);
214 if (RT_SUCCESS (vrc))
215 {
216 vrc = RTPathReal (appDirUtf8, path, RTPATH_MAX);
217 if (RT_SUCCESS (vrc))
218 vrc = RTStrUtf8ToCurrentCP (&appDirCP, appDirUtf8);
219 RTStrFree (appDirUtf8);
220 }
221 }
222 else
223#endif
224 {
225 vrc = RTPathProgram (path, RTPATH_MAX);
226 if (RT_SUCCESS (vrc))
227 vrc = RTStrUtf8ToCurrentCP (&appDirCP, path);
228 }
229
230 if (RT_SUCCESS (vrc))
231 {
232 nsCOMPtr<nsILocalFile> file;
233 rc = NS_NewNativeLocalFile (nsEmbedCString (appDirCP),
234 PR_FALSE, getter_AddRefs (file));
235 if (NS_SUCCEEDED (rc))
236 appDir = do_QueryInterface (file, &rc);
237
238 RTStrFree (appDirCP);
239 }
240 else
241 rc = NS_ERROR_FAILURE;
242 }
243
244 /* Finally, initialize XPCOM */
245 if (NS_SUCCEEDED (rc))
246 {
247 nsCOMPtr <nsIServiceManager> serviceManager;
248 rc = NS_InitXPCOM2 (getter_AddRefs (serviceManager),
249 appDir, dsProv);
250
251 if (NS_SUCCEEDED (rc))
252 {
253 nsCOMPtr <nsIComponentRegistrar> registrar =
254 do_QueryInterface (serviceManager, &rc);
255 if (NS_SUCCEEDED (rc))
256 registrar->AutoRegister (nsnull);
257 }
258 }
259 }
260
261#endif /* !defined (VBOX_WITH_XPCOM) */
262
263 AssertComRC (rc);
264
265 return rc;
266}
267
268HRESULT Shutdown()
269{
270 HRESULT rc = S_OK;
271
272#if !defined (VBOX_WITH_XPCOM)
273
274 CoUninitialize();
275
276#else /* !defined (VBOX_WITH_XPCOM) */
277
278 nsCOMPtr <nsIEventQueue> eventQ;
279 rc = NS_GetMainEventQ (getter_AddRefs (eventQ));
280
281 if (NS_SUCCEEDED (rc) || rc == NS_ERROR_NOT_AVAILABLE)
282 {
283 /* NS_ERROR_NOT_AVAILABLE seems to mean that
284 * nsIEventQueue::StopAcceptingEvents() has been called (see
285 * nsEventQueueService.cpp). We hope that this error code always means
286 * just that in this case and assume that we're on the main thread
287 * (it's a kind of unexpected behavior if a non-main thread ever calls
288 * StopAcceptingEvents() on the main event queue). */
289
290 BOOL isOnMainThread = FALSE;
291 if (NS_SUCCEEDED (rc))
292 {
293 rc = eventQ->IsOnCurrentThread (&isOnMainThread);
294 eventQ = nsnull; /* early release */
295 }
296 else
297 {
298 isOnMainThread = TRUE;
299 rc = NS_OK;
300 }
301
302 if (NS_SUCCEEDED (rc) && isOnMainThread)
303 {
304 /* only the main thread needs to uninitialize XPCOM */
305 rc = NS_ShutdownXPCOM (nsnull);
306 XPCOMGlueShutdown();
307 }
308 }
309
310#endif /* !defined (VBOX_WITH_XPCOM) */
311
312 AssertComRC (rc);
313
314 return rc;
315}
316
317}; // namespace com
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette