• 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 <shared_mutex>
20 
21 #include "iremote_stub.h"
22 #include "napi/native_api.h"
23 #include "uv.h"
24 
25 #include "common_utils.h"
26 #include "constant_definition.h"
27 #include "i_locator_callback.h"
28 #include "location.h"
29 
30 namespace OHOS {
31 namespace Location {
32 class LocatorCallbackHost : public IRemoteStub<ILocatorCallback> {
33 public:
34     LocatorCallbackHost();
35     virtual ~LocatorCallbackHost();
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 
58     template <typename T>
InitContext(T * context)59     bool InitContext(T* context)
60     {
61         if (context == nullptr) {
62             LBSLOGE(LOCATOR_CALLBACK, "context == nullptr.");
63             return false;
64         }
65         context->env = env_;
66         if (IsSystemGeoLocationApi()) {
67             context->callback[SUCCESS_CALLBACK] = successHandlerCb_;
68             context->callback[FAIL_CALLBACK] = failHandlerCb_;
69             context->callback[COMPLETE_CALLBACK] = completeHandlerCb_;
70         } else {
71             context->callback[SUCCESS_CALLBACK] = handlerCb_;
72             context->deferred = deferred_;
73         }
74         return true;
75     }
76 
GetEnv()77     inline napi_env GetEnv() const
78     {
79         return env_;
80     }
81 
SetEnv(const napi_env & env)82     inline void SetEnv(const napi_env& env)
83     {
84         env_ = env;
85     }
86 
GetHandleCb()87     inline napi_ref GetHandleCb() const
88     {
89         return handlerCb_;
90     }
91 
SetHandleCb(const napi_ref & handlerCb)92     inline void SetHandleCb(const napi_ref& handlerCb)
93     {
94         handlerCb_ = handlerCb;
95     }
96 
GetSuccHandleCb()97     inline napi_ref GetSuccHandleCb() const
98     {
99         return successHandlerCb_;
100     }
101 
SetSuccHandleCb(const napi_ref & successHandlerCb)102     inline void SetSuccHandleCb(const napi_ref& successHandlerCb)
103     {
104         successHandlerCb_ = successHandlerCb;
105     }
106 
GetFailHandleCb()107     inline napi_ref GetFailHandleCb() const
108     {
109         return failHandlerCb_;
110     }
111 
SetFailHandleCb(const napi_ref & failHandlerCb)112     inline void SetFailHandleCb(const napi_ref& failHandlerCb)
113     {
114         failHandlerCb_ = failHandlerCb;
115     }
116 
GetCompleteHandleCb()117     inline napi_ref GetCompleteHandleCb() const
118     {
119         return completeHandlerCb_;
120     }
121 
SetCompleteHandleCb(const napi_ref & completeHandlerCb)122     inline void SetCompleteHandleCb(const napi_ref& completeHandlerCb)
123     {
124         completeHandlerCb_ = completeHandlerCb;
125     }
126 
GetFixNumber()127     inline int GetFixNumber() const
128     {
129         return fixNumber_;
130     }
131 
SetFixNumber(const int fixNumber)132     inline void SetFixNumber(const int fixNumber)
133     {
134         fixNumber_ = fixNumber;
135     }
136 
GetDeferred()137     inline napi_deferred GetDeferred() const
138     {
139         return deferred_;
140     }
141 
SetDeferred(const napi_deferred & deferred)142     inline void SetDeferred(const napi_deferred& deferred)
143     {
144         deferred_ = deferred;
145     }
146 
GetSingleLocation()147     inline std::shared_ptr<Location> GetSingleLocation() const
148     {
149         return singleLocation_;
150     }
151 
152 private:
153     napi_env env_;
154     napi_ref handlerCb_;
155     napi_ref successHandlerCb_;
156     napi_ref failHandlerCb_;
157     napi_ref completeHandlerCb_;
158     int fixNumber_;
159     napi_deferred deferred_;
160     std::shared_mutex mutex_;
161     CountDownLatch* latch_;
162     std::shared_ptr<Location> singleLocation_;
163 };
164 } // namespace Location
165 } // namespace OHOS
166 #endif // LOCATOR_CALLBACK_HOST_H
167