• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "header_receive_callback.h"
17 #include "upload_task.h"
18 using namespace OHOS::Request::UploadNapi;
19 
20 namespace OHOS::Request::Upload {
HeaderReceiveCallback(napi_env env,napi_value callback)21 HeaderReceiveCallback::HeaderReceiveCallback(napi_env env, napi_value callback)
22     : env_(env)
23 {
24     napi_create_reference(env, callback, 1, &callback_);
25     napi_get_uv_event_loop(env, &loop_);
26 }
27 
~HeaderReceiveCallback()28 HeaderReceiveCallback::~HeaderReceiveCallback()
29 {
30     napi_delete_reference(env_, callback_);
31 }
32 
GetCallback()33 napi_ref HeaderReceiveCallback::GetCallback()
34 {
35     return callback_;
36 }
37 
GetHeader()38 std::string HeaderReceiveCallback::GetHeader()
39 {
40     std::lock_guard<std::mutex> lock(mutex_);
41     return header_;
42 }
43 
GetEnv()44 napi_env HeaderReceiveCallback::GetEnv()
45 {
46     return env_;
47 }
48 
UvOnHeaderReceive(uv_work_t * work,int status)49 void UvOnHeaderReceive(uv_work_t *work, int status)
50 {
51     UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "HeaderReceive. uv_queue_work start");
52     std::shared_ptr<HeaderReceiveWorker> headerReceiveWorker (reinterpret_cast<HeaderReceiveWorker *>
53         (work->data), [work](HeaderReceiveWorker *data) {
54             delete data;
55             delete work;
56     });
57     if (headerReceiveWorker == nullptr || headerReceiveWorker->observer == nullptr) {
58         UPLOAD_HILOGE(UPLOAD_MODULE_JS_NAPI, "headerReceiveWorker->observer == nullptr");
59         return;
60     }
61     napi_handle_scope scope = nullptr;
62     napi_open_handle_scope(headerReceiveWorker->observer->GetEnv(), &scope);
63     napi_value jsHeader = nullptr;
64     napi_value callback = nullptr;
65     napi_value args[1];
66     napi_value global = nullptr;
67     napi_value result;
68     napi_status callStatus = napi_generic_failure;
69 
70     jsHeader = UploadNapi::JSUtil::Convert2JSString(headerReceiveWorker->observer->GetEnv(),
71         headerReceiveWorker->observer->GetHeader());
72     args[0] = { jsHeader };
73 
74     napi_get_reference_value(headerReceiveWorker->observer->GetEnv(),
75                              headerReceiveWorker->observer->GetCallback(), &callback);
76     napi_get_global(headerReceiveWorker->observer->GetEnv(), &global);
77     callStatus = napi_call_function(headerReceiveWorker->observer->GetEnv(), global, callback, 1, args, &result);
78     if (callStatus != napi_ok) {
79         UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI,
80             "HeaderReceive callback failed callStatus:%{public}d", callStatus);
81     }
82     napi_close_handle_scope(headerReceiveWorker->observer->GetEnv(), scope);
83 }
84 
HeaderReceive(const std::string & header)85 void HeaderReceiveCallback::HeaderReceive(const std::string &header)
86 {
87     UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "HeaderReceive. header : %{public}s", header.c_str());
88     HeaderReceiveWorker *headerReceiveWorker = new (std::nothrow)HeaderReceiveWorker();
89     if (headerReceiveWorker == nullptr) {
90         UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Failed to create headerReceiveWorker");
91         return;
92     }
93 
94     {
95         std::lock_guard<std::mutex> lock(mutex_);
96         headerReceiveWorker->observer = shared_from_this();
97         header_ = header;
98     }
99 
100     uv_work_t *work = new (std::nothrow)uv_work_t();
101     if (work == nullptr) {
102         UPLOAD_HILOGD(UPLOAD_MODULE_JS_NAPI, "Failed to create uv work");
103         delete headerReceiveWorker;
104         return;
105     }
106     work->data = headerReceiveWorker;
107     int ret = uv_queue_work(loop_, work, [](uv_work_t *work) {}, UvOnHeaderReceive);
108     if (ret != 0) {
109         UPLOAD_HILOGE(UPLOAD_MODULE_JS_NAPI, "HeaderReceive. uv_queue_work Failed");
110         delete headerReceiveWorker;
111         delete work;
112     }
113 }
114 } // end of OHOS::Request::Upload