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