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