1 /*
2 * Copyright (c) 2023 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
16 #include "VirtualMessageImpl.h"
17 #include "PreviewerEngineLog.h"
18
GetInstance()19 VirtualMessageImpl& VirtualMessageImpl::GetInstance()
20 {
21 static VirtualMessageImpl instance;
22 return instance;
23 }
24
RegistBundle(const char * bundleAppName,SuccessCallback success,FailCallback fail)25 void VirtualMessageImpl::RegistBundle(const char* bundleAppName, SuccessCallback success, FailCallback fail)
26 {
27 mutex.lock();
28 std::string buildeName(bundleAppName);
29 callBacks[buildeName] = std::make_pair(success, fail);
30 mutex.unlock();
31 }
32
UnregistBundle(const char * bundleAppName)33 void VirtualMessageImpl::UnregistBundle(const char* bundleAppName)
34 {
35 mutex.lock();
36 callBacks.erase(std::string(bundleAppName));
37 mutex.unlock();
38 }
39
StringToCharVector(std::string str) const40 std::vector<char> VirtualMessageImpl::StringToCharVector(std::string str) const
41 {
42 std::vector<char> vec(str.begin(), str.end());
43 vec.push_back('\0');
44 return vec;
45 }
46
SendVirtualMessage(MessageInfo info)47 void VirtualMessageImpl::SendVirtualMessage(MessageInfo info)
48 {
49 OHOS::FeatureAbilityDataInfo osInfo;
50 std::vector<char> deviceId = StringToCharVector(info.deviceID);
51 std::vector<char> bundleName = StringToCharVector(info.bundleName);
52 std::vector<char> abilityName = StringToCharVector(info.abilityName);
53 std::vector<char> message = StringToCharVector(info.message);
54 osInfo.deviceID = &deviceId[0];
55 osInfo.bundleName = &bundleName[0];
56 osInfo.abilityName = &abilityName[0];
57 osInfo.message = &message[0];
58 osInfo.messageLength = static_cast<uint32_t>(message.size());
59
60 mutex.lock();
61 if (callBacks.size() > 1) {
62 ILOG("VirtualMessage callBacks size > 1.");
63 }
64
65 for (auto iter = callBacks.begin(); iter != callBacks.end(); iter++) {
66 if (iter->second.first == nullptr) {
67 ILOG("VirtualAbilityKit::VirtualMessage callback is null");
68 }
69 iter->second.first((reinterpret_cast<void*>(&info)));
70 }
71 mutex.unlock();
72 }
73