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_module_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_ABILITIES = "abilities";
31 }
ReadFromParcel(Parcel & parcel)32 bool DistributedModuleInfo::ReadFromParcel(Parcel &parcel)
33 {
34 moduleName = Str16ToStr8(parcel.ReadString16());
35
36 int32_t abilityInfosSize;
37 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, abilityInfosSize);
38 CONTAINER_SECURITY_VERIFY(parcel, abilityInfosSize, &abilities);
39 for (auto i = 0; i < abilityInfosSize; i++) {
40 std::unique_ptr<DistributedAbilityInfo> abilityInfo(parcel.ReadParcelable<DistributedAbilityInfo>());
41 if (!abilityInfo) {
42 APP_LOGE("ReadParcelable<AbilityInfo> failed");
43 return false;
44 }
45 abilities.emplace_back(*abilityInfo);
46 }
47 return true;
48 }
49
Marshalling(Parcel & parcel) const50 bool DistributedModuleInfo::Marshalling(Parcel &parcel) const
51 {
52 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
53 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, abilities.size());
54 for (auto &ability : abilities) {
55 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &ability);
56 }
57 return true;
58 }
59
Unmarshalling(Parcel & parcel)60 DistributedModuleInfo *DistributedModuleInfo::Unmarshalling(Parcel &parcel)
61 {
62 DistributedModuleInfo *info = new (std::nothrow) DistributedModuleInfo();
63 if (info && !info->ReadFromParcel(parcel)) {
64 APP_LOGW("read from parcel failed");
65 delete info;
66 info = nullptr;
67 }
68 return info;
69 }
70
Dump(const std::string & prefix,int fd)71 void DistributedModuleInfo::Dump(const std::string &prefix, int fd)
72 {
73 APP_LOGI("called dump DistributedModuleInfo");
74 if (fd < 0) {
75 APP_LOGE("dump DistributedModuleInfo fd error");
76 return;
77 }
78 int flags = fcntl(fd, F_GETFL);
79 if (flags < 0) {
80 APP_LOGE("fcntl error %{public}d", errno);
81 return;
82 }
83 uint uflags = static_cast<uint>(flags);
84 uflags &= O_ACCMODE;
85 if ((uflags == O_WRONLY) || (uflags == O_RDWR)) {
86 nlohmann::json jsonObject = *this;
87 std::string result;
88 result.append(prefix);
89 result.append(jsonObject.dump(Constants::DUMP_INDENT));
90 int ret = TEMP_FAILURE_RETRY(write(fd, result.c_str(), result.size()));
91 if (ret < 0) {
92 APP_LOGE("write error %{public}d", errno);
93 }
94 }
95 }
96
to_json(nlohmann::json & jsonObject,const DistributedModuleInfo & distributedModuleInfo)97 void to_json(nlohmann::json& jsonObject, const DistributedModuleInfo& distributedModuleInfo)
98 {
99 jsonObject = nlohmann::json {
100 {Constants::MODULE_NAME, distributedModuleInfo.moduleName},
101 {JSON_KEY_ABILITIES, distributedModuleInfo.abilities},
102 };
103 }
104
from_json(const nlohmann::json & jsonObject,DistributedModuleInfo & distributedModuleInfo)105 void from_json(const nlohmann::json& jsonObject, DistributedModuleInfo& distributedModuleInfo)
106 {
107 const auto &jsonObjectEnd = jsonObject.end();
108 int32_t parseResult = ERR_OK;
109 GetValueIfFindKey<std::string>(jsonObject,
110 jsonObjectEnd,
111 Constants::MODULE_NAME,
112 distributedModuleInfo.moduleName,
113 JsonType::STRING,
114 false,
115 parseResult,
116 ArrayType::NOT_ARRAY);
117 GetValueIfFindKey<std::vector<DistributedAbilityInfo>>(jsonObject,
118 jsonObjectEnd,
119 JSON_KEY_ABILITIES,
120 distributedModuleInfo.abilities,
121 JsonType::ARRAY,
122 false,
123 parseResult,
124 ArrayType::OBJECT);
125 if (parseResult != ERR_OK) {
126 APP_LOGE("read distributedModuleInfo jsonObject error : %{public}d", parseResult);
127 }
128 }
129 } // namespace AppExecFwk
130 } // namespace OHOS
131