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