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 
16 #ifndef LOCATOR_CALLBACK_NAPI_H
17 #define LOCATOR_CALLBACK_NAPI_H
18 
19 #include "iremote_stub.h"
20 #include "napi/native_api.h"
21 #include "uv.h"
22 
23 #include "common_utils.h"
24 #include "constant_definition.h"
25 #include "i_locator_callback.h"
26 #include "location.h"
27 
28 namespace OHOS {
29 namespace Location {
30 bool FindLocationCallback(napi_ref cb);
31 void DeleteLocationCallback(napi_ref cb);
32 class LocatorCallbackNapi : public IRemoteStub<ILocatorCallback> {
33 public:
34     LocatorCallbackNapi();
35     virtual ~LocatorCallbackNapi();
36     virtual int OnRemoteRequest(uint32_t code,
37         MessageParcel& data, MessageParcel& reply, MessageOption& option) override;
38     void DoSendWork(uv_loop_s *&loop, uv_work_t *&work);
39     void DoSendErrorCode(uv_loop_s *&loop, uv_work_t *&work);
40     bool SendErrorCode(const int& errorCode);
41 
42     void OnLocationReport(const std::unique_ptr<Location>& location) override;
43     void OnLocatingStatusChange(const int status) override;
44     void OnErrorReport(const int errorCode) override;
45     void DeleteAllCallbacks();
46     void DeleteHandler();
47     void DeleteSuccessHandler();
48     void DeleteFailHandler();
49     void DeleteCompleteHandler();
50     void InitLatch();
51     bool IsSystemGeoLocationApi();
52     bool IsSingleLocationRequest();
53     void CountDown();
54     void Wait(int time);
55     int GetCount();
56     void SetCount(int count);
57     napi_ref GetHandleCb();
58     void SetHandleCb(const napi_ref& handlerCb);
59     napi_env GetEnv();
60     void SetEnv(const napi_env& env);
61 
62     template <typename T>
InitContext(T * context)63     bool InitContext(T* context)
64     {
65         if (context == nullptr) {
66             LBSLOGE(LOCATOR_CALLBACK, "context == nullptr.");
67             return false;
68         }
69         context->env = env_;
70         if (IsSystemGeoLocationApi()) {
71             context->callback[SUCCESS_CALLBACK] = successHandlerCb_;
72             context->callback[FAIL_CALLBACK] = failHandlerCb_;
73             context->callback[COMPLETE_CALLBACK] = completeHandlerCb_;
74         } else {
75             context->callback[SUCCESS_CALLBACK] = handlerCb_;
76         }
77         return true;
78     }
79 
GetSuccHandleCb()80     inline napi_ref GetSuccHandleCb() const
81     {
82         return successHandlerCb_;
83     }
84 
SetSuccHandleCb(const napi_ref & successHandlerCb)85     inline void SetSuccHandleCb(const napi_ref& successHandlerCb)
86     {
87         successHandlerCb_ = successHandlerCb;
88     }
89 
GetFailHandleCb()90     inline napi_ref GetFailHandleCb() const
91     {
92         return failHandlerCb_;
93     }
94 
SetFailHandleCb(const napi_ref & failHandlerCb)95     inline void SetFailHandleCb(const napi_ref& failHandlerCb)
96     {
97         failHandlerCb_ = failHandlerCb;
98     }
99 
GetCompleteHandleCb()100     inline napi_ref GetCompleteHandleCb() const
101     {
102         return completeHandlerCb_;
103     }
104 
SetCompleteHandleCb(const napi_ref & completeHandlerCb)105     inline void SetCompleteHandleCb(const napi_ref& completeHandlerCb)
106     {
107         completeHandlerCb_ = completeHandlerCb;
108     }
109 
GetFixNumber()110     inline int GetFixNumber() const
111     {
112         return fixNumber_;
113     }
114 
SetFixNumber(const int fixNumber)115     inline void SetFixNumber(const int fixNumber)
116     {
117         fixNumber_ = fixNumber;
118     }
119 
SetLocationPriority(const int locationPriority)120     inline void SetLocationPriority(const int locationPriority)
121     {
122         locationPriority_ = locationPriority;
123     }
124 
GetLocationPriority()125     inline int GetLocationPriority()
126     {
127         return locationPriority_;
128     }
129 
GetSingleLocation()130     inline std::shared_ptr<Location> GetSingleLocation()
131     {
132         std::unique_lock<std::mutex> guard(mutex_);
133         return singleLocation_;
134     }
135     bool NeedSetSingleLocation(const std::unique_ptr<Location>& location);
136     bool IfReportAccuracyLocation();
137     void SetSingleLocation(const std::unique_ptr<Location>& location);
138 
139 private:
140     napi_env env_;
141     napi_ref handlerCb_;
142     napi_ref successHandlerCb_;
143     napi_ref failHandlerCb_;
144     napi_ref completeHandlerCb_;
145     int fixNumber_;
146     std::mutex mutex_;
147     CountDownLatch* latch_;
148     std::shared_ptr<Location> singleLocation_;
149     int locationPriority_;
150     bool inHdArea_;
151 };
152 } // namespace Location
153 } // namespace OHOS
154 #endif // LOCATOR_CALLBACK_NAPI_H
155