• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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_notify.h"
17 
18 #include <uv.h>
19 
20 #include "js_task.h"
21 #include "log.h"
22 #include "napi_utils.h"
23 #include "uv_queue.h"
24 
25 namespace OHOS::Request {
26 constexpr int32_t MAX_WAIT_TIME = 3000;
27 BlockQueue<NotifyEventInfo> RequestNotify::notifyQueue_{ MAX_WAIT_TIME };
28 
RequestNotify(napi_env env,napi_value callback)29 RequestNotify::RequestNotify(napi_env env, napi_value callback) : NotifyStub()
30 {
31     std::lock_guard<std::mutex> lock(validMutex_);
32     env_ = env;
33     napi_create_reference(env, callback, 1, &ref_);
34     valid_ = true;
35 }
36 
~RequestNotify()37 RequestNotify::~RequestNotify()
38 {
39     std::lock_guard<std::mutex> lock(validMutex_);
40     if (valid_ && env_ != nullptr && ref_ != nullptr) {
41         UvQueue::DeleteRef(env_, ref_);
42         ref_ = nullptr;
43     }
44 }
45 
CallBack(const Notify & notify)46 void RequestNotify::CallBack(const Notify &notify)
47 {
48     REQUEST_HILOGD("RequestNotify CallBack in");
49     SetNotify(notify);
50     info_.timestamp = std::chrono::system_clock::now().time_since_epoch().count();
51     notifyQueue_.Push(info_);
52     NotifyDataPtr *dataPtr = new NotifyDataPtr();
53     dataPtr->callback = this;
54 
55     uv_loop_s *loop = nullptr;
56     napi_get_uv_event_loop(env_, &loop);
57     if (loop == nullptr) {
58         return;
59     }
60     uv_work_t *work = new (std::nothrow) uv_work_t;
61     if (work == nullptr) {
62         return;
63     }
64     work->data = reinterpret_cast<void *>(dataPtr);
65     uv_queue_work(
66         loop, work,
67         [](uv_work_t *work) {
68             if (work == nullptr) {
69                 return;
70             }
71             NotifyDataPtr *dataPtr = static_cast<NotifyDataPtr *>(work->data);
72             if (dataPtr != nullptr) {
73                 REQUEST_HILOGI("timestamp is %{public}" PRId64, dataPtr->callback->info_.timestamp);
74                 notifyQueue_.Wait(dataPtr->callback->info_);
75             }
76         },
77         [](uv_work_t *work, int status) {
78             if (work == nullptr) {
79                 return;
80             }
81             NotifyDataPtr *dataPtr = static_cast<NotifyDataPtr *>(work->data);
82             if (dataPtr != nullptr) {
83                 dataPtr->callback->ExecCallBack();
84                 delete dataPtr;
85             }
86             notifyQueue_.Pop();
87             delete work;
88         });
89 }
90 
Done(const TaskInfo & taskInfo)91 void RequestNotify::Done(const TaskInfo &taskInfo)
92 {
93 }
94 
ExecCallBack()95 void RequestNotify::ExecCallBack()
96 {
97     REQUEST_HILOGD("ExecCallBack in");
98     if (!valid_ || ref_ == nullptr) {
99         REQUEST_HILOGE("valid is false");
100         return;
101     }
102     napi_handle_scope scope = nullptr;
103     napi_open_handle_scope(env_, &scope);
104     napi_value callbackFunc = nullptr;
105     napi_get_reference_value(env_, ref_, &callbackFunc);
106     napi_value callbackResult = nullptr;
107     uint32_t paramNumber = 1;
108     napi_value callbackValues[NapiUtils::TWO_ARG] = { nullptr };
109     ConvertCallBackData(paramNumber, callbackValues, NapiUtils::TWO_ARG);
110     napi_call_function(env_, nullptr, callbackFunc, paramNumber, callbackValues, &callbackResult);
111     napi_close_handle_scope(env_, scope);
112 }
113 
ConvertCallBackData(uint32_t & paramNumber,napi_value * value,uint32_t valueSize)114 void RequestNotify::ConvertCallBackData(uint32_t &paramNumber, napi_value *value, uint32_t valueSize)
115 {
116     std::lock_guard<std::mutex> lock(notifyMutex_);
117     if (notify_.type == EventType::DATA_CALLBACK) {
118         paramNumber = notify_.data.size();
119         if (paramNumber > valueSize) {
120             return;
121         }
122         for (uint32_t i = 0; i < paramNumber; i++) {
123             value[i] = NapiUtils::Convert2JSValue(env_, notify_.data[i]);
124         }
125     } else if (notify_.type == EventType::HEADER_CALLBACK) {
126         value[0] = NapiUtils::Convert2JSHeadersAndBody(env_, notify_.progress.extras, notify_.progress.bodyBytes, true);
127     } else if (notify_.type == EventType::TASK_STATE_CALLBACK) {
128         value[0] = NapiUtils::Convert2JSValue(env_, notify_.taskStates);
129     } else if (notify_.type == EventType::PROGRESS_CALLBACK) {
130         value[0] = NapiUtils::Convert2JSValue(env_, notify_.progress);
131     }
132 }
133 
SetNotify(const Notify & notify)134 void RequestNotify::SetNotify(const Notify &notify)
135 {
136     std::lock_guard<std::mutex> lock(notifyMutex_);
137     notify_ = notify;
138 }
139 
DeleteCallbackRef()140 void RequestNotify::DeleteCallbackRef()
141 {
142     std::lock_guard<std::mutex> lock(validMutex_);
143     if (env_ != nullptr && ref_ != nullptr) {
144         valid_ = false;
145         napi_delete_reference(env_, ref_);
146         ref_ = nullptr;
147     }
148 }
149 } // namespace OHOS::Request