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