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 LOCATING_REQUIRED_DATA_CALLBACK_NAPI_H
17 #define LOCATING_REQUIRED_DATA_CALLBACK_NAPI_H
18 
19 #include "i_locating_required_data_callback.h"
20 #include "iremote_stub.h"
21 #include "message_parcel.h"
22 #include "message_option.h"
23 #include "common_utils.h"
24 #include "napi/native_api.h"
25 #include "uv.h"
26 
27 namespace OHOS {
28 namespace Location {
29 bool FindRequiredDataCallback(napi_ref cb);
30 void DeleteRequiredDataCallback(napi_ref cb);
31 class LocatingRequiredDataCallbackNapi : public IRemoteStub<ILocatingRequiredDataCallback> {
32 public:
33     LocatingRequiredDataCallbackNapi();
34     virtual ~LocatingRequiredDataCallbackNapi();
35     virtual int OnRemoteRequest(
36         uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override;
37     bool IsRemoteDied();
38     bool Send(const std::vector<std::shared_ptr<LocatingRequiredData>>& data);
39     void OnLocatingDataChange(const std::vector<std::shared_ptr<LocatingRequiredData>>& data) override;
40     void DeleteHandler();
41     void UvQueueWork(uv_loop_s* loop, uv_work_t* work);
42     bool IsSingleLocationRequest();
43     void CountDown();
44     void Wait(int time);
45     int GetCount();
46     void SetCount(int count);
47     void InitLatch();
48     void ClearSingleResult();
49     void SetSingleResult(
50         std::vector<std::shared_ptr<LocatingRequiredData>> singleResult);
51     napi_ref GetHandleCb();
52     void SetHandleCb(const napi_ref& handlerCb);
53     napi_env GetEnv();
54     void SetEnv(const napi_env& env);
55 
56     template <typename T>
InitContext(T * context)57     bool InitContext(T* context)
58     {
59         if (context == nullptr) {
60             LBSLOGE(LOCATING_DATA_CALLBACK, "context == nullptr.");
61             return false;
62         }
63         context->env = env_;
64         context->callback[SUCCESS_CALLBACK] = handlerCb_;
65         return true;
66     }
67 
GetRemoteDied()68     inline bool GetRemoteDied() const
69     {
70         return remoteDied_;
71     }
72 
SetRemoteDied(const bool remoteDied)73     inline void SetRemoteDied(const bool remoteDied)
74     {
75         remoteDied_ = remoteDied;
76     }
77 
GetSingleResult()78     inline std::vector<std::shared_ptr<LocatingRequiredData>> GetSingleResult()
79     {
80         std::unique_lock<std::mutex> guard(singleResultMutex_);
81         return singleResult_;
82     }
83 
GetFixNumber()84     inline int GetFixNumber() const
85     {
86         return fixNumber_;
87     }
88 
SetFixNumber(const int fixNumber)89     inline void SetFixNumber(const int fixNumber)
90     {
91         fixNumber_ = fixNumber;
92     }
93 private:
94     int fixNumber_;
95     napi_env env_;
96     napi_ref handlerCb_;
97     bool remoteDied_;
98     std::mutex mutex_;
99     std::mutex singleResultMutex_;
100     CountDownLatch* latch_;
101     std::vector<std::shared_ptr<LocatingRequiredData>> singleResult_;
102 };
103 } // namespace Location
104 } // namespace OHOS
105 #endif // LOCATING_REQUIRED_DATA_CALLBACK_NAPI_H
106