1 | /* $Id: VBoxXPCOMC.cpp 16534 2009-02-05 17:30: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 <VBox/log.h>
|
---|
29 |
|
---|
30 | #include "VirtualBox_XPCOM.h"
|
---|
31 | #include "cbinding.h"
|
---|
32 |
|
---|
33 | using namespace std;
|
---|
34 |
|
---|
35 | static ISession *Session;
|
---|
36 | static IVirtualBox *Ivirtualbox;
|
---|
37 | static nsIServiceManager *serviceManager;
|
---|
38 | static nsIComponentManager *manager;
|
---|
39 |
|
---|
40 | VBOXXPCOMC_DECL(int)
|
---|
41 | VBoxUtf16ToUtf8(const PRUnichar *pwszString, char **ppszString)
|
---|
42 | {
|
---|
43 | return RTUtf16ToUtf8(pwszString, ppszString);
|
---|
44 | }
|
---|
45 |
|
---|
46 | VBOXXPCOMC_DECL(int)
|
---|
47 | VBoxUtf8ToUtf16(const char *pszString, PRUnichar **ppwszString)
|
---|
48 | {
|
---|
49 | return RTStrToUtf16(pszString, ppwszString);
|
---|
50 | }
|
---|
51 |
|
---|
52 | VBOXXPCOMC_DECL(void)
|
---|
53 | VBoxUtf16Free(PRUnichar *pwszString)
|
---|
54 | {
|
---|
55 | RTUtf16Free(pwszString);
|
---|
56 | }
|
---|
57 |
|
---|
58 | VBOXXPCOMC_DECL(void)
|
---|
59 | VBoxUtf8Free(char *pszString)
|
---|
60 | {
|
---|
61 | RTStrFree(pszString);
|
---|
62 | }
|
---|
63 |
|
---|
64 | VBOXXPCOMC_DECL(void)
|
---|
65 | VBoxComUnallocMem(void *ptr)
|
---|
66 | {
|
---|
67 | if (ptr)
|
---|
68 | {
|
---|
69 | nsMemory::Free(ptr);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | VBOXXPCOMC_DECL(void)
|
---|
74 | VBoxComInitialize(IVirtualBox **virtualBox, ISession **session)
|
---|
75 | {
|
---|
76 | nsresult rc;
|
---|
77 |
|
---|
78 | *session = NULL;
|
---|
79 | *virtualBox = NULL;
|
---|
80 |
|
---|
81 | Session = *session;
|
---|
82 | Ivirtualbox = *virtualBox;
|
---|
83 |
|
---|
84 | rc = NS_InitXPCOM2(&serviceManager, nsnull, nsnull);
|
---|
85 | if (NS_FAILED(rc))
|
---|
86 | {
|
---|
87 | Log(("Cbinding: XPCOM could not be initialized! rc=%Rhrc\n",rc));
|
---|
88 | VBoxComUninitialize();
|
---|
89 | return;
|
---|
90 | }
|
---|
91 |
|
---|
92 | rc = NS_GetComponentManager (&manager);
|
---|
93 | if (NS_FAILED(rc))
|
---|
94 | {
|
---|
95 | Log(("Cbinding: Could not get component manager! rc=%Rhrc\n",rc));
|
---|
96 | VBoxComUninitialize();
|
---|
97 | return;
|
---|
98 | }
|
---|
99 |
|
---|
100 | rc = manager->CreateInstanceByContractID(NS_VIRTUALBOX_CONTRACTID,
|
---|
101 | nsnull,
|
---|
102 | NS_GET_IID(IVirtualBox),
|
---|
103 | (void **)virtualBox);
|
---|
104 | if (NS_FAILED(rc))
|
---|
105 | {
|
---|
106 | Log(("Cbinding: Could not instantiate VirtualBox object! rc=%Rhrc\n",rc));
|
---|
107 | VBoxComUninitialize();
|
---|
108 | return;
|
---|
109 | }
|
---|
110 |
|
---|
111 | Log(("Cbinding: IVirtualBox object created.\n"));
|
---|
112 |
|
---|
113 | rc = manager->CreateInstanceByContractID (NS_SESSION_CONTRACTID,
|
---|
114 | nsnull,
|
---|
115 | NS_GET_IID(ISession),
|
---|
116 | (void **)session);
|
---|
117 | if (NS_FAILED(rc))
|
---|
118 | {
|
---|
119 | Log(("Cbinding: Could not instantiate Session object! rc=%Rhrc\n",rc));
|
---|
120 | VBoxComUninitialize();
|
---|
121 | return;
|
---|
122 | }
|
---|
123 |
|
---|
124 | Log(("Cbinding: ISession object created.\n"));
|
---|
125 | }
|
---|
126 |
|
---|
127 | VBOXXPCOMC_DECL(void)
|
---|
128 | VBoxComUninitialize(void)
|
---|
129 | {
|
---|
130 | if (Session)
|
---|
131 | NS_RELEASE(Session); // decrement refcount
|
---|
132 | if (Ivirtualbox)
|
---|
133 | NS_RELEASE(Ivirtualbox); // decrement refcount
|
---|
134 | if (manager)
|
---|
135 | NS_RELEASE(manager); // decrement refcount
|
---|
136 | if (serviceManager)
|
---|
137 | NS_RELEASE(serviceManager); // decrement refcount
|
---|
138 | NS_ShutdownXPCOM(nsnull);
|
---|
139 | Log(("Cbinding: Cleaned up the created IVirtualBox and ISession Objects.\n"));
|
---|
140 | }
|
---|
141 |
|
---|
142 | /* vim: set ts=4 sw=4 et: */
|
---|