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