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_SWITCH_CALLBACK_NAPI_H 17 #define LOCATION_SWITCH_CALLBACK_NAPI_H 18 #include "i_switch_callback.h" 19 20 #include "iremote_stub.h" 21 #include "message_parcel.h" 22 #include "message_option.h" 23 #include "napi_util.h" 24 #include "napi/native_api.h" 25 #include "uv.h" 26 27 namespace OHOS { 28 namespace Location { 29 bool FindSwitchRegCallback(napi_ref cb); 30 void DeleteSwitchRegCallback(napi_ref cb); 31 class LocationSwitchCallbackNapi : public IRemoteStub<ISwitchCallback> { 32 public: 33 LocationSwitchCallbackNapi(); 34 virtual ~LocationSwitchCallbackNapi(); 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 napi_ref GetHandleCb(); 44 void SetHandleCb(const napi_ref& handlerCb); 45 46 template <typename T> InitContext(T * context)47 bool InitContext(T* context) 48 { 49 if (context == nullptr) { 50 LBSLOGE(SWITCH_CALLBACK, "context == nullptr."); 51 return false; 52 } 53 context->env = env_; 54 context->callback[SUCCESS_CALLBACK] = handlerCb_; 55 return true; 56 } 57 GetEnv()58 inline napi_env GetEnv() const 59 { 60 return env_; 61 } 62 SetEnv(const napi_env & env)63 inline void SetEnv(const napi_env& env) 64 { 65 env_ = env; 66 } 67 GetRemoteDied()68 inline bool GetRemoteDied() const 69 { 70 return remoteDied_; 71 } 72 SetRemoteDied(const bool remoteDied)73 inline void SetRemoteDied(const bool remoteDied) 74 { 75 remoteDied_ = remoteDied; 76 } 77 78 private: 79 napi_env env_; 80 napi_ref handlerCb_; 81 bool remoteDied_; 82 std::mutex mutex_; 83 }; 84 } // namespace Location 85 } // namespace OHOS 86 #endif // LOCATION_SWITCH_CALLBACK_NAPI_H 87