• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "account_info.h"
17 #include "account_log_wrapper.h"
18 #include "message_parcel.h"
19 
20 namespace OHOS {
21 namespace AccountSA {
22 namespace {
23 const int32_t AVATAR_MAX_SIZE = 10 * 1024 * 1024;
24 }
25 
Marshalling(Parcel & parcel) const26 bool OhosAccountInfo::Marshalling(Parcel& parcel) const
27 {
28     if (!parcel.WriteString16(Str8ToStr16(name_))) {
29         ACCOUNT_LOGE("write name failed!");
30         return false;
31     }
32     if (!parcel.WriteString16(Str8ToStr16(uid_))) {
33         ACCOUNT_LOGE("write uid failed!");
34         return false;
35     }
36     if (!parcel.WriteString16(Str8ToStr16(rawUid_))) {
37         ACCOUNT_LOGE("write rawUid failed!");
38         return false;
39     }
40     if (!parcel.WriteInt32(status_)) {
41         ACCOUNT_LOGE("write status failed!");
42         return false;
43     }
44     if (!parcel.WriteString16(Str8ToStr16(nickname_))) {
45         ACCOUNT_LOGE("write nickname failed!");
46         return false;
47     }
48     if (!parcel.WriteInt32(avatar_.size() + 1)) {
49         ACCOUNT_LOGE("write avatarSize failed!");
50         return false;
51     }
52     if (!static_cast<MessageParcel&>(parcel).WriteRawData(static_cast<const void*>(avatar_.c_str()),
53         avatar_.size() + 1)) {
54         ACCOUNT_LOGE("write avatar failed!");
55         return false;
56     }
57     if (!parcel.WriteParcelable(&(scalableData_))) {
58         ACCOUNT_LOGE("write scalableData failed!");
59         return false;
60     }
61     return true;
62 }
63 
Unmarshalling(Parcel & parcel)64 OhosAccountInfo* OhosAccountInfo::Unmarshalling(Parcel& parcel)
65 {
66     OhosAccountInfo* info = new (std::nothrow) OhosAccountInfo();
67     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
68         ACCOUNT_LOGW("read from parcel failed");
69         delete info;
70         info = nullptr;
71     }
72     return info;
73 }
74 
ReadFromParcel(Parcel & parcel)75 bool OhosAccountInfo::ReadFromParcel(Parcel& parcel)
76 {
77     std::u16string name;
78     if (!parcel.ReadString16(name)) {
79         ACCOUNT_LOGE("read name failed");
80         return false;
81     }
82     std::u16string uid;
83     if (!parcel.ReadString16(uid)) {
84         ACCOUNT_LOGE("read uid failed");
85         return false;
86     }
87     std::u16string rawUid;
88     if (!parcel.ReadString16(rawUid)) {
89         ACCOUNT_LOGE("read rawUid failed");
90         return false;
91     }
92     int32_t status;
93     if (!parcel.ReadInt32(status)) {
94         ACCOUNT_LOGE("read status failed");
95         return false;
96     }
97     std::u16string nickname;
98     if (!parcel.ReadString16(nickname)) {
99         ACCOUNT_LOGE("read nickname failed");
100         return false;
101     }
102 
103     if (!ReadAvatarData(parcel)) {
104         ACCOUNT_LOGE("read avatar failed");
105         return false;
106     }
107     sptr<AAFwk::Want> want = parcel.ReadParcelable<AAFwk::Want>();
108     if (want == nullptr) {
109         ACCOUNT_LOGE("read want failed");
110         return false;
111     }
112     name_ = Str16ToStr8(name);
113     uid_ = Str16ToStr8(uid);
114     status_ = status;
115     nickname_ = Str16ToStr8(nickname);
116     scalableData_ = *want;
117     rawUid_ = Str16ToStr8(rawUid);
118     return true;
119 }
120 
ReadAvatarData(Parcel & parcel)121 bool OhosAccountInfo::ReadAvatarData(Parcel& parcel)
122 {
123     int32_t avatarSize;
124     if (!parcel.ReadInt32(avatarSize)) {
125         ACCOUNT_LOGE("read avatarSize failed");
126         return false;
127     }
128     if ((avatarSize - 1 > AVATAR_MAX_SIZE) || (avatarSize - 1 < 0)) {
129         ACCOUNT_LOGE("avatarSize is invalid");
130         return false;
131     }
132     auto readRawData = static_cast<MessageParcel&>(parcel).ReadRawData(avatarSize);
133     if (readRawData == nullptr) {
134         ACCOUNT_LOGE("read avatar failed");
135         return false;
136     }
137     const char* avatar = reinterpret_cast<const char*>(readRawData);
138     avatar_ = std::string(avatar, avatarSize - 1);
139     return true;
140 }
141 } // namespace AccountSA
142 } // namespace OHOS
143