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