• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
29 
OH_AbilityBase_CreateWant(AbilityBase_Element element)30 AbilityBase_Want* OH_AbilityBase_CreateWant(AbilityBase_Element element)
31 {
32     std::unique_ptr<AbilityBase_Want> want = std::make_unique<AbilityBase_Want>();
33     want->element.bundleName = element.bundleName;
34     want->element.moduleName = element.moduleName;
35     want->element.abilityName = element.abilityName;
36     return want.release();
37 }
38 
OH_AbilityBase_DestroyWant(AbilityBase_Want * want)39 AbilityBase_ErrorCode OH_AbilityBase_DestroyWant(AbilityBase_Want* want)
40 {
41     if (want == nullptr) {
42         ABILITYBASE_LOGE("null want");
43         return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
44     }
45 
46     if (!want->params.empty()) {
47         want->params.clear();
48     }
49 
50     delete want;
51     want = nullptr;
52     return ABILITY_BASE_ERROR_CODE_NO_ERROR;
53 }
54 
OH_AbilityBase_SetWantElement(AbilityBase_Want * want,AbilityBase_Element element)55 AbilityBase_ErrorCode OH_AbilityBase_SetWantElement(AbilityBase_Want* want, AbilityBase_Element element)
56 {
57     if (want == nullptr) {
58         ABILITYBASE_LOGE("null want");
59         return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
60     }
61     want->element.bundleName = element.bundleName;
62     want->element.moduleName = element.moduleName;
63     want->element.abilityName = element.abilityName;
64     return ABILITY_BASE_ERROR_CODE_NO_ERROR;
65 }
66 
OH_AbilityBase_GetWantElement(AbilityBase_Want * want,AbilityBase_Element * element)67 AbilityBase_ErrorCode OH_AbilityBase_GetWantElement(AbilityBase_Want* want, AbilityBase_Element* element)
68 {
69     if (want == nullptr || element == nullptr) {
70         ABILITYBASE_LOGE("null arg");
71         return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
72     }
73     element->bundleName = want->element.bundleName;
74     element->moduleName = want->element.moduleName;
75     element->abilityName = want->element.abilityName;
76     return ABILITY_BASE_ERROR_CODE_NO_ERROR;
77 }
78 
OH_AbilityBase_SetWantCharParam(AbilityBase_Want * want,const char * key,const char * value)79 AbilityBase_ErrorCode OH_AbilityBase_SetWantCharParam(AbilityBase_Want* want, const char* key, const char* value)
80 {
81     if (want == nullptr || key == nullptr || value == nullptr) {
82         ABILITYBASE_LOGE("null want");
83         return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
84     }
85     want->params[std::string(key)] = std::string(value);
86     return ABILITY_BASE_ERROR_CODE_NO_ERROR;
87 }
88 
OH_AbilityBase_GetWantCharParam(AbilityBase_Want * want,const char * key,char * value,size_t valueSize)89 AbilityBase_ErrorCode OH_AbilityBase_GetWantCharParam(AbilityBase_Want* want, const char* key,
90     char* value, size_t valueSize)
91 {
92     if (want == nullptr || key == nullptr || value == nullptr) {
93         ABILITYBASE_LOGE("null arg");
94         return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
95     }
96     auto it = want->params.find(key);
97     if (it == want->params.end()) {
98         ABILITYBASE_LOGE("not found key");
99         return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
100     }
101     const std::string& foundValue = it->second;
102     if (foundValue.size() >= valueSize) {
103         ABILITYBASE_LOGE("no enough buffer");
104         return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
105     }
106     if (strcpy_s(value, valueSize, it->second.c_str()) != EOK) {
107         ABILITYBASE_LOGE("strcpy_s err");
108         return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
109     }
110     return ABILITY_BASE_ERROR_CODE_NO_ERROR;
111 }
112 
OH_AbilityBase_AddWantFd(AbilityBase_Want * want,const char * key,int32_t fd)113 AbilityBase_ErrorCode OH_AbilityBase_AddWantFd(AbilityBase_Want* want, const char* key, int32_t fd)
114 {
115     if (want == nullptr) {
116         ABILITYBASE_LOGE("null want");
117         return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
118     }
119     want->fds[std::string(key)] = fd;
120     return ABILITY_BASE_ERROR_CODE_NO_ERROR;
121 }
122 
OH_AbilityBase_GetWantFd(AbilityBase_Want * want,const char * key,int32_t * fd)123 AbilityBase_ErrorCode OH_AbilityBase_GetWantFd(AbilityBase_Want* want, const char* key, int32_t* fd)
124 {
125     if (want == nullptr || key == nullptr || fd == nullptr) {
126         ABILITYBASE_LOGE("null arg");
127         return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
128     }
129     auto it = want->fds.find(key);
130     if (it == want->fds.end()) {
131         ABILITYBASE_LOGE("not found key: %{public}s", key);
132         return ABILITY_BASE_ERROR_CODE_PARAM_INVALID;
133     }
134     *fd = it->second;
135     return ABILITY_BASE_ERROR_CODE_NO_ERROR;
136 }