• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             NapiHandleScope scope(callbackInfo->env_);
47             napi_value result = nullptr;
48             napi_create_object(callbackInfo->env_, &result);
49             ConvertOppTransferInformationToJS(callbackInfo->env_, result, callbackInfo->information_);
50             napi_value callback = nullptr;
51             napi_value undefined = nullptr;
52             napi_value callResult = nullptr;
53             napi_get_undefined(callbackInfo->env_, &undefined);
54             napi_get_reference_value(callbackInfo->env_, callbackInfo->callback_, &callback);
55             napi_call_function(callbackInfo->env_, undefined, callback, ARGS_SIZE_ONE, &result, &callResult);
56             delete work;
57             work = nullptr;
58         });
59 }
60 
OnTransferStateChanged(const BluetoothOppTransferInformation & transferInformation)61 void NapiBluetoothOppObserver::OnTransferStateChanged(const BluetoothOppTransferInformation &transferInformation)
62 {
63     HILOGI("NapiBluetoothOppObserver::OnTransferStateChanged called");
64     if (!callbackInfos_[STR_BT_OPP_OBSERVER_TRANSFER_STATE_CHANGE]) {
65         HILOGW("NapiBluetoothOppObserver::OnTransferStateChanged: This callback is not registered by ability.");
66         return;
67     }
68     HILOGI("NapiBluetoothOppObserver::OnTransferStateChanged: %{public}s is registered by ability",
69         STR_BT_OPP_OBSERVER_TRANSFER_STATE_CHANGE.c_str());
70     std::shared_ptr<TransforInformationCallbackInfo> callbackInfo =
71         std::static_pointer_cast<TransforInformationCallbackInfo>(
72             callbackInfos_[STR_BT_OPP_OBSERVER_TRANSFER_STATE_CHANGE]);
73 
74     callbackInfo->deviceId_ = transferInformation.GetDeviceAddress();
75     callbackInfo->information_ = transferInformation;
76 
77     uv_loop_s *loop = nullptr;
78     napi_get_uv_event_loop(callbackInfo->env_, &loop);
79     uv_work_t *work = new uv_work_t;
80     work->data = (void*)callbackInfo.get();
81 
82     uv_queue_work(
83         loop,
84         work,
85         [](uv_work_t *work) {},
86         [](uv_work_t *work, int status) {
87             TransforInformationCallbackInfo *callbackInfo = (TransforInformationCallbackInfo *)work->data;
88             napi_value result = nullptr;
89             napi_create_object(callbackInfo->env_, &result);
90             ConvertOppTransferInformationToJS(callbackInfo->env_, result, callbackInfo->information_);
91             napi_value callback = nullptr;
92             napi_value undefined = nullptr;
93             napi_value callResult = nullptr;
94             napi_get_undefined(callbackInfo->env_, &undefined);
95             napi_get_reference_value(callbackInfo->env_, callbackInfo->callback_, &callback);
96             napi_call_function(callbackInfo->env_, undefined, callback, ARGS_SIZE_ONE, &result, &callResult);
97             delete work;
98             work = nullptr;
99         });
100 }
101 } // namespace Bluetooth
102 } // namespace OHOS