1 /*
2 * Copyright (c) 2021-2023 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_proxy.h"
17 #include <ipc_types.h>
18 #include <string_ex.h>
19 #include "account_error_no.h"
20 #include "account_info_parcel.h"
21 #include "account_log_wrapper.h"
22
23 namespace OHOS {
24 namespace AccountSA {
SendRequest(AccountMgrInterfaceCode code,MessageParcel & data,MessageParcel & reply)25 ErrCode AccountProxy::SendRequest(AccountMgrInterfaceCode code, MessageParcel &data, MessageParcel &reply)
26 {
27 sptr<IRemoteObject> remote = Remote();
28 if (remote == nullptr) {
29 ACCOUNT_LOGE("remote is nullptr, code = %{public}d", code);
30 return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
31 }
32 MessageOption option(MessageOption::TF_SYNC);
33 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
34 if (result != ERR_OK) {
35 ACCOUNT_LOGE("failed to send account request, code = %{public}d, result = %{public}d", code, result);
36 }
37 return result;
38 }
39
UpdateOhosAccountInfo(const std::string & accountName,const std::string & uid,const std::string & eventStr)40 bool AccountProxy::UpdateOhosAccountInfo(
41 const std::string &accountName, const std::string &uid, const std::string &eventStr)
42 {
43 MessageParcel data;
44 if (!data.WriteInterfaceToken(GetDescriptor())) {
45 ACCOUNT_LOGE("Write descriptor failed!");
46 return false;
47 }
48 if (!data.WriteString16(Str8ToStr16(accountName))) {
49 ACCOUNT_LOGE("Write accountName failed!");
50 return false;
51 }
52 if (!data.WriteString16(Str8ToStr16(uid))) {
53 ACCOUNT_LOGE("Write uid failed!");
54 return false;
55 }
56 if (!data.WriteString16(Str8ToStr16(eventStr))) {
57 ACCOUNT_LOGE("Write eventStr failed!");
58 return false;
59 }
60 MessageParcel reply;
61 auto ret = SendRequest(AccountMgrInterfaceCode::UPDATE_OHOS_ACCOUNT_INFO, data, reply);
62 if (ret != ERR_NONE) {
63 return false;
64 }
65
66 std::int32_t result = ERR_OK;
67 if (!reply.ReadInt32(result)) {
68 ACCOUNT_LOGE("reply ReadInt32 failed");
69 return false;
70 }
71
72 if (result != ERR_OK) {
73 ACCOUNT_LOGE("UpdateOhosAccountInfo failed: %{public}d", result);
74 return false;
75 }
76
77 ACCOUNT_LOGD("UpdateOhosAccountInfo exit");
78 return true;
79 }
80
SetOhosAccountInfo(const OhosAccountInfo & ohosAccountInfo,const std::string & eventStr)81 std::int32_t AccountProxy::SetOhosAccountInfo(const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr)
82 {
83 MessageParcel data;
84 if (!data.WriteInterfaceToken(GetDescriptor())) {
85 ACCOUNT_LOGE("Write descriptor failed!");
86 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
87 }
88 if (!WriteOhosAccountInfo(data, ohosAccountInfo)) {
89 ACCOUNT_LOGE("Write ohosAccountInfo failed!");
90 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
91 }
92 if (!data.WriteString16(Str8ToStr16(eventStr))) {
93 ACCOUNT_LOGE("Write eventStr failed!");
94 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
95 }
96 MessageParcel reply;
97 auto ret = SendRequest(AccountMgrInterfaceCode::SET_OHOS_ACCOUNT_INFO, data, reply);
98 if (ret != ERR_NONE) {
99 return ret;
100 }
101
102 std::int32_t result = ERR_OK;
103 if (!reply.ReadInt32(result)) {
104 ACCOUNT_LOGE("reply ReadInt32 failed");
105 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
106 }
107
108 if (result != ERR_OK) {
109 ACCOUNT_LOGE("SetOhosAccountInfo failed: %{public}d", result);
110 }
111 ACCOUNT_LOGD("SetOhosAccountInfo exit");
112 return result;
113 }
114
SetOhosAccountInfoByUserId(const int32_t userId,const OhosAccountInfo & ohosAccountInfo,const std::string & eventStr)115 ErrCode AccountProxy::SetOhosAccountInfoByUserId(
116 const int32_t userId, const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr)
117 {
118 MessageParcel data;
119 if (!data.WriteInterfaceToken(GetDescriptor())) {
120 ACCOUNT_LOGE("Write descriptor failed!");
121 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
122 }
123 if (!data.WriteInt32(userId)) {
124 ACCOUNT_LOGE("failed to write userId failed");
125 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
126 }
127 if (!WriteOhosAccountInfo(data, ohosAccountInfo)) {
128 ACCOUNT_LOGE("Write ohosAccountInfo failed!");
129 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
130 }
131 if (!data.WriteString16(Str8ToStr16(eventStr))) {
132 ACCOUNT_LOGE("Write eventStr failed!");
133 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
134 }
135 MessageParcel reply;
136 auto ret = SendRequest(AccountMgrInterfaceCode::SET_OHOS_ACCOUNT_INFO_BY_USER_ID, data, reply);
137 if (ret != ERR_NONE) {
138 return ret;
139 }
140
141 std::int32_t result = ERR_OK;
142 if (!reply.ReadInt32(result)) {
143 ACCOUNT_LOGE("reply ReadInt32 failed");
144 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
145 }
146 return result;
147 }
148
QueryOhosAccountInfo(void)149 std::pair<bool, OhosAccountInfo> AccountProxy::QueryOhosAccountInfo(void)
150 {
151 MessageParcel data;
152 if (!data.WriteInterfaceToken(GetDescriptor())) {
153 ACCOUNT_LOGE("Write descriptor failed");
154 return std::make_pair(false, OhosAccountInfo());
155 }
156 MessageParcel reply;
157 auto ret = SendRequest(AccountMgrInterfaceCode::QUERY_OHOS_ACCOUNT_INFO, data, reply);
158 if (ret != ERR_NONE) {
159 return std::make_pair(false, OhosAccountInfo());
160 }
161
162 std::u16string name = reply.ReadString16();
163 std::u16string uid = reply.ReadString16();
164 std::int32_t status = reply.ReadInt32();
165 ACCOUNT_LOGD("QueryOhosAccountInfo exit");
166 return std::make_pair(true, OhosAccountInfo(Str16ToStr8(name), Str16ToStr8(uid), status));
167 }
168
GetOhosAccountInfo(OhosAccountInfo & ohosAccountInfo)169 ErrCode AccountProxy::GetOhosAccountInfo(OhosAccountInfo &ohosAccountInfo)
170 {
171 MessageParcel data;
172 if (!data.WriteInterfaceToken(GetDescriptor())) {
173 ACCOUNT_LOGE("Write descriptor failed");
174 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
175 }
176 MessageParcel reply;
177 auto ret = SendRequest(AccountMgrInterfaceCode::GET_OHOS_ACCOUNT_INFO, data, reply);
178 if (ret != ERR_NONE) {
179 return ret;
180 }
181 ret = ReadOhosAccountInfo(reply, ohosAccountInfo);
182 if (ret != ERR_OK) {
183 return ret;
184 }
185
186 ACCOUNT_LOGD("QueryOhosAccountInfo exit");
187 return ERR_OK;
188 }
189
GetOhosAccountInfoByUserId(int32_t userId,OhosAccountInfo & ohosAccountInfo)190 ErrCode AccountProxy::GetOhosAccountInfoByUserId(int32_t userId, OhosAccountInfo &ohosAccountInfo)
191 {
192 MessageParcel data;
193 if (!data.WriteInterfaceToken(GetDescriptor())) {
194 ACCOUNT_LOGE("Write descriptor failed");
195 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
196 }
197 if (!data.WriteInt32(userId)) {
198 ACCOUNT_LOGE("failed to write int for userId %{public}d.", userId);
199 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
200 }
201 MessageParcel reply;
202 auto ret = SendRequest(AccountMgrInterfaceCode::GET_OHOS_ACCOUNT_INFO_BY_USER_ID, data, reply);
203 if (ret != ERR_NONE) {
204 return ret;
205 }
206 ret = ReadOhosAccountInfo(reply, ohosAccountInfo);
207 if (ret != ERR_OK) {
208 return ret;
209 }
210 return ERR_OK;
211 }
212
QueryOhosAccountInfoByUserId(std::int32_t userId)213 std::pair<bool, OhosAccountInfo> AccountProxy::QueryOhosAccountInfoByUserId(std::int32_t userId)
214 {
215 MessageParcel data;
216 if (!data.WriteInterfaceToken(GetDescriptor())) {
217 ACCOUNT_LOGE("Write descriptor failed");
218 return std::make_pair(false, OhosAccountInfo());
219 }
220
221 if (!data.WriteInt32(userId)) {
222 ACCOUNT_LOGE("failed to write int for userId %{public}d.", userId);
223 return std::make_pair(false, OhosAccountInfo());
224 }
225 MessageParcel reply;
226 auto ret = SendRequest(AccountMgrInterfaceCode::QUERY_OHOS_ACCOUNT_INFO_BY_USER_ID, data, reply);
227 if (ret != ERR_NONE) {
228 return std::make_pair(false, OhosAccountInfo());
229 }
230
231 std::u16string name;
232 std::u16string uid;
233 std::int32_t status;
234 if ((!reply.ReadString16(name)) || (!reply.ReadString16(uid)) || (!reply.ReadInt32(status))) {
235 ACCOUNT_LOGE("failed to read from parcel");
236 return std::make_pair(false, OhosAccountInfo());
237 }
238 return std::make_pair(true, OhosAccountInfo(Str16ToStr8(name), Str16ToStr8(uid), status));
239 }
240
QueryDeviceAccountId(std::int32_t & accountId)241 std::int32_t AccountProxy::QueryDeviceAccountId(std::int32_t &accountId)
242 {
243 MessageParcel data;
244 if (!data.WriteInterfaceToken(GetDescriptor())) {
245 ACCOUNT_LOGE("Write descriptor failed");
246 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
247 }
248 MessageParcel reply;
249 auto ret = SendRequest(AccountMgrInterfaceCode::QUERY_DEVICE_ACCOUNT_ID, data, reply);
250 if (ret != ERR_NONE) {
251 return ret;
252 }
253 accountId = reply.ReadInt32();
254 return ERR_OK;
255 }
256
GetAppAccountService()257 sptr<IRemoteObject> AccountProxy::GetAppAccountService()
258 {
259 MessageParcel data;
260 if (!data.WriteInterfaceToken(GetDescriptor())) {
261 ACCOUNT_LOGE("Write descriptor failed");
262 return nullptr;
263 }
264 MessageParcel reply;
265 auto ret = SendRequest(AccountMgrInterfaceCode::GET_APP_ACCOUNT_SERVICE, data, reply);
266 if (ret != ERR_NONE) {
267 return nullptr;
268 }
269 return reply.ReadRemoteObject();
270 }
271
GetOsAccountService()272 sptr<IRemoteObject> AccountProxy::GetOsAccountService()
273 {
274 MessageParcel data;
275 if (!data.WriteInterfaceToken(GetDescriptor())) {
276 ACCOUNT_LOGE("Write descriptor failed");
277 return nullptr;
278 }
279 MessageParcel reply;
280 auto ret = SendRequest(AccountMgrInterfaceCode::GET_OS_ACCOUNT_SERVICE, data, reply);
281 if (ret != ERR_NONE) {
282 return nullptr;
283 }
284
285 return reply.ReadRemoteObject();
286 }
287
GetAccountIAMService()288 sptr<IRemoteObject> AccountProxy::GetAccountIAMService()
289 {
290 MessageParcel data;
291 if (!data.WriteInterfaceToken(GetDescriptor())) {
292 ACCOUNT_LOGE("Write descriptor failed");
293 return nullptr;
294 }
295 MessageParcel reply;
296 auto ret = SendRequest(AccountMgrInterfaceCode::GET_ACCOUNT_IAM_SERVICE, data, reply);
297 if (ret != ERR_NONE) {
298 return nullptr;
299 }
300
301 return reply.ReadRemoteObject();
302 }
303
GetDomainAccountService()304 sptr<IRemoteObject> AccountProxy::GetDomainAccountService()
305 {
306 MessageParcel data;
307 if (!data.WriteInterfaceToken(GetDescriptor())) {
308 ACCOUNT_LOGE("Write descriptor failed");
309 return nullptr;
310 }
311 MessageParcel reply;
312 auto ret = SendRequest(AccountMgrInterfaceCode::GET_DOMAIN_ACCOUNT_SERVICE, data, reply);
313 if (ret != ERR_NONE) {
314 return nullptr;
315 }
316 return reply.ReadRemoteObject();
317 }
318 } // namespace AccountSA
319 } // namespace OHOS
320