• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 } // 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, jsonObjectEnd, INNER_FORM_INFO_FORM_ID,
59         formDBInfo_.formId, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
60 
61     GetValueIfFindKey<int32_t>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_USER_ID,
62         formDBInfo_.userId, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
63 
64     GetValueIfFindKey<int32_t>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_PROVIDER_USER_ID,
65         formDBInfo_.providerUserId, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
66 
67     GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_FORM_NAME,
68         formDBInfo_.formName, JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
69 
70     GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_BUNDLE_NAME,
71         formDBInfo_.bundleName, JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
72 
73     GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_MODULE_NAME,
74         formDBInfo_.moduleName, JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
75 
76     GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_ABILITY_NAME,
77         formDBInfo_.abilityName, JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
78 
79     GetValueIfFindKey<std::vector<int>>(jsonObject, jsonObjectEnd, INNER_FORM_INFO_FORM_USER_UIDS,
80         formDBInfo_.formUserUids, JsonType::ARRAY, false, parseResult, ArrayType::NUMBER);
81 
82     return parseResult == ERR_OK;
83 }
84 
AddUserUid(const int callingUid)85 void InnerFormInfo::AddUserUid(const int callingUid)
86 {
87     auto iter = std::find(formDBInfo_.formUserUids.begin(), formDBInfo_.formUserUids.end(), callingUid);
88     if (iter == formDBInfo_.formUserUids.end()) {
89         formDBInfo_.formUserUids.push_back(callingUid);
90     }
91 }
92 
DeleteUserUid(const int callingUid)93 bool InnerFormInfo::DeleteUserUid(const int callingUid)
94 {
95     auto iter = std::find(formDBInfo_.formUserUids.begin(), formDBInfo_.formUserUids.end(), callingUid);
96     if (iter == formDBInfo_.formUserUids.end()) {
97         return false;
98     }
99     formDBInfo_.formUserUids.erase(iter);
100     return true;
101 }
102 }  // namespace AppExecFwk
103 }  // namespace OHOS
104