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 LOCATION_GNSS_GEOFENCE_CALLBACK_NAPI_H 17 #define LOCATION_GNSS_GEOFENCE_CALLBACK_NAPI_H 18 19 #include "iremote_stub.h" 20 #include "message_option.h" 21 #include "message_parcel.h" 22 #include "napi/native_api.h" 23 #include "uv.h" 24 #include "common_utils.h" 25 #include "i_gnss_geofence_callback.h" 26 #include "geofence_definition.h" 27 28 namespace OHOS { 29 namespace Location { 30 class LocationGnssGeofenceCallbackNapi : public IRemoteStub<IGnssGeofenceCallback> { 31 public: 32 LocationGnssGeofenceCallbackNapi(); 33 virtual ~LocationGnssGeofenceCallbackNapi(); 34 virtual int OnRemoteRequest( 35 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; 36 bool IsRemoteDied(); 37 void DeleteHandler(); 38 void CountDown(); 39 void Wait(int time); 40 int GetCount(); 41 void SetCount(int count); 42 int GetFenceId(); 43 void ClearFenceId(); 44 void SetFenceId(int fenceId); 45 LocationErrCode DealGeofenceOperationResult(); 46 GnssGeofenceOperateType GetGeofenceOperationType(); 47 void SetGeofenceOperationType(GnssGeofenceOperateType type); 48 GnssGeofenceOperateResult GetGeofenceOperationResult(); 49 void SetGeofenceOperationResult(GnssGeofenceOperateResult result); 50 void OnTransitionStatusChange(GeofenceTransition transition) override; 51 void OnReportOperationResult(int fenceId, int type, int result) override; 52 53 template <typename T> InitContext(T * context)54 bool InitContext(T* context) 55 { 56 if (context == nullptr) { 57 LBSLOGE(LOCATION_GNSS_GEOFENCE_CALLBACK, "context == nullptr."); 58 return false; 59 } 60 context->env = env_; 61 context->callback[SUCCESS_CALLBACK] = handlerCb_; 62 return true; 63 } 64 GetEnv()65 inline napi_env GetEnv() const 66 { 67 return env_; 68 } 69 SetEnv(const napi_env & env)70 inline void SetEnv(const napi_env& env) 71 { 72 env_ = env; 73 } 74 GetHandleCb()75 inline napi_ref GetHandleCb() const 76 { 77 return handlerCb_; 78 } 79 SetHandleCb(const napi_ref & handlerCb)80 inline void SetHandleCb(const napi_ref& handlerCb) 81 { 82 handlerCb_ = handlerCb; 83 } 84 GetRemoteDied()85 inline bool GetRemoteDied() const 86 { 87 return remoteDied_; 88 } 89 SetRemoteDied(const bool remoteDied)90 inline void SetRemoteDied(const bool remoteDied) 91 { 92 remoteDied_ = remoteDied; 93 } 94 95 private: 96 void UvQueueWork(uv_loop_s* loop, uv_work_t* work); 97 void InitLatch(); 98 99 napi_env env_; 100 napi_ref handlerCb_; 101 bool remoteDied_; 102 std::mutex mutex_; 103 std::mutex operationResultMutex_; 104 CountDownLatch* latch_; 105 int fenceId_; 106 GnssGeofenceOperateType type_; 107 GnssGeofenceOperateResult result_; 108 }; 109 } // namespace Location 110 } // namespace OHOS 111 #endif // LOCATION_GNSS_GEOFENCE_CALLBACK_NAPI_H 112