VirtualBox

source: vbox/trunk/src/VBox/Main/include/ProgressImpl.h@ 7692

Last change on this file since 7692 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

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