VirtualBox

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

Last change on this file since 19017 was 18406, checked in by vboxsync, 16 years ago

Main: add IProgress::GetTimeRemaining()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.2 KB
Line 
1/* $Id: ProgressImpl.h 18406 2009-03-27 15:31:21Z vboxsync $ */
2/** @file
3 *
4 * VirtualBox COM class implementation
5 */
6
7/*
8 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22
23#ifndef ____H_PROGRESSIMPL
24#define ____H_PROGRESSIMPL
25
26#include "VirtualBoxBase.h"
27
28#include <VBox/com/SupportErrorInfo.h>
29
30#include <iprt/semaphore.h>
31
32#include <vector>
33
34class VirtualBox;
35
36////////////////////////////////////////////////////////////////////////////////
37
38/**
39 * Base component class for progress objects.
40 */
41class ATL_NO_VTABLE ProgressBase :
42 public VirtualBoxBaseNEXT,
43 public com::SupportErrorInfoBase,
44 public VirtualBoxSupportTranslation <ProgressBase>,
45 public IProgress
46{
47protected:
48
49 VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT (ProgressBase)
50
51 DECLARE_EMPTY_CTOR_DTOR (ProgressBase)
52
53 HRESULT FinalConstruct();
54
55 // protected initializer/uninitializer for internal purposes only
56 HRESULT protectedInit (AutoInitSpan &aAutoInitSpan,
57#if !defined (VBOX_COM_INPROC)
58 VirtualBox *aParent,
59#endif
60 IUnknown *aInitiator,
61 CBSTR aDescription, OUT_GUID aId = NULL);
62 HRESULT protectedInit (AutoInitSpan &aAutoInitSpan);
63 void protectedUninit (AutoUninitSpan &aAutoUninitSpan);
64
65public:
66
67 // IProgress properties
68 STDMETHOD(COMGETTER(Id)) (OUT_GUID aId);
69 STDMETHOD(COMGETTER(Description)) (BSTR *aDescription);
70 STDMETHOD(COMGETTER(Initiator)) (IUnknown **aInitiator);
71
72 // IProgress properties
73 STDMETHOD(COMGETTER(Cancelable)) (BOOL *aCancelable);
74 STDMETHOD(COMGETTER(Percent)) (ULONG *aPercent);
75 STDMETHOD(COMGETTER(TimeRemaining)) (LONG *aTimeRemaining);
76 STDMETHOD(COMGETTER(Completed)) (BOOL *aCompleted);
77 STDMETHOD(COMGETTER(Canceled)) (BOOL *aCanceled);
78 STDMETHOD(COMGETTER(ResultCode)) (HRESULT *aResultCode);
79 STDMETHOD(COMGETTER(ErrorInfo)) (IVirtualBoxErrorInfo **aErrorInfo);
80 STDMETHOD(COMGETTER(OperationCount)) (ULONG *aOperationCount);
81 STDMETHOD(COMGETTER(Operation)) (ULONG *aCount);
82 STDMETHOD(COMGETTER(OperationDescription)) (BSTR *aOperationDescription);
83 STDMETHOD(COMGETTER(OperationPercent)) (ULONG *aOperationPercent);
84
85 // public methods only for internal purposes
86
87 static HRESULT setErrorInfoOnThread (IProgress *aProgress);
88
89 // unsafe inline public methods for internal purposes only (ensure there is
90 // a caller and a read lock before calling them!)
91
92 BOOL completed() const { return mCompleted; }
93 HRESULT resultCode() const { return mResultCode; }
94 double calcTotalPercent();
95
96protected:
97
98#if !defined (VBOX_COM_INPROC)
99 /** Weak parent. */
100 const ComObjPtr <VirtualBox, ComWeakRef> mParent;
101#endif
102
103 const ComPtr <IUnknown> mInitiator;
104
105 const Guid mId;
106 const Bstr mDescription;
107
108 uint64_t m_ullTimestamp; // progress object creation timestamp, for ETA computation
109
110 /* The fields below are to be properly initalized by subclasses */
111
112 BOOL mCompleted;
113 BOOL mCancelable;
114 BOOL mCanceled;
115 HRESULT mResultCode;
116 ComPtr <IVirtualBoxErrorInfo> mErrorInfo;
117
118 ULONG m_cOperations; // number of operations (so that progress dialog can display something like 1/3)
119 ULONG m_ulTotalOperationsWeight; // sum of weights of all operations, given to constructor
120
121 ULONG m_ulOperationsCompletedWeight; // summed-up weight of operations that have been completed; initially 0
122
123 ULONG m_ulCurrentOperation; // operations counter, incremented with each setNextOperation()
124 Bstr m_bstrOperationDescription; // name of current operation; initially from constructor, changed with setNextOperation()
125 ULONG m_ulCurrentOperationWeight; // weight of current operation, given to setNextOperation()
126 ULONG m_ulOperationPercent; // percentage of current operation, set with setCurrentOperationProgress()
127};
128
129////////////////////////////////////////////////////////////////////////////////
130
131/**
132 * Normal progress object.
133 */
134class ATL_NO_VTABLE Progress :
135 public com::SupportErrorInfoDerived <ProgressBase, Progress, IProgress>,
136 public VirtualBoxSupportTranslation <Progress>
137{
138
139public:
140
141 VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE (Progress)
142
143 DECLARE_NOT_AGGREGATABLE (Progress)
144
145 DECLARE_PROTECT_FINAL_CONSTRUCT()
146
147 BEGIN_COM_MAP (Progress)
148 COM_INTERFACE_ENTRY (ISupportErrorInfo)
149 COM_INTERFACE_ENTRY (IProgress)
150 END_COM_MAP()
151
152 NS_DECL_ISUPPORTS
153
154 HRESULT FinalConstruct();
155 void FinalRelease();
156
157 // public initializer/uninitializer for internal purposes only
158
159 /**
160 * Simplified constructor for progress objects that have only one
161 * operation as a task.
162 * @param aParent
163 * @param aInitiator
164 * @param aDescription
165 * @param aCancelable
166 * @param aId
167 * @return
168 */
169 HRESULT init(
170#if !defined (VBOX_COM_INPROC)
171 VirtualBox *aParent,
172#endif
173 IUnknown *aInitiator,
174 CBSTR aDescription, BOOL aCancelable,
175 OUT_GUID aId = NULL)
176 {
177 return init(
178#if !defined (VBOX_COM_INPROC)
179 aParent,
180#endif
181 aInitiator,
182 aDescription,
183 aCancelable,
184 1, // cOperations
185 1, // ulTotalOperationsWeight
186 aDescription, // bstrFirstOperationDescription
187 1, // ulFirstOperationWeight
188 aId);
189 }
190
191 /**
192 * Not quite so simplified constructor for progress objects that have
193 * more than one operation, but all sub-operations are weighed the same.
194 * @param aParent
195 * @param aInitiator
196 * @param aDescription
197 * @param aCancelable
198 * @param cOperations
199 * @param bstrFirstOperationDescription
200 * @param aId
201 * @return
202 */
203 HRESULT init(
204#if !defined (VBOX_COM_INPROC)
205 VirtualBox *aParent,
206#endif
207 IUnknown *aInitiator,
208 CBSTR aDescription, BOOL aCancelable,
209 ULONG cOperations,
210 CBSTR bstrFirstOperationDescription,
211 OUT_GUID aId = NULL)
212 {
213 return init(
214#if !defined (VBOX_COM_INPROC)
215 aParent,
216#endif
217 aInitiator,
218 aDescription,
219 aCancelable,
220 cOperations, // cOperations
221 cOperations, // ulTotalOperationsWeight = cOperations
222 bstrFirstOperationDescription, // bstrFirstOperationDescription
223 1, // ulFirstOperationWeight: weigh them all the same
224 aId);
225 }
226
227 HRESULT init(
228#if !defined (VBOX_COM_INPROC)
229 VirtualBox *aParent,
230#endif
231 IUnknown *aInitiator,
232 CBSTR aDescription, BOOL aCancelable,
233 ULONG cOperations, ULONG ulTotalOperationsWeight,
234 CBSTR bstrFirstOperationDescription, ULONG ulFirstOperationWeight,
235 OUT_GUID aId = NULL);
236
237 HRESULT init (BOOL aCancelable, ULONG aOperationCount,
238 CBSTR aOperationDescription);
239
240 void uninit();
241
242 // IProgress methods
243 STDMETHOD(WaitForCompletion)(LONG aTimeout);
244 STDMETHOD(WaitForOperationCompletion)(ULONG aOperation, LONG aTimeout);
245 STDMETHOD(Cancel)();
246
247 // public methods only for internal purposes
248
249 HRESULT setCurrentOperationProgress(ULONG aPercent);
250 HRESULT setNextOperation(CBSTR bstrNextOperationDescription, ULONG ulNextOperationsWeight);
251
252 HRESULT notifyComplete(HRESULT aResultCode);
253 HRESULT notifyComplete(HRESULT aResultCode, const GUID &aIID,
254 const Bstr &aComponent,
255 const char *aText, ...);
256 HRESULT notifyCompleteBstr(HRESULT aResultCode, const GUID &aIID,
257 const Bstr &aComponent, const Bstr &aText);
258
259 /** For com::SupportErrorInfoImpl. */
260 static const char *ComponentName() { return "Progress"; }
261
262private:
263
264 RTSEMEVENTMULTI mCompletedSem;
265 ULONG mWaitersCount;
266};
267
268////////////////////////////////////////////////////////////////////////////////
269
270/**
271 * The CombinedProgress class allows to combine several progress objects to a
272 * single progress component. This single progress component will treat all
273 * operations of individual progress objects as a single sequence of operations
274 * that follow each other in the same order as progress objects are passed to
275 * the #init() method.
276 *
277 * Individual progress objects are sequentially combined so that this progress
278 * object:
279 *
280 * - is cancelable only if all progresses are cancelable.
281 * - is canceled once a progress that follows next to successfully completed
282 * ones reports it was canceled.
283 * - is completed successfully only after all progresses are completed
284 * successfully.
285 * - is completed unsuccessfully once a progress that follows next to
286 * successfully completed ones reports it was completed unsuccessfully;
287 * the result code and error info of the unsuccessful progress
288 * will be reported as the result code and error info of this progress.
289 * - returns N as the operation number, where N equals to the number of
290 * operations in all successfully completed progresses starting from the
291 * first one plus the operation number of the next (not yet complete)
292 * progress; the operation description of the latter one is reported as
293 * the operation description of this progress object.
294 * - returns P as the percent value, where P equals to the sum of percents
295 * of all successfully completed progresses starting from the
296 * first one plus the percent value of the next (not yet complete)
297 * progress, normalized to 100%.
298 *
299 * @note It's the respoisibility of the combined progress object creator to
300 * complete individual progresses in the right order: if, let's say, the
301 * last progress is completed before all previous ones,
302 * #WaitForCompletion(-1) will most likely give 100% CPU load because it
303 * will be in a loop calling a method that returns immediately.
304 */
305class ATL_NO_VTABLE CombinedProgress :
306 public com::SupportErrorInfoDerived <ProgressBase, CombinedProgress, IProgress>,
307 public VirtualBoxSupportTranslation <CombinedProgress>
308{
309
310public:
311
312 VIRTUALBOXSUPPORTTRANSLATION_OVERRIDE (CombinedProgress)
313
314 DECLARE_NOT_AGGREGATABLE (CombinedProgress)
315
316 DECLARE_PROTECT_FINAL_CONSTRUCT()
317
318 BEGIN_COM_MAP (CombinedProgress)
319 COM_INTERFACE_ENTRY (ISupportErrorInfo)
320 COM_INTERFACE_ENTRY (IProgress)
321 END_COM_MAP()
322
323 NS_DECL_ISUPPORTS
324
325 HRESULT FinalConstruct();
326 void FinalRelease();
327
328 // public initializer/uninitializer for internal purposes only
329
330 HRESULT init (
331#if !defined (VBOX_COM_INPROC)
332 VirtualBox *aParent,
333#endif
334 IUnknown *aInitiator,
335 CBSTR aDescription,
336 IProgress *aProgress1, IProgress *aProgress2,
337 OUT_GUID aId = NULL);
338
339 /**
340 * Initializes the combined progress object given the first and the last
341 * normal progress object from the list.
342 *
343 * @param aParent See ProgressBase::init().
344 * @param aInitiator See ProgressBase::init().
345 * @param aDescription See ProgressBase::init().
346 * @param aFirstProgress Iterator of the first normal progress object.
347 * @param aSecondProgress Iterator of the last normal progress object.
348 * @param aId See ProgressBase::init().
349 */
350 template <typename InputIterator>
351 HRESULT init (
352#if !defined (VBOX_COM_INPROC)
353 VirtualBox *aParent,
354#endif
355 IUnknown *aInitiator,
356 CBSTR aDescription,
357 InputIterator aFirstProgress, InputIterator aLastProgress,
358 OUT_GUID aId = NULL)
359 {
360 /* Enclose the state transition NotReady->InInit->Ready */
361 AutoInitSpan autoInitSpan (this);
362 AssertReturn (autoInitSpan.isOk(), E_FAIL);
363
364 mProgresses = ProgressVector (aFirstProgress, aLastProgress);
365
366 HRESULT rc = protectedInit (autoInitSpan,
367#if !defined (VBOX_COM_INPROC)
368 aParent,
369#endif
370 aInitiator, aDescription, aId);
371
372 /* Confirm a successful initialization when it's the case */
373 if (SUCCEEDED (rc))
374 autoInitSpan.setSucceeded();
375
376 return rc;
377 }
378
379protected:
380
381 HRESULT protectedInit (AutoInitSpan &aAutoInitSpan,
382#if !defined (VBOX_COM_INPROC)
383 VirtualBox *aParent,
384#endif
385 IUnknown *aInitiator,
386 CBSTR aDescription, OUT_GUID aId);
387
388public:
389
390 void uninit();
391
392 // IProgress properties
393 STDMETHOD(COMGETTER(Percent)) (ULONG *aPercent);
394 STDMETHOD(COMGETTER(Completed)) (BOOL *aCompleted);
395 STDMETHOD(COMGETTER(Canceled)) (BOOL *aCanceled);
396 STDMETHOD(COMGETTER(ResultCode)) (HRESULT *aResultCode);
397 STDMETHOD(COMGETTER(ErrorInfo)) (IVirtualBoxErrorInfo **aErrorInfo);
398 STDMETHOD(COMGETTER(Operation)) (ULONG *aCount);
399 STDMETHOD(COMGETTER(OperationDescription)) (BSTR *aOperationDescription);
400 STDMETHOD(COMGETTER(OperationPercent)) (ULONG *aOperationPercent);
401
402 // IProgress methods
403 STDMETHOD(WaitForCompletion) (LONG aTimeout);
404 STDMETHOD(WaitForOperationCompletion) (ULONG aOperation, LONG aTimeout);
405 STDMETHOD(Cancel)();
406
407 // public methods only for internal purposes
408
409 /** For com::SupportErrorInfoImpl. */
410 static const char *ComponentName() { return "CombinedProgress"; }
411
412private:
413
414 HRESULT checkProgress();
415
416 typedef std::vector <ComPtr <IProgress> > ProgressVector;
417 ProgressVector mProgresses;
418
419 size_t mProgress;
420 ULONG mCompletedOperations;
421};
422
423#endif /* ____H_PROGRESSIMPL */
424
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