• 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::unique_lock<std::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         delete work;
90         return false;
91     }
92     context->env = env_;
93     context->callback[SUCCESS_CALLBACK] = handlerCb_;
94     context->enable = (switchState == 1 ? true : false);
95     work->data = context;
96     UvQueueWork(loop, work);
97     return true;
98 }
99 
UvQueueWork(uv_loop_s * loop,uv_work_t * work)100 void LocationSwitchCallbackHost::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
101 {
102     uv_queue_work(
103         loop,
104         work,
105         [](uv_work_t *work) {},
106         [](uv_work_t *work, int status) {
107             SwitchAsyncContext *context = nullptr;
108             napi_handle_scope scope = nullptr;
109             if (work == nullptr) {
110                 LBSLOGE(LOCATOR_CALLBACK, "work is nullptr!");
111                 return;
112             }
113             context = static_cast<SwitchAsyncContext *>(work->data);
114             if (context == nullptr || context->env == nullptr) {
115                 LBSLOGE(LOCATOR_CALLBACK, "context is nullptr!");
116                 delete work;
117                 return;
118             }
119             NAPI_CALL_RETURN_VOID(context->env, napi_open_handle_scope(context->env, &scope));
120             napi_value jsEvent;
121             CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_boolean(context->env, context->enable, &jsEvent),
122                 scope, context, work);
123             if (scope == nullptr) {
124                 LBSLOGE(SWITCH_CALLBACK, "scope is nullptr");
125                 delete context;
126                 delete work;
127                 return;
128             }
129             if (context->callback[0] != nullptr) {
130                 napi_value undefine;
131                 napi_value handler = nullptr;
132                 CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &undefine),
133                     scope, context, work);
134                 CHK_NAPI_ERR_CLOSE_SCOPE(context->env,
135                     napi_get_reference_value(context->env, context->callback[0], &handler), scope, context, work);
136                 if (napi_call_function(context->env, nullptr, handler, 1,
137                     &jsEvent, &undefine) != napi_ok) {
138                     LBSLOGE(SWITCH_CALLBACK, "Report event failed");
139                 }
140             }
141             NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
142             delete context;
143             delete work;
144     });
145 }
146 
OnSwitchChange(int switchState)147 void LocationSwitchCallbackHost::OnSwitchChange(int switchState)
148 {
149     LBSLOGD(SWITCH_CALLBACK, "LocatorCallbackHost::OnSwitchChange");
150     Send(switchState);
151 }
152 
DeleteHandler()153 void LocationSwitchCallbackHost::DeleteHandler()
154 {
155     std::unique_lock<std::mutex> guard(mutex_);
156     if (handlerCb_ == nullptr || env_ == nullptr) {
157         LBSLOGE(SWITCH_CALLBACK, "handler or env is nullptr.");
158         return;
159     }
160     auto context = new (std::nothrow) AsyncContext(env_);
161     if (context == nullptr) {
162         LBSLOGE(SWITCH_CALLBACK, "context == nullptr.");
163         return;
164     }
165     context->env = env_;
166     context->callback[SUCCESS_CALLBACK] = handlerCb_;
167     DeleteQueueWork(context);
168     handlerCb_ = nullptr;
169 }
170 }  // namespace Location
171 }  // namespace OHOS
172