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 NMEA_MESSAGE_CALLBACK_NAPI_H 17 #define NMEA_MESSAGE_CALLBACK_NAPI_H 18 19 #include <string> 20 21 #include "iremote_stub.h" 22 #include "message_option.h" 23 #include "message_parcel.h" 24 #include "napi_util.h" 25 #include "napi/native_api.h" 26 #include "uv.h" 27 28 #include "i_nmea_message_callback.h" 29 30 namespace OHOS { 31 namespace Location { 32 bool FindNmeaCallback(napi_ref cb); 33 void DeleteNmeaCallback(napi_ref cb); 34 class NmeaMessageCallbackNapi : public IRemoteStub<INmeaMessageCallback> { 35 public: 36 NmeaMessageCallbackNapi(); 37 virtual ~NmeaMessageCallbackNapi(); 38 virtual int OnRemoteRequest( 39 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; 40 bool IsRemoteDied(); 41 bool Send(const std::string msg); 42 napi_value PackResult(const std::string msg); 43 void OnMessageChange(int64_t timestamp, const std::string msg) override; 44 void DeleteHandler(); 45 void UvQueueWork(uv_loop_s* loop, uv_work_t* work); 46 napi_ref GetHandleCb(); 47 void SetHandleCb(const napi_ref& handlerCb); 48 napi_env GetEnv(); 49 void SetEnv(const napi_env& env); 50 51 template <typename T> InitContext(T * context)52 bool InitContext(T* context) 53 { 54 if (context == nullptr) { 55 LBSLOGE(NMEA_MESSAGE_CALLBACK, "context == nullptr."); 56 return false; 57 } 58 context->env = env_; 59 context->callback[SUCCESS_CALLBACK] = handlerCb_; 60 return true; 61 } 62 GetRemoteDied()63 inline bool GetRemoteDied() const 64 { 65 return remoteDied_; 66 } 67 SetRemoteDied(const bool remoteDied)68 inline void SetRemoteDied(const bool remoteDied) 69 { 70 remoteDied_ = remoteDied; 71 } 72 73 private: 74 napi_env env_; 75 napi_ref handlerCb_; 76 bool remoteDied_; 77 std::mutex mutex_; 78 }; 79 } // namespace Location 80 } // namespace OHOS 81 #endif // NMEA_MESSAGE_CALLBACK_NAPI_H 82