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 "bundle_user_info.h"
17
18 #include <cerrno>
19 #include <cstring>
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 #include "json_util.h"
24 #include "nlohmann/json.hpp"
25 #include "parcel_macro.h"
26 #include "string_ex.h"
27
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace {
31 const std::string BUNDLE_USER_INFO_USER_ID = "userId";
32 const std::string BUNDLE_USER_INFO_ENABLE = "enabled";
33 const std::string BUNDLE_USER_INFO_DISABLE_ABILITIES = "disabledAbilities";
34 } // namespace
35
ReadFromParcel(Parcel & parcel)36 bool BundleUserInfo::ReadFromParcel(Parcel &parcel)
37 {
38 userId = parcel.ReadInt32();
39 enabled = parcel.ReadBool();
40 int32_t disabledAbilitiesSize;
41 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, disabledAbilitiesSize);
42 CONTAINER_SECURITY_VERIFY(parcel, disabledAbilitiesSize, &disabledAbilities);
43 for (int32_t i = 0; i < disabledAbilitiesSize; i++) {
44 disabledAbilities.emplace_back(Str16ToStr8(parcel.ReadString16()));
45 }
46
47 return true;
48 }
49
Unmarshalling(Parcel & parcel)50 BundleUserInfo *BundleUserInfo::Unmarshalling(Parcel &parcel)
51 {
52 BundleUserInfo *info = new (std::nothrow) BundleUserInfo();
53 if (info && !info->ReadFromParcel(parcel)) {
54 APP_LOGW("read from parcel failed");
55 delete info;
56 info = nullptr;
57 }
58
59 return info;
60 }
61
Marshalling(Parcel & parcel) const62 bool BundleUserInfo::Marshalling(Parcel &parcel) const
63 {
64 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, userId);
65 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, enabled);
66 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, disabledAbilities.size());
67 for (auto &disabledAbility : disabledAbilities) {
68 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(disabledAbility));
69 }
70
71 return true;
72 }
73
Dump(const std::string & prefix,int fd)74 void BundleUserInfo::Dump(const std::string &prefix, int fd)
75 {
76 APP_LOGI("called dump BundleUserInfo");
77 if (fd < 0) {
78 APP_LOGE("dump BundleUserInfo fd error");
79 return;
80 }
81 int flags = fcntl(fd, F_GETFL);
82 if (flags < 0) {
83 APP_LOGE("dump BundleUserInfo fcntl error %{public}s", strerror(errno));
84 return;
85 }
86 uint uflags = static_cast<uint>(flags);
87 uflags &= O_ACCMODE;
88 if ((uflags == O_WRONLY) || (uflags == O_RDWR)) {
89 nlohmann::json jsonObject = *this;
90 std::string result;
91 result.append(prefix);
92 result.append(jsonObject.dump(Constants::DUMP_INDENT));
93 int ret = TEMP_FAILURE_RETRY(write(fd, result.c_str(), result.size()));
94 if (ret < 0) {
95 APP_LOGE("dump BundleUserInfo write error %{public}s", strerror(errno));
96 }
97 }
98 }
99
IsInitialState() const100 bool BundleUserInfo::IsInitialState() const
101 {
102 return enabled && disabledAbilities.empty();
103 }
104
Reset()105 void BundleUserInfo::Reset()
106 {
107 enabled = true;
108 disabledAbilities.clear();
109 }
110
to_json(nlohmann::json & jsonObject,const BundleUserInfo & bundleUserInfo)111 void to_json(nlohmann::json& jsonObject, const BundleUserInfo& bundleUserInfo)
112 {
113 jsonObject = nlohmann::json {
114 {BUNDLE_USER_INFO_USER_ID, bundleUserInfo.userId},
115 {BUNDLE_USER_INFO_ENABLE, bundleUserInfo.enabled},
116 {BUNDLE_USER_INFO_DISABLE_ABILITIES, bundleUserInfo.disabledAbilities},
117 };
118 }
119
from_json(const nlohmann::json & jsonObject,BundleUserInfo & bundleUserInfo)120 void from_json(const nlohmann::json& jsonObject, BundleUserInfo& bundleUserInfo)
121 {
122 const auto &jsonObjectEnd = jsonObject.end();
123 int32_t parseResult = ERR_OK;
124 GetValueIfFindKey<int32_t>(jsonObject,
125 jsonObjectEnd,
126 BUNDLE_USER_INFO_USER_ID,
127 bundleUserInfo.userId,
128 JsonType::NUMBER,
129 false,
130 parseResult,
131 ArrayType::NOT_ARRAY);
132 GetValueIfFindKey<bool>(jsonObject,
133 jsonObjectEnd,
134 BUNDLE_USER_INFO_ENABLE,
135 bundleUserInfo.enabled,
136 JsonType::BOOLEAN,
137 false,
138 parseResult,
139 ArrayType::NOT_ARRAY);
140 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
141 jsonObjectEnd,
142 BUNDLE_USER_INFO_DISABLE_ABILITIES,
143 bundleUserInfo.disabledAbilities,
144 JsonType::ARRAY,
145 false,
146 parseResult,
147 ArrayType::STRING);
148 }
149 } // namespace AppExecFwk
150 } // namespace OHOS