/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "VirtualMessageImpl.h" #include "PreviewerEngineLog.h" using namespace std; VirtualMessageImpl& VirtualMessageImpl::GetInstance() { static VirtualMessageImpl instance; return instance; } void VirtualMessageImpl::RegistBundle(const char* bundleAppName, SuccessCallback success, FailCallback fail) { mutex.lock(); string buildeName(bundleAppName); callBacks[buildeName] = make_pair(success, fail); mutex.unlock(); } void VirtualMessageImpl::UnregistBundle(const char* bundleAppName) { mutex.lock(); callBacks.erase(string(bundleAppName)); mutex.unlock(); } std::vector VirtualMessageImpl::StringToCharVector(string str) const { vector vec(str.begin(), str.end()); vec.push_back('\0'); return vec; } void VirtualMessageImpl::SendVirtualMessage(MessageInfo info) { OHOS::FeatureAbilityDataInfo osInfo; vector deviceId = StringToCharVector(info.deviceID); vector bundleName = StringToCharVector(info.bundleName); vector abilityName = StringToCharVector(info.abilityName); vector message = StringToCharVector(info.message); osInfo.deviceID = &deviceId[0]; osInfo.bundleName = &bundleName[0]; osInfo.abilityName = &abilityName[0]; osInfo.message = &message[0]; osInfo.messageLength = static_cast(message.size()); mutex.lock(); if (callBacks.size() > 1) { ILOG("VirtualMessage callBacks size > 1."); } for (auto iter = callBacks.begin(); iter != callBacks.end(); iter++) { if (iter->second.first == nullptr) { ILOG("VirtualAbilityKit::VirtualMessage callback is null"); } iter->second.first((reinterpret_cast(&info))); } mutex.unlock(); }