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