diff --git a/interfaces/kits/js/include/native_devicemanager_js.h b/interfaces/kits/js/include/native_devicemanager_js.h index f3a0f1a539f8647492880fc8779eb903078dd031..96a8871ecf5512c8e106561710f6fe3a9dbe2ca6 100644 --- a/interfaces/kits/js/include/native_devicemanager_js.h +++ b/interfaces/kits/js/include/native_devicemanager_js.h @@ -80,6 +80,39 @@ struct AuthAsyncCallbackInfo { int32_t authType = -1; }; +struct DmNapiStateJsCallback { + std::string bundleName_; + uint16_t subscribeId_; + int32_t reason_; + OHOS::DistributedHardware::DmDeviceInfo deviceInfo_; + + DmNapiStateJsCallback(std::string bundleName, uint16_t subscribeId, int32_t reason, + OHOS::DistributedHardware::DmDeviceInfo deviceInfo) + : bundleName_(bundleName), subscribeId_(subscribeId), reason_(reason), deviceInfo_(deviceInfo) {} +}; + +struct DmNapiAuthJsCallback { + std::string bundleName_; + std::string deviceId_; + std::string token_; + int32_t status_; + int32_t reason_; + + DmNapiAuthJsCallback(std::string bundleName, std::string deviceId, std::string token, int32_t status, + int32_t reason) + : bundleName_(bundleName), deviceId_(deviceId), token_(token), status_(status), reason_(reason) {} +}; + +struct DmNapiVerifyJsCallback { + std::string bundleName_; + std::string deviceId_; + int32_t resultCode_; + int32_t flag_; + + DmNapiVerifyJsCallback(std::string bundleName, std::string deviceId, int32_t resultCode, int32_t flag) + : bundleName_(bundleName), deviceId_(deviceId), resultCode_(resultCode), flag_(flag) {} +}; + enum DmNapiDevStateChangeAction { ONLINE = 0, READY = 1, OFFLINE = 2, CHANGE = 3 }; class DmNapiInitCallback : public OHOS::DistributedHardware::DmInitCallback { diff --git a/interfaces/kits/js/src/native_devicemanager_js.cpp b/interfaces/kits/js/src/native_devicemanager_js.cpp index c56e4138132422427ce314d04343b49614f6bd9a..7ac9497758a52f97f7923b7731609915ddcb29c4 100644 --- a/interfaces/kits/js/src/native_devicemanager_js.cpp +++ b/interfaces/kits/js/src/native_devicemanager_js.cpp @@ -68,16 +68,21 @@ void DmNapiInitCallback::OnRemoteDied() return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (loop == nullptr) { + if (work == nullptr) { LOGE("DmNapiInitCallback: OnRemoteDied, No memory"); return; } - static std::string staticBundleName = bundleName_; + DmDeviceInfo info; + std::unique_ptr jsCallback = + std::make_unique(bundleName_, 0, 0, info); + work->data = reinterpret_cast(jsCallback.get()); + int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); + DmNapiStateJsCallback *callback = reinterpret_cast(work->data); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); if (deviceManagerNapi == nullptr) { - LOGE("OnRemoteDied, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); + LOGE("OnRemoteDied, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); return; } deviceManagerNapi->OnEvent("serviceDie", 0, nullptr); @@ -97,20 +102,23 @@ void DmNapiDeviceStateCallback::OnDeviceOnline(const DmDeviceInfo &deviceInfo) return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (loop == nullptr) { + if (work == nullptr) { LOGE("DmNapiDeviceStateCallback: OnDeviceOnline, No memory"); return; } - static std::string staticBundleName = bundleName_; - static DmDeviceInfo staticDeviceInfo = deviceInfo; + std::unique_ptr jsCallback = + std::make_unique(bundleName_, 0, 0, deviceInfo); + work->data = reinterpret_cast(jsCallback.get()); + int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); + DmNapiStateJsCallback *callback = reinterpret_cast(work->data); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); if (deviceManagerNapi == nullptr) { - LOGE("OnDeviceOnline, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); + LOGE("OnDeviceOnline, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); return; } - deviceManagerNapi->OnDeviceStateChange(DmNapiDevStateChangeAction::ONLINE, staticDeviceInfo); + deviceManagerNapi->OnDeviceStateChange(DmNapiDevStateChangeAction::ONLINE, callback->deviceInfo_); delete work; }); if (ret != 0) { @@ -127,20 +135,23 @@ void DmNapiDeviceStateCallback::OnDeviceReady(const DmDeviceInfo &deviceInfo) return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (loop == nullptr) { + if (work == nullptr) { LOGE("DmNapiDeviceStateCallback: OnDeviceReady, No memory"); return; } - static std::string staticBundleName = bundleName_; - static DmDeviceInfo staticDeviceInfo = deviceInfo; + std::unique_ptr jsCallback = + std::make_unique(bundleName_, 0, 0, deviceInfo); + work->data = reinterpret_cast(jsCallback.get()); + int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); + DmNapiStateJsCallback *callback = reinterpret_cast(work->data); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); if (deviceManagerNapi == nullptr) { - LOGE("OnDeviceReady, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); + LOGE("OnDeviceReady, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); return; } - deviceManagerNapi->OnDeviceStateChange(DmNapiDevStateChangeAction::READY, staticDeviceInfo); + deviceManagerNapi->OnDeviceStateChange(DmNapiDevStateChangeAction::READY, callback->deviceInfo_); delete work; }); if (ret != 0) { @@ -157,20 +168,23 @@ void DmNapiDeviceStateCallback::OnDeviceOffline(const DmDeviceInfo &deviceInfo) return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (loop == nullptr) { + if (work == nullptr) { LOGE("DmNapiDeviceStateCallback: OnDeviceOffline, No memory"); return; } - static std::string staticBundleName = bundleName_; - static DmDeviceInfo staticDeviceInfo = deviceInfo; + std::unique_ptr jsCallback = + std::make_unique(bundleName_, 0, 0, deviceInfo); + work->data = reinterpret_cast(jsCallback.get()); + int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); + DmNapiStateJsCallback *callback = reinterpret_cast(work->data); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); if (deviceManagerNapi == nullptr) { - LOGE("OnDeviceOffline, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); + LOGE("OnDeviceOffline, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); return; } - deviceManagerNapi->OnDeviceStateChange(DmNapiDevStateChangeAction::OFFLINE, staticDeviceInfo); + deviceManagerNapi->OnDeviceStateChange(DmNapiDevStateChangeAction::OFFLINE, callback->deviceInfo_); delete work; }); if (ret != 0) { @@ -192,15 +206,18 @@ void DmNapiDeviceStateCallback::OnDeviceChanged(const DmDeviceInfo &deviceInfo) return; } - static std::string staticBundleName = bundleName_; - static DmDeviceInfo staticDeviceInfo = deviceInfo; + std::unique_ptr jsCallback = + std::make_unique(bundleName_, 0, 0, deviceInfo); + work->data = reinterpret_cast(jsCallback.get()); + int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); + DmNapiStateJsCallback *callback = reinterpret_cast(work->data); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); if (deviceManagerNapi == nullptr) { - LOGE("OnDeviceChanged, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); + LOGE("OnDeviceChanged, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); return; } - deviceManagerNapi->OnDeviceStateChange(DmNapiDevStateChangeAction::CHANGE, staticDeviceInfo); + deviceManagerNapi->OnDeviceStateChange(DmNapiDevStateChangeAction::CHANGE, callback->deviceInfo_); delete work; }); if (ret != 0) { @@ -219,21 +236,23 @@ void DmNapiDiscoveryCallback::OnDeviceFound(uint16_t subscribeId, const DmDevice return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (loop == nullptr) { + if (work == nullptr) { LOGE("DmNapiDiscoveryCallback: OnDeviceFound, No memory"); return; } - static std::string staticBundleName = bundleName_; - static uint16_t staticSubscribeId = subscribeId; - static DmDeviceInfo staticDeviceInfo = deviceInfo; + std::unique_ptr jsCallback = + std::make_unique(bundleName_, subscribeId, 0, deviceInfo); + work->data = reinterpret_cast(jsCallback.get()); + int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); + DmNapiStateJsCallback *callback = reinterpret_cast(work->data); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); if (deviceManagerNapi == nullptr) { - LOGE("OnDeviceFound, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); + LOGE("OnDeviceFound, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); return; } - deviceManagerNapi->OnDeviceFound(staticSubscribeId, staticDeviceInfo); + deviceManagerNapi->OnDeviceFound(callback->subscribeId_, callback->deviceInfo_); delete work; }); if (ret != 0) { @@ -252,21 +271,24 @@ void DmNapiDiscoveryCallback::OnDiscoveryFailed(uint16_t subscribeId, int32_t fa return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (loop == nullptr) { + if (work == nullptr) { LOGE("DmNapiDiscoveryCallback: OnDiscoveryFailed, No memory"); return; } - static std::string staticBundleName = bundleName_; - static uint16_t staticSubscribeId = subscribeId; - static int32_t staticReason = failedReason; + DmDeviceInfo info; + std::unique_ptr jsCallback = + std::make_unique(bundleName_, subscribeId, failedReason, info); + work->data = reinterpret_cast(jsCallback.get()); + int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); + DmNapiStateJsCallback *callback = reinterpret_cast(work->data); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); if (deviceManagerNapi == nullptr) { - LOGE("OnDiscoveryFailed, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); + LOGE("OnDiscoveryFailed, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); return; } - deviceManagerNapi->OnDiscoveryFailed(staticSubscribeId, staticReason); + deviceManagerNapi->OnDiscoveryFailed(callback->subscribeId_, callback->reason_); delete work; }); if (ret != 0) { @@ -309,23 +331,23 @@ void DmNapiAuthenticateCallback::OnAuthResult(const std::string &deviceId, const return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (loop == nullptr) { + if (work == nullptr) { LOGE("DmNapiAuthenticateCallback: OnAuthResult, No memory"); return; } - static std::string staticBundleName = bundleName_; - static std::string staticDeviceId = deviceId; - static std::string staticToken = token; - static int32_t staticStatus = status; - static int32_t staticReason = reason; + std::unique_ptr jsCallback = + std::make_unique(bundleName_, deviceId, token, status, reason); + work->data = reinterpret_cast(jsCallback.get()); + int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); + DmNapiAuthJsCallback *callback = reinterpret_cast(work->data); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); if (deviceManagerNapi == nullptr) { - LOGE("OnAuthResult, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); + LOGE("OnAuthResult, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); return; } - deviceManagerNapi->OnAuthResult(staticDeviceId, staticToken, staticStatus, staticReason); + deviceManagerNapi->OnAuthResult(callback->deviceId_, callback->token_, callback->status_, callback->reason_); delete work; }); if (ret != 0) { @@ -342,22 +364,23 @@ void DmNapiVerifyAuthCallback::OnVerifyAuthResult(const std::string &deviceId, i return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (loop == nullptr) { + if (work == nullptr) { LOGE("DmNapiVerifyAuthCallback: OnVerifyAuthResult, No memory"); return; } - static std::string staticBundleName = bundleName_; - static std::string staticDeviceId = deviceId; - static int32_t staticCode = resultCode; - static int32_t staticFlag = flag; + std::unique_ptr jsCallback = + std::make_unique(bundleName_, deviceId, resultCode, flag); + work->data = reinterpret_cast(jsCallback.get()); + int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); + DmNapiVerifyJsCallback *callback = reinterpret_cast(work->data); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); if (deviceManagerNapi == nullptr) { - LOGE("OnVerifyAuthResult, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); + LOGE("OnVerifyAuthResult, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); return; } - deviceManagerNapi->OnVerifyResult(staticDeviceId, staticCode, staticFlag); + deviceManagerNapi->OnVerifyResult(callback->deviceId_, callback->resultCode_, callback->flag_); delete work; }); if (ret != 0) { @@ -1013,20 +1036,23 @@ void DmNapiDeviceManagerFaCallback::OnCall(const std::string ¶mJson) return; } uv_work_t *work = new (std::nothrow) uv_work_t; - if (loop == nullptr) { + if (work == nullptr) { LOGE("DmNapiDeviceManagerFaCallback: OnCall, No memory"); return; } - static std::string staticBundleName = bundleName_; - static std::string staticParamJson = paramJson; + std::unique_ptr jsCallback = + std::make_unique(bundleName_, "", paramJson, 0, 0); + work->data = reinterpret_cast(jsCallback.get()); + int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { - DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(staticBundleName); + DmNapiAuthJsCallback *callback = reinterpret_cast(work->data); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName_); if (deviceManagerNapi == nullptr) { - LOGE("OnCall, deviceManagerNapi not find for bunderName %s", staticBundleName.c_str()); + LOGE("OnCall, deviceManagerNapi not find for bunderName %s", callback->bundleName_.c_str()); return; } - deviceManagerNapi->OnDmfaCall(staticParamJson); + deviceManagerNapi->OnDmfaCall(callback->token_); delete work; }); if (ret != 0) {