1 | /* $Id: VBoxXPCOMC.cpp 19081 2009-04-21 14:17:41Z 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 | #define LOG_GROUP LOG_GROUP_MAIN
|
---|
23 | #include <nsMemory.h>
|
---|
24 | #include <nsIServiceManager.h>
|
---|
25 | #include <nsEventQueueUtils.h>
|
---|
26 |
|
---|
27 | #include <iprt/string.h>
|
---|
28 | #include <iprt/env.h>
|
---|
29 | #include <VBox/log.h>
|
---|
30 |
|
---|
31 | #include "VBoxCAPI_v2_5.h"
|
---|
32 | #include "VBox/com/com.h"
|
---|
33 | #include "VBox/version.h"
|
---|
34 |
|
---|
35 | using namespace std;
|
---|
36 |
|
---|
37 | static ISession *Session = NULL;
|
---|
38 | static IVirtualBox *Ivirtualbox = NULL;
|
---|
39 | static nsIServiceManager *serviceManager = NULL;
|
---|
40 | static nsIComponentManager *manager = NULL;
|
---|
41 | static nsIEventQueue *eventQ = NULL;
|
---|
42 |
|
---|
43 | static void VBoxComUninitialize(void);
|
---|
44 |
|
---|
45 | static int
|
---|
46 | VBoxUtf16ToUtf8(const PRUnichar *pwszString, char **ppszString)
|
---|
47 | {
|
---|
48 | return RTUtf16ToUtf8(pwszString, ppszString);
|
---|
49 | }
|
---|
50 |
|
---|
51 | static int
|
---|
52 | VBoxUtf8ToUtf16(const char *pszString, PRUnichar **ppwszString)
|
---|
53 | {
|
---|
54 | return RTStrToUtf16(pszString, ppwszString);
|
---|
55 | }
|
---|
56 |
|
---|
57 | static void
|
---|
58 | VBoxUtf16Free(PRUnichar *pwszString)
|
---|
59 | {
|
---|
60 | RTUtf16Free(pwszString);
|
---|
61 | }
|
---|
62 |
|
---|
63 | static void
|
---|
64 | VBoxUtf8Free(char *pszString)
|
---|
65 | {
|
---|
66 | RTStrFree(pszString);
|
---|
67 | }
|
---|
68 |
|
---|
69 | static void
|
---|
70 | VBoxComUnallocMem(void *ptr)
|
---|
71 | {
|
---|
72 | if (ptr)
|
---|
73 | {
|
---|
74 | nsMemory::Free(ptr);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | static void
|
---|
79 | VBoxComInitialize(const char *pszVirtualBoxIID, IVirtualBox **ppVirtualBox,
|
---|
80 | const char *pszSessionIID, ISession **ppSession)
|
---|
81 | {
|
---|
82 | nsresult rc;
|
---|
83 |
|
---|
84 | *ppSession = NULL;
|
---|
85 | *ppVirtualBox = NULL;
|
---|
86 |
|
---|
87 | /** @todo r=bird: what exactly is this supposed to acomplish? */
|
---|
88 | Session = *ppSession;
|
---|
89 | Ivirtualbox = *ppVirtualBox;
|
---|
90 |
|
---|
91 | rc = com::Initialize();
|
---|
92 | if (NS_FAILED(rc))
|
---|
93 | {
|
---|
94 | Log(("Cbinding: XPCOM could not be initialized! rc=%Rhrc\n", rc));
|
---|
95 | VBoxComUninitialize();
|
---|
96 | return;
|
---|
97 | }
|
---|
98 |
|
---|
99 | rc = NS_GetComponentManager (&manager);
|
---|
100 | if (NS_FAILED(rc))
|
---|
101 | {
|
---|
102 | Log(("Cbinding: Could not get component manager! rc=%Rhrc\n", rc));
|
---|
103 | VBoxComUninitialize();
|
---|
104 | return;
|
---|
105 | }
|
---|
106 |
|
---|
107 | rc = NS_GetMainEventQ (&eventQ);
|
---|
108 | if (NS_FAILED(rc))
|
---|
109 | {
|
---|
110 | Log(("Cbinding: Could not get xpcom event queue! rc=%Rhrc\n", rc));
|
---|
111 | VBoxComUninitialize();
|
---|
112 | return;
|
---|
113 | }
|
---|
114 |
|
---|
115 | rc = manager->CreateInstanceByContractID(NS_VIRTUALBOX_CONTRACTID,
|
---|
116 | nsnull,
|
---|
117 | NS_GET_IID(IVirtualBox), /** @todo Use pszVirtualBoxIID here! */
|
---|
118 | (void **)ppVirtualBox);
|
---|
119 | if (NS_FAILED(rc))
|
---|
120 | {
|
---|
121 | Log(("Cbinding: Could not instantiate VirtualBox object! rc=%Rhrc\n",rc));
|
---|
122 | VBoxComUninitialize();
|
---|
123 | return;
|
---|
124 | }
|
---|
125 |
|
---|
126 | Log(("Cbinding: IVirtualBox object created.\n"));
|
---|
127 |
|
---|
128 | rc = manager->CreateInstanceByContractID (NS_SESSION_CONTRACTID,
|
---|
129 | nsnull,
|
---|
130 | NS_GET_IID(ISession), /** @todo Use */
|
---|
131 | (void **)ppSession);
|
---|
132 | if (NS_FAILED(rc))
|
---|
133 | {
|
---|
134 | Log(("Cbinding: Could not instantiate Session object! rc=%Rhrc\n",rc));
|
---|
135 | VBoxComUninitialize();
|
---|
136 | return;
|
---|
137 | }
|
---|
138 |
|
---|
139 | Log(("Cbinding: ISession object created.\n"));
|
---|
140 | }
|
---|
141 |
|
---|
142 | static void
|
---|
143 | VBoxComInitializeV1(IVirtualBox **ppVirtualBox, ISession **ppSession)
|
---|
144 | {
|
---|
145 | /* stub that always fails. */
|
---|
146 | *ppVirtualBox = NULL;
|
---|
147 | *ppSession = NULL;
|
---|
148 | }
|
---|
149 |
|
---|
150 | static void
|
---|
151 | VBoxComUninitialize(void)
|
---|
152 | {
|
---|
153 | if (Session)
|
---|
154 | NS_RELEASE(Session); // decrement refcount
|
---|
155 | if (Ivirtualbox)
|
---|
156 | NS_RELEASE(Ivirtualbox); // decrement refcount
|
---|
157 | if (eventQ)
|
---|
158 | NS_RELEASE(eventQ); // decrement refcount
|
---|
159 | if (manager)
|
---|
160 | NS_RELEASE(manager); // decrement refcount
|
---|
161 | if (serviceManager)
|
---|
162 | NS_RELEASE(serviceManager); // decrement refcount
|
---|
163 | com::Shutdown();
|
---|
164 | Log(("Cbinding: Cleaned up the created IVirtualBox and ISession Objects.\n"));
|
---|
165 | }
|
---|
166 |
|
---|
167 | static void
|
---|
168 | VBoxGetEventQueue(nsIEventQueue **eventQueue)
|
---|
169 | {
|
---|
170 | *eventQueue = eventQ;
|
---|
171 | }
|
---|
172 |
|
---|
173 | static uint32_t
|
---|
174 | VBoxVersion(void)
|
---|
175 | {
|
---|
176 | uint32_t version = 0;
|
---|
177 |
|
---|
178 | version = (VBOX_VERSION_MAJOR * 1000 * 1000) + (VBOX_VERSION_MINOR * 1000) + (VBOX_VERSION_BUILD);
|
---|
179 |
|
---|
180 | return version;
|
---|
181 | }
|
---|
182 |
|
---|
183 | VBOXXPCOMC_DECL(PCVBOXXPCOM)
|
---|
184 | VBoxGetXPCOMCFunctions(unsigned uVersion)
|
---|
185 | {
|
---|
186 | /*
|
---|
187 | * The current interface version.
|
---|
188 | */
|
---|
189 | static const VBOXXPCOMC s_Functions =
|
---|
190 | {
|
---|
191 | sizeof(VBOXXPCOMC),
|
---|
192 | VBOX_XPCOMC_VERSION,
|
---|
193 |
|
---|
194 | VBoxVersion,
|
---|
195 |
|
---|
196 | VBoxComInitialize,
|
---|
197 | VBoxComUninitialize,
|
---|
198 |
|
---|
199 | VBoxComUnallocMem,
|
---|
200 | VBoxUtf16Free,
|
---|
201 | VBoxUtf8Free,
|
---|
202 |
|
---|
203 | VBoxUtf16ToUtf8,
|
---|
204 | VBoxUtf8ToUtf16,
|
---|
205 |
|
---|
206 | VBoxGetEventQueue,
|
---|
207 |
|
---|
208 | VBOX_XPCOMC_VERSION
|
---|
209 | };
|
---|
210 |
|
---|
211 | if ((uVersion & 0xffff0000U) == (VBOX_XPCOMC_VERSION & 0xffff0000U))
|
---|
212 | return &s_Functions;
|
---|
213 |
|
---|
214 | /*
|
---|
215 | * Legacy interface version 1.0.
|
---|
216 | */
|
---|
217 | static const struct VBOXXPCOMCV1
|
---|
218 | {
|
---|
219 | /** The size of the structure. */
|
---|
220 | unsigned cb;
|
---|
221 | /** The structure version. */
|
---|
222 | unsigned uVersion;
|
---|
223 |
|
---|
224 | unsigned int (*pfnGetVersion)(void);
|
---|
225 |
|
---|
226 | void (*pfnComInitialize)(IVirtualBox **virtualBox, ISession **session);
|
---|
227 | void (*pfnComUninitialize)(void);
|
---|
228 |
|
---|
229 | void (*pfnComUnallocMem)(void *pv);
|
---|
230 | void (*pfnUtf16Free)(PRUnichar *pwszString);
|
---|
231 | void (*pfnUtf8Free)(char *pszString);
|
---|
232 |
|
---|
233 | int (*pfnUtf16ToUtf8)(const PRUnichar *pwszString, char **ppszString);
|
---|
234 | int (*pfnUtf8ToUtf16)(const char *pszString, PRUnichar **ppwszString);
|
---|
235 |
|
---|
236 | /** Tail version, same as uVersion. */
|
---|
237 | unsigned uEndVersion;
|
---|
238 | } s_Functions_v1_0 =
|
---|
239 | {
|
---|
240 | sizeof(s_Functions_v1_0),
|
---|
241 | 0x00010000U,
|
---|
242 |
|
---|
243 | VBoxVersion,
|
---|
244 |
|
---|
245 | VBoxComInitializeV1,
|
---|
246 | VBoxComUninitialize,
|
---|
247 |
|
---|
248 | VBoxComUnallocMem,
|
---|
249 | VBoxUtf16Free,
|
---|
250 | VBoxUtf8Free,
|
---|
251 |
|
---|
252 | VBoxUtf16ToUtf8,
|
---|
253 | VBoxUtf8ToUtf16,
|
---|
254 |
|
---|
255 | 0x00010000U
|
---|
256 | };
|
---|
257 |
|
---|
258 | if ((uVersion & 0xffff0000U) == 0x00010000U)
|
---|
259 | return (PCVBOXXPCOM)&s_Functions_v1_0;
|
---|
260 |
|
---|
261 | /*
|
---|
262 | * Unsupported interface version.
|
---|
263 | */
|
---|
264 | return NULL;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /* vim: set ts=4 sw=4 et: */
|
---|