1 /*
2 * Copyright (c) 2021-2022 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 "form_db_info.h"
17
18 #include "json_util_form.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
22 namespace {
23 const std::string INNER_FORM_INFO_FORM_ID = "formId";
24 const std::string INNER_FORM_INFO_USER_ID = "userId";
25 const std::string INNER_FORM_INFO_PROVIDER_USER_ID = "providerUserId";
26 const std::string INNER_FORM_INFO_FORM_NAME = "formName";
27 const std::string INNER_FORM_INFO_BUNDLE_NAME = "bundleName";
28 const std::string INNER_FORM_INFO_MODULE_NAME = "moduleName";
29 const std::string INNER_FORM_INFO_ABILITY_NAME = "abilityName";
30 const std::string INNER_FORM_INFO_FORM_USER_UIDS = "formUserUids";
31 } // namespace
32
33 /**
34 * @brief Transform the InnerFormInfo object to json.
35 * @param jsonObject Indicates the obtained json object.
36 */
ToJson(nlohmann::json & jsonObject) const37 void InnerFormInfo::ToJson(nlohmann::json &jsonObject) const
38 {
39 jsonObject[INNER_FORM_INFO_FORM_ID] = formDBInfo_.formId;
40 jsonObject[INNER_FORM_INFO_USER_ID] = formDBInfo_.userId;
41 jsonObject[INNER_FORM_INFO_PROVIDER_USER_ID] = formDBInfo_.providerUserId;
42 jsonObject[INNER_FORM_INFO_FORM_NAME] = formDBInfo_.formName;
43 jsonObject[INNER_FORM_INFO_BUNDLE_NAME] = formDBInfo_.bundleName;
44 jsonObject[INNER_FORM_INFO_MODULE_NAME] = formDBInfo_.moduleName;
45 jsonObject[INNER_FORM_INFO_ABILITY_NAME] = formDBInfo_.abilityName;
46 jsonObject[INNER_FORM_INFO_FORM_USER_UIDS] = formDBInfo_.formUserUids;
47 }
48
49 /**
50 * @brief Transform the json object to InnerFormInfo object.
51 * @param jsonObject Indicates the obtained json object.
52 * @return true on success or false on failure
53 */
FromJson(const nlohmann::json & jsonObject)54 bool InnerFormInfo::FromJson(const nlohmann::json &jsonObject)
55 {
56 const auto &jsonObjectEnd = jsonObject.end();
57 int32_t parseResult = ERR_OK;
58 GetValueIfFindKey<int64_t>(jsonObject,
59 jsonObjectEnd,
60 INNER_FORM_INFO_FORM_ID,
61 formDBInfo_.formId,
62 JsonType::NUMBER,
63 false,
64 parseResult,
65 ArrayType::NOT_ARRAY);
66 GetValueIfFindKey<int32_t>(jsonObject,
67 jsonObjectEnd,
68 INNER_FORM_INFO_USER_ID,
69 formDBInfo_.userId,
70 JsonType::NUMBER,
71 false,
72 parseResult,
73 ArrayType::NOT_ARRAY);
74 GetValueIfFindKey<int32_t>(jsonObject,
75 jsonObjectEnd,
76 INNER_FORM_INFO_PROVIDER_USER_ID,
77 formDBInfo_.providerUserId,
78 JsonType::NUMBER,
79 false,
80 parseResult,
81 ArrayType::NOT_ARRAY);
82 GetValueIfFindKey<std::string>(jsonObject,
83 jsonObjectEnd,
84 INNER_FORM_INFO_FORM_NAME,
85 formDBInfo_.formName,
86 JsonType::STRING,
87 false,
88 parseResult,
89 ArrayType::NOT_ARRAY);
90 GetValueIfFindKey<std::string>(jsonObject,
91 jsonObjectEnd,
92 INNER_FORM_INFO_BUNDLE_NAME,
93 formDBInfo_.bundleName,
94 JsonType::STRING,
95 false,
96 parseResult,
97 ArrayType::NOT_ARRAY);
98 GetValueIfFindKey<std::string>(jsonObject,
99 jsonObjectEnd,
100 INNER_FORM_INFO_MODULE_NAME,
101 formDBInfo_.moduleName,
102 JsonType::STRING,
103 false,
104 parseResult,
105 ArrayType::NOT_ARRAY);
106 GetValueIfFindKey<std::string>(jsonObject,
107 jsonObjectEnd,
108 INNER_FORM_INFO_ABILITY_NAME,
109 formDBInfo_.abilityName,
110 JsonType::STRING,
111 false,
112 parseResult,
113 ArrayType::NOT_ARRAY);
114 GetValueIfFindKey<std::vector<int>>(jsonObject,
115 jsonObjectEnd,
116 INNER_FORM_INFO_FORM_USER_UIDS,
117 formDBInfo_.formUserUids,
118 JsonType::ARRAY,
119 false,
120 parseResult,
121 ArrayType::NUMBER);
122 return parseResult == ERR_OK;
123 }
124
AddUserUid(const int callingUid)125 void InnerFormInfo::AddUserUid(const int callingUid)
126 {
127 auto iter = std::find(formDBInfo_.formUserUids.begin(), formDBInfo_.formUserUids.end(), callingUid);
128 if (iter == formDBInfo_.formUserUids.end()) {
129 formDBInfo_.formUserUids.push_back(callingUid);
130 }
131 }
132
DeleteUserUid(const int callingUid)133 bool InnerFormInfo::DeleteUserUid(const int callingUid)
134 {
135 auto iter = std::find(formDBInfo_.formUserUids.begin(), formDBInfo_.formUserUids.end(), callingUid);
136 if (iter == formDBInfo_.formUserUids.end()) {
137 return false;
138 }
139 formDBInfo_.formUserUids.erase(iter);
140 return true;
141 }
142 } // namespace AppExecFwk
143 } // namespace OHOS