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 #include "nmea_message_callback_napi.h"
16 
17 #include "common_utils.h"
18 #include "ipc_skeleton.h"
19 #include "location_log.h"
20 #include "napi_util.h"
21 #include "constant_definition.h"
22 #include "napi/native_api.h"
23 #include "napi/native_common.h"
24 
25 namespace OHOS {
26 namespace Location {
27 static std::mutex g_regCallbackMutex;
28 static std::vector<napi_ref> g_registerCallbacks;
NmeaMessageCallbackNapi()29 NmeaMessageCallbackNapi::NmeaMessageCallbackNapi()
30 {
31     env_ = nullptr;
32     handlerCb_ = nullptr;
33     remoteDied_ = false;
34 }
35 
~NmeaMessageCallbackNapi()36 NmeaMessageCallbackNapi::~NmeaMessageCallbackNapi()
37 {
38 }
39 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)40 int NmeaMessageCallbackNapi::OnRemoteRequest(
41     uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
42 {
43     LBSLOGD(NMEA_MESSAGE_CALLBACK, "NmeaMessageCallbackNapi::OnRemoteRequest!");
44     if (data.ReadInterfaceToken() != GetDescriptor()) {
45         LBSLOGE(NMEA_MESSAGE_CALLBACK, "invalid token.");
46         return -1;
47     }
48     if (remoteDied_) {
49         LBSLOGD(NMEA_MESSAGE_CALLBACK, "Failed to `%{public}s`,Remote service is died!", __func__);
50         return -1;
51     }
52 
53     switch (code) {
54         case RECEIVE_NMEA_MESSAGE_EVENT: {
55             int64_t timestamp = data.ReadInt64();
56             std::string msg = Str16ToStr8(data.ReadString16());
57             OnMessageChange(timestamp, msg);
58             break;
59         }
60         default: {
61             IPCObjectStub::OnRemoteRequest(code, data, reply, option);
62             break;
63         }
64     }
65     return 0;
66 }
67 
GetEnv()68 napi_env NmeaMessageCallbackNapi::GetEnv()
69 {
70     std::unique_lock<std::mutex> guard(mutex_);
71     return env_;
72 }
73 
SetEnv(const napi_env & env)74 void NmeaMessageCallbackNapi::SetEnv(const napi_env& env)
75 {
76     std::unique_lock<std::mutex> guard(mutex_);
77     env_ = env;
78 }
79 
GetHandleCb()80 napi_ref NmeaMessageCallbackNapi::GetHandleCb()
81 {
82     std::unique_lock<std::mutex> guard(mutex_);
83     return handlerCb_;
84 }
85 
SetHandleCb(const napi_ref & handlerCb)86 void NmeaMessageCallbackNapi::SetHandleCb(const napi_ref& handlerCb)
87 {
88     {
89         std::unique_lock<std::mutex> guard(mutex_);
90         handlerCb_ = handlerCb;
91     }
92     std::unique_lock<std::mutex> guard(g_regCallbackMutex);
93     g_registerCallbacks.emplace_back(handlerCb);
94 }
95 
FindNmeaCallback(napi_ref cb)96 bool FindNmeaCallback(napi_ref cb)
97 {
98     std::unique_lock<std::mutex> guard(g_regCallbackMutex);
99     auto iter = std::find(g_registerCallbacks.begin(), g_registerCallbacks.end(), cb);
100     if (iter == g_registerCallbacks.end()) {
101         return false;
102     }
103     return true;
104 }
105 
DeleteNmeaCallback(napi_ref cb)106 void DeleteNmeaCallback(napi_ref cb)
107 {
108     std::unique_lock<std::mutex> guard(g_regCallbackMutex);
109     for (auto iter = g_registerCallbacks.begin(); iter != g_registerCallbacks.end(); iter++) {
110         if (*iter == cb) {
111             iter = g_registerCallbacks.erase(iter);
112             break;
113         }
114     }
115 }
116 
IsRemoteDied()117 bool NmeaMessageCallbackNapi::IsRemoteDied()
118 {
119     return remoteDied_;
120 }
121 
PackResult(const std::string msg)122 napi_value NmeaMessageCallbackNapi::PackResult(const std::string msg)
123 {
124     napi_value result;
125     NAPI_CALL(env_, napi_create_string_utf8(env_, msg.c_str(), NAPI_AUTO_LENGTH, &result));
126     return result;
127 }
128 
Send(const std::string msg)129 bool NmeaMessageCallbackNapi::Send(const std::string msg)
130 {
131     std::unique_lock<std::mutex> guard(mutex_);
132     uv_loop_s *loop = nullptr;
133     NAPI_CALL_BASE(env_, napi_get_uv_event_loop(env_, &loop), false);
134     if (loop == nullptr) {
135         LBSLOGE(NMEA_MESSAGE_CALLBACK, "loop == nullptr.");
136         return false;
137     }
138     if (handlerCb_ == nullptr) {
139         LBSLOGE(NMEA_MESSAGE_CALLBACK, "handler is nullptr.");
140         return false;
141     }
142     uv_work_t *work = new (std::nothrow) uv_work_t;
143     if (work == nullptr) {
144         LBSLOGE(NMEA_MESSAGE_CALLBACK, "work == nullptr.");
145         return false;
146     }
147     NmeaAsyncContext *context = new (std::nothrow) NmeaAsyncContext(env_);
148     if (context == nullptr) {
149         LBSLOGE(NMEA_MESSAGE_CALLBACK, "context == nullptr.");
150         delete work;
151         return false;
152     }
153     if (!InitContext(context)) {
154         LBSLOGE(NMEA_MESSAGE_CALLBACK, "InitContext fail");
155         return false;
156     }
157     context->msg = msg;
158     work->data = context;
159     UvQueueWork(loop, work);
160     return true;
161 }
162 
163 
UvQueueWork(uv_loop_s * loop,uv_work_t * work)164 void NmeaMessageCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
165 {
166     uv_queue_work(
167         loop,
168         work,
169         [](uv_work_t *work) {},
170         [](uv_work_t *work, int status) {
171             NmeaAsyncContext *context = nullptr;
172             napi_handle_scope scope = nullptr;
173             if (work == nullptr) {
174                 LBSLOGE(NMEA_MESSAGE_CALLBACK, "work is nullptr!");
175                 return;
176             }
177             context = static_cast<NmeaAsyncContext *>(work->data);
178             if (context == nullptr || context->env == nullptr) {
179                 LBSLOGE(NMEA_MESSAGE_CALLBACK, "context is nullptr!");
180                 delete work;
181                 return;
182             }
183             if (!FindNmeaCallback(context->callback[0])) {
184                 LBSLOGE(NMEA_MESSAGE_CALLBACK, "no valid callback");
185                 delete context;
186                 delete work;
187                 return;
188             }
189             NAPI_CALL_RETURN_VOID(context->env, napi_open_handle_scope(context->env, &scope));
190             if (scope == nullptr) {
191                 LBSLOGE(NMEA_MESSAGE_CALLBACK, "scope is nullptr");
192                 delete context;
193                 delete work;
194                 return;
195             }
196             napi_value jsEvent;
197             CHK_NAPI_ERR_CLOSE_SCOPE(context->env,
198                 napi_create_string_utf8(context->env, context->msg.c_str(), NAPI_AUTO_LENGTH, &jsEvent),
199                 scope, context, work);
200             if (context->callback[0] != nullptr) {
201                 napi_value undefine;
202                 napi_value handler = nullptr;
203                 CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &undefine),
204                     scope, context, work);
205                 CHK_NAPI_ERR_CLOSE_SCOPE(context->env,
206                     napi_get_reference_value(context->env, context->callback[0], &handler),
207                     scope, context, work);
208                 if (napi_call_function(context->env, nullptr, handler, 1, &jsEvent, &undefine) != napi_ok) {
209                     LBSLOGE(NMEA_MESSAGE_CALLBACK, "Report event failed");
210                 }
211             }
212             NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
213             delete context;
214             delete work;
215     });
216 }
217 
OnMessageChange(int64_t timestamp,const std::string msg)218 void NmeaMessageCallbackNapi::OnMessageChange(int64_t timestamp, const std::string msg)
219 {
220     LBSLOGD(NMEA_MESSAGE_CALLBACK, "NmeaMessageCallbackNapi::OnMessageChange");
221     Send(msg);
222 }
223 
DeleteHandler()224 void NmeaMessageCallbackNapi::DeleteHandler()
225 {
226     std::unique_lock<std::mutex> guard(mutex_);
227     if (handlerCb_ == nullptr || env_ == nullptr) {
228         LBSLOGE(NMEA_MESSAGE_CALLBACK, "handler or env is nullptr.");
229         return;
230     }
231     DeleteNmeaCallback(handlerCb_);
232     NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
233     handlerCb_ = nullptr;
234 }
235 }  // namespace Location
236 }  // namespace OHOS
237