1 /*
2  * Copyright (c) 2024 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 #include "inner_bundle_clone_info.h"
16 
17 namespace OHOS {
18 namespace AppExecFwk {
19 namespace {
20 const std::string BUNDLE_CLONE_INFO_USER_ID = "userId";
21 const std::string BUNDLE_CLONE_INFO_APP_INDEX = "appIndex";
22 const std::string BUNDLE_CLONE_INFO_UID = "uid";
23 const std::string BUNDLE_CLONE_INFO_GIDS = "gids";
24 const std::string BUNDLE_CLONE_INFO_ENABLE = "enabled";
25 const std::string BUNDLE_CLONE_INFO_DISABLE_ABILITIES = "disabledAbilities";
26 
27 const std::string BUNDLE_CLONE_INFO_ACCESS_TOKEN_ID = "accessTokenId";
28 const std::string BUNDLE_CLONE_INFO_ACCESS_TOKEN_ID_EX = "accessTokenIdEx";
29 const std::string BUNDLE_CLONE_INFO_INSTALL_TIME = "installTime";
30 const std::string BUNDLE_CLONE_INFO_INFO_SET_ENABLED_CALLER = "setEnabledCaller";
31 } // namespace
32 
to_json(nlohmann::json & jsonObject,const InnerBundleCloneInfo & bundleCloneInfo)33 void to_json(nlohmann::json& jsonObject, const InnerBundleCloneInfo& bundleCloneInfo)
34 {
35     jsonObject = nlohmann::json {
36         {BUNDLE_CLONE_INFO_USER_ID, bundleCloneInfo.userId},
37         {BUNDLE_CLONE_INFO_APP_INDEX, bundleCloneInfo.appIndex},
38         {BUNDLE_CLONE_INFO_UID, bundleCloneInfo.uid},
39         {BUNDLE_CLONE_INFO_GIDS, bundleCloneInfo.gids},
40         {BUNDLE_CLONE_INFO_ENABLE, bundleCloneInfo.enabled},
41         {BUNDLE_CLONE_INFO_DISABLE_ABILITIES, bundleCloneInfo.disabledAbilities},
42         {BUNDLE_CLONE_INFO_ACCESS_TOKEN_ID, bundleCloneInfo.accessTokenId},
43         {BUNDLE_CLONE_INFO_ACCESS_TOKEN_ID_EX, bundleCloneInfo.accessTokenIdEx},
44         {BUNDLE_CLONE_INFO_INSTALL_TIME, bundleCloneInfo.installTime},
45         {BUNDLE_CLONE_INFO_INFO_SET_ENABLED_CALLER, bundleCloneInfo.setEnabledCaller},
46     };
47 }
48 
from_json(const nlohmann::json & jsonObject,InnerBundleCloneInfo & bundleCloneInfo)49 void from_json(const nlohmann::json& jsonObject, InnerBundleCloneInfo& bundleCloneInfo)
50 {
51     const auto &jsonObjectEnd = jsonObject.end();
52     int32_t parseResult = ERR_OK;
53     GetValueIfFindKey<int32_t>(jsonObject, jsonObjectEnd, BUNDLE_CLONE_INFO_USER_ID,
54         bundleCloneInfo.userId, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
55     GetValueIfFindKey<int32_t>(jsonObject, jsonObjectEnd, BUNDLE_CLONE_INFO_APP_INDEX,
56         bundleCloneInfo.appIndex, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
57     GetValueIfFindKey<int32_t>(jsonObject, jsonObjectEnd, BUNDLE_CLONE_INFO_UID,
58         bundleCloneInfo.uid, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
59     GetValueIfFindKey<std::vector<int32_t>>(jsonObject, jsonObjectEnd, BUNDLE_CLONE_INFO_GIDS,
60         bundleCloneInfo.gids, JsonType::ARRAY, false, parseResult, ArrayType::NUMBER);
61     GetValueIfFindKey<bool>(jsonObject, jsonObjectEnd, BUNDLE_CLONE_INFO_ENABLE,
62         bundleCloneInfo.enabled, JsonType::BOOLEAN, false, parseResult, ArrayType::NOT_ARRAY);
63     GetValueIfFindKey<std::vector<std::string>>(jsonObject, jsonObjectEnd, BUNDLE_CLONE_INFO_DISABLE_ABILITIES,
64         bundleCloneInfo.disabledAbilities, JsonType::ARRAY, false, parseResult, ArrayType::STRING);
65     GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, BUNDLE_CLONE_INFO_ACCESS_TOKEN_ID,
66         bundleCloneInfo.accessTokenId, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
67     GetValueIfFindKey<uint64_t>(jsonObject, jsonObjectEnd, BUNDLE_CLONE_INFO_ACCESS_TOKEN_ID_EX,
68         bundleCloneInfo.accessTokenIdEx, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
69     GetValueIfFindKey<int64_t>(jsonObject, jsonObjectEnd, BUNDLE_CLONE_INFO_INSTALL_TIME,
70         bundleCloneInfo.installTime, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
71     GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, BUNDLE_CLONE_INFO_INFO_SET_ENABLED_CALLER,
72         bundleCloneInfo.setEnabledCaller, JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
73     if (parseResult != ERR_OK) {
74         APP_LOGE("read module bundleCloneInfo from jsonObject error, error code : %{public}d", parseResult);
75     }
76 }
77 } // namespace AppExecFwk
78 } // namespace OHOS