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