• 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 
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 FindRegCallback(napi_ref cb);
31 void DeleteRegCallback(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 
60     template <typename T>
InitContext(T * context)61     bool InitContext(T* context)
62     {
63         if (context == nullptr) {
64             LBSLOGE(LOCATOR_CALLBACK, "context == nullptr.");
65             return false;
66         }
67         context->env = env_;
68         if (IsSystemGeoLocationApi()) {
69             context->callback[SUCCESS_CALLBACK] = successHandlerCb_;
70             context->callback[FAIL_CALLBACK] = failHandlerCb_;
71             context->callback[COMPLETE_CALLBACK] = completeHandlerCb_;
72         } else {
73             context->callback[SUCCESS_CALLBACK] = handlerCb_;
74         }
75         return true;
76     }
77 
GetEnv()78     inline napi_env GetEnv() const
79     {
80         return env_;
81     }
82 
SetEnv(const napi_env & env)83     inline void SetEnv(const napi_env& env)
84     {
85         env_ = env;
86     }
87 
GetSuccHandleCb()88     inline napi_ref GetSuccHandleCb() const
89     {
90         return successHandlerCb_;
91     }
92 
SetSuccHandleCb(const napi_ref & successHandlerCb)93     inline void SetSuccHandleCb(const napi_ref& successHandlerCb)
94     {
95         successHandlerCb_ = successHandlerCb;
96     }
97 
GetFailHandleCb()98     inline napi_ref GetFailHandleCb() const
99     {
100         return failHandlerCb_;
101     }
102 
SetFailHandleCb(const napi_ref & failHandlerCb)103     inline void SetFailHandleCb(const napi_ref& failHandlerCb)
104     {
105         failHandlerCb_ = failHandlerCb;
106     }
107 
GetCompleteHandleCb()108     inline napi_ref GetCompleteHandleCb() const
109     {
110         return completeHandlerCb_;
111     }
112 
SetCompleteHandleCb(const napi_ref & completeHandlerCb)113     inline void SetCompleteHandleCb(const napi_ref& completeHandlerCb)
114     {
115         completeHandlerCb_ = completeHandlerCb;
116     }
117 
GetFixNumber()118     inline int GetFixNumber() const
119     {
120         return fixNumber_;
121     }
122 
SetFixNumber(const int fixNumber)123     inline void SetFixNumber(const int fixNumber)
124     {
125         fixNumber_ = fixNumber;
126     }
127 
SetLocationPriority(const int locationPriority)128     inline void SetLocationPriority(const int locationPriority)
129     {
130         locationPriority_ = locationPriority;
131     }
132 
GetLocationPriority()133     inline int GetLocationPriority()
134     {
135         return locationPriority_;
136     }
137 
GetSingleLocation()138     inline std::shared_ptr<Location> GetSingleLocation()
139     {
140         std::unique_lock<std::mutex> guard(mutex_);
141         return singleLocation_;
142     }
143     bool NeedSetSingleLocation(const std::unique_ptr<Location>& location);
144     bool IfReportAccuracyLocation();
145     void SetSingleLocation(const std::unique_ptr<Location>& location);
146 
147 private:
148     napi_env env_;
149     napi_ref handlerCb_;
150     napi_ref successHandlerCb_;
151     napi_ref failHandlerCb_;
152     napi_ref completeHandlerCb_;
153     int fixNumber_;
154     std::mutex mutex_;
155     CountDownLatch* latch_;
156     std::shared_ptr<Location> singleLocation_;
157     int locationPriority_;
158     bool inHdArea_;
159 };
160 } // namespace Location
161 } // namespace OHOS
162 #endif // LOCATOR_CALLBACK_NAPI_H
163