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