1 /*
2 * Copyright (c) 2021 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 "element_name.h"
17
18 #include "string_ex.h"
19
20 #include "app_log_wrapper.h"
21 #include "parcel_macro.h"
22
23 namespace OHOS {
24 namespace AppExecFwk {
25
SetElementDeviceID(ElementName * element,const char * deviceId)26 void ElementName::SetElementDeviceID(ElementName *element, const char *deviceId)
27 {
28 if (element == nullptr) {
29 return;
30 }
31 element->SetDeviceID(deviceId);
32 }
33
SetElementBundleName(ElementName * element,const char * bundleName)34 void ElementName::SetElementBundleName(ElementName *element, const char *bundleName)
35 {
36 if (element == nullptr) {
37 return;
38 }
39 element->SetBundleName(bundleName);
40 }
41
SetElementAbilityName(ElementName * element,const char * abilityName)42 void ElementName::SetElementAbilityName(ElementName *element, const char *abilityName)
43 {
44 if (element == nullptr) {
45 return;
46 }
47 element->SetAbilityName(abilityName);
48 }
49
ClearElement(ElementName * element)50 void ElementName::ClearElement(ElementName *element)
51 {
52 if (element == nullptr) {
53 return;
54 }
55 element->SetDeviceID("");
56 element->SetBundleName("");
57 element->SetAbilityName("");
58 }
59
ElementName(const std::string & deviceId,const std::string & bundleName,const std::string & abilityName)60 ElementName::ElementName(const std::string &deviceId, const std::string &bundleName, const std::string &abilityName)
61 : deviceId_(deviceId), bundleName_(bundleName), abilityName_(abilityName)
62 {
63 APP_LOGD("instance is created with parameters");
64 }
65
ElementName()66 ElementName::ElementName()
67 {
68 APP_LOGD("instance is created without parameter");
69 }
70
~ElementName()71 ElementName::~ElementName()
72 {
73 APP_LOGD("instance is destroyed");
74 }
75
GetURI() const76 std::string ElementName::GetURI() const
77 {
78 return deviceId_ + "/" + bundleName_ + "/" + abilityName_;
79 }
80
operator ==(const ElementName & element) const81 bool ElementName::operator==(const ElementName &element) const
82 {
83 return (deviceId_ == element.GetDeviceID() && bundleName_ == element.GetBundleName() &&
84 abilityName_ == element.GetAbilityName());
85 }
86
ReadFromParcel(Parcel & parcel)87 bool ElementName::ReadFromParcel(Parcel &parcel)
88 {
89 bundleName_ = Str16ToStr8(parcel.ReadString16());
90 abilityName_ = Str16ToStr8(parcel.ReadString16());
91 deviceId_ = Str16ToStr8(parcel.ReadString16());
92 return true;
93 }
94
Unmarshalling(Parcel & parcel)95 ElementName *ElementName::Unmarshalling(Parcel &parcel)
96 {
97 ElementName *elementName = new (std::nothrow) ElementName();
98 if (elementName && !elementName->ReadFromParcel(parcel)) {
99 APP_LOGW("read from parcel failed");
100 delete elementName;
101 elementName = nullptr;
102 }
103 return elementName;
104 }
105
Marshalling(Parcel & parcel) const106 bool ElementName::Marshalling(Parcel &parcel) const
107 {
108 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName_));
109 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(abilityName_));
110 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(deviceId_));
111 return true;
112 }
113
114 } // namespace AppExecFwk
115 } // namespace OHOS
116