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 "distributed_ability_info.h"
17
18 #include <fcntl.h>
19 #include <unistd.h>
20
21 #include "app_log_wrapper.h"
22 #include "json_util.h"
23 #include "nlohmann/json.hpp"
24 #include "parcel_macro.h"
25 #include "string_ex.h"
26
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace {
30 const char* JSON_KEY_PERMISSIONS = "permissions";
31 const char* JSON_KEY_TYPE = "type";
32 const char* JSON_KEY_ENABLED = "enabled";
33 }
ReadFromParcel(Parcel & parcel)34 bool DistributedAbilityInfo::ReadFromParcel(Parcel &parcel)
35 {
36 abilityName = Str16ToStr8(parcel.ReadString16());
37
38 int32_t permissionsSize;
39 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, permissionsSize);
40 CONTAINER_SECURITY_VERIFY(parcel, permissionsSize, &permissions);
41 for (auto i = 0; i < permissionsSize; i++) {
42 permissions.emplace_back(Str16ToStr8(parcel.ReadString16()));
43 }
44
45 type = static_cast<AbilityType>(parcel.ReadInt32());
46 enabled = parcel.ReadBool();
47
48 return true;
49 }
50
Marshalling(Parcel & parcel) const51 bool DistributedAbilityInfo::Marshalling(Parcel &parcel) const
52 {
53 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(abilityName));
54 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, permissions.size());
55 for (auto &permission : permissions) {
56 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(permission));
57 }
58 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(type));
59 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, enabled);
60 return true;
61 }
62
Unmarshalling(Parcel & parcel)63 DistributedAbilityInfo *DistributedAbilityInfo::Unmarshalling(Parcel &parcel)
64 {
65 DistributedAbilityInfo *info = new (std::nothrow) DistributedAbilityInfo();
66 if (info && !info->ReadFromParcel(parcel)) {
67 APP_LOGW("read from parcel failed");
68 delete info;
69 info = nullptr;
70 }
71 return info;
72 }
73
Dump(const std::string & prefix,int fd)74 void DistributedAbilityInfo::Dump(const std::string &prefix, int fd)
75 {
76 APP_LOGI("called dump DistributedAbilityInfo");
77 if (fd < 0) {
78 APP_LOGE("dump DistributedAbilityInfo fd error");
79 return;
80 }
81 int flags = fcntl(fd, F_GETFL);
82 if (flags < 0) {
83 APP_LOGE("dump DistributedAbilityInfo fcntl error %{public}d", 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 DistributedAbilityInfo write error %{public}d", errno);
96 }
97 }
98 }
99
to_json(nlohmann::json & jsonObject,const DistributedAbilityInfo & distributedAbilityInfo)100 void to_json(nlohmann::json& jsonObject, const DistributedAbilityInfo& distributedAbilityInfo)
101 {
102 jsonObject = nlohmann::json {
103 {Constants::ABILITY_NAME, distributedAbilityInfo.abilityName},
104 {JSON_KEY_PERMISSIONS, distributedAbilityInfo.permissions},
105 {JSON_KEY_TYPE, distributedAbilityInfo.type},
106 {JSON_KEY_ENABLED, distributedAbilityInfo.enabled},
107 };
108 }
109
from_json(const nlohmann::json & jsonObject,DistributedAbilityInfo & distributedAbilityInfo)110 void from_json(const nlohmann::json& jsonObject, DistributedAbilityInfo& distributedAbilityInfo)
111 {
112 const auto &jsonObjectEnd = jsonObject.end();
113 int32_t parseResult = ERR_OK;
114 GetValueIfFindKey<std::string>(jsonObject,
115 jsonObjectEnd,
116 Constants::ABILITY_NAME,
117 distributedAbilityInfo.abilityName,
118 JsonType::STRING,
119 false,
120 parseResult,
121 ArrayType::NOT_ARRAY);
122 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
123 jsonObjectEnd,
124 JSON_KEY_PERMISSIONS,
125 distributedAbilityInfo.permissions,
126 JsonType::ARRAY,
127 false,
128 parseResult,
129 ArrayType::STRING);
130 GetValueIfFindKey<AbilityType>(jsonObject,
131 jsonObjectEnd,
132 JSON_KEY_TYPE,
133 distributedAbilityInfo.type,
134 JsonType::NUMBER,
135 false,
136 parseResult,
137 ArrayType::NOT_ARRAY);
138 GetValueIfFindKey<bool>(jsonObject,
139 jsonObjectEnd,
140 JSON_KEY_ENABLED,
141 distributedAbilityInfo.enabled,
142 JsonType::BOOLEAN,
143 false,
144 parseResult,
145 ArrayType::NOT_ARRAY);
146 if (parseResult != ERR_OK) {
147 APP_LOGE("read distributedAbilityInfo jsonObject error : %{public}d", parseResult);
148 }
149 }
150 } // namespace AppExecFwk
151 } // namespace OHOS
152