• 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 "location_switch_callback_host.h"
17 #include "common_utils.h"
18 #include "ipc_skeleton.h"
19 #include "location_log.h"
20 
21 namespace OHOS {
22 namespace Location {
LocationSwitchCallbackHost()23 LocationSwitchCallbackHost::LocationSwitchCallbackHost()
24 {
25     env_ = nullptr;
26     handlerCb_ = nullptr;
27     remoteDied_ = false;
28 }
29 
~LocationSwitchCallbackHost()30 LocationSwitchCallbackHost::~LocationSwitchCallbackHost()
31 {
32 }
33 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int LocationSwitchCallbackHost::OnRemoteRequest(
35     uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
36 {
37     LBSLOGD(SWITCH_CALLBACK, "LocatorCallbackHost::OnRemoteRequest!");
38     if (data.ReadInterfaceToken() != GetDescriptor()) {
39         LBSLOGE(SWITCH_CALLBACK, "invalid token.");
40         return -1;
41     }
42     if (remoteDied_) {
43         LBSLOGD(SWITCH_CALLBACK, "Failed to `%{public}s`,Remote service is died!", __func__);
44         return -1;
45     }
46 
47     switch (code) {
48         case RECEIVE_SWITCH_STATE_EVENT: {
49             OnSwitchChange(data.ReadInt32());
50             break;
51         }
52         default: {
53             IPCObjectStub::OnRemoteRequest(code, data, reply, option);
54             break;
55         }
56     }
57     return 0;
58 }
59 
IsRemoteDied()60 bool LocationSwitchCallbackHost::IsRemoteDied()
61 {
62     return remoteDied_;
63 }
64 
PackResult(bool switchState)65 napi_value LocationSwitchCallbackHost::PackResult(bool switchState)
66 {
67     napi_value result;
68     NAPI_CALL(env_, napi_get_boolean(env_, switchState, &result));
69     return result;
70 }
71 
Send(int switchState)72 bool LocationSwitchCallbackHost::Send(int switchState)
73 {
74     std::shared_lock<std::shared_mutex> guard(mutex_);
75     uv_loop_s *loop = nullptr;
76     NAPI_CALL_BASE(env_, napi_get_uv_event_loop(env_, &loop), false);
77     if (loop == nullptr) {
78         LBSLOGE(SWITCH_CALLBACK, "loop == nullptr.");
79         return false;
80     }
81     uv_work_t *work = new (std::nothrow) uv_work_t;
82     if (work == nullptr) {
83         LBSLOGE(SWITCH_CALLBACK, "work == nullptr.");
84         return false;
85     }
86     SwitchAsyncContext *context = new (std::nothrow) SwitchAsyncContext(env_);
87     if (context == nullptr) {
88         LBSLOGE(SWITCH_CALLBACK, "context == nullptr.");
89         return false;
90     }
91     context->env = env_;
92     context->callback[SUCCESS_CALLBACK] = handlerCb_;
93     context->enable = (switchState == 1 ? true : false);
94     work->data = context;
95     UvQueueWork(loop, work);
96     return true;
97 }
98 
UvQueueWork(uv_loop_s * loop,uv_work_t * work)99 void LocationSwitchCallbackHost::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
100 {
101     uv_queue_work(
102         loop,
103         work,
104         [](uv_work_t *work) {},
105         [](uv_work_t *work, int status) {
106             SwitchAsyncContext *context = nullptr;
107             napi_handle_scope scope = nullptr;
108             if (work == nullptr) {
109                 LBSLOGE(LOCATOR_CALLBACK, "work is nullptr!");
110                 return;
111             }
112             context = static_cast<SwitchAsyncContext *>(work->data);
113             if (context == nullptr || context->env == nullptr) {
114                 LBSLOGE(LOCATOR_CALLBACK, "context is nullptr!");
115                 delete work;
116                 return;
117             }
118             NAPI_CALL_RETURN_VOID(context->env, napi_open_handle_scope(context->env, &scope));
119             napi_value jsEvent;
120             CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_boolean(context->env, context->enable, &jsEvent),
121                 scope, context, work);
122             if (scope == nullptr) {
123                 LBSLOGE(SWITCH_CALLBACK, "scope is nullptr");
124                 delete context;
125                 delete work;
126                 return;
127             }
128             if (context->callback[0] != nullptr) {
129                 napi_value undefine;
130                 napi_value handler = nullptr;
131                 CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &undefine),
132                     scope, context, work);
133                 CHK_NAPI_ERR_CLOSE_SCOPE(context->env,
134                     napi_get_reference_value(context->env, context->callback[0], &handler), scope, context, work);
135                 if (napi_call_function(context->env, nullptr, handler, 1,
136                     &jsEvent, &undefine) != napi_ok) {
137                     LBSLOGE(SWITCH_CALLBACK, "Report event failed");
138                 }
139             }
140             NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
141             delete context;
142             delete work;
143     });
144 }
145 
OnSwitchChange(int switchState)146 void LocationSwitchCallbackHost::OnSwitchChange(int switchState)
147 {
148     LBSLOGD(SWITCH_CALLBACK, "LocatorCallbackHost::OnSwitchChange");
149     Send(switchState);
150 }
151 
DeleteHandler()152 void LocationSwitchCallbackHost::DeleteHandler()
153 {
154     std::shared_lock<std::shared_mutex> guard(mutex_);
155     if (handlerCb_ == nullptr || env_ == nullptr) {
156         LBSLOGE(SWITCH_CALLBACK, "handler or env is nullptr.");
157         return;
158     }
159     auto context = new (std::nothrow) AsyncContext(env_);
160     if (context == nullptr) {
161         LBSLOGE(SWITCH_CALLBACK, "context == nullptr.");
162         return;
163     }
164     context->env = env_;
165     context->callback[SUCCESS_CALLBACK] = handlerCb_;
166     DeleteQueueWork(context);
167     handlerCb_ = nullptr;
168 }
169 }  // namespace Location
170 }  // namespace OHOS
171