• 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 "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 std::string JSON_KEY_ABILITIES = "abilities";
31 }
ReadFromParcel(Parcel & parcel)32 bool DistributedModuleInfo::ReadFromParcel(Parcel &parcel)
33 {
34     MessageParcel *messageParcel = reinterpret_cast<MessageParcel *>(&parcel);
35     if (!messageParcel) {
36         APP_LOGE("Type conversion failed");
37         return false;
38     }
39     uint32_t length = messageParcel->ReadUint32();
40     if (length == 0) {
41         APP_LOGE("Invalid data length");
42         return false;
43     }
44     const char *data = reinterpret_cast<const char *>(messageParcel->ReadRawData(length));
45     if (!data) {
46         APP_LOGE("Fail to read raw data, length = %{public}d", length);
47         return false;
48     }
49     nlohmann::json jsonObject = nlohmann::json::parse(data, nullptr, false);
50     if (jsonObject.is_discarded()) {
51         APP_LOGE("failed to parse DistributedModuleInfo");
52         return false;
53     }
54     *this = jsonObject.get<DistributedModuleInfo>();
55     return true;
56 }
57 
Marshalling(Parcel & parcel) const58 bool DistributedModuleInfo::Marshalling(Parcel &parcel) const
59 {
60     MessageParcel *messageParcel = reinterpret_cast<MessageParcel *>(&parcel);
61     if (!messageParcel) {
62         APP_LOGE("Type conversion failed");
63         return false;
64     }
65     nlohmann::json json = *this;
66     std::string str = json.dump();
67     if (!messageParcel->WriteUint32(str.size() + 1)) {
68         APP_LOGE("Failed to write data size");
69         return false;
70     }
71     if (!messageParcel->WriteRawData(str.c_str(), str.size() + 1)) {
72         APP_LOGE("Failed to write data");
73         return false;
74     }
75     return true;
76 }
77 
Unmarshalling(Parcel & parcel)78 DistributedModuleInfo *DistributedModuleInfo::Unmarshalling(Parcel &parcel)
79 {
80     DistributedModuleInfo *info = new (std::nothrow) DistributedModuleInfo();
81     if (info && !info->ReadFromParcel(parcel)) {
82         APP_LOGW("read from parcel failed");
83         delete info;
84         info = nullptr;
85     }
86     return info;
87 }
88 
Dump(const std::string & prefix,int fd)89 void DistributedModuleInfo::Dump(const std::string &prefix, int fd)
90 {
91     APP_LOGI("called dump DistributedModuleInfo");
92     if (fd < 0) {
93         APP_LOGE("dump DistributedModuleInfo fd error");
94         return;
95     }
96     int flags = fcntl(fd, F_GETFL);
97     if (flags < 0) {
98         APP_LOGE("dump DistributedModuleInfo fcntl error %{public}d", errno);
99         return;
100     }
101     uint uflags = static_cast<uint>(flags);
102     uflags &= O_ACCMODE;
103     if ((uflags == O_WRONLY) || (uflags == O_RDWR)) {
104         nlohmann::json jsonObject = *this;
105         std::string result;
106         result.append(prefix);
107         result.append(jsonObject.dump(Constants::DUMP_INDENT));
108         int ret = TEMP_FAILURE_RETRY(write(fd, result.c_str(), result.size()));
109         if (ret < 0) {
110             APP_LOGE("dump DistributedModuleInfo write error %{public}d", errno);
111         }
112     }
113 }
114 
to_json(nlohmann::json & jsonObject,const DistributedModuleInfo & distributedModuleInfo)115 void to_json(nlohmann::json& jsonObject, const DistributedModuleInfo& distributedModuleInfo)
116 {
117     jsonObject = nlohmann::json {
118         {Constants::MODULE_NAME, distributedModuleInfo.moduleName},
119         {JSON_KEY_ABILITIES, distributedModuleInfo.abilities},
120     };
121 }
122 
from_json(const nlohmann::json & jsonObject,DistributedModuleInfo & distributedModuleInfo)123 void from_json(const nlohmann::json& jsonObject, DistributedModuleInfo& distributedModuleInfo)
124 {
125     const auto &jsonObjectEnd = jsonObject.end();
126     int32_t parseResult = ERR_OK;
127     GetValueIfFindKey<std::string>(jsonObject,
128         jsonObjectEnd,
129         Constants::MODULE_NAME,
130         distributedModuleInfo.moduleName,
131         JsonType::STRING,
132         false,
133         parseResult,
134         ArrayType::NOT_ARRAY);
135     GetValueIfFindKey<std::vector<DistributedAbilityInfo>>(jsonObject,
136         jsonObjectEnd,
137         JSON_KEY_ABILITIES,
138         distributedModuleInfo.abilities,
139         JsonType::ARRAY,
140         false,
141         parseResult,
142         ArrayType::OBJECT);
143 }
144 }  // namespace AppExecFwk
145 }  // namespace OHOS
146