24
24
25
25
namespace common {
26
26
27
- class HttpFileDownloader ::Runner : public std::enable_shared_from_this<Runner>
28
- {
29
- public:
30
- Runner (std::shared_ptr<base::TaskRunner> owner_task_runner, Delegate* delegate)
31
- : owner_task_runner_(std::move(owner_task_runner)),
32
- delegate_ (delegate)
33
- {
34
- DCHECK (owner_task_runner_);
35
- DCHECK (delegate_);
36
- }
37
-
38
- ~Runner ()
39
- {
40
- dettach ();
41
- }
42
-
43
- void dettach ()
44
- {
45
- delegate_ = nullptr ;
46
- }
47
-
48
- void onError (int error_code)
49
- {
50
- if (!owner_task_runner_->belongsToCurrentThread ())
51
- {
52
- owner_task_runner_->postTask (std::bind (&Runner::onError, shared_from_this (), error_code));
53
- return ;
54
- }
55
-
56
- if (delegate_)
57
- {
58
- delegate_->onFileDownloaderError (error_code);
59
- delegate_ = nullptr ;
60
- }
61
- }
62
-
63
- void onCompleted ()
64
- {
65
- if (!owner_task_runner_->belongsToCurrentThread ())
66
- {
67
- owner_task_runner_->postTask (std::bind (&Runner::onCompleted, shared_from_this ()));
68
- return ;
69
- }
70
-
71
- if (delegate_)
72
- {
73
- delegate_->onFileDownloaderCompleted ();
74
- delegate_ = nullptr ;
75
- }
76
- }
77
-
78
- void onProgress (int percentage)
79
- {
80
- if (!owner_task_runner_->belongsToCurrentThread ())
81
- {
82
- owner_task_runner_->postTask (std::bind (&Runner::onProgress, shared_from_this (), percentage));
83
- return ;
84
- }
85
-
86
- if (delegate_)
87
- delegate_->onFileDownloaderProgress (percentage);
88
- }
89
-
90
- private:
91
- std::shared_ptr<base::TaskRunner> owner_task_runner_;
92
- Delegate* delegate_ = nullptr ;
93
-
94
- DISALLOW_COPY_AND_ASSIGN (Runner);
95
- };
96
-
97
27
// --------------------------------------------------------------------------------------------------
98
- HttpFileDownloader::HttpFileDownloader ()
28
+ HttpFileDownloader::HttpFileDownloader (QObject* parent)
29
+ : QThread(parent)
99
30
{
100
31
LOG (LS_INFO) << " Ctor" ;
101
32
}
@@ -104,24 +35,20 @@ HttpFileDownloader::HttpFileDownloader()
104
35
HttpFileDownloader::~HttpFileDownloader ()
105
36
{
106
37
LOG (LS_INFO) << " Dtor" ;
107
-
108
- if (runner_)
109
- {
110
- runner_->dettach ();
111
- runner_.reset ();
112
- }
113
- thread_.stop ();
38
+ interrupted_.store (true , std::memory_order_relaxed);
39
+ wait ();
114
40
}
115
41
116
42
// --------------------------------------------------------------------------------------------------
117
- void HttpFileDownloader::start (const QString& url,
118
- std::shared_ptr<base::TaskRunner> owner_task_runner,
119
- Delegate* delegate)
43
+ void HttpFileDownloader::setUrl (const QString& url)
120
44
{
121
- LOG (LS_INFO) << " Starting http file downloader" ;
122
45
url_ = url;
123
- runner_ = std::make_shared<Runner>(std::move (owner_task_runner), delegate);
124
- thread_.start (std::bind (&HttpFileDownloader::run, this ));
46
+ }
47
+
48
+ // --------------------------------------------------------------------------------------------------
49
+ const QByteArray& HttpFileDownloader::data () const
50
+ {
51
+ return data_;
125
52
}
126
53
127
54
// --------------------------------------------------------------------------------------------------
@@ -153,7 +80,8 @@ static int debugFunc(
153
80
// --------------------------------------------------------------------------------------------------
154
81
void HttpFileDownloader::run ()
155
82
{
156
- LOG (LS_INFO) << " run BEGIN" ;
83
+ LOG (LS_INFO) << " Starting http file downloader:" << url_;
84
+ interrupted_.store (false , std::memory_order_relaxed);
157
85
158
86
base::ScopedCURL curl;
159
87
@@ -201,7 +129,7 @@ void HttpFileDownloader::run()
201
129
break ;
202
130
}
203
131
204
- if (thread_. isStopping ( ))
132
+ if (interrupted_. load (std::memory_order_relaxed ))
205
133
{
206
134
LOG (LS_INFO) << " Downloading canceled" ;
207
135
break ;
@@ -211,18 +139,16 @@ void HttpFileDownloader::run()
211
139
212
140
curl_multi_remove_handle (multi_curl.get (), curl.get ());
213
141
214
- if (!thread_. isStopping ( ))
142
+ if (!interrupted_. load (std::memory_order_relaxed ))
215
143
{
216
144
if (error_code != CURLM_OK)
217
145
{
218
- if (runner_)
219
- runner_->onError (error_code);
146
+ emit sig_downloadError (error_code);
220
147
}
221
148
else
222
149
{
223
150
LOG (LS_INFO) << " Download is finished: " << data_.size () << " bytes" ;
224
- if (runner_)
225
- runner_->onCompleted ();
151
+ emit sig_downloadCompleted ();
226
152
}
227
153
}
228
154
@@ -238,7 +164,7 @@ size_t HttpFileDownloader::writeDataCallback(
238
164
239
165
if (self)
240
166
{
241
- if (self->thread_ . isStopping ( ))
167
+ if (self->interrupted_ . load (std::memory_order_relaxed ))
242
168
{
243
169
LOG (LS_INFO) << " Interrupted by user" ;
244
170
return 0 ;
@@ -256,14 +182,13 @@ size_t HttpFileDownloader::writeDataCallback(
256
182
int HttpFileDownloader::progressCallback (
257
183
HttpFileDownloader* self, double dltotal, double dlnow, double /* ultotal */ , double /* ulnow */ )
258
184
{
259
- if (self && !self->thread_ . isStopping ( ))
185
+ if (self && !self->interrupted_ . load (std::memory_order_relaxed ))
260
186
{
261
187
int percentage = 0 ;
262
188
if (dltotal > 0 )
263
189
percentage = static_cast <int >((dlnow * 100 ) / dltotal);
264
190
265
- if (self->runner_ )
266
- self->runner_ ->onProgress (percentage);
191
+ emit self->sig_downloadProgress (percentage);
267
192
}
268
193
269
194
return 0 ;
0 commit comments