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