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