• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 "hilog_tag_wrapper.h"
19 #include "parcel_macro.h"
20 
21 namespace OHOS {
22 namespace AAFwk {
23 constexpr int32_t CYCLE_LIMIT = 1000;
24 constexpr size_t MEMBER_NUM = 11;
25 
GetURI() const26 std::string DialogAbilityInfo::GetURI() const
27 {
28     return bundleName + "/" + moduleName + "/" + abilityName + "/" +
29         std::to_string(bundleIconId) + "/" + std::to_string(bundleLabelId) + "/" +
30         std::to_string(abilityIconId) + "/" + std::to_string(abilityLabelId) + "/" +
31         std::to_string(visible) + "/" + std::to_string(appIndex) + "/" +
32         std::to_string(static_cast<int32_t>(multiAppMode.multiAppModeType)) + "/" +
33         std::to_string(multiAppMode.maxCount);
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         TAG_LOGE(AAFwkTag::DIALOG, "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     try {
52         bundleIconId = static_cast<int32_t>(std::stoi(uriVec[index++]));
53         bundleLabelId = static_cast<int32_t>(std::stoi(uriVec[index++]));
54         abilityIconId = static_cast<int32_t>(std::stoi(uriVec[index++]));
55         abilityLabelId = static_cast<int32_t>(std::stoi(uriVec[index++]));
56         visible = std::stoi(uriVec[index++]);
57         appIndex = static_cast<int32_t>(std::stoi(uriVec[index++]));
58         multiAppMode.multiAppModeType = static_cast<AppExecFwk::MultiAppModeType>(std::stoi(uriVec[index++]));
59         multiAppMode.maxCount = static_cast<int32_t>(std::stoi(uriVec[index++]));
60     } catch (...) {
61         TAG_LOGW(AAFwkTag::DIALOG, "stoi(%{public}s) failed", uriVec[index++].c_str());
62         return false;
63     }
64     return true;
65 }
66 
Split(const std::string & str,const std::string & delim,std::vector<std::string> & vec)67 void DialogAbilityInfo::Split(const std::string &str, const std::string &delim, std::vector<std::string> &vec)
68 {
69     std::string::size_type posLeft = 0;
70     std::string::size_type posRight = str.find(delim);
71     while (std::string::npos != posRight) {
72         vec.push_back(str.substr(posLeft, posRight - posLeft));
73         posLeft = posRight + delim.size();
74         posRight = str.find(delim, posLeft);
75     }
76     if (posLeft != str.size()) {
77         vec.push_back(str.substr(posLeft));
78     }
79 }
80 
ReadFromParcel(Parcel & parcel)81 bool DialogSessionInfo::ReadFromParcel(Parcel &parcel)
82 {
83     std::string callerAbilityInfoUri = Str16ToStr8(parcel.ReadString16());
84     if (!callerAbilityInfo.ParseURI(callerAbilityInfoUri)) {
85         TAG_LOGE(AAFwkTag::DIALOG, "parse callerAbilityInfo failed");
86         return false;
87     }
88     int32_t targetAbilityInfoSize = 0;
89     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, targetAbilityInfoSize);
90     CONTAINER_SECURITY_VERIFY(parcel, targetAbilityInfoSize, &targetAbilityInfos);
91     if (targetAbilityInfoSize > CYCLE_LIMIT) {
92         TAG_LOGE(AAFwkTag::DIALOG, "size too large");
93         return false;
94     }
95     for (auto i = 0; i < targetAbilityInfoSize; i++) {
96         std::string targetAbilityInfoUri = Str16ToStr8(parcel.ReadString16());
97         DialogAbilityInfo targetAbilityInfo;
98         if (!targetAbilityInfo.ParseURI(targetAbilityInfoUri)) {
99             TAG_LOGE(AAFwkTag::DIALOG, "parse targetAbilityInfo failed");
100             return false;
101         }
102         targetAbilityInfos.emplace_back(targetAbilityInfo);
103     }
104     std::unique_ptr<AAFwk::WantParams> params(parcel.ReadParcelable<AAFwk::WantParams>());
105     if (!params) {
106         APP_LOGE("ReadParcelable WantParams failed");
107         return false;
108     }
109     parameters = *params;
110     return true;
111 }
112 
Marshalling(Parcel & parcel) const113 bool DialogSessionInfo::Marshalling(Parcel &parcel) const
114 {
115     std::string callerAbilityInfoUri = callerAbilityInfo.GetURI();
116     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(callerAbilityInfoUri));
117 
118     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, targetAbilityInfos.size());
119     for (const auto &targetAbilityInfo : targetAbilityInfos) {
120         std::string targetAbilityInfoUri = targetAbilityInfo.GetURI();
121         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(targetAbilityInfoUri));
122     }
123     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &parameters);
124     return true;
125 }
126 
Unmarshalling(Parcel & parcel)127 DialogSessionInfo *DialogSessionInfo::Unmarshalling(Parcel &parcel)
128 {
129     DialogSessionInfo *info = new (std::nothrow) DialogSessionInfo();
130     if (info && !info->ReadFromParcel(parcel)) {
131         TAG_LOGE(AAFwkTag::DIALOG, "read from parcel failed");
132         delete info;
133         info = nullptr;
134     }
135     return info;
136 }
137 }  // namespace AAFwk
138 }  // namespace OHOS
139