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