1 /*
2  * Copyright (c) 2021-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 <algorithm>
17 
18 #include "element_name.h"
19 
20 #include "string_ex.h"
21 
22 #include "ability_base_log_wrapper.h"
23 #include "parcel_macro_base.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
SetElementDeviceID(ElementName * element,const char * deviceId)27 void ElementName::SetElementDeviceID(ElementName *element, const char *deviceId)
28 {
29     if (element == nullptr) {
30         return;
31     }
32     element->SetDeviceID(deviceId);
33 }
34 
SetElementBundleName(ElementName * element,const char * bundleName)35 void ElementName::SetElementBundleName(ElementName *element, const char *bundleName)
36 {
37     if (element == nullptr) {
38         return;
39     }
40     element->SetBundleName(bundleName);
41 }
42 
SetElementAbilityName(ElementName * element,const char * abilityName)43 void ElementName::SetElementAbilityName(ElementName *element, const char *abilityName)
44 {
45     if (element == nullptr) {
46         return;
47     }
48     element->SetAbilityName(abilityName);
49 }
50 
SetElementModuleName(ElementName * element,const char * moduleName)51 void ElementName::SetElementModuleName(ElementName *element, const char *moduleName)
52 {
53     if (element == nullptr) {
54         return;
55     }
56     element->SetModuleName(moduleName);
57 }
58 
ClearElement(ElementName * element)59 void ElementName::ClearElement(ElementName *element)
60 {
61     if (element == nullptr) {
62         return;
63     }
64     element->SetDeviceID("");
65     element->SetBundleName("");
66     element->SetAbilityName("");
67     element->SetModuleName("");
68 }
69 
ElementName(const std::string & deviceId,const std::string & bundleName,const std::string & abilityName,const std::string & moduleName)70 ElementName::ElementName(const std::string &deviceId, const std::string &bundleName,
71     const std::string &abilityName, const std::string &moduleName)
72     : deviceId_(deviceId), bundleName_(bundleName), abilityName_(abilityName), moduleName_(moduleName)
73 {
74 }
75 
ElementName()76 ElementName::ElementName()
77 {
78 }
79 
~ElementName()80 ElementName::~ElementName()
81 {
82 }
83 
GetURI() const84 std::string ElementName::GetURI() const
85 {
86     return deviceId_ + "/" + bundleName_ + "/" + moduleName_ + "/" + abilityName_;
87 }
88 
ParseURI(const std::string & uri)89 bool ElementName::ParseURI(const std::string &uri)
90 {
91     const size_t memberNum = 4;
92     if (std::count(uri.begin(), uri.end(), '/') != memberNum - 1) {
93         ABILITYBASE_LOGE("Invalid uri: %{public}s.", uri.c_str());
94         return false;
95     }
96 
97     std::vector<std::string> uriVec;
98     Split(uri, "/", uriVec);
99     uriVec.resize(memberNum);
100 
101     int index = 0;
102     deviceId_ = uriVec[index++];
103     bundleName_ = uriVec[index++];
104     moduleName_ = uriVec[index++];
105     abilityName_ = uriVec[index++];
106     return true;
107 }
108 
Split(const std::string & str,const std::string & delim,std::vector<std::string> & vec)109 void ElementName::Split(const std::string &str, const std::string &delim, std::vector<std::string> &vec)
110 {
111     std::string::size_type pos1 = 0;
112     std::string::size_type pos2 = str.find(delim);
113     while (std::string::npos != pos2) {
114         vec.push_back(str.substr(pos1, pos2 - pos1));
115         pos1 = pos2 + delim.size();
116         pos2 = str.find(delim, pos1);
117     }
118     if (pos1 != str.size()) {
119         vec.push_back(str.substr(pos1));
120     }
121 }
122 
operator ==(const ElementName & element) const123 bool ElementName::operator==(const ElementName &element) const
124 {
125     return (deviceId_ == element.GetDeviceID() && bundleName_ == element.GetBundleName() &&
126         abilityName_ == element.GetAbilityName() && moduleName_ == element.GetModuleName());
127 }
128 
ReadFromParcel(Parcel & parcel)129 bool ElementName::ReadFromParcel(Parcel &parcel)
130 {
131     bundleName_ = Str16ToStr8(parcel.ReadString16());
132     abilityName_ = Str16ToStr8(parcel.ReadString16());
133     deviceId_ = Str16ToStr8(parcel.ReadString16());
134     return true;
135 }
136 
Unmarshalling(Parcel & parcel)137 ElementName *ElementName::Unmarshalling(Parcel &parcel)
138 {
139     ElementName *elementName = new (std::nothrow) ElementName();
140     if (elementName && !elementName->ReadFromParcel(parcel)) {
141         ABILITYBASE_LOGW("read from parcel failed");
142         delete elementName;
143         elementName = nullptr;
144     }
145     return elementName;
146 }
147 
Marshalling(Parcel & parcel) const148 bool ElementName::Marshalling(Parcel &parcel) const
149 {
150     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName_));
151     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(abilityName_));
152     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(deviceId_));
153     return true;
154 }
155 }  // namespace AppExecFwk
156 }  // namespace OHOS
157