1 /*
2 * Copyright (c) 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 "inner_bundle_user_info.h"
17
18 namespace OHOS {
19 namespace AppExecFwk {
20 namespace {
21 constexpr const char* INNER_BUNDLE_USER_INFO_UID = "uid";
22 constexpr const char* INNER_BUNDLE_USER_INFO_GIDS = "gids";
23 constexpr const char* INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID = "accessTokenId";
24 constexpr const char* INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID_EX = "accessTokenIdEx";
25 constexpr const char* INNER_BUNDLE_USER_INFO_BUNDLE_NAME = "bundleName";
26 constexpr const char* INNER_BUNDLE_USER_INFO_INSTALL_TIME = "installTime";
27 constexpr const char* INNER_BUNDLE_USER_INFO_UPDATE_TIME = "updateTime";
28 constexpr const char* INNER_BUNDLE_USER_INFO_FIRST_INSTALL_TIME = "firstInstallTime";
29 constexpr const char* INNER_BUNDLE_USER_INFO_BUNDLE_USER_INFO = "bundleUserInfo";
30 constexpr const char* INNER_BUNDLE_USER_INFO_IS_REMOVABLE = "isRemovable";
31 constexpr const char* INNER_BUNDLE_USER_INFO_CLONE_INFOS = "cloneInfos";
32 constexpr const char* INNER_BUNDLE_USER_INFO_KEYID = "keyId";
33 constexpr const char* INNER_BUNDLE_USER_INFO_PLUGIN_BUNDLE_INFOS = "pluginBundleInfos";
34 } // namespace
35
to_json(nlohmann::json & jsonObject,const InnerBundleUserInfo & innerBundleUserInfo)36 void to_json(nlohmann::json& jsonObject, const InnerBundleUserInfo& innerBundleUserInfo)
37 {
38 jsonObject = nlohmann::json {
39 {INNER_BUNDLE_USER_INFO_UID, innerBundleUserInfo.uid},
40 {INNER_BUNDLE_USER_INFO_GIDS, innerBundleUserInfo.gids},
41 {INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID, innerBundleUserInfo.accessTokenId},
42 {INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID_EX, innerBundleUserInfo.accessTokenIdEx},
43 {INNER_BUNDLE_USER_INFO_BUNDLE_NAME, innerBundleUserInfo.bundleName},
44 {INNER_BUNDLE_USER_INFO_INSTALL_TIME, innerBundleUserInfo.installTime},
45 {INNER_BUNDLE_USER_INFO_UPDATE_TIME, innerBundleUserInfo.updateTime},
46 {INNER_BUNDLE_USER_INFO_FIRST_INSTALL_TIME, innerBundleUserInfo.firstInstallTime},
47 {INNER_BUNDLE_USER_INFO_BUNDLE_USER_INFO, innerBundleUserInfo.bundleUserInfo},
48 {INNER_BUNDLE_USER_INFO_IS_REMOVABLE, innerBundleUserInfo.isRemovable},
49 {INNER_BUNDLE_USER_INFO_CLONE_INFOS, innerBundleUserInfo.cloneInfos},
50 {INNER_BUNDLE_USER_INFO_KEYID, innerBundleUserInfo.keyId},
51 {INNER_BUNDLE_USER_INFO_PLUGIN_BUNDLE_INFOS, innerBundleUserInfo.pluginBundleInfos},
52 };
53 }
54
from_json(const nlohmann::json & jsonObject,InnerBundleUserInfo & innerBundleUserInfo)55 void from_json(const nlohmann::json& jsonObject, InnerBundleUserInfo& innerBundleUserInfo)
56 {
57 const auto &jsonObjectEnd = jsonObject.end();
58 int32_t parseResult = ERR_OK;
59 GetValueIfFindKey<int32_t>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_UID,
60 innerBundleUserInfo.uid, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
61 GetValueIfFindKey<std::vector<int32_t>>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_GIDS,
62 innerBundleUserInfo.gids, JsonType::ARRAY, false, parseResult, ArrayType::NUMBER);
63 GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID,
64 innerBundleUserInfo.accessTokenId, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
65 GetValueIfFindKey<uint64_t>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_ACCESS_TOKEN_ID_EX,
66 innerBundleUserInfo.accessTokenIdEx, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
67 BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_BUNDLE_NAME,
68 innerBundleUserInfo.bundleName, false, parseResult);
69 GetValueIfFindKey<int64_t>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_INSTALL_TIME,
70 innerBundleUserInfo.installTime, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
71 GetValueIfFindKey<int64_t>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_UPDATE_TIME,
72 innerBundleUserInfo.updateTime, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
73 GetValueIfFindKey<int64_t>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_FIRST_INSTALL_TIME,
74 innerBundleUserInfo.firstInstallTime, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
75 GetValueIfFindKey<BundleUserInfo>(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_BUNDLE_USER_INFO,
76 innerBundleUserInfo.bundleUserInfo, JsonType::OBJECT, false, parseResult, ArrayType::NOT_ARRAY);
77 BMSJsonUtil::GetBoolValueIfFindKey(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_IS_REMOVABLE,
78 innerBundleUserInfo.isRemovable, false, parseResult);
79 GetValueIfFindKey<std::map<std::string, InnerBundleCloneInfo>>(jsonObject, jsonObjectEnd,
80 INNER_BUNDLE_USER_INFO_CLONE_INFOS,
81 innerBundleUserInfo.cloneInfos, JsonType::OBJECT, false, parseResult, ArrayType::NOT_ARRAY);
82 BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd, INNER_BUNDLE_USER_INFO_KEYID,
83 innerBundleUserInfo.keyId, false, parseResult);
84 GetValueIfFindKey<std::unordered_map<std::string, PluginBundleInfo>>(jsonObject, jsonObjectEnd,
85 INNER_BUNDLE_USER_INFO_PLUGIN_BUNDLE_INFOS,
86 innerBundleUserInfo.pluginBundleInfos, JsonType::OBJECT, false, parseResult, ArrayType::NOT_ARRAY);
87 }
88 } // namespace AppExecFwk
89 } // namespace OHOS