VirtualBox

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

Last change on this file since 53390 was 50117, checked in by vboxsync, 11 years ago

Main+Frontends: clear out some cruft code, outdated EventQueue stuff and whitespace cleanup

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