1 /*
2  * Copyright (c) 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 "form_instance.h"
17 #include "accesstoken_log.h"
18 #include "string_ex.h"
19 
20 namespace OHOS {
21 namespace Security {
22 namespace AccessToken {
23 namespace {
24 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
25     LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "FormInstance"
26 };
27 }
ReadFromParcel(Parcel & parcel)28 bool FormInstance::ReadFromParcel(Parcel &parcel)
29 {
30     if (!parcel.ReadInt64(formId_)) {
31         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadInt64 failed.");
32         return false;
33     }
34 
35     std::u16string u16FormHostName;
36     if (!parcel.ReadString16(u16FormHostName)) {
37         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadString16 failed.");
38         return false;
39     }
40     formHostName_ = Str16ToStr8(u16FormHostName);
41 
42     int32_t formVisiblity;
43     if (!parcel.ReadInt32(formVisiblity)) {
44         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadInt32 failed.");
45         return false;
46     }
47     formVisiblity_ = static_cast<FormVisibilityType>(formVisiblity);
48     if (!parcel.ReadInt32(specification_)) {
49         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadInt32 failed.");
50         return false;
51     }
52 
53     std::u16string u16BundleName;
54     std::u16string u16ModuleName;
55     std::u16string u16AbilityName;
56     std::u16string u16FormName;
57     if (!parcel.ReadString16(u16BundleName) || (!parcel.ReadString16(u16ModuleName)) ||
58         (!parcel.ReadString16(u16AbilityName)) || (!parcel.ReadString16(u16FormName))) {
59         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadString16 failed.");
60         return false;
61     }
62     bundleName_ = Str16ToStr8(u16BundleName);
63     moduleName_ = Str16ToStr8(u16ModuleName);
64     abilityName_ = Str16ToStr8(u16AbilityName);
65     formName_ = Str16ToStr8(u16FormName);
66 
67     int32_t formUsageState;
68     if (!parcel.ReadInt32(formUsageState)) {
69         ACCESSTOKEN_LOG_ERROR(LABEL, "ReadInt32 failed.");
70         return false;
71     }
72     formUsageState_ = static_cast<FormUsageState>(formUsageState);
73     return true;
74 }
75 
Marshalling(Parcel & parcel) const76 bool FormInstance::Marshalling(Parcel &parcel) const
77 {
78     if (!parcel.WriteInt64(formId_)) {
79         ACCESSTOKEN_LOG_ERROR(LABEL, "WriteInt64 failed.");
80         return false;
81     }
82 
83     if (!parcel.WriteString16(Str8ToStr16(formHostName_))) {
84         ACCESSTOKEN_LOG_ERROR(LABEL, "WriteString16 failed.");
85         return false;
86     }
87 
88     if (!parcel.WriteInt32(static_cast<int32_t>(formVisiblity_))) {
89         ACCESSTOKEN_LOG_ERROR(LABEL, "WriteInt32 failed.");
90         return false;
91     }
92 
93     if (!parcel.WriteInt32(specification_)) {
94         ACCESSTOKEN_LOG_ERROR(LABEL, "WriteInt32 failed.");
95         return false;
96     }
97 
98     if (!parcel.WriteString16(Str8ToStr16(bundleName_))) {
99         ACCESSTOKEN_LOG_ERROR(LABEL, "WriteString16 failed.");
100         return false;
101     }
102 
103     if (!parcel.WriteString16(Str8ToStr16(moduleName_))) {
104         ACCESSTOKEN_LOG_ERROR(LABEL, "WriteString16 failed.");
105         return false;
106     }
107 
108     if (!parcel.WriteString16(Str8ToStr16(abilityName_))) {
109         ACCESSTOKEN_LOG_ERROR(LABEL, "WriteString16 failed.");
110         return false;
111     }
112 
113     if (!parcel.WriteString16(Str8ToStr16(formName_))) {
114         ACCESSTOKEN_LOG_ERROR(LABEL, "WriteString16 failed.");
115         return false;
116     }
117 
118     if (!parcel.WriteInt32(static_cast<int32_t>(formUsageState_))) {
119         ACCESSTOKEN_LOG_ERROR(LABEL, "WriteInt32 failed.");
120         return false;
121     }
122     return true;
123 }
124 
Unmarshalling(Parcel & parcel)125 FormInstance* FormInstance::Unmarshalling(Parcel &parcel)
126 {
127     std::unique_ptr<FormInstance> object = std::make_unique<FormInstance>();
128     if (object && !object->ReadFromParcel(parcel)) {
129         object = nullptr;
130         return nullptr;
131     }
132     return object.release();
133 }
134 }  // namespace AccessToken
135 }  // namespace Security
136 }  // namespace OHOS
137