1 | /* $Id: ProgressImpl.h 69500 2017-10-28 15:14:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VirtualBox COM class implementation
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2017 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 "ProgressWrap.h"
|
---|
23 | #include "VirtualBoxBase.h"
|
---|
24 | #include "EventImpl.h"
|
---|
25 |
|
---|
26 | #include <iprt/semaphore.h>
|
---|
27 |
|
---|
28 | ////////////////////////////////////////////////////////////////////////////////
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Class for progress objects.
|
---|
32 | */
|
---|
33 | class ATL_NO_VTABLE Progress :
|
---|
34 | public ProgressWrap
|
---|
35 | {
|
---|
36 | public:
|
---|
37 | DECLARE_NOT_AGGREGATABLE(Progress)
|
---|
38 |
|
---|
39 | HRESULT FinalConstruct();
|
---|
40 | void FinalRelease();
|
---|
41 |
|
---|
42 | // public initializer/uninitializer for internal purposes only
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Simplified constructor for progress objects that have only one
|
---|
46 | * operation as a task.
|
---|
47 | * @param aParent
|
---|
48 | * @param aInitiator
|
---|
49 | * @param aDescription
|
---|
50 | * @param aCancelable
|
---|
51 | * @return
|
---|
52 | */
|
---|
53 | HRESULT init(
|
---|
54 | #if !defined(VBOX_COM_INPROC)
|
---|
55 | VirtualBox *aParent,
|
---|
56 | #endif
|
---|
57 | IUnknown *aInitiator,
|
---|
58 | Utf8Str aDescription,
|
---|
59 | BOOL aCancelable)
|
---|
60 | {
|
---|
61 | return init(
|
---|
62 | #if !defined(VBOX_COM_INPROC)
|
---|
63 | aParent,
|
---|
64 | #endif
|
---|
65 | aInitiator,
|
---|
66 | aDescription,
|
---|
67 | aCancelable,
|
---|
68 | 1, // cOperations
|
---|
69 | 1, // ulTotalOperationsWeight
|
---|
70 | aDescription, // aFirstOperationDescription
|
---|
71 | 1); // ulFirstOperationWeight
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Not quite so simplified constructor for progress objects that have
|
---|
76 | * more than one operation, but all sub-operations are weighed the same.
|
---|
77 | * @param aParent
|
---|
78 | * @param aInitiator
|
---|
79 | * @param aDescription
|
---|
80 | * @param aCancelable
|
---|
81 | * @param cOperations
|
---|
82 | * @param aFirstOperationDescription
|
---|
83 | * @return
|
---|
84 | */
|
---|
85 | HRESULT init(
|
---|
86 | #if !defined(VBOX_COM_INPROC)
|
---|
87 | VirtualBox *aParent,
|
---|
88 | #endif
|
---|
89 | IUnknown *aInitiator,
|
---|
90 | Utf8Str aDescription, BOOL aCancelable,
|
---|
91 | ULONG cOperations,
|
---|
92 | Utf8Str aFirstOperationDescription)
|
---|
93 | {
|
---|
94 | return init(
|
---|
95 | #if !defined(VBOX_COM_INPROC)
|
---|
96 | aParent,
|
---|
97 | #endif
|
---|
98 | aInitiator,
|
---|
99 | aDescription,
|
---|
100 | aCancelable,
|
---|
101 | cOperations, // cOperations
|
---|
102 | cOperations, // ulTotalOperationsWeight = cOperations
|
---|
103 | aFirstOperationDescription, // aFirstOperationDescription
|
---|
104 | 1); // ulFirstOperationWeight: weigh them all the same
|
---|
105 | }
|
---|
106 |
|
---|
107 | HRESULT init(
|
---|
108 | #if !defined(VBOX_COM_INPROC)
|
---|
109 | VirtualBox *aParent,
|
---|
110 | #endif
|
---|
111 | IUnknown *aInitiator,
|
---|
112 | Utf8Str aDescription,
|
---|
113 | BOOL aCancelable,
|
---|
114 | ULONG cOperations,
|
---|
115 | ULONG ulTotalOperationsWeight,
|
---|
116 | Utf8Str aFirstOperationDescription,
|
---|
117 | ULONG ulFirstOperationWeight);
|
---|
118 |
|
---|
119 | HRESULT init(BOOL aCancelable,
|
---|
120 | ULONG aOperationCount,
|
---|
121 | Utf8Str aOperationDescription);
|
---|
122 |
|
---|
123 | void uninit();
|
---|
124 |
|
---|
125 |
|
---|
126 | // public methods only for internal purposes
|
---|
127 | HRESULT i_notifyComplete(HRESULT aResultCode);
|
---|
128 | HRESULT i_notifyComplete(HRESULT aResultCode,
|
---|
129 | const GUID &aIID,
|
---|
130 | const char *pcszComponent,
|
---|
131 | const char *aText,
|
---|
132 | ...);
|
---|
133 | HRESULT i_notifyCompleteV(HRESULT aResultCode,
|
---|
134 | const GUID &aIID,
|
---|
135 | const char *pcszComponent,
|
---|
136 | const char *aText,
|
---|
137 | va_list va);
|
---|
138 | HRESULT i_notifyCompleteEI(HRESULT aResultCode,
|
---|
139 | const ComPtr<IVirtualBoxErrorInfo> &aErrorInfo);
|
---|
140 |
|
---|
141 | bool i_notifyPointOfNoReturn(void);
|
---|
142 | bool i_setCancelCallback(void (*pfnCallback)(void *), void *pvUser);
|
---|
143 |
|
---|
144 | static DECLCALLBACK(int) i_iprtProgressCallback(unsigned uPercentage, void *pvUser);
|
---|
145 | static DECLCALLBACK(int) i_vdProgressCallback(void *pvUser, unsigned uPercentage);
|
---|
146 |
|
---|
147 | protected:
|
---|
148 | DECLARE_EMPTY_CTOR_DTOR(Progress)
|
---|
149 |
|
---|
150 | #if !defined(VBOX_COM_INPROC)
|
---|
151 | /** Weak parent. */
|
---|
152 | VirtualBox * const mParent;
|
---|
153 | #endif
|
---|
154 | const ComObjPtr<EventSource> pEventSource;
|
---|
155 | const ComPtr<IUnknown> mInitiator;
|
---|
156 |
|
---|
157 | const Guid mId;
|
---|
158 | const com::Utf8Str mDescription;
|
---|
159 |
|
---|
160 | uint64_t m_ullTimestamp; // progress object creation timestamp, for ETA computation
|
---|
161 |
|
---|
162 | void (*m_pfnCancelCallback)(void *);
|
---|
163 | void *m_pvCancelUserArg;
|
---|
164 |
|
---|
165 | /* The fields below are to be properly initialized by subclasses */
|
---|
166 |
|
---|
167 | BOOL mCompleted;
|
---|
168 | BOOL mCancelable;
|
---|
169 | BOOL mCanceled;
|
---|
170 | HRESULT mResultCode;
|
---|
171 | ComPtr<IVirtualBoxErrorInfo> mErrorInfo;
|
---|
172 |
|
---|
173 | ULONG m_cOperations; // number of operations (so that progress dialog can
|
---|
174 | // display something like 1/3)
|
---|
175 | ULONG m_ulTotalOperationsWeight; // sum of weights of all operations, given to constructor
|
---|
176 |
|
---|
177 | ULONG m_ulOperationsCompletedWeight; // summed-up weight of operations that have been completed; initially 0
|
---|
178 |
|
---|
179 | ULONG m_ulCurrentOperation; // operations counter, incremented with
|
---|
180 | // each setNextOperation()
|
---|
181 | com::Utf8Str m_operationDescription; // name of current operation; initially
|
---|
182 | // from constructor, changed with setNextOperation()
|
---|
183 | ULONG m_ulCurrentOperationWeight; // weight of current operation, given to setNextOperation()
|
---|
184 | ULONG m_ulOperationPercent; // percentage of current operation, set with setCurrentOperationProgress()
|
---|
185 | ULONG m_cMsTimeout; /**< Automatic timeout value. 0 means none. */
|
---|
186 |
|
---|
187 | private:
|
---|
188 | // wrapped IProgress properties
|
---|
189 | HRESULT getId(com::Guid &aId);
|
---|
190 | HRESULT getDescription(com::Utf8Str &aDescription);
|
---|
191 | HRESULT getInitiator(ComPtr<IUnknown> &aInitiator);
|
---|
192 | HRESULT getCancelable(BOOL *aCancelable);
|
---|
193 | HRESULT getPercent(ULONG *aPercent);
|
---|
194 | HRESULT getTimeRemaining(LONG *aTimeRemaining);
|
---|
195 | HRESULT getCompleted(BOOL *aCompleted);
|
---|
196 | HRESULT getCanceled(BOOL *aCanceled);
|
---|
197 | HRESULT getResultCode(LONG *aResultCode);
|
---|
198 | HRESULT getErrorInfo(ComPtr<IVirtualBoxErrorInfo> &aErrorInfo);
|
---|
199 | HRESULT getOperationCount(ULONG *aOperationCount);
|
---|
200 | HRESULT getOperation(ULONG *aOperation);
|
---|
201 | HRESULT getOperationDescription(com::Utf8Str &aOperationDescription);
|
---|
202 | HRESULT getOperationPercent(ULONG *aOperationPercent);
|
---|
203 | HRESULT getOperationWeight(ULONG *aOperationWeight);
|
---|
204 | HRESULT getTimeout(ULONG *aTimeout);
|
---|
205 | HRESULT setTimeout(ULONG aTimeout);
|
---|
206 | HRESULT getEventSource(ComPtr<IEventSource> &aEventSource);
|
---|
207 |
|
---|
208 | // wrapped IProgress methods
|
---|
209 | HRESULT setCurrentOperationProgress(ULONG aPercent);
|
---|
210 | HRESULT setNextOperation(const com::Utf8Str &aNextOperationDescription,
|
---|
211 | ULONG aNextOperationsWeight);
|
---|
212 | HRESULT waitForCompletion(LONG aTimeout);
|
---|
213 | HRESULT waitForOperationCompletion(ULONG aOperation,
|
---|
214 | LONG aTimeout);
|
---|
215 | HRESULT waitForAsyncProgressCompletion(const ComPtr<IProgress> &aPProgressAsync);
|
---|
216 | HRESULT cancel();
|
---|
217 |
|
---|
218 | // internal helper methods
|
---|
219 | double i_calcTotalPercent();
|
---|
220 | void i_checkForAutomaticTimeout(void);
|
---|
221 |
|
---|
222 | RTSEMEVENTMULTI mCompletedSem;
|
---|
223 | ULONG mWaitersCount;
|
---|
224 |
|
---|
225 | private:
|
---|
226 | DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(Progress); /* Shuts up MSC warning C4625. */
|
---|
227 | };
|
---|
228 |
|
---|
229 | #endif /* ____H_PROGRESSIMPL */
|
---|
230 |
|
---|