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