1 /*
2 * Copyright (c) 2025 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 "mission/notification/dms_continue_recommend_info.h"
17
18 #include "cJSON.h"
19 #include "distributed_sched_utils.h"
20 #include "dtbschedmgr_log.h"
21
22 namespace OHOS {
23 namespace DistributedSchedule {
24 namespace {
25 const std::string TAG = "DmsContinueRecommendInfo";
26 }
27
MarshalCandidates() const28 std::string ContinueRecommendInfo::MarshalCandidates() const
29 {
30 cJSON *rootValue = cJSON_CreateObject();
31 if (rootValue == nullptr) {
32 return "";
33 }
34
35 const auto candidatesSize = static_cast<int32_t>(candidates_.size());
36 cJSON *candidates = cJSON_CreateArray();
37 if (candidates == nullptr) {
38 cJSON_Delete(rootValue);
39 return "";
40 }
41 for (auto i = 0; i < candidatesSize; i++) {
42 cJSON *candidateStr = cJSON_CreateString(MarshalCandidate(candidates_[i]).c_str());
43 if (candidateStr == nullptr) {
44 cJSON_Delete(rootValue);
45 cJSON_Delete(candidates);
46 return "";
47 }
48 cJSON_AddItemToArray(candidates, candidateStr);
49 }
50 cJSON_AddItemToObject(rootValue, "Candidates", candidates);
51
52 char *data = cJSON_Print(rootValue);
53 if (data == nullptr) {
54 cJSON_Delete(rootValue);
55 return "";
56 }
57 std::string jsonStr = std::string(data);
58 cJSON_Delete(rootValue);
59 cJSON_free(data);
60 return jsonStr;
61 }
62
MarshalCandidate(const ContinueCandidate & candidate) const63 std::string ContinueRecommendInfo::MarshalCandidate(const ContinueCandidate& candidate) const
64 {
65 cJSON *rootValue = cJSON_CreateObject();
66 if (rootValue == nullptr) {
67 return "";
68 }
69 cJSON_AddStringToObject(rootValue, "DeviceId", candidate.deviceId_.c_str());
70 cJSON_AddStringToObject(rootValue, "DstBundleName", candidate.dstBundleName_.c_str());
71
72 char *data = cJSON_Print(rootValue);
73 if (data == nullptr) {
74 cJSON_Delete(rootValue);
75 return "";
76 }
77
78 std::string jsonStr = std::string(data);
79 cJSON_Delete(rootValue);
80 cJSON_free(data);
81 return jsonStr;
82 }
83
ToString() const84 std::string ContinueRecommendInfo::ToString() const
85 {
86 std::string str =
87 "State: " + std::to_string(state_) + ", " +
88 "SrcBundleName: " + srcBundleName_ + ", "+
89 "ContinueType: " + continueType_ + ", " +
90 "UserId: " + std::to_string(userId_) + ", " +
91 "Candidates: ";
92 for (auto candidate: candidates_) {
93 str += "\n{ DeviceId: " + GetAnonymStr(candidate.deviceId_) + ", " +
94 "DstBundleName: " + candidate.dstBundleName_ + " }";
95 }
96 return str;
97 }
98 } // namespace DistributedSchedule
99 } // namespace OHOS
100