1 /*
2 * Copyright (c) 2024 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 "want.h"
17 #include <map>
18 #include <string>
19 #include <unistd.h>
20 #include "ability_base_log_wrapper.h"
21 #include "securec.h"
22 #include "want/include/want.h"
23
24 struct AbilityBase_Want {
25 AbilityBase_Element element;
26 std::map<std::string, std::string> params;
27 std::map<std::string, int32_t> fds;
28 std::map<std::string, int32_t> intParams;
29 std::map<std::string, bool> boolParams;
30 std::map<std::string, double> doubleParams;
31 std::string uri = "";
32 int32_t flag = 0;
33 };
34
OH_AbilityBase_CreateWant(AbilityBase_Element element)35 AbilityBase_Want* OH_AbilityBase_CreateWant(AbilityBase_Element element)
36 {
37 std::unique_ptr<AbilityBase_Want> want = std::make_unique<AbilityBase_Want>();
38 want->element.bundleName = element.bundleName;
39 want->element.moduleName = element.moduleName;
40 want->element.abilityName = element.abilityName;
41 return want.release();
42 }
43
OH_AbilityBase_DestroyWant(AbilityBase_Want * want)44 AbilityBase_ErrorCode OH_AbilityBase_DestroyWant(AbilityBase_Want* want)
45 {
46 if (want == nullptr) {
47 ABILITYBASE_LOGE("null want");
48 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
49 }
50
51 if (!want->params.empty()) {
52 want->params.clear();
53 }
54
55 delete want;
56 want = nullptr;
57 return ABILITY_BASE_ERROR_CODE_NO_ERROR;
58 }
59
OH_AbilityBase_SetWantElement(AbilityBase_Want * want,AbilityBase_Element element)60 AbilityBase_ErrorCode OH_AbilityBase_SetWantElement(AbilityBase_Want* want, AbilityBase_Element element)
61 {
62 if (want == nullptr) {
63 ABILITYBASE_LOGE("null want");
64 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
65 }
66 want->element.bundleName = element.bundleName;
67 want->element.moduleName = element.moduleName;
68 want->element.abilityName = element.abilityName;
69 return ABILITY_BASE_ERROR_CODE_NO_ERROR;
70 }
71
OH_AbilityBase_GetWantElement(AbilityBase_Want * want,AbilityBase_Element * element)72 AbilityBase_ErrorCode OH_AbilityBase_GetWantElement(AbilityBase_Want* want, AbilityBase_Element* element)
73 {
74 if (want == nullptr || element == nullptr) {
75 ABILITYBASE_LOGE("null arg");
76 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
77 }
78 element->bundleName = want->element.bundleName;
79 element->moduleName = want->element.moduleName;
80 element->abilityName = want->element.abilityName;
81 return ABILITY_BASE_ERROR_CODE_NO_ERROR;
82 }
83
OH_AbilityBase_SetWantCharParam(AbilityBase_Want * want,const char * key,const char * value)84 AbilityBase_ErrorCode OH_AbilityBase_SetWantCharParam(AbilityBase_Want* want, const char* key, const char* value)
85 {
86 if (want == nullptr || key == nullptr || value == nullptr) {
87 ABILITYBASE_LOGE("null want");
88 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
89 }
90 want->params[std::string(key)] = std::string(value);
91 return ABILITY_BASE_ERROR_CODE_NO_ERROR;
92 }
93
OH_AbilityBase_GetWantCharParam(AbilityBase_Want * want,const char * key,char * value,size_t valueSize)94 AbilityBase_ErrorCode OH_AbilityBase_GetWantCharParam(AbilityBase_Want* want, const char* key,
95 char* value, size_t valueSize)
96 {
97 if (want == nullptr || key == nullptr || value == nullptr) {
98 ABILITYBASE_LOGE("null arg");
99 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
100 }
101 auto it = want->params.find(key);
102 if (it == want->params.end()) {
103 ABILITYBASE_LOGE("not found key");
104 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
105 }
106 const std::string& foundValue = it->second;
107 if (foundValue.size() >= valueSize) {
108 ABILITYBASE_LOGE("no enough buffer");
109 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
110 }
111 if (strcpy_s(value, valueSize, it->second.c_str()) != EOK) {
112 ABILITYBASE_LOGE("strcpy_s err");
113 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
114 }
115 return ABILITY_BASE_ERROR_CODE_NO_ERROR;
116 }
117
OH_AbilityBase_AddWantFd(AbilityBase_Want * want,const char * key,int32_t fd)118 AbilityBase_ErrorCode OH_AbilityBase_AddWantFd(AbilityBase_Want* want, const char* key, int32_t fd)
119 {
120 if (want == nullptr || key == nullptr) {
121 ABILITYBASE_LOGE("null want");
122 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
123 }
124 want->fds[std::string(key)] = fd;
125 return ABILITY_BASE_ERROR_CODE_NO_ERROR;
126 }
127
OH_AbilityBase_GetWantFd(AbilityBase_Want * want,const char * key,int32_t * fd)128 AbilityBase_ErrorCode OH_AbilityBase_GetWantFd(AbilityBase_Want* want, const char* key, int32_t* fd)
129 {
130 if (want == nullptr || key == nullptr || fd == nullptr) {
131 ABILITYBASE_LOGE("null arg");
132 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
133 }
134 auto it = want->fds.find(key);
135 if (it == want->fds.end()) {
136 ABILITYBASE_LOGE("not found key: %{public}s", key);
137 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
138 }
139 *fd = it->second;
140 return ABILITY_BASE_ERROR_CODE_NO_ERROR;
141 }
142
OH_AbilityBase_SetWantUri(AbilityBase_Want * want,const char * uri)143 AbilityBase_ErrorCode OH_AbilityBase_SetWantUri(AbilityBase_Want* want, const char* uri)
144 {
145 if (want == nullptr || uri == nullptr) {
146 ABILITYBASE_LOGE("null arg");
147 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
148 }
149 want->uri = std::string(uri);
150 return ABILITY_BASE_ERROR_CODE_NO_ERROR;
151 }
152
OH_AbilityBase_GetWantUri(AbilityBase_Want * want,char * uri,size_t uriSize)153 AbilityBase_ErrorCode OH_AbilityBase_GetWantUri(AbilityBase_Want* want, char* uri, size_t uriSize)
154 {
155 if (want == nullptr || uri == nullptr) {
156 ABILITYBASE_LOGE("null arg");
157 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
158 }
159 const std::string foundValue = want->uri;
160 if (foundValue.size() >= uriSize) {
161 ABILITYBASE_LOGE("no enough buffer");
162 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
163 }
164
165 if (strcpy_s(uri, uriSize, foundValue.c_str()) != EOK) {
166 ABILITYBASE_LOGE("strcpy_s err");
167 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
168 }
169 return ABILITY_BASE_ERROR_CODE_NO_ERROR;
170 }
171
OH_AbilityBase_SetWantInt32Param(AbilityBase_Want * want,const char * key,int32_t value)172 AbilityBase_ErrorCode OH_AbilityBase_SetWantInt32Param(AbilityBase_Want* want, const char* key, int32_t value)
173 {
174 if (want == nullptr || key == nullptr) {
175 ABILITYBASE_LOGE("null arg");
176 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
177 }
178 want->intParams[std::string(key)] = value;
179 return ABILITY_BASE_ERROR_CODE_NO_ERROR;
180 }
181
OH_AbilityBase_GetWantInt32Param(AbilityBase_Want * want,const char * key,int32_t * value)182 AbilityBase_ErrorCode OH_AbilityBase_GetWantInt32Param(AbilityBase_Want* want, const char* key, int32_t* value)
183 {
184 if (want == nullptr || key == nullptr || value == nullptr) {
185 ABILITYBASE_LOGE("null arg");
186 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
187 }
188 auto it = want->intParams.find(key);
189 if (it == want->intParams.end()) {
190 ABILITYBASE_LOGE("not found key: %{public}s", key);
191 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
192 }
193 *value = it->second;
194 return ABILITY_BASE_ERROR_CODE_NO_ERROR;
195 }
196
OH_AbilityBase_SetWantBoolParam(AbilityBase_Want * want,const char * key,bool value)197 AbilityBase_ErrorCode OH_AbilityBase_SetWantBoolParam(AbilityBase_Want* want, const char* key, bool value)
198 {
199 if (want == nullptr || key == nullptr) {
200 ABILITYBASE_LOGE("null arg");
201 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
202 }
203 want->boolParams[std::string(key)] = value;
204 return ABILITY_BASE_ERROR_CODE_NO_ERROR;
205 }
206
OH_AbilityBase_GetWantBoolParam(AbilityBase_Want * want,const char * key,bool * value)207 AbilityBase_ErrorCode OH_AbilityBase_GetWantBoolParam(AbilityBase_Want* want, const char* key, bool* value)
208 {
209 if (want == nullptr || key == nullptr || value == nullptr) {
210 ABILITYBASE_LOGE("null arg");
211 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
212 }
213 auto it = want->boolParams.find(key);
214 if (it == want->boolParams.end()) {
215 ABILITYBASE_LOGE("not found key: %{public}s", key);
216 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
217 }
218 *value = it->second;
219 return ABILITY_BASE_ERROR_CODE_NO_ERROR;
220 }
221
OH_AbilityBase_SetWantDoubleParam(AbilityBase_Want * want,const char * key,double value)222 AbilityBase_ErrorCode OH_AbilityBase_SetWantDoubleParam(AbilityBase_Want* want, const char* key, double value)
223 {
224 if (want == nullptr || key == nullptr) {
225 ABILITYBASE_LOGE("null arg");
226 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
227 }
228 want->doubleParams[std::string(key)] = value;
229 return ABILITY_BASE_ERROR_CODE_NO_ERROR;
230 }
231
OH_AbilityBase_GetWantDoubleParam(AbilityBase_Want * want,const char * key,double * value)232 AbilityBase_ErrorCode OH_AbilityBase_GetWantDoubleParam(AbilityBase_Want* want, const char* key, double* value)
233 {
234 if (want == nullptr || key == nullptr || value == nullptr) {
235 ABILITYBASE_LOGE("null arg");
236 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
237 }
238 auto it = want->doubleParams.find(key);
239 if (it == want->doubleParams.end()) {
240 ABILITYBASE_LOGE("not found key: %{public}s", key);
241 return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
242 }
243 *value = it->second;
244 return ABILITY_BASE_ERROR_CODE_NO_ERROR;
245 }