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 "wrapper.h"
17 
18 #include <memory>
19 
20 #include "http_client_error.h"
21 #include "wrapper.rs.h"
22 namespace OHOS::Request {
23 using namespace OHOS::NetStack::HttpClient;
24 
OnCallback(std::shared_ptr<HttpClientTask> task, rust::Box<CallbackWrapper> callback)25 void OnCallback(std::shared_ptr<HttpClientTask> task, rust::Box<CallbackWrapper> callback)
26 {
27     CallbackWrapper *raw_ptr = callback.into_raw();
28     auto shared = std::shared_ptr<CallbackWrapper>(
29         raw_ptr, [](CallbackWrapper *ptr) { rust::Box<CallbackWrapper>::from_raw(ptr); });
30     task->OnSuccess(
31         [shared](const HttpClientRequest &, const HttpClientResponse &response) { shared->on_success(response); });
32     task->OnFail([shared](const HttpClientRequest &, const HttpClientResponse &response,
33                      const HttpClientError &error) { shared->on_fail(response, error); });
34     task->OnCancel(
35         [shared](const HttpClientRequest &, const HttpClientResponse &response) { shared->on_cancel(response); });
36     task->OnDataReceive(
37         [shared](const HttpClientRequest &, const uint8_t *data, size_t size) { shared->on_data_receive(data, size); });
38     task->OnProgress([shared](const HttpClientRequest &, u_long dlTotal, u_long dlNow, u_long ulTotal, u_long ulNow) {
39         shared->on_progress(dlTotal, dlNow, ulTotal, ulNow);
40     });
41 }
42 
43 } // namespace OHOS::Request