1 | /* $Id: VBoxXPCOMC.cpp 16519 2009-02-04 17:03:19Z vboxsync $ */
|
---|
2 | /** @file VBoxXPCOMC.cpp
|
---|
3 | * Utility functions to use with the C binding for XPCOM.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include <iostream>
|
---|
23 | #include <iomanip>
|
---|
24 |
|
---|
25 | #include <nsMemory.h>
|
---|
26 | #include <nsIServiceManager.h>
|
---|
27 | #include <nsEventQueueUtils.h>
|
---|
28 |
|
---|
29 | #include <iprt/string.h>
|
---|
30 | #define LOG_GROUP_MAIN
|
---|
31 | #include <iprt/log.h>
|
---|
32 |
|
---|
33 | #include "VirtualBox_XPCOM.h"
|
---|
34 | #include "cbinding.h"
|
---|
35 |
|
---|
36 | using namespace std;
|
---|
37 |
|
---|
38 | static ISession *Session;
|
---|
39 | static IVirtualBox *Ivirtualbox;
|
---|
40 | static nsIServiceManager *serviceManager;
|
---|
41 | static nsIComponentManager *manager;
|
---|
42 |
|
---|
43 | VBOXXPCOMC_DECL(int)
|
---|
44 | VBoxUtf16ToUtf8(const PRUnichar *pwszString, char **ppszString)
|
---|
45 | {
|
---|
46 | return RTUtf16ToUtf8(pwszString, ppszString);
|
---|
47 | }
|
---|
48 |
|
---|
49 | VBOXXPCOMC_DECL(int)
|
---|
50 | VBoxUtf8ToUtf16(const char *pszString, PRUnichar **ppwszString)
|
---|
51 | {
|
---|
52 | return RTStrToUtf16(pszString, ppwszString);
|
---|
53 | }
|
---|
54 |
|
---|
55 | VBOXXPCOMC_DECL(void)
|
---|
56 | VBoxUtf16Free(PRUnichar *pwszString)
|
---|
57 | {
|
---|
58 | RTUtf16Free(pwszString);
|
---|
59 | }
|
---|
60 |
|
---|
61 | VBOXXPCOMC_DECL(void)
|
---|
62 | VBoxUtf8Free(char *pszString)
|
---|
63 | {
|
---|
64 | RTStrFree(pszString);
|
---|
65 | }
|
---|
66 |
|
---|
67 | VBOXXPCOMC_DECL(void)
|
---|
68 | VBoxComUnallocMem(void *ptr)
|
---|
69 | {
|
---|
70 | if (ptr)
|
---|
71 | {
|
---|
72 | nsMemory::Free(ptr);
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | VBOXXPCOMC_DECL(void)
|
---|
77 | VBoxComInitialize(IVirtualBox **virtualBox, ISession **session)
|
---|
78 | {
|
---|
79 | nsresult rc;
|
---|
80 |
|
---|
81 | *session = NULL;
|
---|
82 | *virtualBox = NULL;
|
---|
83 |
|
---|
84 | Session = *session;
|
---|
85 | Ivirtualbox = *virtualBox;
|
---|
86 |
|
---|
87 | /** @todo r=bird: Why is cout/cerr used unconditionally here?
|
---|
88 | * It would be preferred to use RTPrintf/RTStrmPrintf(g_pStdErr,..). If this is
|
---|
89 | * going to be used in real life, the cout(/RTPrintf) bits should be optional,
|
---|
90 | * add a flag argument and define VBOXCOMINIT_FLAG_VERBOSE for the purpose. */
|
---|
91 |
|
---|
92 | rc = NS_InitXPCOM2(&serviceManager, nsnull, nsnull);
|
---|
93 | if (NS_FAILED(rc))
|
---|
94 | {
|
---|
95 | Log(("Cbinding: XPCOM could not be initialized! rc=%Rhrc\n",rc));
|
---|
96 | VBoxComUninitialize();
|
---|
97 | return;
|
---|
98 | }
|
---|
99 |
|
---|
100 | rc = NS_GetComponentManager (&manager);
|
---|
101 | if (NS_FAILED(rc))
|
---|
102 | {
|
---|
103 | Log(("Cbinding: Could not get component manager! rc=%Rhrc\n",rc));
|
---|
104 | VBoxComUninitialize();
|
---|
105 | return;
|
---|
106 | }
|
---|
107 |
|
---|
108 | rc = manager->CreateInstanceByContractID(NS_VIRTUALBOX_CONTRACTID,
|
---|
109 | nsnull,
|
---|
110 | NS_GET_IID(IVirtualBox),
|
---|
111 | (void **)virtualBox);
|
---|
112 | if (NS_FAILED(rc))
|
---|
113 | {
|
---|
114 | Log(("Cbinding: Could not instantiate VirtualBox object! rc=%Rhrc\n",rc));
|
---|
115 | VBoxComUninitialize();
|
---|
116 | return;
|
---|
117 | }
|
---|
118 |
|
---|
119 | Log(("Cbinding: IVirtualBox object created.\n"));
|
---|
120 |
|
---|
121 | rc = manager->CreateInstanceByContractID (NS_SESSION_CONTRACTID,
|
---|
122 | nsnull,
|
---|
123 | NS_GET_IID(ISession),
|
---|
124 | (void **)session);
|
---|
125 | if (NS_FAILED(rc))
|
---|
126 | {
|
---|
127 | Log(("Cbinding: Could not instantiate Session object! rc=%Rhrc\n",rc));
|
---|
128 | VBoxComUninitialize();
|
---|
129 | return;
|
---|
130 | }
|
---|
131 |
|
---|
132 | Log(("Cbinding: ISession object created.\n"));
|
---|
133 | }
|
---|
134 |
|
---|
135 | VBOXXPCOMC_DECL(void)
|
---|
136 | VBoxComUninitialize(void)
|
---|
137 | {
|
---|
138 | if (Session)
|
---|
139 | NS_RELEASE(Session); // decrement refcount
|
---|
140 | if (Ivirtualbox)
|
---|
141 | NS_RELEASE(Ivirtualbox); // decrement refcount
|
---|
142 | if (manager)
|
---|
143 | NS_RELEASE(manager); // decrement refcount
|
---|
144 | if (serviceManager)
|
---|
145 | NS_RELEASE(serviceManager); // decrement refcount
|
---|
146 | NS_ShutdownXPCOM(nsnull);
|
---|
147 | Log(("Cbinding: Cleaned up the created IVirtualBox and ISession Objects.\n"));
|
---|
148 | }
|
---|
149 |
|
---|
150 | /* vim: set ts=4 sw=4 et: */
|
---|