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 class LocatingRequiredDataCallbackNapi : public IRemoteStub<ILocatingRequiredDataCallback> { 30 public: 31 LocatingRequiredDataCallbackNapi(); 32 virtual ~LocatingRequiredDataCallbackNapi(); 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 50 template <typename T> InitContext(T * context)51 bool InitContext(T* context) 52 { 53 if (context == nullptr) { 54 LBSLOGE(LOCATING_DATA_CALLBACK, "context == nullptr."); 55 return false; 56 } 57 context->env = env_; 58 context->callback[SUCCESS_CALLBACK] = handlerCb_; 59 return true; 60 } 61 GetEnv()62 inline napi_env GetEnv() const 63 { 64 return env_; 65 } 66 SetEnv(const napi_env & env)67 inline void SetEnv(const napi_env& env) 68 { 69 env_ = env; 70 } 71 GetHandleCb()72 inline napi_ref GetHandleCb() const 73 { 74 return handlerCb_; 75 } 76 SetHandleCb(const napi_ref & handlerCb)77 inline void SetHandleCb(const napi_ref& handlerCb) 78 { 79 handlerCb_ = handlerCb; 80 } 81 GetRemoteDied()82 inline bool GetRemoteDied() const 83 { 84 return remoteDied_; 85 } 86 SetRemoteDied(const bool remoteDied)87 inline void SetRemoteDied(const bool remoteDied) 88 { 89 remoteDied_ = remoteDied; 90 } 91 GetSingleResult()92 inline std::vector<std::shared_ptr<LocatingRequiredData>> GetSingleResult() 93 { 94 std::unique_lock<std::mutex> guard(singleResultMutex_); 95 return singleResult_; 96 } 97 GetFixNumber()98 inline int GetFixNumber() const 99 { 100 return fixNumber_; 101 } 102 SetFixNumber(const int fixNumber)103 inline void SetFixNumber(const int fixNumber) 104 { 105 fixNumber_ = fixNumber; 106 } 107 private: 108 int fixNumber_; 109 napi_env env_; 110 napi_ref handlerCb_; 111 bool remoteDied_; 112 std::mutex mutex_; 113 std::mutex singleResultMutex_; 114 CountDownLatch* latch_; 115 std::vector<std::shared_ptr<LocatingRequiredData>> singleResult_; 116 }; 117 } // namespace Location 118 } // namespace OHOS 119 #endif // LOCATING_REQUIRED_DATA_CALLBACK_NAPI_H 120