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 #include <uv.h>
16 #include "napi_async_callback.h"
17 #include "napi_bluetooth_opp_observer.h"
18
19 namespace OHOS {
20 namespace Bluetooth {
OnReceiveIncomingFileChanged(const BluetoothOppTransferInformation & transferInformation)21 void NapiBluetoothOppObserver::OnReceiveIncomingFileChanged(const BluetoothOppTransferInformation &transferInformation)
22 {
23 HILOGI("NapiBluetoothOppObserver::OnReceiveIncomingFileChanged called");
24 if (!callbackInfos_[STR_BT_OPP_OBSERVER_RECEIVE_INCOMING_FILE]) {
25 HILOGW("NapiBluetoothOppObserver::OnReceiveIncomingFileChanged: This callback is not registered by ability.");
26 return;
27 }
28 HILOGI("NapiBluetoothOppObserver::OnReceiveIncomingFileChanged: %{public}s is registered by ability",
29 STR_BT_OPP_OBSERVER_RECEIVE_INCOMING_FILE.c_str());
30 std::shared_ptr<TransforInformationCallbackInfo> callbackInfo =
31 std::static_pointer_cast<TransforInformationCallbackInfo>(
32 callbackInfos_[STR_BT_OPP_OBSERVER_RECEIVE_INCOMING_FILE]);
33 callbackInfo->deviceId_ = transferInformation.GetDeviceAddress();
34 callbackInfo->information_ = transferInformation;
35 uv_loop_s *loop = nullptr;
36 napi_get_uv_event_loop(callbackInfo->env_, &loop);
37 uv_work_t *work = new uv_work_t;
38 work->data = static_cast<void *>(callbackInfo.get());
39
40 uv_queue_work(
41 loop,
42 work,
43 [](uv_work_t *work) {},
44 [](uv_work_t *work, int status) {
45 TransforInformationCallbackInfo *callbackInfo = static_cast<TransforInformationCallbackInfo *>(work->data);
46 CHECK_AND_RETURN_LOG(callbackInfo, "callbackInfo is null");
47 NapiHandleScope scope(callbackInfo->env_);
48 napi_value result = nullptr;
49 napi_create_object(callbackInfo->env_, &result);
50 ConvertOppTransferInformationToJS(callbackInfo->env_, result, callbackInfo->information_);
51 napi_value callback = nullptr;
52 napi_value undefined = nullptr;
53 napi_value callResult = nullptr;
54 napi_get_undefined(callbackInfo->env_, &undefined);
55 napi_get_reference_value(callbackInfo->env_, callbackInfo->callback_, &callback);
56 napi_call_function(callbackInfo->env_, undefined, callback, ARGS_SIZE_ONE, &result, &callResult);
57 delete work;
58 work = nullptr;
59 });
60 }
61
OnTransferStateChanged(const BluetoothOppTransferInformation & transferInformation)62 void NapiBluetoothOppObserver::OnTransferStateChanged(const BluetoothOppTransferInformation &transferInformation)
63 {
64 HILOGI("NapiBluetoothOppObserver::OnTransferStateChanged called");
65 if (!callbackInfos_[STR_BT_OPP_OBSERVER_TRANSFER_STATE_CHANGE]) {
66 HILOGW("NapiBluetoothOppObserver::OnTransferStateChanged: This callback is not registered by ability.");
67 return;
68 }
69 HILOGI("NapiBluetoothOppObserver::OnTransferStateChanged: %{public}s is registered by ability",
70 STR_BT_OPP_OBSERVER_TRANSFER_STATE_CHANGE.c_str());
71 std::shared_ptr<TransforInformationCallbackInfo> callbackInfo =
72 std::static_pointer_cast<TransforInformationCallbackInfo>(
73 callbackInfos_[STR_BT_OPP_OBSERVER_TRANSFER_STATE_CHANGE]);
74
75 callbackInfo->deviceId_ = transferInformation.GetDeviceAddress();
76 callbackInfo->information_ = transferInformation;
77
78 uv_loop_s *loop = nullptr;
79 napi_get_uv_event_loop(callbackInfo->env_, &loop);
80 uv_work_t *work = new uv_work_t;
81 work->data = (void*)callbackInfo.get();
82
83 uv_queue_work(
84 loop,
85 work,
86 [](uv_work_t *work) {},
87 [](uv_work_t *work, int status) {
88 TransforInformationCallbackInfo *callbackInfo = (TransforInformationCallbackInfo *)work->data;
89 CHECK_AND_RETURN_LOG(callbackInfo, "callbackInfo is null");
90 NapiHandleScope scope(callbackInfo->env_);
91 napi_value result = nullptr;
92 napi_create_object(callbackInfo->env_, &result);
93 ConvertOppTransferInformationToJS(callbackInfo->env_, result, callbackInfo->information_);
94 napi_value callback = nullptr;
95 napi_value undefined = nullptr;
96 napi_value callResult = nullptr;
97 napi_get_undefined(callbackInfo->env_, &undefined);
98 napi_get_reference_value(callbackInfo->env_, callbackInfo->callback_, &callback);
99 napi_call_function(callbackInfo->env_, undefined, callback, ARGS_SIZE_ONE, &result, &callResult);
100 delete work;
101 work = nullptr;
102 });
103 }
104 } // namespace Bluetooth
105 } // namespace OHOS