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