• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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_HOST_H
17 #define LOCATING_REQUIRED_DATA_CALLBACK_HOST_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 class LocatingRequiredDataCallbackHost : public IRemoteStub<ILocatingRequiredDataCallback> {
30 public:
31     LocatingRequiredDataCallbackHost();
32     virtual ~LocatingRequiredDataCallbackHost();
33     virtual int OnRemoteRequest(
34         uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override;
35     bool IsRemoteDied();
36     bool Send(const std::vector<std::shared_ptr<LocatingRequiredData>>& data);
37     void OnLocatingDataChange(const std::vector<std::shared_ptr<LocatingRequiredData>>& data) override;
38     void DeleteHandler();
39     void UvQueueWork(uv_loop_s* loop, uv_work_t* work);
40     bool IsSingleLocationRequest();
41     void CountDown();
42     void Wait(int time);
43     int GetCount();
44     void SetCount(int count);
45     void InitLatch();
46     void ClearSingleResult();
47     void SetSingleResult(
48         std::vector<std::shared_ptr<LocatingRequiredData>> singleResult);
49 
GetEnv()50     inline napi_env GetEnv() const
51     {
52         return env_;
53     }
54 
SetEnv(const napi_env & env)55     inline void SetEnv(const napi_env& env)
56     {
57         env_ = env;
58     }
59 
GetHandleCb()60     inline napi_ref GetHandleCb() const
61     {
62         return handlerCb_;
63     }
64 
SetHandleCb(const napi_ref & handlerCb)65     inline void SetHandleCb(const napi_ref& handlerCb)
66     {
67         handlerCb_ = handlerCb;
68     }
69 
GetRemoteDied()70     inline bool GetRemoteDied() const
71     {
72         return remoteDied_;
73     }
74 
SetRemoteDied(const bool remoteDied)75     inline void SetRemoteDied(const bool remoteDied)
76     {
77         remoteDied_ = remoteDied;
78     }
79 
GetSingleResult()80     inline std::vector<std::shared_ptr<LocatingRequiredData>> GetSingleResult()
81     {
82         std::unique_lock<std::mutex> guard(singleResultMutex_);
83         return singleResult_;
84     }
85 
GetFixNumber()86     inline int GetFixNumber() const
87     {
88         return fixNumber_;
89     }
90 
SetFixNumber(const int fixNumber)91     inline void SetFixNumber(const int fixNumber)
92     {
93         fixNumber_ = fixNumber;
94     }
95 private:
96     int fixNumber_;
97     napi_env env_;
98     napi_ref handlerCb_;
99     bool remoteDied_;
100     std::mutex mutex_;
101     std::mutex singleResultMutex_;
102     CountDownLatch* latch_;
103     std::vector<std::shared_ptr<LocatingRequiredData>> singleResult_;
104 };
105 } // namespace Location
106 } // namespace OHOS
107 #endif // LOCATING_REQUIRED_DATA_CALLBACK_HOST_H
108