1 | /* $Id: initterm.cpp 59385 2016-01-18 17:37:59Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | * MS COM / XPCOM Abstraction Layer - Initialization and Termination.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2014 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #if !defined(VBOX_WITH_XPCOM)
|
---|
20 |
|
---|
21 | # include <iprt/nt/nt-and-windows.h>
|
---|
22 | # include <objbase.h>
|
---|
23 |
|
---|
24 | #else /* !defined(VBOX_WITH_XPCOM) */
|
---|
25 |
|
---|
26 | # include <stdlib.h>
|
---|
27 |
|
---|
28 | # include <nsIComponentRegistrar.h>
|
---|
29 | # include <nsIServiceManager.h>
|
---|
30 | # include <nsCOMPtr.h>
|
---|
31 | # include <nsEventQueueUtils.h>
|
---|
32 | # include <nsEmbedString.h>
|
---|
33 |
|
---|
34 | # include <nsILocalFile.h>
|
---|
35 | # include <nsIDirectoryService.h>
|
---|
36 | # include <nsDirectoryServiceDefs.h>
|
---|
37 |
|
---|
38 | #endif /* !defined(VBOX_WITH_XPCOM) */
|
---|
39 |
|
---|
40 | #include "VBox/com/com.h"
|
---|
41 | #include "VBox/com/assert.h"
|
---|
42 | #include "VBox/com/NativeEventQueue.h"
|
---|
43 | #include "VBox/com/AutoLock.h"
|
---|
44 |
|
---|
45 | #include "../include/Logging.h"
|
---|
46 |
|
---|
47 | #include <iprt/asm.h>
|
---|
48 | #include <iprt/env.h>
|
---|
49 | #include <iprt/ldr.h>
|
---|
50 | #include <iprt/param.h>
|
---|
51 | #include <iprt/path.h>
|
---|
52 | #include <iprt/string.h>
|
---|
53 | #include <iprt/thread.h>
|
---|
54 |
|
---|
55 | #include <VBox/err.h>
|
---|
56 |
|
---|
57 | namespace com
|
---|
58 | {
|
---|
59 |
|
---|
60 | #if defined(VBOX_WITH_XPCOM)
|
---|
61 |
|
---|
62 | class DirectoryServiceProvider : public nsIDirectoryServiceProvider
|
---|
63 | {
|
---|
64 | public:
|
---|
65 |
|
---|
66 | NS_DECL_ISUPPORTS
|
---|
67 |
|
---|
68 | DirectoryServiceProvider()
|
---|
69 | : mCompRegLocation(NULL), mXPTIDatLocation(NULL)
|
---|
70 | , mComponentDirLocation(NULL), mCurrProcDirLocation(NULL)
|
---|
71 | {}
|
---|
72 |
|
---|
73 | virtual ~DirectoryServiceProvider();
|
---|
74 |
|
---|
75 | HRESULT init(const char *aCompRegLocation,
|
---|
76 | const char *aXPTIDatLocation,
|
---|
77 | const char *aComponentDirLocation,
|
---|
78 | const char *aCurrProcDirLocation);
|
---|
79 |
|
---|
80 | NS_DECL_NSIDIRECTORYSERVICEPROVIDER
|
---|
81 |
|
---|
82 | private:
|
---|
83 | /** @remarks This is not a UTF-8 string. */
|
---|
84 | char *mCompRegLocation;
|
---|
85 | /** @remarks This is not a UTF-8 string. */
|
---|
86 | char *mXPTIDatLocation;
|
---|
87 | /** @remarks This is not a UTF-8 string. */
|
---|
88 | char *mComponentDirLocation;
|
---|
89 | /** @remarks This is not a UTF-8 string. */
|
---|
90 | char *mCurrProcDirLocation;
|
---|
91 | };
|
---|
92 |
|
---|
93 | NS_IMPL_ISUPPORTS1(DirectoryServiceProvider, nsIDirectoryServiceProvider)
|
---|
94 |
|
---|
95 | DirectoryServiceProvider::~DirectoryServiceProvider()
|
---|
96 | {
|
---|
97 | if (mCompRegLocation)
|
---|
98 | {
|
---|
99 | RTStrFree(mCompRegLocation);
|
---|
100 | mCompRegLocation = NULL;
|
---|
101 | }
|
---|
102 | if (mXPTIDatLocation)
|
---|
103 | {
|
---|
104 | RTStrFree(mXPTIDatLocation);
|
---|
105 | mXPTIDatLocation = NULL;
|
---|
106 | }
|
---|
107 | if (mComponentDirLocation)
|
---|
108 | {
|
---|
109 | RTStrFree(mComponentDirLocation);
|
---|
110 | mComponentDirLocation = NULL;
|
---|
111 | }
|
---|
112 | if (mCurrProcDirLocation)
|
---|
113 | {
|
---|
114 | RTStrFree(mCurrProcDirLocation);
|
---|
115 | mCurrProcDirLocation = NULL;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * @param aCompRegLocation Path to compreg.dat, in Utf8.
|
---|
121 | * @param aXPTIDatLocation Path to xpti.data, in Utf8.
|
---|
122 | */
|
---|
123 | HRESULT
|
---|
124 | DirectoryServiceProvider::init(const char *aCompRegLocation,
|
---|
125 | const char *aXPTIDatLocation,
|
---|
126 | const char *aComponentDirLocation,
|
---|
127 | const char *aCurrProcDirLocation)
|
---|
128 | {
|
---|
129 | AssertReturn(aCompRegLocation, NS_ERROR_INVALID_ARG);
|
---|
130 | AssertReturn(aXPTIDatLocation, NS_ERROR_INVALID_ARG);
|
---|
131 |
|
---|
132 | /** @todo r=bird: Gotta check how this encoding stuff plays out on darwin!
|
---|
133 | * We get down to [VBoxNsxp]NS_NewNativeLocalFile and that file isn't
|
---|
134 | * nsLocalFileUnix.cpp on 32-bit darwin. On 64-bit darwin it's a question
|
---|
135 | * of what we're doing in IPRT and such... We should probably add a
|
---|
136 | * RTPathConvertToNative for use here. */
|
---|
137 | int vrc = RTStrUtf8ToCurrentCP(&mCompRegLocation, aCompRegLocation);
|
---|
138 | if (RT_SUCCESS(vrc))
|
---|
139 | vrc = RTStrUtf8ToCurrentCP(&mXPTIDatLocation, aXPTIDatLocation);
|
---|
140 | if (RT_SUCCESS(vrc) && aComponentDirLocation)
|
---|
141 | vrc = RTStrUtf8ToCurrentCP(&mComponentDirLocation, aComponentDirLocation);
|
---|
142 | if (RT_SUCCESS(vrc) && aCurrProcDirLocation)
|
---|
143 | vrc = RTStrUtf8ToCurrentCP(&mCurrProcDirLocation, aCurrProcDirLocation);
|
---|
144 |
|
---|
145 | return RT_SUCCESS(vrc) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
---|
146 | }
|
---|
147 |
|
---|
148 | NS_IMETHODIMP
|
---|
149 | DirectoryServiceProvider::GetFile(const char *aProp,
|
---|
150 | PRBool *aPersistent,
|
---|
151 | nsIFile **aRetval)
|
---|
152 | {
|
---|
153 | nsCOMPtr <nsILocalFile> localFile;
|
---|
154 | nsresult rv = NS_ERROR_FAILURE;
|
---|
155 |
|
---|
156 | *aRetval = nsnull;
|
---|
157 | *aPersistent = PR_TRUE;
|
---|
158 |
|
---|
159 | const char *fileLocation = NULL;
|
---|
160 |
|
---|
161 | if (strcmp(aProp, NS_XPCOM_COMPONENT_REGISTRY_FILE) == 0)
|
---|
162 | fileLocation = mCompRegLocation;
|
---|
163 | else if (strcmp(aProp, NS_XPCOM_XPTI_REGISTRY_FILE) == 0)
|
---|
164 | fileLocation = mXPTIDatLocation;
|
---|
165 | else if (mComponentDirLocation && strcmp(aProp, NS_XPCOM_COMPONENT_DIR) == 0)
|
---|
166 | fileLocation = mComponentDirLocation;
|
---|
167 | else if (mCurrProcDirLocation && strcmp(aProp, NS_XPCOM_CURRENT_PROCESS_DIR) == 0)
|
---|
168 | fileLocation = mCurrProcDirLocation;
|
---|
169 | else
|
---|
170 | return NS_ERROR_FAILURE;
|
---|
171 |
|
---|
172 | rv = NS_NewNativeLocalFile(nsEmbedCString(fileLocation),
|
---|
173 | PR_TRUE, getter_AddRefs(localFile));
|
---|
174 | if (NS_FAILED(rv))
|
---|
175 | return rv;
|
---|
176 |
|
---|
177 | return localFile->QueryInterface(NS_GET_IID(nsIFile), (void **)aRetval);
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Global XPCOM initialization flag (we maintain it ourselves since XPCOM
|
---|
182 | * doesn't provide such functionality)
|
---|
183 | */
|
---|
184 | static bool volatile gIsXPCOMInitialized = false;
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Number of Initialize() calls on the main thread.
|
---|
188 | */
|
---|
189 | static unsigned int gXPCOMInitCount = 0;
|
---|
190 |
|
---|
191 | #else /* !defined(VBOX_WITH_XPCOM) */
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * The COM main thread handle. (The first caller of com::Initialize().)
|
---|
195 | */
|
---|
196 | static RTTHREAD volatile gCOMMainThread = NIL_RTTHREAD;
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Number of Initialize() calls on the main thread.
|
---|
200 | */
|
---|
201 | static uint32_t gCOMMainInitCount = 0;
|
---|
202 |
|
---|
203 | #endif /* !defined(VBOX_WITH_XPCOM) */
|
---|
204 |
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Initializes the COM runtime.
|
---|
208 | *
|
---|
209 | * This method must be called on each thread of the client application that
|
---|
210 | * wants to access COM facilities. The initialization must be performed before
|
---|
211 | * calling any other COM method or attempting to instantiate COM objects.
|
---|
212 | *
|
---|
213 | * On platforms using XPCOM, this method uses the following scheme to search for
|
---|
214 | * XPCOM runtime:
|
---|
215 | *
|
---|
216 | * 1. If the VBOX_APP_HOME environment variable is set, the path it specifies
|
---|
217 | * is used to search XPCOM libraries and components. If this method fails to
|
---|
218 | * initialize XPCOM runtime using this path, it will immediately return a
|
---|
219 | * failure and will NOT check for other paths as described below.
|
---|
220 | *
|
---|
221 | * 2. If VBOX_APP_HOME is not set, this methods tries the following paths in the
|
---|
222 | * given order:
|
---|
223 | *
|
---|
224 | * a) Compiled-in application data directory (as returned by
|
---|
225 | * RTPathAppPrivateArch())
|
---|
226 | * b) "/usr/lib/virtualbox" (Linux only)
|
---|
227 | * c) "/opt/VirtualBox" (Linux only)
|
---|
228 | *
|
---|
229 | * The first path for which the initialization succeeds will be used.
|
---|
230 | *
|
---|
231 | * On MS COM platforms, the COM runtime is provided by the system and does not
|
---|
232 | * need to be searched for.
|
---|
233 | *
|
---|
234 | * Once the COM subsystem is no longer necessary on a given thread, Shutdown()
|
---|
235 | * must be called to free resources allocated for it. Note that a thread may
|
---|
236 | * call Initialize() several times but for each of tese calls there must be a
|
---|
237 | * corresponding Shutdown() call.
|
---|
238 | *
|
---|
239 | * @return S_OK on success and a COM result code in case of failure.
|
---|
240 | */
|
---|
241 | HRESULT Initialize(bool fGui /*= false*/, bool fAutoRegUpdate /*= true*/)
|
---|
242 | {
|
---|
243 | HRESULT rc = E_FAIL;
|
---|
244 | NOREF(fAutoRegUpdate);
|
---|
245 |
|
---|
246 | #if !defined(VBOX_WITH_XPCOM)
|
---|
247 |
|
---|
248 | # ifdef VBOX_WITH_AUTO_COM_REG_UPDATE
|
---|
249 | /*
|
---|
250 | * First time we're called in a process, we refresh the VBox COM registrations.
|
---|
251 | */
|
---|
252 | if (fAutoRegUpdate && gCOMMainThread == NIL_RTTHREAD)
|
---|
253 | {
|
---|
254 | char szPath[RTPATH_MAX];
|
---|
255 | int vrc = RTPathAppPrivateArch(szPath, sizeof(szPath));
|
---|
256 | if (RT_SUCCESS(vrc))
|
---|
257 | {
|
---|
258 | # ifndef VBOX_IN_32_ON_64_MAIN_API
|
---|
259 | rc = RTPathAppend(szPath, sizeof(szPath),
|
---|
260 | # if ARCH_BITS == 64
|
---|
261 | RT_MAKE_U64(((PKUSER_SHARED_DATA)MM_SHARED_USER_DATA_VA)->NtMinorVersion,
|
---|
262 | ((PKUSER_SHARED_DATA)MM_SHARED_USER_DATA_VA)->NtMajorVersion)
|
---|
263 | >= RT_MAKE_U64(1/*Lo*/,6/*Hi*/)
|
---|
264 | ? "VBoxProxyStub.dll" : "VBoxProxyStubLegacy.dll"
|
---|
265 | # else
|
---|
266 | "VBoxProxyStub.dll"
|
---|
267 | # endif
|
---|
268 | );
|
---|
269 | # else
|
---|
270 | rc = RTPathAppend(szPath, sizeof(szPath), "x86\\VBoxProxyStub-x86.dll");
|
---|
271 | # endif
|
---|
272 | }
|
---|
273 | if (RT_SUCCESS(vrc))
|
---|
274 | {
|
---|
275 | RTLDRMOD hMod;
|
---|
276 | vrc = RTLdrLoad(szPath, &hMod);
|
---|
277 | if (RT_SUCCESS(vrc))
|
---|
278 | {
|
---|
279 | union
|
---|
280 | {
|
---|
281 | void *pv;
|
---|
282 | DECLCALLBACKMEMBER(uint32_t, pfnRegUpdate)(void);
|
---|
283 | } u;
|
---|
284 | rc = RTLdrGetSymbol(hMod, "VbpsUpdateRegistrations", &u.pv);
|
---|
285 | if (RT_SUCCESS(rc))
|
---|
286 | u.pfnRegUpdate();
|
---|
287 | /* Just keep it loaded. */
|
---|
288 | }
|
---|
289 | }
|
---|
290 | }
|
---|
291 | # endif
|
---|
292 |
|
---|
293 | /*
|
---|
294 | * We initialize COM in GUI thread in STA, to be compliant with QT and
|
---|
295 | * OLE requirments (for example to allow D&D), while other threads
|
---|
296 | * initialized in regular MTA. To allow fast proxyless access from
|
---|
297 | * GUI thread to COM objects, we explicitly provide our COM objects
|
---|
298 | * with free threaded marshaller.
|
---|
299 | * !!!!! Please think twice before touching this code !!!!!
|
---|
300 | */
|
---|
301 | DWORD flags = fGui ?
|
---|
302 | COINIT_APARTMENTTHREADED
|
---|
303 | | COINIT_SPEED_OVER_MEMORY
|
---|
304 | :
|
---|
305 | COINIT_MULTITHREADED
|
---|
306 | | COINIT_DISABLE_OLE1DDE
|
---|
307 | | COINIT_SPEED_OVER_MEMORY;
|
---|
308 |
|
---|
309 | rc = CoInitializeEx(NULL, flags);
|
---|
310 |
|
---|
311 | /* the overall result must be either S_OK or S_FALSE (S_FALSE means
|
---|
312 | * "already initialized using the same apartment model") */
|
---|
313 | AssertMsg(rc == S_OK || rc == S_FALSE, ("rc=%08X\n", rc));
|
---|
314 |
|
---|
315 | /* To be flow compatible with the XPCOM case, we return here if this isn't
|
---|
316 | * the main thread or if it isn't its first initialization call.
|
---|
317 | * Note! CoInitializeEx and CoUninitialize does it's own reference
|
---|
318 | * counting, so this exercise is entirely for the EventQueue init. */
|
---|
319 | bool fRc;
|
---|
320 | RTTHREAD hSelf = RTThreadSelf();
|
---|
321 | if (hSelf != NIL_RTTHREAD)
|
---|
322 | ASMAtomicCmpXchgHandle(&gCOMMainThread, hSelf, NIL_RTTHREAD, fRc);
|
---|
323 | else
|
---|
324 | fRc = false;
|
---|
325 |
|
---|
326 | if (fGui)
|
---|
327 | Assert(RTThreadIsMain(hSelf));
|
---|
328 |
|
---|
329 | if (!fRc)
|
---|
330 | {
|
---|
331 | if ( gCOMMainThread == hSelf
|
---|
332 | && SUCCEEDED(rc))
|
---|
333 | gCOMMainInitCount++;
|
---|
334 |
|
---|
335 | AssertComRC(rc);
|
---|
336 | return rc;
|
---|
337 | }
|
---|
338 | Assert(RTThreadIsMain(hSelf));
|
---|
339 |
|
---|
340 | /* this is the first main thread initialization */
|
---|
341 | Assert(gCOMMainInitCount == 0);
|
---|
342 | if (SUCCEEDED(rc))
|
---|
343 | gCOMMainInitCount = 1;
|
---|
344 |
|
---|
345 | #else /* !defined(VBOX_WITH_XPCOM) */
|
---|
346 |
|
---|
347 | /* Unused here */
|
---|
348 | NOREF(fGui);
|
---|
349 |
|
---|
350 | if (ASMAtomicXchgBool(&gIsXPCOMInitialized, true) == true)
|
---|
351 | {
|
---|
352 | /* XPCOM is already initialized on the main thread, no special
|
---|
353 | * initialization is necessary on additional threads. Just increase
|
---|
354 | * the init counter if it's a main thread again (to correctly support
|
---|
355 | * nested calls to Initialize()/Shutdown() for compatibility with
|
---|
356 | * Win32). */
|
---|
357 |
|
---|
358 | nsCOMPtr<nsIEventQueue> eventQ;
|
---|
359 | rc = NS_GetMainEventQ(getter_AddRefs(eventQ));
|
---|
360 |
|
---|
361 | if (NS_SUCCEEDED(rc))
|
---|
362 | {
|
---|
363 | PRBool isOnMainThread = PR_FALSE;
|
---|
364 | rc = eventQ->IsOnCurrentThread(&isOnMainThread);
|
---|
365 | if (NS_SUCCEEDED(rc) && isOnMainThread)
|
---|
366 | ++gXPCOMInitCount;
|
---|
367 | }
|
---|
368 |
|
---|
369 | AssertComRC(rc);
|
---|
370 | return rc;
|
---|
371 | }
|
---|
372 | Assert(RTThreadIsMain(RTThreadSelf()));
|
---|
373 |
|
---|
374 | /* this is the first initialization */
|
---|
375 | gXPCOMInitCount = 1;
|
---|
376 |
|
---|
377 | /* prepare paths for registry files */
|
---|
378 | char szCompReg[RTPATH_MAX];
|
---|
379 | char szXptiDat[RTPATH_MAX];
|
---|
380 |
|
---|
381 | int vrc = GetVBoxUserHomeDirectory(szCompReg, sizeof(szCompReg));
|
---|
382 | if (vrc == VERR_ACCESS_DENIED)
|
---|
383 | return NS_ERROR_FILE_ACCESS_DENIED;
|
---|
384 | AssertRCReturn(vrc, NS_ERROR_FAILURE);
|
---|
385 | vrc = RTStrCopy(szXptiDat, sizeof(szXptiDat), szCompReg);
|
---|
386 | AssertRCReturn(vrc, NS_ERROR_FAILURE);
|
---|
387 | #ifdef VBOX_IN_32_ON_64_MAIN_API
|
---|
388 | vrc = RTPathAppend(szCompReg, sizeof(szCompReg), "compreg-x86.dat");
|
---|
389 | AssertRCReturn(vrc, NS_ERROR_FAILURE);
|
---|
390 | vrc = RTPathAppend(szXptiDat, sizeof(szXptiDat), "xpti-x86.dat");
|
---|
391 | AssertRCReturn(vrc, NS_ERROR_FAILURE);
|
---|
392 | #else
|
---|
393 | vrc = RTPathAppend(szCompReg, sizeof(szCompReg), "compreg.dat");
|
---|
394 | AssertRCReturn(vrc, NS_ERROR_FAILURE);
|
---|
395 | vrc = RTPathAppend(szXptiDat, sizeof(szXptiDat), "xpti.dat");
|
---|
396 | AssertRCReturn(vrc, NS_ERROR_FAILURE);
|
---|
397 | #endif
|
---|
398 |
|
---|
399 | LogFlowFunc(("component registry : \"%s\"\n", szCompReg));
|
---|
400 | LogFlowFunc(("XPTI data file : \"%s\"\n", szXptiDat));
|
---|
401 |
|
---|
402 | static const char *kAppPathsToProbe[] =
|
---|
403 | {
|
---|
404 | NULL, /* 0: will use VBOX_APP_HOME */
|
---|
405 | NULL, /* 1: will try RTPathAppPrivateArch(), correctly installed release builds will never go further */
|
---|
406 | NULL, /* 2: will try parent directory of RTPathAppPrivateArch(), only for testcases in non-hardened builds */
|
---|
407 | /* There used to be hard coded paths, but they only caused trouble
|
---|
408 | * because they often led to mixing of builds or even versions.
|
---|
409 | * If you feel tempted to add anything here, think again. They would
|
---|
410 | * only be used if option 1 would not work, which is a sign of a big
|
---|
411 | * problem, as it returns a fixed location defined at compile time.
|
---|
412 | * It is better to fail than blindly trying to cover the problem. */
|
---|
413 | };
|
---|
414 |
|
---|
415 | /* Find out the directory where VirtualBox binaries are located */
|
---|
416 | for (size_t i = 0; i < RT_ELEMENTS(kAppPathsToProbe); ++ i)
|
---|
417 | {
|
---|
418 | char szAppHomeDir[RTPATH_MAX];
|
---|
419 |
|
---|
420 | if (i == 0)
|
---|
421 | {
|
---|
422 | /* Use VBOX_APP_HOME if present */
|
---|
423 | vrc = RTEnvGetEx(RTENV_DEFAULT, "VBOX_APP_HOME", szAppHomeDir, sizeof(szAppHomeDir), NULL);
|
---|
424 | if (vrc == VERR_ENV_VAR_NOT_FOUND)
|
---|
425 | continue;
|
---|
426 | AssertRC(vrc);
|
---|
427 | }
|
---|
428 | else if (i == 1)
|
---|
429 | {
|
---|
430 | /* Use RTPathAppPrivateArch() first */
|
---|
431 | vrc = RTPathAppPrivateArch(szAppHomeDir, sizeof(szAppHomeDir));
|
---|
432 | AssertRC(vrc);
|
---|
433 | }
|
---|
434 | else if (i == 2)
|
---|
435 | {
|
---|
436 | #ifdef VBOX_WITH_HARDENING
|
---|
437 | continue;
|
---|
438 | #else /* !VBOX_WITH_HARDENING */
|
---|
439 | /* Use parent of RTPathAppPrivateArch() if ends with "testcase" */
|
---|
440 | vrc = RTPathAppPrivateArch(szAppHomeDir, sizeof(szAppHomeDir));
|
---|
441 | AssertRC(vrc);
|
---|
442 | vrc = RTPathStripTrailingSlash(szAppHomeDir);
|
---|
443 | AssertRC(vrc);
|
---|
444 | char *filename = RTPathFilename(szAppHomeDir);
|
---|
445 | if (!filename || strcmp(filename, "testcase"))
|
---|
446 | continue;
|
---|
447 | RTPathStripFilename(szAppHomeDir);
|
---|
448 | #endif /* !VBOX_WITH_HARDENING */
|
---|
449 | }
|
---|
450 | else
|
---|
451 | {
|
---|
452 | /* Iterate over all other paths */
|
---|
453 | RTStrCopy(szAppHomeDir, sizeof(szAppHomeDir), kAppPathsToProbe[i]);
|
---|
454 | vrc = VINF_SUCCESS;
|
---|
455 | }
|
---|
456 | if (RT_FAILURE(vrc))
|
---|
457 | {
|
---|
458 | rc = NS_ERROR_FAILURE;
|
---|
459 | continue;
|
---|
460 | }
|
---|
461 | char szCompDir[RTPATH_MAX];
|
---|
462 | vrc = RTStrCopy(szCompDir, sizeof(szCompDir), szAppHomeDir);
|
---|
463 | if (RT_FAILURE(vrc))
|
---|
464 | {
|
---|
465 | rc = NS_ERROR_FAILURE;
|
---|
466 | continue;
|
---|
467 | }
|
---|
468 | vrc = RTPathAppend(szCompDir, sizeof(szCompDir), "components");
|
---|
469 | if (RT_FAILURE(vrc))
|
---|
470 | {
|
---|
471 | rc = NS_ERROR_FAILURE;
|
---|
472 | continue;
|
---|
473 | }
|
---|
474 | LogFlowFunc(("component directory : \"%s\"\n", szCompDir));
|
---|
475 |
|
---|
476 | nsCOMPtr<DirectoryServiceProvider> dsProv;
|
---|
477 | dsProv = new DirectoryServiceProvider();
|
---|
478 | if (dsProv)
|
---|
479 | rc = dsProv->init(szCompReg, szXptiDat, szCompDir, szAppHomeDir);
|
---|
480 | else
|
---|
481 | rc = NS_ERROR_OUT_OF_MEMORY;
|
---|
482 | if (NS_FAILED(rc))
|
---|
483 | break;
|
---|
484 |
|
---|
485 | /* Setup the application path for NS_InitXPCOM2. Note that we properly
|
---|
486 | * answer the NS_XPCOM_CURRENT_PROCESS_DIR query in our directory
|
---|
487 | * service provider but it seems to be activated after the directory
|
---|
488 | * service is used for the first time (see the source NS_InitXPCOM2). So
|
---|
489 | * use the same value here to be on the safe side. */
|
---|
490 | nsCOMPtr <nsIFile> appDir;
|
---|
491 | {
|
---|
492 | char *appDirCP = NULL;
|
---|
493 | vrc = RTStrUtf8ToCurrentCP(&appDirCP, szAppHomeDir);
|
---|
494 | if (RT_SUCCESS(vrc))
|
---|
495 | {
|
---|
496 | nsCOMPtr<nsILocalFile> file;
|
---|
497 | rc = NS_NewNativeLocalFile(nsEmbedCString(appDirCP),
|
---|
498 | PR_FALSE, getter_AddRefs(file));
|
---|
499 | if (NS_SUCCEEDED(rc))
|
---|
500 | appDir = do_QueryInterface(file, &rc);
|
---|
501 |
|
---|
502 | RTStrFree(appDirCP);
|
---|
503 | }
|
---|
504 | else
|
---|
505 | rc = NS_ERROR_FAILURE;
|
---|
506 | }
|
---|
507 | if (NS_FAILED(rc))
|
---|
508 | break;
|
---|
509 |
|
---|
510 | /* Set VBOX_XPCOM_HOME to the same app path to make XPCOM sources that
|
---|
511 | * still use it instead of the directory service happy */
|
---|
512 | vrc = RTEnvSetEx(RTENV_DEFAULT, "VBOX_XPCOM_HOME", szAppHomeDir);
|
---|
513 | AssertRC(vrc);
|
---|
514 |
|
---|
515 | /* Finally, initialize XPCOM */
|
---|
516 | {
|
---|
517 | nsCOMPtr<nsIServiceManager> serviceManager;
|
---|
518 | rc = NS_InitXPCOM2(getter_AddRefs(serviceManager), appDir, dsProv);
|
---|
519 | if (NS_SUCCEEDED(rc))
|
---|
520 | {
|
---|
521 | nsCOMPtr<nsIComponentRegistrar> registrar =
|
---|
522 | do_QueryInterface(serviceManager, &rc);
|
---|
523 | if (NS_SUCCEEDED(rc))
|
---|
524 | {
|
---|
525 | rc = registrar->AutoRegister(nsnull);
|
---|
526 | if (NS_SUCCEEDED(rc))
|
---|
527 | {
|
---|
528 | /* We succeeded, stop probing paths */
|
---|
529 | LogFlowFunc(("Succeeded.\n"));
|
---|
530 | break;
|
---|
531 | }
|
---|
532 | }
|
---|
533 | }
|
---|
534 | }
|
---|
535 |
|
---|
536 | /* clean up before the new try */
|
---|
537 | HRESULT rc2 = NS_ShutdownXPCOM(nsnull);
|
---|
538 | if (SUCCEEDED(rc))
|
---|
539 | rc = rc2;
|
---|
540 |
|
---|
541 | if (i == 0)
|
---|
542 | {
|
---|
543 | /* We failed with VBOX_APP_HOME, don't probe other paths */
|
---|
544 | break;
|
---|
545 | }
|
---|
546 | }
|
---|
547 |
|
---|
548 | #endif /* !defined(VBOX_WITH_XPCOM) */
|
---|
549 |
|
---|
550 | AssertComRCReturnRC(rc);
|
---|
551 |
|
---|
552 | // for both COM and XPCOM, we only get here if this is the main thread;
|
---|
553 | // only then initialize the autolock system (AutoLock.cpp)
|
---|
554 | Assert(RTThreadIsMain(RTThreadSelf()));
|
---|
555 | util::InitAutoLockSystem();
|
---|
556 |
|
---|
557 | /*
|
---|
558 | * Init the main event queue (ASSUMES it cannot fail).
|
---|
559 | */
|
---|
560 | if (SUCCEEDED(rc))
|
---|
561 | NativeEventQueue::init();
|
---|
562 |
|
---|
563 | return rc;
|
---|
564 | }
|
---|
565 |
|
---|
566 | HRESULT Shutdown()
|
---|
567 | {
|
---|
568 | HRESULT rc = S_OK;
|
---|
569 |
|
---|
570 | #if !defined(VBOX_WITH_XPCOM)
|
---|
571 |
|
---|
572 | /* EventQueue::uninit reference counting fun. */
|
---|
573 | RTTHREAD hSelf = RTThreadSelf();
|
---|
574 | if ( hSelf == gCOMMainThread
|
---|
575 | && hSelf != NIL_RTTHREAD)
|
---|
576 | {
|
---|
577 | if (-- gCOMMainInitCount == 0)
|
---|
578 | {
|
---|
579 | NativeEventQueue::uninit();
|
---|
580 | ASMAtomicWriteHandle(&gCOMMainThread, NIL_RTTHREAD);
|
---|
581 | }
|
---|
582 | }
|
---|
583 |
|
---|
584 | CoUninitialize();
|
---|
585 |
|
---|
586 | #else /* !defined(VBOX_WITH_XPCOM) */
|
---|
587 |
|
---|
588 | nsCOMPtr<nsIEventQueue> eventQ;
|
---|
589 | rc = NS_GetMainEventQ(getter_AddRefs(eventQ));
|
---|
590 |
|
---|
591 | if (NS_SUCCEEDED(rc) || rc == NS_ERROR_NOT_AVAILABLE)
|
---|
592 | {
|
---|
593 | /* NS_ERROR_NOT_AVAILABLE seems to mean that
|
---|
594 | * nsIEventQueue::StopAcceptingEvents() has been called (see
|
---|
595 | * nsEventQueueService.cpp). We hope that this error code always means
|
---|
596 | * just that in this case and assume that we're on the main thread
|
---|
597 | * (it's a kind of unexpected behavior if a non-main thread ever calls
|
---|
598 | * StopAcceptingEvents() on the main event queue). */
|
---|
599 |
|
---|
600 | PRBool isOnMainThread = PR_FALSE;
|
---|
601 | if (NS_SUCCEEDED(rc))
|
---|
602 | {
|
---|
603 | rc = eventQ->IsOnCurrentThread(&isOnMainThread);
|
---|
604 | eventQ = nsnull; /* early release before shutdown */
|
---|
605 | }
|
---|
606 | else
|
---|
607 | {
|
---|
608 | isOnMainThread = RTThreadIsMain(RTThreadSelf());
|
---|
609 | rc = NS_OK;
|
---|
610 | }
|
---|
611 |
|
---|
612 | if (NS_SUCCEEDED(rc) && isOnMainThread)
|
---|
613 | {
|
---|
614 | /* only the main thread needs to uninitialize XPCOM and only if
|
---|
615 | * init counter drops to zero */
|
---|
616 | if (--gXPCOMInitCount == 0)
|
---|
617 | {
|
---|
618 | NativeEventQueue::uninit();
|
---|
619 | rc = NS_ShutdownXPCOM(nsnull);
|
---|
620 |
|
---|
621 | /* This is a thread initialized XPCOM and set gIsXPCOMInitialized to
|
---|
622 | * true. Reset it back to false. */
|
---|
623 | bool wasInited = ASMAtomicXchgBool(&gIsXPCOMInitialized, false);
|
---|
624 | Assert(wasInited == true);
|
---|
625 | NOREF(wasInited);
|
---|
626 | }
|
---|
627 | }
|
---|
628 | }
|
---|
629 |
|
---|
630 | #endif /* !defined(VBOX_WITH_XPCOM) */
|
---|
631 |
|
---|
632 | AssertComRC(rc);
|
---|
633 |
|
---|
634 | return rc;
|
---|
635 | }
|
---|
636 |
|
---|
637 | } /* namespace com */
|
---|