• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "dialog_session_info.h"
17 
18 #include <string>
19 
20 #include "hilog_wrapper.h"
21 #include "parcel_macro.h"
22 #include "string_ex.h"
23 
24 namespace OHOS {
25 namespace AAFwk {
26 constexpr int32_t CYCLE_LIMIT = 1000;
27 constexpr size_t MEMBER_NUM = 7;
28 
GetURI() const29 std::string DialogAbilityInfo::GetURI() const
30 {
31     return bundleName + "/" + moduleName + "/" + abilityName + "/" +
32         std::to_string(bundleIconId) + "/" + std::to_string(bundleLabelId) + "/" +
33         std::to_string(abilityIconId) + "/" + std::to_string(abilityLabelId);
34 }
35 
ParseURI(const std::string & uri)36 bool DialogAbilityInfo::ParseURI(const std::string &uri)
37 {
38     if (std::count(uri.begin(), uri.end(), '/') != MEMBER_NUM - 1) {
39         HILOG_ERROR("Invalid uri: %{public}s.", uri.c_str());
40         return false;
41     }
42 
43     std::vector<std::string> uriVec;
44     Split(uri, "/", uriVec);
45     uriVec.resize(MEMBER_NUM);
46 
47     int index = 0;
48     bundleName = uriVec[index++];
49     moduleName = uriVec[index++];
50     abilityName = uriVec[index++];
51     bundleIconId = static_cast<int32_t>(std::stoi(uriVec[index++]));
52     bundleLabelId = static_cast<int32_t>(std::stoi(uriVec[index++]));
53     abilityIconId = static_cast<int32_t>(std::stoi(uriVec[index++]));
54     abilityLabelId = static_cast<int32_t>(std::stoi(uriVec[index++]));
55     return true;
56 }
57 
Split(const std::string & str,const std::string & delim,std::vector<std::string> & vec)58 void DialogAbilityInfo::Split(const std::string &str, const std::string &delim, std::vector<std::string> &vec)
59 {
60     std::string::size_type posLeft = 0;
61     std::string::size_type posRight = str.find(delim);
62     while (std::string::npos != posRight) {
63         vec.push_back(str.substr(posLeft, posRight - posLeft));
64         posLeft = posRight + delim.size();
65         posRight = str.find(delim, posLeft);
66     }
67     if (posLeft != str.size()) {
68         vec.push_back(str.substr(posLeft));
69     }
70 }
71 
ReadFromParcel(Parcel & parcel)72 bool DialogSessionInfo::ReadFromParcel(Parcel &parcel)
73 {
74     std::string callerAbilityInfoUri = Str16ToStr8(parcel.ReadString16());
75     if (!callerAbilityInfo.ParseURI(callerAbilityInfoUri)) {
76         HILOG_ERROR("parse callerAbilityInfo failed");
77         return false;
78     }
79     int32_t targetAbilityInfoSize = 0;
80     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, targetAbilityInfoSize);
81     CONTAINER_SECURITY_VERIFY(parcel, targetAbilityInfoSize, &targetAbilityInfos);
82     if (targetAbilityInfoSize > CYCLE_LIMIT) {
83         HILOG_ERROR("size is too large.");
84         return false;
85     }
86     for (auto i = 0; i < targetAbilityInfoSize; i++) {
87         std::string targetAbilityInfoUri = Str16ToStr8(parcel.ReadString16());
88         DialogAbilityInfo targetAbilityInfo;
89         if (!targetAbilityInfo.ParseURI(targetAbilityInfoUri)) {
90             HILOG_ERROR("parse targetAbilityInfo failed");
91             return false;
92         }
93         targetAbilityInfos.emplace_back(targetAbilityInfo);
94     }
95     std::unique_ptr<AAFwk::WantParams> params(parcel.ReadParcelable<AAFwk::WantParams>());
96     if (!params) {
97         APP_LOGE("ReadParcelable WantParams failed");
98         return false;
99     }
100     parameters = *params;
101     return true;
102 }
103 
Marshalling(Parcel & parcel) const104 bool DialogSessionInfo::Marshalling(Parcel &parcel) const
105 {
106     std::string callerAbilityInfoUri = callerAbilityInfo.GetURI();
107     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(callerAbilityInfoUri));
108 
109     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, targetAbilityInfos.size());
110     for (const auto &targetAbilityInfo : targetAbilityInfos) {
111         std::string targetAbilityInfoUri = targetAbilityInfo.GetURI();
112         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(targetAbilityInfoUri));
113     }
114     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &parameters);
115     return true;
116 }
117 
Unmarshalling(Parcel & parcel)118 DialogSessionInfo *DialogSessionInfo::Unmarshalling(Parcel &parcel)
119 {
120     DialogSessionInfo *info = new (std::nothrow) DialogSessionInfo();
121     if (info && !info->ReadFromParcel(parcel)) {
122         HILOG_ERROR("read from parcel failed");
123         delete info;
124         info = nullptr;
125     }
126     return info;
127 }
128 }  // namespace AAFwk
129 }  // namespace OHOS
130