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 bool FindErrorCallback(napi_ref cb);
30 void DeleteErrorCallback(napi_ref cb);
31 class LocationErrorCallbackNapi : public IRemoteStub<ILocatorCallback> {
32 public:
33     LocationErrorCallbackNapi();
34     virtual ~LocationErrorCallbackNapi();
35     virtual int OnRemoteRequest(
36         uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override;
37     void OnLocationReport(const std::unique_ptr<Location>& location) override;
38     void OnLocatingStatusChange(const int status) override;
39     void OnErrorReport(const int errorCode) override;
40     napi_value PackResult(bool switchState);
41     bool Send(int switchState);
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(LOCATION_ERR_CALLBACK, "context == nullptr.");
54             return false;
55         }
56         context->env = env_;
57         context->callback[SUCCESS_CALLBACK] = handlerCb_;
58         return true;
59     }
60 private:
61     napi_env env_;
62     napi_ref handlerCb_;
63     std::mutex mutex_;
64 };
65 } // namespace Location
66 } // namespace OHOS
67 #endif // LOCATION_ERROR_CALLBACK_NAPI_H
68