This repository was archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathHttpDownloadManager.cpp
More file actions
70 lines (57 loc) · 2.28 KB
/
Copy pathHttpDownloadManager.cpp
File metadata and controls
70 lines (57 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) 2022 The Orbit Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "Http/HttpDownloadManager.h"
#include <QList>
#include <optional>
#include <type_traits>
#include <utility>
#include "HttpDownloadOperation.h"
#include "OrbitBase/Logging.h"
#include "OrbitBase/Promise.h"
namespace orbit_http {
using orbit_base::CanceledOr;
using orbit_base::Future;
using orbit_base::NotFoundOr;
using orbit_base::Promise;
using orbit_base::StopToken;
HttpDownloadManager::~HttpDownloadManager() {
for (const auto& download_operation : findChildren<HttpDownloadOperation*>()) {
download_operation->Abort();
}
}
Future<ErrorMessageOr<CanceledOr<NotFoundOr<void>>>> HttpDownloadManager::Download(
std::string url, std::filesystem::path save_file_path, orbit_base::StopToken stop_token) {
Promise<ErrorMessageOr<CanceledOr<NotFoundOr<void>>>> promise;
auto future = promise.GetFuture();
auto current_download_operation = new HttpDownloadOperation{
std::move(url), std::move(save_file_path), std::move(stop_token), &manager_, this};
auto finish_handler = [current_download_operation, promise = std::move(promise)](
HttpDownloadOperation::State state,
std::optional<std::string> maybe_error_msg) mutable {
if (promise.HasResult()) return;
current_download_operation->deleteLater();
switch (state) {
case HttpDownloadOperation::State::kError:
promise.SetResult(ErrorMessage{std::move(maybe_error_msg.value())});
break;
case HttpDownloadOperation::State::kCancelled:
promise.SetResult(orbit_base::Canceled{});
break;
case HttpDownloadOperation::State::kNotFound:
promise.SetResult(orbit_base::NotFound{""});
break;
case HttpDownloadOperation::State::kDone:
promise.SetResult(outcome::success());
break;
case HttpDownloadOperation::State::kStarted:
case HttpDownloadOperation::State::kInitial:
ORBIT_UNREACHABLE();
}
};
QObject::connect(current_download_operation, &HttpDownloadOperation::finished,
std::move(finish_handler));
current_download_operation->Start();
return future;
}
} // namespace orbit_http