1 | /** @file
|
---|
2 | *
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifndef ____H_PROGRESSIMPL
|
---|
23 | #define ____H_PROGRESSIMPL
|
---|
24 |
|
---|
25 | #include "VirtualBoxBase.h"
|
---|
26 | #include "Collection.h"
|
---|
27 |
|
---|
28 | #include <iprt/semaphore.h>
|
---|
29 |
|
---|
30 | #include <vector>
|
---|
31 |
|
---|
32 | class VirtualBox;
|
---|
33 |
|
---|
34 | ////////////////////////////////////////////////////////////////////////////////
|
---|
35 |
|
---|
36 | class ATL_NO_VTABLE ProgressBase :
|
---|
37 | public VirtualBoxSupportErrorInfoImpl <ProgressBase, IProgress>,
|
---|
38 | public VirtualBoxSupportTranslation <ProgressBase>,
|
---|
39 | public VirtualBoxBase,
|
---|
40 | public IProgress
|
---|
41 | {
|
---|
42 | protected:
|
---|
43 |
|
---|
44 | BEGIN_COM_MAP(ProgressBase)
|
---|
45 | COM_INTERFACE_ENTRY(ISupportErrorInfo)
|
---|
46 | COM_INTERFACE_ENTRY(IProgress)
|
---|
47 | END_COM_MAP()
|
---|
48 |
|
---|
49 | HRESULT FinalConstruct();
|
---|
50 |
|
---|
51 | // public initializer/uninitializer for internal purposes only
|
---|
52 | HRESULT protectedInit (
|
---|
53 | #if !defined (VBOX_COM_INPROC)
|
---|
54 | VirtualBox *aParent,
|
---|
55 | #endif
|
---|
56 | IUnknown *aInitiator,
|
---|
57 | const BSTR aDescription, GUIDPARAMOUT aId = NULL);
|
---|
58 | HRESULT protectedInit();
|
---|
59 | void protectedUninit (AutoLock &alock);
|
---|
60 |
|
---|
61 | public:
|
---|
62 |
|
---|
63 | // IProgress properties
|
---|
64 | STDMETHOD(COMGETTER(Id)) (GUIDPARAMOUT aId);
|
---|
65 | STDMETHOD(COMGETTER(Description)) (BSTR *aDescription);
|
---|
66 | STDMETHOD(COMGETTER(Initiator)) (IUnknown **aInitiator);
|
---|
67 |
|
---|
68 | // IProgress properties
|
---|
69 | STDMETHOD(COMGETTER(Cancelable)) (BOOL *aCancelable);
|
---|
70 | STDMETHOD(COMGETTER(Percent)) (LONG *aPercent);
|
---|
71 | STDMETHOD(COMGETTER(Completed)) (BOOL *aCompleted);
|
---|
72 | STDMETHOD(COMGETTER(Canceled)) (BOOL *aCanceled);
|
---|
73 | STDMETHOD(COMGETTER(ResultCode)) (HRESULT *aResultCode);
|
---|
74 | STDMETHOD(COMGETTER(ErrorInfo)) (IVirtualBoxErrorInfo **aErrorInfo);
|
---|
75 | STDMETHOD(COMGETTER(OperationCount)) (ULONG *aOperationCount);
|
---|
76 | STDMETHOD(COMGETTER(Operation)) (ULONG *aCount);
|
---|
77 | STDMETHOD(COMGETTER(OperationDescription)) (BSTR *aOperationDescription);
|
---|
78 | STDMETHOD(COMGETTER(OperationPercent)) (LONG *aOperationPercent);
|
---|
79 |
|
---|
80 | // public methods only for internal purposes
|
---|
81 |
|
---|
82 | Guid id() { AutoLock alock (this); return mId; }
|
---|
83 | BOOL completed() { AutoLock alock (this); return mCompleted; }
|
---|
84 | HRESULT resultCode() { AutoLock alock (this); return mResultCode; }
|
---|
85 |
|
---|
86 | // for VirtualBoxSupportErrorInfoImpl
|
---|
87 | static const wchar_t *getComponentName() { return L"Progress"; }
|
---|
88 |
|
---|
89 | protected:
|
---|
90 |
|
---|
91 | #if !defined (VBOX_COM_INPROC)
|
---|
92 | /** weak parent */
|
---|
93 | ComObjPtr <VirtualBox, ComWeakRef> mParent;
|
---|
94 | #endif
|
---|
95 | ComPtr <IUnknown> mInitiator;
|
---|
96 |
|
---|
97 | Guid mId;
|
---|
98 | Bstr mDescription;
|
---|
99 |
|
---|
100 | // the fields below are to be initalized by subclasses
|
---|
101 |
|
---|
102 | BOOL mCompleted;
|
---|
103 | BOOL mCancelable;
|
---|
104 | BOOL mCanceled;
|
---|
105 | HRESULT mResultCode;
|
---|
106 | ComPtr <IVirtualBoxErrorInfo> mErrorInfo;
|
---|
107 |
|
---|
108 | ULONG mOperationCount;
|
---|
109 | ULONG mOperation;
|
---|
110 | Bstr mOperationDescription;
|
---|
111 | LONG mOperationPercent;
|
---|
112 | };
|
---|
113 |
|
---|
114 | ////////////////////////////////////////////////////////////////////////////////
|
---|
115 |
|
---|
116 | class ATL_NO_VTABLE Progress :
|
---|
117 | public VirtualBoxSupportTranslation <Progress>,
|
---|
118 | public ProgressBase
|
---|
119 | {
|
---|
120 |
|
---|
121 | public:
|
---|
122 |
|
---|
123 | VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(Progress)
|
---|
124 |
|
---|
125 | DECLARE_NOT_AGGREGATABLE(Progress)
|
---|
126 |
|
---|
127 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
128 |
|
---|
129 | BEGIN_COM_MAP(Progress)
|
---|
130 | COM_INTERFACE_ENTRY(ISupportErrorInfo)
|
---|
131 | COM_INTERFACE_ENTRY(IProgress)
|
---|
132 | END_COM_MAP()
|
---|
133 |
|
---|
134 | NS_DECL_ISUPPORTS
|
---|
135 |
|
---|
136 | HRESULT FinalConstruct();
|
---|
137 | void FinalRelease();
|
---|
138 |
|
---|
139 | // public initializer/uninitializer for internal purposes only
|
---|
140 |
|
---|
141 | HRESULT init (
|
---|
142 | #if !defined (VBOX_COM_INPROC)
|
---|
143 | VirtualBox *aParent,
|
---|
144 | #endif
|
---|
145 | IUnknown *aInitiator,
|
---|
146 | const BSTR aDescription, BOOL aCancelable,
|
---|
147 | GUIDPARAMOUT aId = NULL)
|
---|
148 | {
|
---|
149 | return init (
|
---|
150 | #if !defined (VBOX_COM_INPROC)
|
---|
151 | aParent,
|
---|
152 | #endif
|
---|
153 | aInitiator, aDescription, aCancelable, 1, aDescription, aId);
|
---|
154 | }
|
---|
155 |
|
---|
156 | HRESULT init (
|
---|
157 | #if !defined (VBOX_COM_INPROC)
|
---|
158 | VirtualBox *aParent,
|
---|
159 | #endif
|
---|
160 | IUnknown *aInitiator,
|
---|
161 | const BSTR aDescription, BOOL aCancelable,
|
---|
162 | ULONG aOperationCount, const BSTR aOperationDescription,
|
---|
163 | GUIDPARAMOUT aId = NULL);
|
---|
164 |
|
---|
165 | HRESULT init (BOOL aCancelable, ULONG aOperationCount,
|
---|
166 | const BSTR aOperationDescription);
|
---|
167 |
|
---|
168 | void uninit();
|
---|
169 |
|
---|
170 | // IProgress methods
|
---|
171 | STDMETHOD(WaitForCompletion) (LONG aTimeout);
|
---|
172 | STDMETHOD(WaitForOperationCompletion) (ULONG aOperation, LONG aTimeout);
|
---|
173 | STDMETHOD(Cancel)();
|
---|
174 |
|
---|
175 | // public methods only for internal purposes
|
---|
176 |
|
---|
177 | HRESULT notifyProgress (LONG aPercent);
|
---|
178 | HRESULT advanceOperation (const BSTR aOperationDescription);
|
---|
179 |
|
---|
180 | HRESULT notifyComplete (HRESULT aResultCode);
|
---|
181 | HRESULT notifyComplete (HRESULT aResultCode, const GUID &aIID,
|
---|
182 | const Bstr &aComponent,
|
---|
183 | const char *aText, ...);
|
---|
184 |
|
---|
185 | private:
|
---|
186 |
|
---|
187 | RTSEMEVENTMULTI mCompletedSem;
|
---|
188 | ULONG mWaitersCount;
|
---|
189 | };
|
---|
190 |
|
---|
191 | ////////////////////////////////////////////////////////////////////////////////
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * The CombinedProgress class allows to combine several progress objects
|
---|
195 | * to a single progress component. This single progress component will treat
|
---|
196 | * all operations of individual progress objects as a single sequence of
|
---|
197 | * operations, that follow each other in the same order as progress objects are
|
---|
198 | * passed to the #init() method.
|
---|
199 | *
|
---|
200 | * Individual progress objects are sequentially combined so that this progress
|
---|
201 | * object:
|
---|
202 | *
|
---|
203 | * - is cancelable only if all progresses are cancelable.
|
---|
204 | * - is canceled once a progress that follows next to successfully completed
|
---|
205 | * ones reports it was canceled.
|
---|
206 | * - is completed successfully only after all progresses are completed
|
---|
207 | * successfully.
|
---|
208 | * - is completed unsuccessfully once a progress that follows next to
|
---|
209 | * successfully completed ones reports it was completed unsuccessfully;
|
---|
210 | * the result code and error info of the unsuccessful progress
|
---|
211 | * will be reported as the result code and error info of this progress.
|
---|
212 | * - returns N as the operation number, where N equals to the number of
|
---|
213 | * operations in all successfully completed progresses starting from the
|
---|
214 | * first one plus the operation number of the next (not yet complete)
|
---|
215 | * progress; the operation description of the latter one is reported as
|
---|
216 | * the operation description of this progress object.
|
---|
217 | * - returns P as the percent value, where P equals to the sum of percents
|
---|
218 | * of all successfully completed progresses starting from the
|
---|
219 | * first one plus the percent value of the next (not yet complete)
|
---|
220 | * progress, normalized to 100%.
|
---|
221 | *
|
---|
222 | * @note
|
---|
223 | * It's the respoisibility of the combined progress object creator
|
---|
224 | * to complete individual progresses in the right order: if, let's say,
|
---|
225 | * the last progress is completed before all previous ones,
|
---|
226 | * #WaitForCompletion(-1) will most likely give 100% CPU load because it
|
---|
227 | * will be in a loop calling a method that returns immediately.
|
---|
228 | */
|
---|
229 | class ATL_NO_VTABLE CombinedProgress :
|
---|
230 | public VirtualBoxSupportTranslation <CombinedProgress>,
|
---|
231 | public ProgressBase
|
---|
232 | {
|
---|
233 |
|
---|
234 | public:
|
---|
235 |
|
---|
236 | VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE(CombinedProgress)
|
---|
237 |
|
---|
238 | DECLARE_NOT_AGGREGATABLE(CombinedProgress)
|
---|
239 |
|
---|
240 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
241 |
|
---|
242 | BEGIN_COM_MAP(CombinedProgress)
|
---|
243 | COM_INTERFACE_ENTRY(ISupportErrorInfo)
|
---|
244 | COM_INTERFACE_ENTRY(IProgress)
|
---|
245 | END_COM_MAP()
|
---|
246 |
|
---|
247 | NS_DECL_ISUPPORTS
|
---|
248 |
|
---|
249 | HRESULT FinalConstruct();
|
---|
250 | void FinalRelease();
|
---|
251 |
|
---|
252 | // public initializer/uninitializer for internal purposes only
|
---|
253 |
|
---|
254 | HRESULT init (
|
---|
255 | #if !defined (VBOX_COM_INPROC)
|
---|
256 | VirtualBox *aParent,
|
---|
257 | #endif
|
---|
258 | IUnknown *aInitiator,
|
---|
259 | const BSTR aDescription,
|
---|
260 | IProgress *aProgress1, IProgress *aProgress2,
|
---|
261 | GUIDPARAMOUT aId = NULL)
|
---|
262 | {
|
---|
263 | AutoLock lock (this);
|
---|
264 | ComAssertRet (!isReady(), E_UNEXPECTED);
|
---|
265 |
|
---|
266 | mProgresses.resize (2);
|
---|
267 | mProgresses [0] = aProgress1;
|
---|
268 | mProgresses [1] = aProgress2;
|
---|
269 |
|
---|
270 | return protectedInit (
|
---|
271 | #if !defined (VBOX_COM_INPROC)
|
---|
272 | aParent,
|
---|
273 | #endif
|
---|
274 | aInitiator, aDescription, aId);
|
---|
275 | }
|
---|
276 |
|
---|
277 | template <typename InputIterator>
|
---|
278 | HRESULT init (
|
---|
279 | #if !defined (VBOX_COM_INPROC)
|
---|
280 | VirtualBox *aParent,
|
---|
281 | #endif
|
---|
282 | IUnknown *aInitiator,
|
---|
283 | const BSTR aDescription,
|
---|
284 | InputIterator aFirstProgress, InputIterator aLastProgress,
|
---|
285 | GUIDPARAMOUT aId = NULL)
|
---|
286 | {
|
---|
287 | AutoLock lock (this);
|
---|
288 | ComAssertRet (!isReady(), E_UNEXPECTED);
|
---|
289 |
|
---|
290 | mProgresses = ProgressVector (aFirstProgress, aLastProgress);
|
---|
291 |
|
---|
292 | return protectedInit (
|
---|
293 | #if !defined (VBOX_COM_INPROC)
|
---|
294 | aParent,
|
---|
295 | #endif
|
---|
296 | aInitiator, aDescription, aId);
|
---|
297 | }
|
---|
298 |
|
---|
299 | protected:
|
---|
300 |
|
---|
301 | HRESULT protectedInit (
|
---|
302 | #if !defined (VBOX_COM_INPROC)
|
---|
303 | VirtualBox *aParent,
|
---|
304 | #endif
|
---|
305 | IUnknown *aInitiator,
|
---|
306 | const BSTR aDescription, GUIDPARAMOUT aId);
|
---|
307 |
|
---|
308 | public:
|
---|
309 |
|
---|
310 | void uninit();
|
---|
311 |
|
---|
312 | // IProgress properties
|
---|
313 | STDMETHOD(COMGETTER(Percent)) (LONG *aPercent);
|
---|
314 | STDMETHOD(COMGETTER(Completed)) (BOOL *aCompleted);
|
---|
315 | STDMETHOD(COMGETTER(Canceled)) (BOOL *aCanceled);
|
---|
316 | STDMETHOD(COMGETTER(ResultCode)) (HRESULT *aResultCode);
|
---|
317 | STDMETHOD(COMGETTER(ErrorInfo)) (IVirtualBoxErrorInfo **aErrorInfo);
|
---|
318 | STDMETHOD(COMGETTER(Operation)) (ULONG *aCount);
|
---|
319 | STDMETHOD(COMGETTER(OperationDescription)) (BSTR *aOperationDescription);
|
---|
320 | STDMETHOD(COMGETTER(OperationPercent)) (LONG *aOperationPercent);
|
---|
321 |
|
---|
322 | // IProgress methods
|
---|
323 | STDMETHOD(WaitForCompletion) (LONG aTimeout);
|
---|
324 | STDMETHOD(WaitForOperationCompletion) (ULONG aOperation, LONG aTimeout);
|
---|
325 | STDMETHOD(Cancel)();
|
---|
326 |
|
---|
327 | // public methods only for internal purposes
|
---|
328 |
|
---|
329 | private:
|
---|
330 |
|
---|
331 | HRESULT checkProgress();
|
---|
332 |
|
---|
333 | typedef std::vector <ComPtr <IProgress> > ProgressVector;
|
---|
334 | ProgressVector mProgresses;
|
---|
335 |
|
---|
336 | size_t mProgress;
|
---|
337 | ULONG mCompletedOperations;
|
---|
338 | };
|
---|
339 |
|
---|
340 | COM_DECL_READONLY_ENUM_AND_COLLECTION_AS (Progress, IProgress)
|
---|
341 |
|
---|
342 | #endif // ____H_PROGRESSIMPL
|
---|