1 | /* $Id: ProgressImpl.h 30739 2010-07-08 12:27:42Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VirtualBox COM class implementation
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
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 |
|
---|
19 | #ifndef ____H_PROGRESSIMPL
|
---|
20 | #define ____H_PROGRESSIMPL
|
---|
21 |
|
---|
22 | #include "VirtualBoxBase.h"
|
---|
23 |
|
---|
24 | #include <iprt/semaphore.h>
|
---|
25 |
|
---|
26 | ////////////////////////////////////////////////////////////////////////////////
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Base component class for progress objects.
|
---|
30 | */
|
---|
31 | class ATL_NO_VTABLE ProgressBase :
|
---|
32 | public VirtualBoxBase,
|
---|
33 | VBOX_SCRIPTABLE_IMPL(IProgress)
|
---|
34 | {
|
---|
35 | protected:
|
---|
36 |
|
---|
37 | // VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(ProgressBase, IProgress)
|
---|
38 | // cannot be added here or Windows will not buuld; as a result, ProgressBase cannot be
|
---|
39 | // instantiated, but we're not doing that anyway (but only its children)
|
---|
40 |
|
---|
41 | DECLARE_EMPTY_CTOR_DTOR (ProgressBase)
|
---|
42 |
|
---|
43 | HRESULT FinalConstruct();
|
---|
44 |
|
---|
45 | // protected initializer/uninitializer for internal purposes only
|
---|
46 | HRESULT protectedInit(AutoInitSpan &aAutoInitSpan,
|
---|
47 | #if !defined (VBOX_COM_INPROC)
|
---|
48 | VirtualBox *aParent,
|
---|
49 | #endif
|
---|
50 | IUnknown *aInitiator,
|
---|
51 | CBSTR aDescription, OUT_GUID aId = NULL);
|
---|
52 | HRESULT protectedInit(AutoInitSpan &aAutoInitSpan);
|
---|
53 | void protectedUninit(AutoUninitSpan &aAutoUninitSpan);
|
---|
54 |
|
---|
55 | public:
|
---|
56 |
|
---|
57 | // IProgress properties
|
---|
58 | STDMETHOD(COMGETTER(Id)) (BSTR *aId);
|
---|
59 | STDMETHOD(COMGETTER(Description)) (BSTR *aDescription);
|
---|
60 | STDMETHOD(COMGETTER(Initiator)) (IUnknown **aInitiator);
|
---|
61 |
|
---|
62 | // IProgress properties
|
---|
63 | STDMETHOD(COMGETTER(Cancelable)) (BOOL *aCancelable);
|
---|
64 | STDMETHOD(COMGETTER(Percent)) (ULONG *aPercent);
|
---|
65 | STDMETHOD(COMGETTER(TimeRemaining)) (LONG *aTimeRemaining);
|
---|
66 | STDMETHOD(COMGETTER(Completed)) (BOOL *aCompleted);
|
---|
67 | STDMETHOD(COMGETTER(Canceled)) (BOOL *aCanceled);
|
---|
68 | STDMETHOD(COMGETTER(ResultCode)) (LONG *aResultCode);
|
---|
69 | STDMETHOD(COMGETTER(ErrorInfo)) (IVirtualBoxErrorInfo **aErrorInfo);
|
---|
70 | STDMETHOD(COMGETTER(OperationCount)) (ULONG *aOperationCount);
|
---|
71 | STDMETHOD(COMGETTER(Operation)) (ULONG *aOperation);
|
---|
72 | STDMETHOD(COMGETTER(OperationDescription)) (BSTR *aOperationDescription);
|
---|
73 | STDMETHOD(COMGETTER(OperationPercent)) (ULONG *aOperationPercent);
|
---|
74 | STDMETHOD(COMSETTER(Timeout)) (ULONG aTimeout);
|
---|
75 | STDMETHOD(COMGETTER(Timeout)) (ULONG *aTimeout);
|
---|
76 |
|
---|
77 | // public methods only for internal purposes
|
---|
78 |
|
---|
79 | bool setCancelCallback(void (*pfnCallback)(void *), void *pvUser);
|
---|
80 |
|
---|
81 |
|
---|
82 | // unsafe inline public methods for internal purposes only (ensure there is
|
---|
83 | // a caller and a read lock before calling them!)
|
---|
84 |
|
---|
85 | BOOL getCompleted() const { return mCompleted; }
|
---|
86 | HRESULT getResultCode() const { return mResultCode; }
|
---|
87 | double calcTotalPercent();
|
---|
88 |
|
---|
89 | protected:
|
---|
90 | void checkForAutomaticTimeout(void);
|
---|
91 |
|
---|
92 | #if !defined (VBOX_COM_INPROC)
|
---|
93 | /** Weak parent. */
|
---|
94 | VirtualBox * const mParent;
|
---|
95 | #endif
|
---|
96 |
|
---|
97 | const ComPtr<IUnknown> mInitiator;
|
---|
98 |
|
---|
99 | const Guid mId;
|
---|
100 | const Bstr mDescription;
|
---|
101 |
|
---|
102 | uint64_t m_ullTimestamp; // progress object creation timestamp, for ETA computation
|
---|
103 |
|
---|
104 | void (*m_pfnCancelCallback)(void *);
|
---|
105 | void *m_pvCancelUserArg;
|
---|
106 |
|
---|
107 | /* The fields below are to be properly initalized by subclasses */
|
---|
108 |
|
---|
109 | BOOL mCompleted;
|
---|
110 | BOOL mCancelable;
|
---|
111 | BOOL mCanceled;
|
---|
112 | HRESULT mResultCode;
|
---|
113 | ComPtr<IVirtualBoxErrorInfo> mErrorInfo;
|
---|
114 |
|
---|
115 | ULONG m_cOperations; // number of operations (so that progress dialog can display something like 1/3)
|
---|
116 | ULONG m_ulTotalOperationsWeight; // sum of weights of all operations, given to constructor
|
---|
117 |
|
---|
118 | ULONG m_ulOperationsCompletedWeight; // summed-up weight of operations that have been completed; initially 0
|
---|
119 |
|
---|
120 | ULONG m_ulCurrentOperation; // operations counter, incremented with each setNextOperation()
|
---|
121 | Bstr m_bstrOperationDescription; // name of current operation; initially from constructor, changed with setNextOperation()
|
---|
122 | ULONG m_ulCurrentOperationWeight; // weight of current operation, given to setNextOperation()
|
---|
123 | ULONG m_ulOperationPercent; // percentage of current operation, set with setCurrentOperationProgress()
|
---|
124 | ULONG m_cMsTimeout; /**< Automatic timeout value. 0 means none. */
|
---|
125 | };
|
---|
126 |
|
---|
127 | ////////////////////////////////////////////////////////////////////////////////
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Normal progress object.
|
---|
131 | */
|
---|
132 | class ATL_NO_VTABLE Progress :
|
---|
133 | public ProgressBase
|
---|
134 | {
|
---|
135 |
|
---|
136 | public:
|
---|
137 | VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(Progress, IProgress)
|
---|
138 |
|
---|
139 | DECLARE_NOT_AGGREGATABLE (Progress)
|
---|
140 |
|
---|
141 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
142 |
|
---|
143 | BEGIN_COM_MAP (Progress)
|
---|
144 | COM_INTERFACE_ENTRY (ISupportErrorInfo)
|
---|
145 | COM_INTERFACE_ENTRY (IProgress)
|
---|
146 | COM_INTERFACE_ENTRY2 (IDispatch, IProgress)
|
---|
147 | END_COM_MAP()
|
---|
148 |
|
---|
149 | HRESULT FinalConstruct();
|
---|
150 | void FinalRelease();
|
---|
151 |
|
---|
152 | // public initializer/uninitializer for internal purposes only
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Simplified constructor for progress objects that have only one
|
---|
156 | * operation as a task.
|
---|
157 | * @param aParent
|
---|
158 | * @param aInitiator
|
---|
159 | * @param aDescription
|
---|
160 | * @param aCancelable
|
---|
161 | * @param aId
|
---|
162 | * @return
|
---|
163 | */
|
---|
164 | HRESULT init(
|
---|
165 | #if !defined (VBOX_COM_INPROC)
|
---|
166 | VirtualBox *aParent,
|
---|
167 | #endif
|
---|
168 | IUnknown *aInitiator,
|
---|
169 | CBSTR aDescription,
|
---|
170 | BOOL aCancelable,
|
---|
171 | OUT_GUID aId = NULL)
|
---|
172 | {
|
---|
173 | return init(
|
---|
174 | #if !defined (VBOX_COM_INPROC)
|
---|
175 | aParent,
|
---|
176 | #endif
|
---|
177 | aInitiator,
|
---|
178 | aDescription,
|
---|
179 | aCancelable,
|
---|
180 | 1, // cOperations
|
---|
181 | 1, // ulTotalOperationsWeight
|
---|
182 | aDescription, // bstrFirstOperationDescription
|
---|
183 | 1, // ulFirstOperationWeight
|
---|
184 | aId);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Not quite so simplified constructor for progress objects that have
|
---|
189 | * more than one operation, but all sub-operations are weighed the same.
|
---|
190 | * @param aParent
|
---|
191 | * @param aInitiator
|
---|
192 | * @param aDescription
|
---|
193 | * @param aCancelable
|
---|
194 | * @param cOperations
|
---|
195 | * @param bstrFirstOperationDescription
|
---|
196 | * @param aId
|
---|
197 | * @return
|
---|
198 | */
|
---|
199 | HRESULT init(
|
---|
200 | #if !defined (VBOX_COM_INPROC)
|
---|
201 | VirtualBox *aParent,
|
---|
202 | #endif
|
---|
203 | IUnknown *aInitiator,
|
---|
204 | CBSTR aDescription, BOOL aCancelable,
|
---|
205 | ULONG cOperations,
|
---|
206 | CBSTR bstrFirstOperationDescription,
|
---|
207 | OUT_GUID aId = NULL)
|
---|
208 | {
|
---|
209 | return init(
|
---|
210 | #if !defined (VBOX_COM_INPROC)
|
---|
211 | aParent,
|
---|
212 | #endif
|
---|
213 | aInitiator,
|
---|
214 | aDescription,
|
---|
215 | aCancelable,
|
---|
216 | cOperations, // cOperations
|
---|
217 | cOperations, // ulTotalOperationsWeight = cOperations
|
---|
218 | bstrFirstOperationDescription, // bstrFirstOperationDescription
|
---|
219 | 1, // ulFirstOperationWeight: weigh them all the same
|
---|
220 | aId);
|
---|
221 | }
|
---|
222 |
|
---|
223 | HRESULT init(
|
---|
224 | #if !defined (VBOX_COM_INPROC)
|
---|
225 | VirtualBox *aParent,
|
---|
226 | #endif
|
---|
227 | IUnknown *aInitiator,
|
---|
228 | CBSTR aDescription,
|
---|
229 | BOOL aCancelable,
|
---|
230 | ULONG cOperations,
|
---|
231 | ULONG ulTotalOperationsWeight,
|
---|
232 | CBSTR bstrFirstOperationDescription,
|
---|
233 | ULONG ulFirstOperationWeight,
|
---|
234 | OUT_GUID aId = NULL);
|
---|
235 |
|
---|
236 | HRESULT init(BOOL aCancelable,
|
---|
237 | 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 | STDMETHOD(SetCurrentOperationProgress)(ULONG aPercent);
|
---|
248 | STDMETHOD(SetNextOperation)(IN_BSTR bstrNextOperationDescription, ULONG ulNextOperationsWeight);
|
---|
249 |
|
---|
250 | // public methods only for internal purposes
|
---|
251 |
|
---|
252 | HRESULT setResultCode(HRESULT aResultCode);
|
---|
253 |
|
---|
254 | HRESULT notifyComplete(HRESULT aResultCode);
|
---|
255 | HRESULT notifyComplete(HRESULT aResultCode,
|
---|
256 | const GUID &aIID,
|
---|
257 | const char *pcszComponent,
|
---|
258 | const char *aText,
|
---|
259 | ...);
|
---|
260 | HRESULT notifyCompleteV(HRESULT aResultCode,
|
---|
261 | const GUID &aIID,
|
---|
262 | const char *pcszComponent,
|
---|
263 | const char *aText,
|
---|
264 | va_list va);
|
---|
265 | bool notifyPointOfNoReturn(void);
|
---|
266 |
|
---|
267 | private:
|
---|
268 |
|
---|
269 | RTSEMEVENTMULTI mCompletedSem;
|
---|
270 | ULONG mWaitersCount;
|
---|
271 | };
|
---|
272 |
|
---|
273 | #endif /* ____H_PROGRESSIMPL */
|
---|
274 |
|
---|