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 FindSwitchCallback(napi_ref cb); 30 void DeleteSwitchCallback(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 napi_env GetEnv(); 46 void SetEnv(const napi_env& env); 47 48 template <typename T> InitContext(T * context)49 bool InitContext(T* context) 50 { 51 if (context == nullptr) { 52 LBSLOGE(SWITCH_CALLBACK, "context == nullptr."); 53 return false; 54 } 55 context->env = env_; 56 context->callback[SUCCESS_CALLBACK] = handlerCb_; 57 return true; 58 } 59 GetRemoteDied()60 inline bool GetRemoteDied() const 61 { 62 return remoteDied_; 63 } 64 SetRemoteDied(const bool remoteDied)65 inline void SetRemoteDied(const bool remoteDied) 66 { 67 remoteDied_ = remoteDied; 68 } 69 70 private: 71 napi_env env_; 72 napi_ref handlerCb_; 73 bool remoteDied_; 74 std::mutex mutex_; 75 }; 76 } // namespace Location 77 } // namespace OHOS 78 #endif // LOCATION_SWITCH_CALLBACK_NAPI_H 79