• 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 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 
85     void SetSuccHandleCb(const napi_ref& successHandlerCb);
86 
GetFailHandleCb()87     inline napi_ref GetFailHandleCb() const
88     {
89         return failHandlerCb_;
90     }
91 
SetFailHandleCb(const napi_ref & failHandlerCb)92     inline void SetFailHandleCb(const napi_ref& failHandlerCb)
93     {
94         failHandlerCb_ = failHandlerCb;
95     }
96 
GetCompleteHandleCb()97     inline napi_ref GetCompleteHandleCb() const
98     {
99         return completeHandlerCb_;
100     }
101 
SetCompleteHandleCb(const napi_ref & completeHandlerCb)102     inline void SetCompleteHandleCb(const napi_ref& completeHandlerCb)
103     {
104         completeHandlerCb_ = completeHandlerCb;
105     }
106 
GetFixNumber()107     inline int GetFixNumber() const
108     {
109         return fixNumber_;
110     }
111 
SetFixNumber(const int fixNumber)112     inline void SetFixNumber(const int fixNumber)
113     {
114         fixNumber_ = fixNumber;
115     }
116 
SetLocationPriority(const int locationPriority)117     inline void SetLocationPriority(const int locationPriority)
118     {
119         locationPriority_ = locationPriority;
120     }
121 
GetLocationPriority()122     inline int GetLocationPriority()
123     {
124         return locationPriority_;
125     }
126 
SetErrorType(const int errorType)127     inline void SetErrorType(const int errorType)
128     {
129         std::unique_lock<std::mutex> guard(mutex_);
130         errorType_ = errorType;
131     }
132 
GetErrorType()133     inline int GetErrorType()
134     {
135         std::unique_lock<std::mutex> guard(mutex_);
136         return errorType_;
137     }
138 
GetSingleLocation()139     inline std::shared_ptr<Location> GetSingleLocation()
140     {
141         std::unique_lock<std::mutex> guard(mutex_);
142         return singleLocation_;
143     }
144     bool NeedSetSingleLocation(const std::unique_ptr<Location>& location);
145     bool IfReportAccuracyLocation();
146     void SetSingleLocation(const std::unique_ptr<Location>& location);
147 
148 private:
149     napi_env env_;
150     napi_ref handlerCb_;
151     napi_ref successHandlerCb_;
152     napi_ref failHandlerCb_;
153     napi_ref completeHandlerCb_;
154     int fixNumber_;
155     std::mutex mutex_;
156     CountDownLatch* latch_;
157     std::shared_ptr<Location> singleLocation_;
158     int locationPriority_;
159     bool inHdArea_;
160     int errorType_;
161 };
162 } // namespace Location
163 } // namespace OHOS
164 #endif // LOCATOR_CALLBACK_NAPI_H
165