1 | /* $Id: VBoxXPCOMC.cpp 17837 2009-03-13 15:28:59Z 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_2.h"
|
---|
32 | #include "VBox/com/com.h"
|
---|
33 | #include "VBox/version.h"
|
---|
34 |
|
---|
35 | using namespace std;
|
---|
36 |
|
---|
37 | static ISession *Session;
|
---|
38 | static IVirtualBox *Ivirtualbox;
|
---|
39 | static nsIServiceManager *serviceManager;
|
---|
40 | static nsIComponentManager *manager;
|
---|
41 |
|
---|
42 | void VBoxComUninitialize(void);
|
---|
43 |
|
---|
44 | int
|
---|
45 | VBoxUtf16ToUtf8(const PRUnichar *pwszString, char **ppszString)
|
---|
46 | {
|
---|
47 | return RTUtf16ToUtf8(pwszString, ppszString);
|
---|
48 | }
|
---|
49 |
|
---|
50 | int
|
---|
51 | VBoxUtf8ToUtf16(const char *pszString, PRUnichar **ppwszString)
|
---|
52 | {
|
---|
53 | return RTStrToUtf16(pszString, ppwszString);
|
---|
54 | }
|
---|
55 |
|
---|
56 | void
|
---|
57 | VBoxUtf16Free(PRUnichar *pwszString)
|
---|
58 | {
|
---|
59 | RTUtf16Free(pwszString);
|
---|
60 | }
|
---|
61 |
|
---|
62 | void
|
---|
63 | VBoxUtf8Free(char *pszString)
|
---|
64 | {
|
---|
65 | RTStrFree(pszString);
|
---|
66 | }
|
---|
67 |
|
---|
68 | void
|
---|
69 | VBoxComUnallocMem(void *ptr)
|
---|
70 | {
|
---|
71 | if (ptr)
|
---|
72 | {
|
---|
73 | nsMemory::Free(ptr);
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | void
|
---|
78 | VBoxComInitialize(IVirtualBox **virtualBox, ISession **session)
|
---|
79 | {
|
---|
80 | nsresult rc;
|
---|
81 |
|
---|
82 | *session = NULL;
|
---|
83 | *virtualBox = NULL;
|
---|
84 |
|
---|
85 | Session = *session;
|
---|
86 | Ivirtualbox = *virtualBox;
|
---|
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 = manager->CreateInstanceByContractID(NS_VIRTUALBOX_CONTRACTID,
|
---|
105 | nsnull,
|
---|
106 | NS_GET_IID(IVirtualBox),
|
---|
107 | (void **)virtualBox);
|
---|
108 | if (NS_FAILED(rc))
|
---|
109 | {
|
---|
110 | Log(("Cbinding: Could not instantiate VirtualBox object! rc=%Rhrc\n",rc));
|
---|
111 | VBoxComUninitialize();
|
---|
112 | return;
|
---|
113 | }
|
---|
114 |
|
---|
115 | Log(("Cbinding: IVirtualBox object created.\n"));
|
---|
116 |
|
---|
117 | rc = manager->CreateInstanceByContractID (NS_SESSION_CONTRACTID,
|
---|
118 | nsnull,
|
---|
119 | NS_GET_IID(ISession),
|
---|
120 | (void **)session);
|
---|
121 | if (NS_FAILED(rc))
|
---|
122 | {
|
---|
123 | Log(("Cbinding: Could not instantiate Session object! rc=%Rhrc\n",rc));
|
---|
124 | VBoxComUninitialize();
|
---|
125 | return;
|
---|
126 | }
|
---|
127 |
|
---|
128 | Log(("Cbinding: ISession object created.\n"));
|
---|
129 | }
|
---|
130 |
|
---|
131 | void
|
---|
132 | VBoxComUninitialize(void)
|
---|
133 | {
|
---|
134 | if (Session)
|
---|
135 | NS_RELEASE(Session); // decrement refcount
|
---|
136 | if (Ivirtualbox)
|
---|
137 | NS_RELEASE(Ivirtualbox); // decrement refcount
|
---|
138 | if (manager)
|
---|
139 | NS_RELEASE(manager); // decrement refcount
|
---|
140 | if (serviceManager)
|
---|
141 | NS_RELEASE(serviceManager); // decrement refcount
|
---|
142 | com::Shutdown();
|
---|
143 | Log(("Cbinding: Cleaned up the created IVirtualBox and ISession Objects.\n"));
|
---|
144 | }
|
---|
145 |
|
---|
146 | uint32_t
|
---|
147 | VBoxVersion(void)
|
---|
148 | {
|
---|
149 | uint32_t version = 0;
|
---|
150 |
|
---|
151 | version = (VBOX_VERSION_MAJOR * 1000 * 1000) + (VBOX_VERSION_MINOR * 1000) + (VBOX_VERSION_BUILD);
|
---|
152 |
|
---|
153 | return version;
|
---|
154 | }
|
---|
155 |
|
---|
156 | VBOXXPCOMC_DECL(PCVBOXXPCOM)
|
---|
157 | VBoxGetXPCOMCFunctions(unsigned uVersion)
|
---|
158 | {
|
---|
159 | /* The current version. */
|
---|
160 | static const VBOXXPCOMC s_Functions =
|
---|
161 | {
|
---|
162 | sizeof(VBOXXPCOMC),
|
---|
163 | VBOX_XPCOMC_VERSION,
|
---|
164 |
|
---|
165 | VBoxVersion,
|
---|
166 |
|
---|
167 | VBoxComInitialize,
|
---|
168 | VBoxComUninitialize,
|
---|
169 |
|
---|
170 | VBoxComUnallocMem,
|
---|
171 | VBoxUtf16Free,
|
---|
172 | VBoxUtf8Free,
|
---|
173 |
|
---|
174 | VBoxUtf16ToUtf8,
|
---|
175 | VBoxUtf8ToUtf16,
|
---|
176 |
|
---|
177 | VBOX_XPCOMC_VERSION
|
---|
178 | };
|
---|
179 |
|
---|
180 | if ((uVersion & 0xffff0000U) != VBOX_XPCOMC_VERSION)
|
---|
181 | return NULL; /* not supported. */
|
---|
182 |
|
---|
183 | return &s_Functions;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /* vim: set ts=4 sw=4 et: */
|
---|