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 "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 const std::string INNER_FORM_INFO_FORM_LOCATION = "formLocation";
32 const std::string INNER_FORM_INFO_FORM_ENABLE = "enableForm";
33 const std::string INNER_FORM_INFO_FORM_LOCK = "lockForm";
34 const std::string INNER_FORM_INFO_IS_THEMEFORM = "isThemeForm";
35 } // namespace
36
37 /**
38 * @brief Transform the InnerFormInfo object to json.
39 * @param jsonObject Indicates the obtained json object.
40 */
ToJson(nlohmann::json & jsonObject) const41 void InnerFormInfo::ToJson(nlohmann::json &jsonObject) const
42 {
43 jsonObject[INNER_FORM_INFO_FORM_ID] = formDBInfo_.formId;
44 jsonObject[INNER_FORM_INFO_USER_ID] = formDBInfo_.userId;
45 jsonObject[INNER_FORM_INFO_PROVIDER_USER_ID] = formDBInfo_.providerUserId;
46 jsonObject[INNER_FORM_INFO_FORM_NAME] = formDBInfo_.formName;
47 jsonObject[INNER_FORM_INFO_BUNDLE_NAME] = formDBInfo_.bundleName;
48 jsonObject[INNER_FORM_INFO_MODULE_NAME] = formDBInfo_.moduleName;
49 jsonObject[INNER_FORM_INFO_ABILITY_NAME] = formDBInfo_.abilityName;
50 jsonObject[INNER_FORM_INFO_FORM_USER_UIDS] = formDBInfo_.formUserUids;
51 jsonObject[INNER_FORM_INFO_FORM_LOCATION] = (int)formDBInfo_.formLocation;
52 jsonObject[INNER_FORM_INFO_FORM_ENABLE] = formDBInfo_.enableForm;
53 jsonObject[INNER_FORM_INFO_FORM_LOCK] = formDBInfo_.lockForm;
54 jsonObject[INNER_FORM_INFO_IS_THEMEFORM] = formDBInfo_.isThemeForm;
55 }
56
57 /**
58 * @brief Transform the json object to InnerFormInfo object.
59 * @param jsonObject Indicates the obtained json object.
60 * @return true on success or false on failure
61 */
FromJson(const nlohmann::json & jsonObject)62 bool InnerFormInfo::FromJson(const nlohmann::json &jsonObject)
63 {
64 const auto &jsonObjectEnd = jsonObject.end();
65 int32_t parseResult = ERR_OK;
66 GetValueIfFindKey<int64_t>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_FORM_ID,
67 formDBInfo_.formId, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
68
69 GetValueIfFindKey<int32_t>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_USER_ID,
70 formDBInfo_.userId, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
71
72 GetValueIfFindKey<int32_t>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_PROVIDER_USER_ID,
73 formDBInfo_.providerUserId, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
74
75 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_FORM_NAME,
76 formDBInfo_.formName, JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
77
78 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_BUNDLE_NAME,
79 formDBInfo_.bundleName, JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
80
81 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_MODULE_NAME,
82 formDBInfo_.moduleName, JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
83
84 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_ABILITY_NAME,
85 formDBInfo_.abilityName, JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
86
87 GetValueIfFindKey<std::vector<int>>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_FORM_USER_UIDS,
88 formDBInfo_.formUserUids, JsonType::ARRAY, false, parseResult, ArrayType::NUMBER);
89
90 GetValueIfFindKey<Constants::FormLocation>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_FORM_LOCATION,
91 formDBInfo_.formLocation, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
92
93 GetValueIfFindKey<bool>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_FORM_ENABLE,
94 formDBInfo_.enableForm, JsonType::BOOLEAN, false, parseResult, ArrayType::NOT_ARRAY);
95
96 GetValueIfFindKey<bool>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_FORM_LOCK,
97 formDBInfo_.lockForm, JsonType::BOOLEAN, false, parseResult, ArrayType::NOT_ARRAY);
98 GetValueIfFindKey<bool>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_IS_THEMEFORM,
99 formDBInfo_.isThemeForm, JsonType::BOOLEAN, false, parseResult, ArrayType::NOT_ARRAY);
100
101 return parseResult == ERR_OK;
102 }
103
AddUserUid(const int callingUid)104 void InnerFormInfo::AddUserUid(const int callingUid)
105 {
106 auto iter = std::find(formDBInfo_.formUserUids.begin(), formDBInfo_.formUserUids.end(), callingUid);
107 if (iter == formDBInfo_.formUserUids.end()) {
108 formDBInfo_.formUserUids.push_back(callingUid);
109 }
110 }
111
DeleteUserUid(const int callingUid)112 bool InnerFormInfo::DeleteUserUid(const int callingUid)
113 {
114 auto iter = std::find(formDBInfo_.formUserUids.begin(), formDBInfo_.formUserUids.end(), callingUid);
115 if (iter == formDBInfo_.formUserUids.end()) {
116 return false;
117 }
118 formDBInfo_.formUserUids.erase(iter);
119 return true;
120 }
121 } // namespace AppExecFwk
122 } // namespace OHOS
123