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