1 /* 2 * Copyright (C) 2022 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_SWITCH_CALLBACK_HOST_H 17 #define LOCATION_SWITCH_CALLBACK_HOST_H 18 19 #include <shared_mutex> 20 #include "i_switch_callback.h" 21 22 #include "iremote_stub.h" 23 #include "message_parcel.h" 24 #include "message_option.h" 25 #include "napi_util.h" 26 #include "napi/native_api.h" 27 #include "uv.h" 28 29 namespace OHOS { 30 namespace Location { 31 class LocationSwitchCallbackHost : public IRemoteStub<ISwitchCallback> { 32 public: 33 LocationSwitchCallbackHost(); 34 virtual ~LocationSwitchCallbackHost(); 35 virtual int OnRemoteRequest( 36 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; 37 bool IsRemoteDied(); 38 napi_value PackResult(bool switchState); 39 bool Send(int switchState); 40 void OnSwitchChange(int switchState) override; 41 void DeleteHandler(); 42 void UvQueueWork(uv_loop_s* loop, uv_work_t* work); 43 GetEnv()44 inline napi_env GetEnv() const 45 { 46 return env_; 47 } 48 SetEnv(const napi_env & env)49 inline void SetEnv(const napi_env& env) 50 { 51 env_ = env; 52 } 53 GetHandleCb()54 inline napi_ref GetHandleCb() const 55 { 56 return handlerCb_; 57 } 58 SetHandleCb(const napi_ref & handlerCb)59 inline void SetHandleCb(const napi_ref& handlerCb) 60 { 61 handlerCb_ = handlerCb; 62 } 63 GetRemoteDied()64 inline bool GetRemoteDied() const 65 { 66 return remoteDied_; 67 } 68 SetRemoteDied(const bool remoteDied)69 inline void SetRemoteDied(const bool remoteDied) 70 { 71 remoteDied_ = remoteDied; 72 } 73 74 private: 75 napi_env env_; 76 napi_ref handlerCb_; 77 bool remoteDied_; 78 std::shared_mutex mutex_; 79 }; 80 } // namespace Location 81 } // namespace OHOS 82 #endif // LOCATION_SWITCH_CALLBACK_HOST_H 83