• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 BLUETOOTH_SCAN_RESULT_CALLBACK_NAPI_H
17 #define BLUETOOTH_SCAN_RESULT_CALLBACK_NAPI_H
18 
19 #include "iremote_stub.h"
20 #include "napi/native_api.h"
21 #include "uv.h"
22 
23 #include "constant_definition.h"
24 #include "location_log.h"
25 #include "i_bluetooth_scan_result_callback.h"
26 #include "bluetooth_scan_result.h"
27 
28 namespace OHOS {
29 namespace Location {
30 bool FindBlueToohCallback(napi_ref cb);
31 void DeleteBlueToohCallback(napi_ref cb);
32 class BluetoothScanResultCallbackNapi : public IRemoteStub<IBluetoohScanResultCallback> {
33 public:
34     BluetoothScanResultCallbackNapi();
35     virtual ~BluetoothScanResultCallbackNapi();
36     virtual int OnRemoteRequest(uint32_t code,
37         MessageParcel& data, MessageParcel& reply, MessageOption& option) override;
38 
39     void DoSendWork(uv_loop_s *&loop, uv_work_t *&work);
40     void OnBluetoohScanResultChange(const std::unique_ptr<BluetoothScanResult>& bluetoothScanResult) override;
41     void DeleteAllCallbacks();
42     void DeleteHandler();
43     bool FindCallback();
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(LOCATOR_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 // BLUETOOTH_SCAN_RESULT_CALLBACK_NAPI_H
68