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