VirtualBox

source: vbox/trunk/src/VBox/Main/cbinding/VBoxXPCOMC.cpp@ 39895

Last change on this file since 39895 was 39895, checked in by vboxsync, 13 years ago

Main/cbinding: more fixes, decrease necessary manual work

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