VirtualBox

source: vbox/trunk/src/VBox/Main/webservice/vboxweb.h@ 22214

Last change on this file since 22214 was 21878, checked in by vboxsync, 15 years ago

Main: coding style: have Main obey the standard VirtualBox coding style rules (no functional changes)

  • Property filesplitter.c set to Makefile.kmk
  • Property svn:eol-style set to native
File size: 8.6 KB
Line 
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
26void WebLog(const char *pszFormat, ...);
27
28// #ifdef DEBUG
29#define WEBDEBUG(a) if (g_fVerbose) { WebLog a; }
30// #else
31// #define WEBDEBUG(a) do { } while(0)
32// #endif
33
34/****************************************************************************
35 *
36 * global variables
37 *
38 ****************************************************************************/
39
40extern ComPtr<IVirtualBox> G_pVirtualBox;
41extern bool g_fVerbose;
42
43extern PRTSTREAM g_pstrLog;
44
45/****************************************************************************
46 *
47 * typedefs
48 *
49 ****************************************************************************/
50
51// type used by gSOAP-generated code
52typedef std::string WSDLT_ID; // combined managed object ref (session ID plus object ID)
53typedef std::string vbox__uuid;
54
55// type used internally by our class
56// typedef WSDLT_ID ManagedObjectID;
57
58// #define VBOXWEB_ERROR_BASE 10000
59// #define VBOXWEB_INVALID_MANAGED_OBJECT (VBOXWEB_ERROR_BASE + 0)
60// #define VBOXWEB_INVALID_MANAGED_OBJECT_TYPE (VBOXWEB_ERROR_BASE + 1)
61
62/****************************************************************************
63 *
64 * SOAP exceptions
65 *
66 ****************************************************************************/
67
68void RaiseSoapInvalidObjectFault(struct soap *soap, WSDLT_ID obj);
69
70void RaiseSoapRuntimeFault(struct soap *soap, HRESULT apirc, IUnknown *pObj);
71
72/****************************************************************************
73 *
74 * conversion helpers
75 *
76 ****************************************************************************/
77
78std::string ConvertComString(const com::Bstr &bstr);
79
80std::string ConvertComString(const com::Guid &bstr);
81
82/****************************************************************************
83 *
84 * managed object reference classes
85 *
86 ****************************************************************************/
87
88class WebServiceSessionPrivate;
89class ManagedObjectRef;
90
91/**
92 *
93 */
94class WebServiceSession
95{
96 friend class ManagedObjectRef;
97
98 private:
99 uint64_t _uSessionID;
100 WebServiceSessionPrivate *_pp;
101 bool _fDestructing;
102
103 ManagedObjectRef *_pISession;
104
105 time_t _tLastObjectLookup;
106
107 // hide the copy constructor because we're not copyable
108 WebServiceSession(const WebServiceSession &copyFrom);
109
110 public:
111 WebServiceSession();
112
113 ~WebServiceSession();
114
115 int authenticate(const char *pcszUsername,
116 const char *pcszPassword);
117
118 ManagedObjectRef* findRefFromPtr(const ComPtr<IUnknown> &pcu);
119
120 uint64_t getID() const
121 {
122 return _uSessionID;
123 }
124
125 WSDLT_ID getSessionObject() const;
126
127 void touch();
128
129 time_t getLastObjectLookup() const
130 {
131 return _tLastObjectLookup;
132 }
133
134 static WebServiceSession* findSessionFromRef(const WSDLT_ID &id);
135
136 void DumpRefs();
137};
138
139/**
140 * ManagedObjectRef is used to map COM pointers to object IDs
141 * within a session. Such object IDs are 64-bit integers.
142 *
143 * When a webservice method call is invoked on an object, it
144 * has an opaque string called a "managed object reference". Such
145 * a string consists of a session ID combined with an object ID.
146 *
147 */
148class ManagedObjectRef
149{
150 protected:
151 // owning session:
152 WebServiceSession &_session;
153
154 // value:
155 ComPtr<IUnknown> _pObj;
156 const char *_pcszInterface;
157
158 // keys:
159 uint64_t _id;
160 uintptr_t _ulp;
161
162 // long ID as string
163 WSDLT_ID _strID;
164
165 public:
166 ManagedObjectRef(WebServiceSession &session,
167 const char *pcszInterface,
168 const ComPtr<IUnknown> &obj);
169 ~ManagedObjectRef();
170
171 uint64_t getID()
172 {
173 return _id;
174 }
175
176 ComPtr<IUnknown> getComPtr()
177 {
178 return _pObj;
179 }
180
181 WSDLT_ID toWSDL() const;
182 const char* getInterfaceName() const
183 {
184 return _pcszInterface;
185 }
186
187 static int findRefFromId(const WSDLT_ID &id,
188 ManagedObjectRef **pRef,
189 bool fNullAllowed);
190
191 static ManagedObjectRef* findFromPtr(ComPtr<IUnknown> pcu);
192 static ManagedObjectRef* create(const WSDLT_ID &idParent,
193 ComPtr<IUnknown> pcu);
194
195};
196
197/**
198 * Template function that resolves a managed object reference to a COM pointer
199 * of the template class T. Gets called from tons of generated code in
200 * methodmaps.cpp.
201 *
202 * This is a template function so that we can support ComPtr's for arbitrary
203 * interfaces and automatically verify that the managed object reference on
204 * the internal stack actually is of the expected interface.
205 *
206 * @param soap
207 * @param id in: integer managed object reference, as passed in by web service client
208 * @param pComPtr out: reference to COM pointer object that receives the com pointer,
209 * if SOAP_OK is returned
210 * @param fNullAllowed in: if true, then this func returns a NULL COM pointer if an
211 * empty MOR is passed in (i.e. NULL pointers are allowed). If false,
212 * then this fails; this will be false when called for the "this"
213 * argument of method calls, which really shouldn't be NULL.
214 * @return error code or SOAP_OK if no error
215 */
216template <class T>
217int findComPtrFromId(struct soap *soap,
218 const WSDLT_ID &id,
219 ComPtr<T> &pComPtr,
220 bool fNullAllowed)
221{
222 int rc;
223 ManagedObjectRef *pRef;
224 if ((rc = ManagedObjectRef::findRefFromId(id, &pRef, fNullAllowed)))
225 RaiseSoapInvalidObjectFault(soap, id);
226 else
227 {
228 if (fNullAllowed && pRef == NULL)
229 {
230 pComPtr.setNull();
231 return 0;
232 }
233
234 // pRef->getComPtr returns a ComPtr<IUnknown>; by casting it to
235 // ComPtr<T>, we implicitly do a queryInterface() call
236 if (pComPtr = pRef->getComPtr())
237 return 0;
238
239 WEBDEBUG((" Interface not supported for object reference %s, which is of class %s\n", id.c_str(), pRef->getInterfaceName()));
240 rc = VERR_WEB_UNSUPPORTED_INTERFACE;
241 RaiseSoapInvalidObjectFault(soap, id); // @todo better message
242 }
243
244 return rc;
245}
246
247/**
248 * Template function that creates a new managed object for the given COM
249 * pointer of the template class T. If a reference already exists for the
250 * given pointer, then that reference's ID is returned instead.
251 *
252 * @param idParent managed object reference of calling object; used to extract session ID
253 * @param pc COM object for which to create a reference
254 * @return existing or new managed object reference
255 */
256template <class T>
257WSDLT_ID createOrFindRefFromComPtr(const WSDLT_ID &idParent,
258 const char *pcszInterface,
259 const ComPtr<T> &pc)
260{
261 // NULL comptr should return NULL MOR
262 if (pc.isNull())
263 {
264 WEBDEBUG((" createOrFindRefFromComPtr(): returning empty MOR for NULL COM pointer\n"));
265 return "";
266 }
267
268 WebServiceSession* pSession;
269 if ((pSession = WebServiceSession::findSessionFromRef(idParent)))
270 {
271 // WEBDEBUG(("\n-- found session for %s\n", idParent.c_str()));
272 ManagedObjectRef *pRef;
273 if ( ((pRef = pSession->findRefFromPtr(pc)))
274 || ((pRef = new ManagedObjectRef(*pSession, pcszInterface, pc)))
275 )
276 return pRef->toWSDL();
277 }
278
279 return 0;
280}
281
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