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