1 | /*
|
---|
2 | * vboxweb.h:
|
---|
3 | * header file for "real" web server code.
|
---|
4 | *
|
---|
5 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
6 | *
|
---|
7 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
8 | * available from http://www.virtualbox.org. This file is free software;
|
---|
9 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
10 | * General Public License (GPL) as published by the Free Software
|
---|
11 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
12 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
13 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
14 | *
|
---|
15 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
16 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
17 | * additional information or have any questions.
|
---|
18 | */
|
---|
19 |
|
---|
20 | /****************************************************************************
|
---|
21 | *
|
---|
22 | * debug macro
|
---|
23 | *
|
---|
24 | ****************************************************************************/
|
---|
25 |
|
---|
26 | void WebLog(const char *pszFormat, ...);
|
---|
27 |
|
---|
28 | #define WEBDEBUG(a) if (g_fVerbose) { WebLog a; }
|
---|
29 |
|
---|
30 | /****************************************************************************
|
---|
31 | *
|
---|
32 | * global variables
|
---|
33 | *
|
---|
34 | ****************************************************************************/
|
---|
35 |
|
---|
36 | extern ComPtr<IVirtualBox> g_pVirtualBox;
|
---|
37 | extern bool g_fVerbose;
|
---|
38 |
|
---|
39 | extern PRTSTREAM g_pstrLog;
|
---|
40 |
|
---|
41 | /****************************************************************************
|
---|
42 | *
|
---|
43 | * typedefs
|
---|
44 | *
|
---|
45 | ****************************************************************************/
|
---|
46 |
|
---|
47 | // type used by gSOAP-generated code
|
---|
48 | typedef std::string WSDLT_ID; // combined managed object ref (session ID plus object ID)
|
---|
49 | typedef std::string vbox__uuid;
|
---|
50 |
|
---|
51 | /****************************************************************************
|
---|
52 | *
|
---|
53 | * SOAP exceptions
|
---|
54 | *
|
---|
55 | ****************************************************************************/
|
---|
56 |
|
---|
57 | void RaiseSoapInvalidObjectFault(struct soap *soap, WSDLT_ID obj);
|
---|
58 |
|
---|
59 | void RaiseSoapRuntimeFault(struct soap *soap, HRESULT apirc, IUnknown *pObj);
|
---|
60 |
|
---|
61 | /****************************************************************************
|
---|
62 | *
|
---|
63 | * conversion helpers
|
---|
64 | *
|
---|
65 | ****************************************************************************/
|
---|
66 |
|
---|
67 | std::string ConvertComString(const com::Bstr &bstr);
|
---|
68 |
|
---|
69 | std::string ConvertComString(const com::Guid &bstr);
|
---|
70 |
|
---|
71 | /****************************************************************************
|
---|
72 | *
|
---|
73 | * managed object reference classes
|
---|
74 | *
|
---|
75 | ****************************************************************************/
|
---|
76 |
|
---|
77 | class WebServiceSessionPrivate;
|
---|
78 | class ManagedObjectRef;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * An instance of this gets created for every client that logs onto the
|
---|
82 | * webservice (via the special IWebsessionManager::logon() SOAP API) and
|
---|
83 | * maintains the managed object references for that session.
|
---|
84 | */
|
---|
85 | class WebServiceSession
|
---|
86 | {
|
---|
87 | friend class ManagedObjectRef;
|
---|
88 |
|
---|
89 | private:
|
---|
90 | uint64_t _uSessionID;
|
---|
91 | WebServiceSessionPrivate *_pp; // opaque data struct (defined in vboxweb.cpp)
|
---|
92 | bool _fDestructing;
|
---|
93 |
|
---|
94 | ManagedObjectRef *_pISession;
|
---|
95 |
|
---|
96 | time_t _tLastObjectLookup;
|
---|
97 |
|
---|
98 | // hide the copy constructor because we're not copyable
|
---|
99 | WebServiceSession(const WebServiceSession ©From);
|
---|
100 |
|
---|
101 | public:
|
---|
102 | WebServiceSession();
|
---|
103 |
|
---|
104 | ~WebServiceSession();
|
---|
105 |
|
---|
106 | int authenticate(const char *pcszUsername,
|
---|
107 | const char *pcszPassword);
|
---|
108 |
|
---|
109 | ManagedObjectRef* findRefFromPtr(const ComPtr<IUnknown> &pcu);
|
---|
110 |
|
---|
111 | uint64_t getID() const
|
---|
112 | {
|
---|
113 | return _uSessionID;
|
---|
114 | }
|
---|
115 |
|
---|
116 | WSDLT_ID getSessionObject() const;
|
---|
117 |
|
---|
118 | void touch();
|
---|
119 |
|
---|
120 | time_t getLastObjectLookup() const
|
---|
121 | {
|
---|
122 | return _tLastObjectLookup;
|
---|
123 | }
|
---|
124 |
|
---|
125 | static WebServiceSession* findSessionFromRef(const WSDLT_ID &id);
|
---|
126 |
|
---|
127 | void DumpRefs();
|
---|
128 | };
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * ManagedObjectRef is used to map COM pointers to object IDs
|
---|
132 | * within a session. Such object IDs are 64-bit integers.
|
---|
133 | *
|
---|
134 | * When a webservice method call is invoked on an object, it
|
---|
135 | * has an opaque string called a "managed object reference". Such
|
---|
136 | * a string consists of a session ID combined with an object ID.
|
---|
137 | *
|
---|
138 | */
|
---|
139 | class ManagedObjectRef
|
---|
140 | {
|
---|
141 | protected:
|
---|
142 | // owning session:
|
---|
143 | WebServiceSession &_session;
|
---|
144 |
|
---|
145 | // value:
|
---|
146 | ComPtr<IUnknown> _pObj;
|
---|
147 | const char *_pcszInterface;
|
---|
148 |
|
---|
149 | // keys:
|
---|
150 | uint64_t _id;
|
---|
151 | uintptr_t _ulp;
|
---|
152 |
|
---|
153 | // long ID as string
|
---|
154 | WSDLT_ID _strID;
|
---|
155 |
|
---|
156 | public:
|
---|
157 | ManagedObjectRef(WebServiceSession &session,
|
---|
158 | const char *pcszInterface,
|
---|
159 | const ComPtr<IUnknown> &obj);
|
---|
160 | ~ManagedObjectRef();
|
---|
161 |
|
---|
162 | uint64_t getID()
|
---|
163 | {
|
---|
164 | return _id;
|
---|
165 | }
|
---|
166 |
|
---|
167 | ComPtr<IUnknown> getComPtr()
|
---|
168 | {
|
---|
169 | return _pObj;
|
---|
170 | }
|
---|
171 |
|
---|
172 | WSDLT_ID toWSDL() const;
|
---|
173 | const char* getInterfaceName() const
|
---|
174 | {
|
---|
175 | return _pcszInterface;
|
---|
176 | }
|
---|
177 |
|
---|
178 | static int findRefFromId(const WSDLT_ID &id,
|
---|
179 | ManagedObjectRef **pRef,
|
---|
180 | bool fNullAllowed);
|
---|
181 |
|
---|
182 | static ManagedObjectRef* findFromPtr(ComPtr<IUnknown> pcu);
|
---|
183 | static ManagedObjectRef* create(const WSDLT_ID &idParent,
|
---|
184 | ComPtr<IUnknown> pcu);
|
---|
185 |
|
---|
186 | };
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Template function that resolves a managed object reference to a COM pointer
|
---|
190 | * of the template class T. Gets called from tons of generated code in
|
---|
191 | * methodmaps.cpp.
|
---|
192 | *
|
---|
193 | * This is a template function so that we can support ComPtr's for arbitrary
|
---|
194 | * interfaces and automatically verify that the managed object reference on
|
---|
195 | * the internal stack actually is of the expected interface.
|
---|
196 | *
|
---|
197 | * @param soap
|
---|
198 | * @param id in: integer managed object reference, as passed in by web service client
|
---|
199 | * @param pComPtr out: reference to COM pointer object that receives the com pointer,
|
---|
200 | * if SOAP_OK is returned
|
---|
201 | * @param fNullAllowed in: if true, then this func returns a NULL COM pointer if an
|
---|
202 | * empty MOR is passed in (i.e. NULL pointers are allowed). If false,
|
---|
203 | * then this fails; this will be false when called for the "this"
|
---|
204 | * argument of method calls, which really shouldn't be NULL.
|
---|
205 | * @return error code or SOAP_OK if no error
|
---|
206 | */
|
---|
207 | template <class T>
|
---|
208 | int findComPtrFromId(struct soap *soap,
|
---|
209 | const WSDLT_ID &id,
|
---|
210 | ComPtr<T> &pComPtr,
|
---|
211 | bool fNullAllowed)
|
---|
212 | {
|
---|
213 | int rc;
|
---|
214 | ManagedObjectRef *pRef;
|
---|
215 | if ((rc = ManagedObjectRef::findRefFromId(id, &pRef, fNullAllowed)))
|
---|
216 | RaiseSoapInvalidObjectFault(soap, id);
|
---|
217 | else
|
---|
218 | {
|
---|
219 | if (fNullAllowed && pRef == NULL)
|
---|
220 | {
|
---|
221 | pComPtr.setNull();
|
---|
222 | return 0;
|
---|
223 | }
|
---|
224 |
|
---|
225 | // pRef->getComPtr returns a ComPtr<IUnknown>; by casting it to
|
---|
226 | // ComPtr<T>, we implicitly do a COM queryInterface() call
|
---|
227 | if (pComPtr = pRef->getComPtr())
|
---|
228 | return 0;
|
---|
229 |
|
---|
230 | WEBDEBUG((" Interface not supported for object reference %s, which is of class %s\n", id.c_str(), pRef->getInterfaceName()));
|
---|
231 | rc = VERR_WEB_UNSUPPORTED_INTERFACE;
|
---|
232 | RaiseSoapInvalidObjectFault(soap, id); // @todo better message
|
---|
233 | }
|
---|
234 |
|
---|
235 | return rc;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Template function that creates a new managed object for the given COM
|
---|
240 | * pointer of the template class T. If a reference already exists for the
|
---|
241 | * given pointer, then that reference's ID is returned instead.
|
---|
242 | *
|
---|
243 | * @param idParent managed object reference of calling object; used to extract session ID
|
---|
244 | * @param pc COM object for which to create a reference
|
---|
245 | * @return existing or new managed object reference
|
---|
246 | */
|
---|
247 | template <class T>
|
---|
248 | WSDLT_ID createOrFindRefFromComPtr(const WSDLT_ID &idParent,
|
---|
249 | const char *pcszInterface,
|
---|
250 | const ComPtr<T> &pc)
|
---|
251 | {
|
---|
252 | // NULL comptr should return NULL MOR
|
---|
253 | if (pc.isNull())
|
---|
254 | {
|
---|
255 | WEBDEBUG((" createOrFindRefFromComPtr(): returning empty MOR for NULL COM pointer\n"));
|
---|
256 | return "";
|
---|
257 | }
|
---|
258 |
|
---|
259 | WebServiceSession *pSession;
|
---|
260 | if ((pSession = WebServiceSession::findSessionFromRef(idParent)))
|
---|
261 | {
|
---|
262 | // WEBDEBUG(("\n-- found session for %s\n", idParent.c_str()));
|
---|
263 | ManagedObjectRef *pRef;
|
---|
264 | if ( ((pRef = pSession->findRefFromPtr(pc)))
|
---|
265 | || ((pRef = new ManagedObjectRef(*pSession, pcszInterface, pc)))
|
---|
266 | )
|
---|
267 | return pRef->toWSDL();
|
---|
268 | }
|
---|
269 |
|
---|
270 | return 0;
|
---|
271 | }
|
---|
272 |
|
---|