1 /*
2 * Copyright (c) 2021-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 "distributed_want_params_wrapper.h"
17
18 #include <algorithm>
19
20 using namespace OHOS::AAFwk;
21 namespace OHOS {
22 namespace DistributedSchedule {
23 constexpr int32_t DISTRIBUTEDWANT_PARAM_WRAPPER_TWO = 2;
24
25 IINTERFACE_IMPL_1(DistributedWantParamWrapper, Object, IDistributedWantParams);
26 const InterfaceID g_IID_IDistributedWantParams = {
27 0xa75b9db6, 0x9813, 0x4371, 0x8848, {0xd, 0x2, 0x9, 0x6, 0x6, 0xc, 0xe, 0x6, 0xe, 0xc, 0x6, 0x8}
28 };
GetValue(DistributedWantParams & value)29 ErrCode DistributedWantParamWrapper::GetValue(DistributedWantParams& value)
30 {
31 value = wantParams_;
32 return ERR_OK;
33 }
34
Equals(IObject & other)35 bool DistributedWantParamWrapper::Equals(IObject& other)
36 {
37 DistributedWantParamWrapper* otherObj =
38 static_cast<DistributedWantParamWrapper*>(IDistributedWantParams::Query(&other));
39 return otherObj != nullptr && otherObj->wantParams_ == wantParams_;
40 }
41
ToString()42 std::string DistributedWantParamWrapper::ToString()
43 {
44 std::string result;
45 if (wantParams_.Size() != 0) {
46 result += "{";
47 for (auto it : wantParams_.GetParams()) {
48 int dTypeId = DistributedWantParams::GetDataType(it.second);
49 result = result + "\"" + it.first + "\":{\"" + std::to_string(dTypeId) + "\":";
50 if (IDistributedWantParams::Query(it.second) != nullptr) {
51 result = result +
52 static_cast<DistributedWantParamWrapper*>(IDistributedWantParams::Query(it.second))->ToString();
53 } else {
54 result = result + "\"" + DistributedWantParams::GetStringByType(it.second, dTypeId) + "\"";
55 }
56 if (it == *wantParams_.GetParams().rbegin()) {
57 result += "}";
58 } else {
59 result += "},";
60 }
61 }
62 result += "}";
63 } else {
64 result = "{}";
65 }
66 return result;
67 }
68
Box(const DistributedWantParams & value)69 sptr<IDistributedWantParams> DistributedWantParamWrapper::Box(const DistributedWantParams& value)
70 {
71 sptr<IDistributedWantParams> object = new (std::nothrow)DistributedWantParamWrapper(value);
72 return object;
73 }
74
Box(DistributedWantParams && value)75 sptr<IDistributedWantParams> DistributedWantParamWrapper::Box(DistributedWantParams&& value)
76 {
77 sptr<IDistributedWantParams> object = new (std::nothrow) DistributedWantParamWrapper(std::move(value));
78 return object;
79 }
80
Unbox(IDistributedWantParams * object)81 DistributedWantParams DistributedWantParamWrapper::Unbox(IDistributedWantParams* object)
82 {
83 DistributedWantParams value;
84 if (object != nullptr) {
85 object->GetValue(value);
86 }
87 return value;
88 }
ValidateStr(const std::string & str)89 bool DistributedWantParamWrapper::ValidateStr(const std::string& str)
90 {
91 if (str == "" || str == "{}" || str == "{\"\"}") {
92 return false;
93 }
94 if (count(str.begin(), str.end(), '\"') % DISTRIBUTEDWANT_PARAM_WRAPPER_TWO != 0) {
95 return false;
96 }
97 if (count(str.begin(), str.end(), '{') != count(str.begin(), str.end(), '}')) {
98 return false;
99 }
100 int counter = 0;
101 for (auto it : str) {
102 if (it == '{') {
103 counter++;
104 }
105 if (it == '}') {
106 counter--;
107 }
108 if (counter < 0) {
109 return false;
110 }
111 }
112 return true;
113 }
114
Parse(const std::string & str)115 sptr<IDistributedWantParams> DistributedWantParamWrapper::Parse(const std::string& str)
116 {
117 DistributedWantParams wantParams;
118 if (ValidateStr(str)) {
119 std::string strKey = "";
120 int typeId = 0;
121 for (size_t strnum = 0; strnum < str.size(); strnum++) {
122 if (str[strnum] == '{' && strKey != "" && typeId == DistributedWantParams::VALUE_TYPE_WANTPARAMS) {
123 size_t num;
124 int count = 0;
125 for (num = strnum; num < str.size(); num++) {
126 if (str[num] == '{') {
127 count++;
128 } else if (str[num] == '}') {
129 count--;
130 }
131 if (count == 0) {
132 break;
133 }
134 }
135 wantParams.SetParam(strKey, DistributedWantParamWrapper::Parse(str.substr(strnum, num - strnum + 1)));
136 strKey = "";
137 typeId = 0;
138 strnum = num + 1;
139 } else if (str[strnum] == '"') {
140 if (strKey == "") {
141 strnum++;
142 strKey = str.substr(strnum, str.find('"', strnum) - strnum);
143 strnum = str.find('"', strnum);
144 } else if (typeId == 0) {
145 strnum++;
146 typeId = atoi(str.substr(strnum, str.find('"', strnum) - strnum).c_str());
147 if (errno == ERANGE) {
148 return nullptr;
149 }
150 strnum = str.find('"', strnum);
151 } else {
152 strnum++;
153 wantParams.SetParam(strKey,
154 DistributedWantParams::GetInterfaceByType(typeId,
155 str.substr(strnum, str.find('"', strnum) - strnum)));
156 strnum = str.find('"', strnum);
157 typeId = 0;
158 strKey = "";
159 }
160 }
161 }
162 }
163 sptr<IDistributedWantParams> iwantParams = new (std::nothrow) DistributedWantParamWrapper(wantParams);
164 return iwantParams;
165 }
ParseWantParams(const std::string & str)166 DistributedWantParams DistributedWantParamWrapper::ParseWantParams(const std::string& str)
167 {
168 DistributedWantParams wantParams;
169 std::string key = "";
170 int typeId = 0;
171 if (!ValidateStr(str)) {
172 return wantParams;
173 }
174 for (size_t strnum = 0; strnum < str.size(); strnum++) {
175 if (str[strnum] == '{' && key != "" && typeId == DistributedWantParams::VALUE_TYPE_WANTPARAMS) {
176 size_t num;
177 int counter = 0;
178 for (num = strnum; num < str.size(); num++) {
179 if (str[num] == '{') {
180 counter++;
181 } else if (str[num] == '}') {
182 counter--;
183 }
184 if (counter == 0) {
185 break;
186 }
187 }
188 wantParams.SetParam(key, DistributedWantParamWrapper::Parse(str.substr(strnum, num - strnum)));
189 key = "";
190 typeId = 0;
191 strnum = num + 1;
192 } else if (str[strnum] == '"') {
193 if (key == "") {
194 strnum++;
195 key = str.substr(strnum, str.find('"', strnum) - strnum);
196 strnum = str.find('"', strnum);
197 } else if (typeId == 0) {
198 strnum++;
199 typeId = atoi(str.substr(strnum, str.find('"', strnum) - strnum).c_str());
200 if (errno == ERANGE) {
201 return wantParams;
202 }
203 strnum = str.find('"', strnum);
204 } else {
205 strnum++;
206 wantParams.SetParam(key,
207 DistributedWantParams::GetInterfaceByType(typeId,
208 str.substr(strnum, str.find('"', strnum) - strnum)));
209 strnum = str.find('"', strnum);
210 typeId = 0;
211 key = "";
212 }
213 }
214 }
215 return wantParams;
216 }
217 } // namespace DistributedSchedule
218 } // namespace OHOS