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