• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "request_preload.h"
17 
18 #include <cstdint>
19 #include <memory>
20 
21 #include "cxx.h"
22 #include "wrapper.rs.h"
23 
24 namespace OHOS::Request {
Data(rust::Box<RustData> && data)25 Data::Data(rust::Box<RustData> &&data)
26 {
27     data_ = data.into_raw();
28 }
29 
~Data()30 Data::~Data()
31 {
32     rust::Box<RustData>::from_raw(data_);
33 }
34 
Data(Data && other)35 Data::Data(Data &&other) noexcept : data_(other.data_)
36 {
37     other.data_ = nullptr;
38 }
39 
operator =(Data && other)40 Data &Data::operator=(Data &&other) & noexcept
41 {
42     if (data_) {
43         rust::Box<RustData>::from_raw(data_);
44     }
45     data_ = other.data_;
46     other.data_ = nullptr;
47     return *this;
48 }
49 
CppDownloadInfo(rust::Box<RustDownloadInfo> rust_info)50 CppDownloadInfo::CppDownloadInfo(rust::Box<RustDownloadInfo> rust_info)
51 {
52     rust_info_ = rust_info.into_raw();
53 }
54 
~CppDownloadInfo()55 CppDownloadInfo::~CppDownloadInfo()
56 {
57     rust::Box<RustDownloadInfo>::from_raw(rust_info_);
58 }
59 
CppDownloadInfo(CppDownloadInfo && other)60 CppDownloadInfo::CppDownloadInfo(CppDownloadInfo &&other) noexcept : rust_info_(other.rust_info_)
61 {
62     other.rust_info_ = nullptr;
63 }
64 
operator =(CppDownloadInfo && other)65 CppDownloadInfo &CppDownloadInfo::operator=(CppDownloadInfo &&other) noexcept
66 {
67     if (this != &other) {
68         if (rust_info_) {
69             rust::Box<RustDownloadInfo>::from_raw(rust_info_);
70         }
71         rust_info_ = other.rust_info_;
72         other.rust_info_ = nullptr;
73     }
74     return *this;
75 }
76 
dns_time() const77 double CppDownloadInfo::dns_time() const
78 {
79     return rust_info_->dns_time();
80 }
81 
connect_time() const82 double CppDownloadInfo::connect_time() const
83 {
84     return rust_info_->connect_time();
85 }
86 
total_time() const87 double CppDownloadInfo::total_time() const
88 {
89     return rust_info_->total_time();
90 }
91 
tls_time() const92 double CppDownloadInfo::tls_time() const
93 {
94     return rust_info_->tls_time();
95 }
96 
first_send_time() const97 double CppDownloadInfo::first_send_time() const
98 {
99     return rust_info_->first_send_time();
100 }
101 
first_recv_time() const102 double CppDownloadInfo::first_recv_time() const
103 {
104     return rust_info_->first_recv_time();
105 }
106 
redirect_time() const107 double CppDownloadInfo::redirect_time() const
108 {
109     return rust_info_->redirect_time();
110 }
111 
resource_size() const112 int64_t CppDownloadInfo::resource_size() const
113 {
114     return rust_info_->resource_size();
115 }
116 
network_ip() const117 std::string CppDownloadInfo::network_ip() const
118 {
119     return std::string(rust_info_->ip());
120 }
121 
dns_servers() const122 std::vector<std::string> CppDownloadInfo::dns_servers() const
123 {
124     std::vector<std::string> result;
125 
126     const auto &servers = rust_info_->dns_servers();
127 
128     for (const auto &server : servers) {
129         result.push_back(std::string(server));
130     }
131 
132     return result;
133 }
134 
Slice(std::unique_ptr<rust::Slice<T>> && slice)135 template<typename T> Slice<T>::Slice(std::unique_ptr<rust::Slice<T>> &&slice) : slice_(std::move(slice))
136 {
137 }
138 
~Slice()139 template<typename T> Slice<T>::~Slice()
140 {
141 }
142 
data() const143 template<typename T> T *Slice<T>::data() const noexcept
144 {
145     return slice_->data();
146 }
147 
size() const148 template<typename T> std::size_t Slice<T>::size() const noexcept
149 {
150     return slice_->size();
151 }
152 
length() const153 template<typename T> std::size_t Slice<T>::length() const noexcept
154 {
155     return slice_->length();
156 }
157 
empty() const158 template<typename T> bool Slice<T>::empty() const noexcept
159 {
160     return slice_->empty();
161 }
162 
operator [](std::size_t n) const163 template<typename T> T &Slice<T>::operator[](std::size_t n) const noexcept
164 {
165     return (*slice_)[n];
166 }
167 
bytes() const168 Slice<const uint8_t> Data::bytes() const
169 {
170     auto bytes = std::make_unique<rust::Slice<const uint8_t>>(data_->bytes());
171     return Slice<const uint8_t>(std::move(bytes));
172 }
173 
rustSlice() const174 rust::Slice<const uint8_t> Data::rustSlice() const
175 {
176     return data_->bytes();
177 }
178 
PreloadError(rust::Box<CacheDownloadError> && error)179 PreloadError::PreloadError(rust::Box<CacheDownloadError> &&error)
180 {
181     error_ = error.into_raw();
182 }
183 
PreloadError(PreloadError && other)184 PreloadError::PreloadError(PreloadError &&other) noexcept : error_(other.error_)
185 {
186     other.error_ = nullptr;
187 }
188 
operator =(PreloadError && other)189 PreloadError &PreloadError::operator=(PreloadError &&other) & noexcept
190 {
191     if (error_) {
192         rust::Box<CacheDownloadError>::from_raw(error_);
193     }
194     error_ = other.error_;
195     other.error_ = nullptr;
196     return *this;
197 }
198 
~PreloadError()199 PreloadError::~PreloadError()
200 {
201     rust::Box<CacheDownloadError>::from_raw(error_);
202 }
203 
GetCode() const204 int32_t PreloadError::GetCode() const
205 {
206     return error_->code();
207 }
208 
GetMessage() const209 std::string PreloadError::GetMessage() const
210 {
211     return std::string(error_->message());
212 }
213 
GetErrorKind() const214 ErrorKind PreloadError::GetErrorKind() const
215 {
216     return static_cast<ErrorKind>(error_->ffi_kind());
217 }
218 
Preload()219 Preload::Preload()
220 {
221     agent_ = cache_download_service();
222 }
223 
PreloadHandle(PreloadHandle && other)224 PreloadHandle::PreloadHandle(PreloadHandle &&other) noexcept : handle_(other.handle_)
225 {
226     other.handle_ = nullptr;
227 }
228 
operator =(PreloadHandle && other)229 PreloadHandle &PreloadHandle::operator=(PreloadHandle &&other) & noexcept
230 {
231     if (handle_) {
232         rust::Box<TaskHandle>::from_raw(handle_);
233     }
234     handle_ = other.handle_;
235     other.handle_ = nullptr;
236     return *this;
237 }
238 
load(std::string const & url,std::unique_ptr<PreloadCallback> callback,std::unique_ptr<PreloadOptions> options,bool update)239 std::shared_ptr<PreloadHandle> Preload::load(std::string const &url, std::unique_ptr<PreloadCallback> callback,
240     std::unique_ptr<PreloadOptions> options, bool update)
241 {
242     auto callback_wrapper = std::make_unique<PreloadCallbackWrapper>(callback);
243 
244     std::shared_ptr<PreloadProgressCallbackWrapper> progress_callback_wrapper = nullptr;
245     if (callback != nullptr && callback->OnProgress != nullptr) {
246         progress_callback_wrapper = std::make_shared<PreloadProgressCallbackWrapper>(callback);
247     }
248 
249     FfiPredownloadOptions ffiOptions = { .headers = rust::Vec<rust::str>() };
250     if (options != nullptr) {
251         for (const auto& [key, value] : options->headers) {
252             ffiOptions.headers.push_back(rust::str(key));
253             ffiOptions.headers.push_back(rust::str(value));
254         }
255     }
256     auto taskHandle = agent_->ffi_preload(
257         rust::str(url), std::move(callback_wrapper), std::move(progress_callback_wrapper), update, ffiOptions);
258     return taskHandle;
259 }
260 
fetch(std::string const & url)261 std::optional<Data> Preload::fetch(std::string const &url)
262 {
263     std::unique_ptr<Data> data = agent_->ffi_fetch(rust::str(url));
264     if (data == nullptr) {
265         return std::nullopt;
266     }
267     return std::move(*data);
268 }
269 
GetDownloadInfo(std::string const & url)270 std::optional<CppDownloadInfo> Preload::GetDownloadInfo(std::string const &url)
271 {
272     std::unique_ptr<CppDownloadInfo> info = agent_->ffi_get_download_info(rust::str(url));
273     if (info == nullptr) {
274         return std::nullopt;
275     }
276     return std::move(*info);
277 }
278 
SetRamCacheSize(uint64_t size)279 void Preload::SetRamCacheSize(uint64_t size)
280 {
281     agent_->set_ram_cache_size(size);
282 }
SetFileCacheSize(uint64_t size)283 void Preload::SetFileCacheSize(uint64_t size)
284 {
285     agent_->set_file_cache_size(size);
286 }
287 
SetDownloadInfoListSize(uint16_t size)288 void Preload::SetDownloadInfoListSize(uint16_t size)
289 {
290     agent_->set_info_list_size(size);
291 }
292 
Cancel(std::string const & url)293 void Preload::Cancel(std::string const &url)
294 {
295     agent_->cancel(rust::str(url));
296 }
297 
Remove(std::string const & url)298 void Preload::Remove(std::string const &url)
299 {
300     agent_->remove(rust::str(url));
301 }
302 
Contains(const std::string & url)303 bool Preload::Contains(const std::string &url)
304 {
305     return agent_->contains(rust::str(url));
306 }
307 
GetInstance()308 Preload *Preload::GetInstance()
309 {
310     static Preload agent;
311     return &agent;
312 }
313 
PreloadHandle(rust::Box<TaskHandle> handle)314 PreloadHandle::PreloadHandle(rust::Box<TaskHandle> handle)
315 {
316     handle_ = handle.into_raw();
317 }
318 
~PreloadHandle()319 PreloadHandle::~PreloadHandle()
320 {
321     rust::Box<TaskHandle>::from_raw(handle_);
322 }
323 
Cancel()324 void PreloadHandle::Cancel()
325 {
326     handle_->cancel();
327 }
328 
GetTaskId()329 std::string PreloadHandle::GetTaskId()
330 {
331     return std::string(handle_->task_id());
332 }
333 
IsFinish()334 bool PreloadHandle::IsFinish()
335 {
336     return handle_->is_finish();
337 }
338 
GetState()339 PreloadState PreloadHandle::GetState()
340 {
341     return static_cast<PreloadState>(handle_->state());
342 }
343 template class Slice<const uint8_t>;
344 
345 } // namespace OHOS::Request