1 /* 2 * Copyright (C) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef REQUEST_PRE_DOWNLOAD_H 17 #define REQUEST_PRE_DOWNLOAD_H 18 19 #include <cstdint> 20 #include <functional> 21 #include <memory> 22 #include <optional> 23 #include <tuple> 24 #include <vector> 25 26 namespace rust { 27 inline namespace cxxbridge1 { 28 template<typename T> class Box; 29 template<typename T> class Slice; 30 } // namespace cxxbridge1 31 } // namespace rust 32 33 namespace OHOS::Request { 34 struct RustData; 35 struct TaskHandle; 36 struct CacheDownloadService; 37 struct CacheDownloadError; 38 struct RustDownloadInfo; 39 40 enum class PreloadState { 41 INIT, 42 RUNNING, 43 SUCCESS, 44 FAIL, 45 CANCEL, 46 }; 47 48 template<typename T> class Slice { 49 public: 50 Slice(std::unique_ptr<rust::Slice<T>> &&slice); 51 ~Slice(); 52 T *data() const noexcept; 53 std::size_t size() const noexcept; 54 std::size_t length() const noexcept; 55 bool empty() const noexcept; 56 T &operator[](std::size_t n) const noexcept; 57 58 private: 59 std::unique_ptr<rust::Slice<T>> slice_; 60 }; 61 62 class Data { 63 public: 64 Data(rust::Box<RustData> &&data); 65 Data(Data &&) noexcept; 66 ~Data(); 67 Data &operator=(Data &&) & noexcept; 68 69 Slice<const uint8_t> bytes() const; 70 rust::Slice<const uint8_t> rustSlice() const; 71 72 private: 73 RustData *data_; 74 }; 75 76 enum ErrorKind { 77 HTTP, 78 IO, 79 CACHE, 80 }; 81 82 class PreloadError { 83 public: 84 PreloadError(rust::Box<CacheDownloadError> &&error); 85 PreloadError(PreloadError &&) noexcept; 86 PreloadError &operator=(PreloadError &&) & noexcept; 87 ~PreloadError(); 88 89 int32_t GetCode() const; 90 std::string GetMessage() const; 91 ErrorKind GetErrorKind() const; 92 93 private: 94 CacheDownloadError *error_; 95 }; 96 97 struct PreloadCallback { 98 std::function<void(const std::shared_ptr<Data> &&, const std::string &TaskId)> OnSuccess; 99 std::function<void(const PreloadError &, const std::string &TaskId)> OnFail; 100 std::function<void()> OnCancel; 101 std::function<void(uint64_t current, uint64_t total)> OnProgress; 102 }; 103 104 class PreloadHandle { 105 public: 106 PreloadHandle(PreloadHandle &&) noexcept; 107 PreloadHandle(rust::Box<TaskHandle>); 108 PreloadHandle &operator=(PreloadHandle &&) & noexcept; 109 110 ~PreloadHandle(); 111 void Cancel(); 112 std::string GetTaskId(); 113 bool IsFinish(); 114 PreloadState GetState(); 115 116 private: 117 TaskHandle *handle_; 118 }; 119 120 struct PreloadOptions { 121 std::vector<std::tuple<std::string, std::string>> headers; 122 }; 123 124 class CppDownloadInfo { 125 public: 126 CppDownloadInfo(rust::Box<RustDownloadInfo> rust_info); 127 CppDownloadInfo(CppDownloadInfo &&other) noexcept; 128 CppDownloadInfo &operator=(CppDownloadInfo &&other) noexcept; 129 130 CppDownloadInfo(const CppDownloadInfo &) = delete; 131 CppDownloadInfo &operator=(const CppDownloadInfo &) = delete; 132 133 ~CppDownloadInfo(); 134 135 double dns_time() const; 136 double connect_time() const; 137 double tls_time() const; 138 double first_send_time() const; 139 double first_recv_time() const; 140 double redirect_time() const; 141 double total_time() const; 142 int64_t resource_size() const; 143 std::string network_ip() const; 144 std::vector<std::string> dns_servers() const; 145 146 private: 147 RustDownloadInfo *rust_info_; 148 }; 149 150 class Preload { 151 public: 152 Preload(); 153 static Preload *GetInstance(); 154 virtual ~Preload() = default; 155 void Cancel(std::string const &url); 156 void Remove(std::string const &url); 157 bool Contains(std::string const &url); 158 159 void SetRamCacheSize(uint64_t size); 160 void SetFileCacheSize(uint64_t size); 161 void SetDownloadInfoListSize(uint16_t size); 162 163 std::shared_ptr<PreloadHandle> load(std::string const &url, std::unique_ptr<PreloadCallback>, 164 std::unique_ptr<PreloadOptions> options = nullptr, bool update = false); 165 166 std::optional<Data> fetch(std::string const &url); 167 std::optional<CppDownloadInfo> GetDownloadInfo(std::string const &url); 168 169 private: 170 const CacheDownloadService *agent_; 171 }; 172 173 } // namespace OHOS::Request 174 175 #endif // REQUEST_PRE_DOWNLOAD_H