• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class NmeaMessageCallbackNapi : public IRemoteStub<INmeaMessageCallback> {
33 public:
34     NmeaMessageCallbackNapi();
35     virtual ~NmeaMessageCallbackNapi();
36     virtual int OnRemoteRequest(
37         uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override;
38     bool IsRemoteDied();
39     bool Send(const std::string msg);
40     napi_value PackResult(const std::string msg);
41     void OnMessageChange(int64_t timestamp, const std::string msg) override;
42     void DeleteHandler();
43     void UvQueueWork(uv_loop_s* loop, uv_work_t* work);
44 
45     template <typename T>
InitContext(T * context)46     bool InitContext(T* context)
47     {
48         if (context == nullptr) {
49             LBSLOGE(NMEA_MESSAGE_CALLBACK, "context == nullptr.");
50             return false;
51         }
52         context->env = env_;
53         context->callback[SUCCESS_CALLBACK] = handlerCb_;
54         return true;
55     }
56 
GetEnv()57     inline napi_env GetEnv() const
58     {
59         return env_;
60     }
61 
SetEnv(const napi_env & env)62     inline void SetEnv(const napi_env& env)
63     {
64         env_ = env;
65     }
66 
GetHandleCb()67     inline napi_ref GetHandleCb() const
68     {
69         return handlerCb_;
70     }
71 
SetHandleCb(const napi_ref & handlerCb)72     inline void SetHandleCb(const napi_ref& handlerCb)
73     {
74         handlerCb_ = handlerCb;
75     }
76 
GetRemoteDied()77     inline bool GetRemoteDied() const
78     {
79         return remoteDied_;
80     }
81 
SetRemoteDied(const bool remoteDied)82     inline void SetRemoteDied(const bool remoteDied)
83     {
84         remoteDied_ = remoteDied;
85     }
86 
87 private:
88     napi_env env_;
89     napi_ref handlerCb_;
90     bool remoteDied_;
91     std::mutex mutex_;
92 };
93 } // namespace Location
94 } // namespace OHOS
95 #endif // NMEA_MESSAGE_CALLBACK_NAPI_H
96