1 /*
2 * Copyright (c) 2021 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 #include <uv.h>
16
17 #include "bundle_status_callback.h"
18
19 #include "napi/native_common.h"
20
BundleStatusCallback(napi_env env,napi_ref addedCallback,napi_ref updatedCallback,napi_ref removeCallback)21 BundleStatusCallback::BundleStatusCallback(napi_env env, napi_ref addedCallback,
22 napi_ref updatedCallback,
23 napi_ref removeCallback)
24 : env_(env), addedCallback_(addedCallback),
25 updatedCallback_(updatedCallback), removeCallback_(removeCallback) {}
26
~BundleStatusCallback()27 BundleStatusCallback::~BundleStatusCallback()
28 {
29 uv_loop_s* loop = nullptr;
30 napi_get_uv_event_loop(env_, &loop);
31 uv_work_t* work = new uv_work_t;
32 DelRefCallbackInfo* delRefCallbackInfo = new DelRefCallbackInfo {
33 .env_ = env_,
34 .addedCallback_ = addedCallback_,
35 .updatedCallback_ = updatedCallback_,
36 .removeCallback_ = removeCallback_,
37 };
38 if (work == nullptr || delRefCallbackInfo == nullptr) {
39 return;
40 }
41 work->data = (void*)delRefCallbackInfo;
42 int ret = uv_queue_work(
43 loop, work, [](uv_work_t* work) {},
44 [](uv_work_t* work, int status) {
45 // JS Thread
46 DelRefCallbackInfo* delRefCallbackInfo = reinterpret_cast<DelRefCallbackInfo*>(work->data);
47 napi_delete_reference(delRefCallbackInfo->env_, delRefCallbackInfo->addedCallback_);
48 napi_delete_reference(delRefCallbackInfo->env_, delRefCallbackInfo->updatedCallback_);
49 napi_delete_reference(delRefCallbackInfo->env_, delRefCallbackInfo->removeCallback_);
50 if (delRefCallbackInfo != nullptr) {
51 delete delRefCallbackInfo;
52 delRefCallbackInfo = nullptr;
53 }
54 if (work != nullptr) {
55 delete work;
56 work = nullptr;
57 }
58 });
59 if (ret != 0) {
60 if (delRefCallbackInfo != nullptr) {
61 delete delRefCallbackInfo;
62 }
63 if (work != nullptr) {
64 delete work;
65 }
66 }
67 }
68
OnBundleAdded(const std::string & bundleName,const int userId)69 void BundleStatusCallback::OnBundleAdded(const std::string& bundleName, const int userId)
70 {
71 uv_loop_s* loop = nullptr;
72 napi_get_uv_event_loop(env_, &loop);
73 uv_work_t* work = new uv_work_t;
74 AsyncCallbackInfo* asyncCallbackInfo = new AsyncCallbackInfo {
75 .env_ = env_,
76 .callback_ = addedCallback_,
77 .bundleName_ = bundleName,
78 .userId_ = userId,
79 };
80 if (work == nullptr || asyncCallbackInfo == nullptr) {
81 return;
82 }
83 work->data = (void*)asyncCallbackInfo;
84 int ret = uv_queue_work(
85 loop, work, [](uv_work_t* work) {},
86 [](uv_work_t* work, int status) {
87 // JS Thread
88 AsyncCallbackInfo* asyncCallbackInfo = reinterpret_cast<AsyncCallbackInfo*>(work->data);
89 napi_value callback = nullptr;
90 napi_value placeHolder = nullptr;
91 napi_value result[2] = { 0 };
92 napi_get_reference_value(asyncCallbackInfo->env_, asyncCallbackInfo->callback_, &callback);
93 napi_create_string_utf8(
94 asyncCallbackInfo->env_, asyncCallbackInfo->bundleName_.c_str(), NAPI_AUTO_LENGTH, &result[0]);
95 napi_create_uint32(asyncCallbackInfo->env_, asyncCallbackInfo->userId_, &result[1]);
96 napi_call_function(
97 asyncCallbackInfo->env_, nullptr, callback, sizeof(result) / sizeof(result[0]), result, &placeHolder);
98 if (asyncCallbackInfo != nullptr) {
99 delete asyncCallbackInfo;
100 asyncCallbackInfo = nullptr;
101 }
102 if (work != nullptr) {
103 delete work;
104 work = nullptr;
105 }
106 });
107 if (ret != 0) {
108 if (asyncCallbackInfo != nullptr) {
109 delete asyncCallbackInfo;
110 }
111 if (work != nullptr) {
112 delete work;
113 }
114 }
115 }
116
OnBundleUpdated(const std::string & bundleName,const int userId)117 void BundleStatusCallback::OnBundleUpdated(const std::string& bundleName, const int userId)
118 {
119 uv_loop_s* loop = nullptr;
120 napi_get_uv_event_loop(env_, &loop);
121 uv_work_t* work = new uv_work_t;
122 AsyncCallbackInfo* asyncCallbackInfo = new AsyncCallbackInfo {
123 .env_ = env_,
124 .callback_ = updatedCallback_,
125 .bundleName_ = bundleName,
126 .userId_ = userId,
127 };
128 if (work == nullptr || asyncCallbackInfo == nullptr) {
129 return;
130 }
131 work->data = (void*)asyncCallbackInfo;
132 int ret = uv_queue_work(
133 loop, work, [](uv_work_t* work) {},
134 [](uv_work_t* work, int status) {
135 // JS Thread
136 AsyncCallbackInfo* asyncCallbackInfo = reinterpret_cast<AsyncCallbackInfo*>(work->data);
137 napi_value callback = nullptr;
138 napi_value placeHolder = nullptr;
139 napi_value result[2] = { 0 };
140 napi_get_reference_value(asyncCallbackInfo->env_, asyncCallbackInfo->callback_, &callback);
141 napi_create_string_utf8(
142 asyncCallbackInfo->env_, asyncCallbackInfo->bundleName_.c_str(), NAPI_AUTO_LENGTH, &result[0]);
143 napi_create_uint32(asyncCallbackInfo->env_, asyncCallbackInfo->userId_, &result[1]);
144 napi_call_function(
145 asyncCallbackInfo->env_, nullptr, callback, sizeof(result) / sizeof(result[0]), result, &placeHolder);
146 if (asyncCallbackInfo != nullptr) {
147 delete asyncCallbackInfo;
148 asyncCallbackInfo = nullptr;
149 }
150 if (work != nullptr) {
151 delete work;
152 work = nullptr;
153 }
154 });
155 if (ret != 0) {
156 if (asyncCallbackInfo != nullptr) {
157 delete asyncCallbackInfo;
158 }
159 if (work != nullptr) {
160 delete work;
161 }
162 }
163 }
164
OnBundleRemoved(const std::string & bundleName,const int userId)165 void BundleStatusCallback::OnBundleRemoved(const std::string& bundleName, const int userId)
166 {
167 uv_loop_s* loop = nullptr;
168 napi_get_uv_event_loop(env_, &loop);
169 uv_work_t* work = new uv_work_t;
170 AsyncCallbackInfo* asyncCallbackInfo = new AsyncCallbackInfo {
171 .env_ = env_,
172 .callback_ = removeCallback_,
173 .bundleName_ = bundleName,
174 .userId_ = userId,
175 };
176 if (work == nullptr || asyncCallbackInfo == nullptr) {
177 return;
178 }
179 work->data = (void*)asyncCallbackInfo;
180 int ret = uv_queue_work(
181 loop, work, [](uv_work_t* work) {},
182 [](uv_work_t* work, int status) {
183 // JS Thread
184 AsyncCallbackInfo* asyncCallbackInfo = reinterpret_cast<AsyncCallbackInfo*>(work->data);
185 napi_value callback = nullptr;
186 napi_value placeHolder = nullptr;
187 napi_value result[2] = { 0 };
188 napi_get_reference_value(asyncCallbackInfo->env_, asyncCallbackInfo->callback_, &callback);
189 napi_create_string_utf8(
190 asyncCallbackInfo->env_, asyncCallbackInfo->bundleName_.c_str(), NAPI_AUTO_LENGTH, &result[0]);
191 napi_create_uint32(asyncCallbackInfo->env_, asyncCallbackInfo->userId_, &result[1]);
192 napi_call_function(
193 asyncCallbackInfo->env_, nullptr, callback, sizeof(result) / sizeof(result[0]), result, &placeHolder);
194 if (asyncCallbackInfo != nullptr) {
195 delete asyncCallbackInfo;
196 asyncCallbackInfo = nullptr;
197 }
198 if (work != nullptr) {
199 delete work;
200 work = nullptr;
201 }
202 });
203 if (ret != 0) {
204 if (asyncCallbackInfo != nullptr) {
205 delete asyncCallbackInfo;
206 }
207 if (work != nullptr) {
208 delete work;
209 }
210 }
211 }