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 {
25 const size_t INTERCEPT_HEAD_PART_LEN_FOR_NAME = 1;
26 const char DEFAULT_ANON_STR[] = "**********";
27
~AccountProxy()28 AccountProxy::~AccountProxy()
29 {
30 destroyedMagic_ = 0x6b6b6b6b;
31 }
32
AnonymizeNameStr(const std::string & nameStr)33 static std::string AnonymizeNameStr(const std::string& nameStr)
34 {
35 if (nameStr.empty()) {
36 return nameStr;
37 }
38 return nameStr.substr(0, INTERCEPT_HEAD_PART_LEN_FOR_NAME) + DEFAULT_ANON_STR;
39 }
40
SendRequest(AccountMgrInterfaceCode code,MessageParcel & data,MessageParcel & reply)41 ErrCode AccountProxy::SendRequest(AccountMgrInterfaceCode code, MessageParcel &data, MessageParcel &reply)
42 {
43 sptr<IRemoteObject> remote = Remote();
44 if (remote == nullptr) {
45 ACCOUNT_LOGE("remote is nullptr, code = %{public}d", code);
46 return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
47 }
48 MessageOption option(MessageOption::TF_SYNC);
49 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
50 if (result != ERR_OK) {
51 ACCOUNT_LOGE("failed to send account request, code = %{public}d, result = %{public}d", code, result);
52 }
53 return result;
54 }
55
UpdateOhosAccountInfo(const std::string & accountName,const std::string & uid,const std::string & eventStr)56 ErrCode AccountProxy::UpdateOhosAccountInfo(
57 const std::string &accountName, const std::string &uid, const std::string &eventStr)
58 {
59 MessageParcel data;
60 if (!data.WriteInterfaceToken(GetDescriptor())) {
61 ACCOUNT_LOGE("Write descriptor failed!");
62 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
63 }
64 if (!data.WriteString16(Str8ToStr16(accountName))) {
65 ACCOUNT_LOGE("Write accountName failed!");
66 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
67 }
68 if (!data.WriteString16(Str8ToStr16(uid))) {
69 ACCOUNT_LOGE("Write uid failed!");
70 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
71 }
72 if (!data.WriteString16(Str8ToStr16(eventStr))) {
73 ACCOUNT_LOGE("Write eventStr failed!");
74 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
75 }
76 MessageParcel reply;
77 auto ret = SendRequest(AccountMgrInterfaceCode::UPDATE_OHOS_ACCOUNT_INFO, data, reply);
78 if (ret != ERR_NONE) {
79 return ret;
80 }
81
82 std::int32_t result = ERR_OK;
83 if (!reply.ReadInt32(result)) {
84 ACCOUNT_LOGE("reply ReadInt32 failed");
85 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
86 }
87
88 if (result != ERR_OK) {
89 ACCOUNT_LOGE("UpdateOhosAccountInfo failed: %{public}d", result);
90 }
91
92 return ERR_OK;
93 }
94
SetOhosAccountInfo(const OhosAccountInfo & ohosAccountInfo,const std::string & eventStr)95 ErrCode AccountProxy::SetOhosAccountInfo(const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr)
96 {
97 MessageParcel data;
98 if (!data.WriteInterfaceToken(GetDescriptor())) {
99 ACCOUNT_LOGE("Write descriptor failed!");
100 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
101 }
102 if (!WriteOhosAccountInfo(data, ohosAccountInfo)) {
103 ACCOUNT_LOGE("Write ohosAccountInfo failed!");
104 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
105 }
106 if (!data.WriteString16(Str8ToStr16(eventStr))) {
107 ACCOUNT_LOGE("Write eventStr failed!");
108 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
109 }
110 MessageParcel reply;
111 auto ret = SendRequest(AccountMgrInterfaceCode::SET_OHOS_ACCOUNT_INFO, data, reply);
112 if (ret != ERR_NONE) {
113 return ret;
114 }
115
116 std::int32_t result = ERR_OK;
117 if (!reply.ReadInt32(result)) {
118 ACCOUNT_LOGE("reply ReadInt32 failed");
119 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
120 }
121
122 if (result != ERR_OK) {
123 ACCOUNT_LOGE("SetOhosAccountInfo failed: %{public}d", result);
124 }
125
126 return result;
127 }
128
SetOsAccountDistributedInfo(const int32_t localId,const OhosAccountInfo & ohosAccountInfo,const std::string & eventStr)129 ErrCode AccountProxy::SetOsAccountDistributedInfo(
130 const int32_t localId, const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr)
131 {
132 MessageParcel data;
133 if (!data.WriteInterfaceToken(GetDescriptor())) {
134 ACCOUNT_LOGE("Write descriptor failed!");
135 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
136 }
137 if (!data.WriteInt32(localId)) {
138 ACCOUNT_LOGE("Failed to write localId.");
139 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
140 }
141 if (!WriteOhosAccountInfo(data, ohosAccountInfo)) {
142 ACCOUNT_LOGE("Write ohosAccountInfo failed!");
143 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
144 }
145 if (!data.WriteString16(Str8ToStr16(eventStr))) {
146 ACCOUNT_LOGE("Write eventStr failed!");
147 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
148 }
149 MessageParcel reply;
150 auto ret = SendRequest(AccountMgrInterfaceCode::SET_OHOS_ACCOUNT_INFO_BY_USER_ID, data, reply);
151 if (ret != ERR_NONE) {
152 return ret;
153 }
154
155 std::int32_t result = ERR_OK;
156 if (!reply.ReadInt32(result)) {
157 ACCOUNT_LOGE("reply ReadInt32 failed");
158 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
159 }
160 return result;
161 }
162
QueryDistributedVirtualDeviceId(std::string & dvid)163 ErrCode AccountProxy::QueryDistributedVirtualDeviceId(std::string &dvid)
164 {
165 dvid = "";
166 MessageParcel data;
167
168 if (!data.WriteInterfaceToken(GetDescriptor())) {
169 ACCOUNT_LOGE("Failed to write descriptor!");
170 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
171 }
172
173 MessageParcel reply;
174 ErrCode code = SendRequest(AccountMgrInterfaceCode::QUERY_DISTRIBUTE_VIRTUAL_DEVICE_ID, data, reply);
175 if (code != ERR_OK) {
176 ACCOUNT_LOGE("Failed to send request, code %{public}d.", code);
177 return code;
178 }
179 int32_t result = ERR_OK;
180 if (!reply.ReadInt32(result)) {
181 ACCOUNT_LOGE("Failed to read result");
182 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
183 }
184 if (result != ERR_OK) {
185 ACCOUNT_LOGE("Failed to query dvid, result %{public}d.", result);
186 return result;
187 }
188 if (!reply.ReadString(dvid)) {
189 ACCOUNT_LOGE("Failed to read dvid");
190 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
191 }
192 return ERR_OK;
193 }
194
QueryDistributedVirtualDeviceId(const std::string & bundleName,int32_t localId,std::string & dvid)195 ErrCode AccountProxy::QueryDistributedVirtualDeviceId(const std::string &bundleName, int32_t localId, std::string &dvid)
196 {
197 dvid = "";
198 MessageParcel data;
199 if (!data.WriteInterfaceToken(GetDescriptor())) {
200 ACCOUNT_LOGE("Failed to write descriptor!");
201 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
202 }
203 if (!data.WriteString(bundleName)) {
204 ACCOUNT_LOGE("Failed to write bundleName=%{public}s", bundleName.c_str());
205 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
206 }
207 if (!data.WriteInt32(localId)) {
208 ACCOUNT_LOGE("Failed to write localId %{public}d.", localId);
209 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
210 }
211
212 MessageParcel reply;
213 ErrCode code = SendRequest(AccountMgrInterfaceCode::QUERY_DISTRIBUTE_VIRTUAL_DEVICE_ID_BY_BUNDLE_NAME, data, reply);
214 if (code != ERR_OK) {
215 ACCOUNT_LOGE("Failed to send request, code %{public}d.", code);
216 return code;
217 }
218 int32_t result = ERR_OK;
219 if (!reply.ReadInt32(result)) {
220 ACCOUNT_LOGE("Failed to read result");
221 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
222 }
223 if (result != ERR_OK) {
224 ACCOUNT_LOGE("Failed to query dvid, result %{public}d.", result);
225 return result;
226 }
227 if (!reply.ReadString(dvid)) {
228 ACCOUNT_LOGE("Failed to read dvid");
229 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
230 }
231 return ERR_OK;
232 }
233
QueryOhosAccountInfo(OhosAccountInfo & accountInfo)234 ErrCode AccountProxy::QueryOhosAccountInfo(OhosAccountInfo &accountInfo)
235 {
236 MessageParcel data;
237 if (!data.WriteInterfaceToken(GetDescriptor())) {
238 ACCOUNT_LOGE("Write descriptor failed");
239 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
240 }
241 MessageParcel reply;
242 auto ret = SendRequest(AccountMgrInterfaceCode::QUERY_OHOS_ACCOUNT_INFO, data, reply);
243 if (ret != ERR_NONE) {
244 return ret;
245 }
246
247 std::u16string name;
248 std::u16string uid;
249 std::int32_t status;
250 if ((!reply.ReadString16(name)) || (!reply.ReadString16(uid)) || (!reply.ReadInt32(status))) {
251 ACCOUNT_LOGE("failed to read from parcel");
252 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
253 }
254 accountInfo.name_ = Str16ToStr8(name);
255 accountInfo.uid_ = Str16ToStr8(uid);
256 accountInfo.status_ = status;
257 return ERR_OK;
258 }
259
GetOhosAccountInfo(OhosAccountInfo & ohosAccountInfo)260 ErrCode AccountProxy::GetOhosAccountInfo(OhosAccountInfo &ohosAccountInfo)
261 {
262 MessageParcel data;
263 if (!data.WriteInterfaceToken(GetDescriptor())) {
264 ACCOUNT_LOGE("Write descriptor failed");
265 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
266 }
267 MessageParcel reply;
268 auto ret = SendRequest(AccountMgrInterfaceCode::GET_OHOS_ACCOUNT_INFO, data, reply);
269 if (ret != ERR_NONE) {
270 return ret;
271 }
272 ret = ReadOhosAccountInfo(reply, ohosAccountInfo);
273 if (ret != ERR_OK) {
274 return ret;
275 }
276 ACCOUNT_LOGI("Get ohos account %{public}s.", AnonymizeNameStr(ohosAccountInfo.nickname_).c_str());
277 return ERR_OK;
278 }
279
GetOsAccountDistributedInfo(int32_t localId,OhosAccountInfo & ohosAccountInfo)280 ErrCode AccountProxy::GetOsAccountDistributedInfo(int32_t localId, OhosAccountInfo &ohosAccountInfo)
281 {
282 MessageParcel data;
283 if (!data.WriteInterfaceToken(GetDescriptor())) {
284 ACCOUNT_LOGE("Write descriptor failed");
285 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
286 }
287 if (!data.WriteInt32(localId)) {
288 ACCOUNT_LOGE("Failed to write localId %{public}d.", localId);
289 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
290 }
291 MessageParcel reply;
292 auto ret = SendRequest(AccountMgrInterfaceCode::GET_OHOS_ACCOUNT_INFO_BY_USER_ID, data, reply);
293 if (ret != ERR_NONE) {
294 return ret;
295 }
296 ret = ReadOhosAccountInfo(reply, ohosAccountInfo);
297 if (ret != ERR_OK) {
298 return ret;
299 }
300 ACCOUNT_LOGI("Get ohos account %{public}s.", AnonymizeNameStr(ohosAccountInfo.nickname_).c_str());
301 return ERR_OK;
302 }
303
QueryOsAccountDistributedInfo(std::int32_t localId,OhosAccountInfo & accountInfo)304 ErrCode AccountProxy::QueryOsAccountDistributedInfo(std::int32_t localId, OhosAccountInfo &accountInfo)
305 {
306 MessageParcel data;
307 if (!data.WriteInterfaceToken(GetDescriptor())) {
308 ACCOUNT_LOGE("Write descriptor failed");
309 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
310 }
311
312 if (!data.WriteInt32(localId)) {
313 ACCOUNT_LOGE("Failed to write localId %{public}d.", localId);
314 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
315 }
316 MessageParcel reply;
317 auto ret = SendRequest(AccountMgrInterfaceCode::QUERY_OHOS_ACCOUNT_INFO_BY_USER_ID, data, reply);
318 if (ret != ERR_NONE) {
319 return ret;
320 }
321
322 std::u16string name;
323 std::u16string uid;
324 std::int32_t status;
325 if ((!reply.ReadString16(name)) || (!reply.ReadString16(uid)) || (!reply.ReadInt32(status))) {
326 ACCOUNT_LOGE("failed to read from parcel");
327 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
328 }
329 accountInfo.name_ = Str16ToStr8(name);
330 accountInfo.uid_ = Str16ToStr8(uid);
331 accountInfo.status_ = status;
332 return ERR_OK;
333 }
334
QueryDeviceAccountId(std::int32_t & accountId)335 std::int32_t AccountProxy::QueryDeviceAccountId(std::int32_t &accountId)
336 {
337 MessageParcel data;
338 if (!data.WriteInterfaceToken(GetDescriptor())) {
339 ACCOUNT_LOGE("Write descriptor failed");
340 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
341 }
342 MessageParcel reply;
343 auto ret = SendRequest(AccountMgrInterfaceCode::QUERY_DEVICE_ACCOUNT_ID, data, reply);
344 if (ret != ERR_NONE) {
345 return ret;
346 }
347 accountId = reply.ReadInt32();
348 return ERR_OK;
349 }
350
SubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,const sptr<IRemoteObject> & eventListener)351 ErrCode AccountProxy::SubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
352 const sptr<IRemoteObject> &eventListener)
353 {
354 MessageParcel data;
355 MessageParcel reply;
356
357 if (!data.WriteInterfaceToken(GetDescriptor())) {
358 ACCOUNT_LOGE("Write descriptor failed.");
359 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
360 }
361
362 if (!data.WriteInt32(static_cast<int32_t>(type))) {
363 ACCOUNT_LOGE("Write type failed.");
364 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
365 }
366
367 if (!data.WriteRemoteObject(eventListener)) {
368 ACCOUNT_LOGE("Write remote object for eventListener failed.");
369 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
370 }
371
372 ErrCode result = SendRequest(AccountMgrInterfaceCode::SUBSCRIBE_DISTRIBUTED_ACCOUNT_EVENT, data, reply);
373 if (result != ERR_OK) {
374 ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
375 return result;
376 }
377
378 if (!reply.ReadInt32(result)) {
379 ACCOUNT_LOGE("Read reply failed.");
380 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
381 }
382 if (result != ERR_OK) {
383 ACCOUNT_LOGE("Subscribe distributed account event failed, result=%{public}d.", result);
384 return result;
385 }
386 return ERR_OK;
387 }
388
UnsubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,const sptr<IRemoteObject> & eventListener)389 ErrCode AccountProxy::UnsubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
390 const sptr<IRemoteObject> &eventListener)
391 {
392 MessageParcel data;
393 MessageParcel reply;
394
395 if (!data.WriteInterfaceToken(GetDescriptor())) {
396 ACCOUNT_LOGE("Write descriptor failed.");
397 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
398 }
399
400 if (!data.WriteInt32(static_cast<int32_t>(type))) {
401 ACCOUNT_LOGE("Write type failed.");
402 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
403 }
404
405 if (!data.WriteRemoteObject(eventListener)) {
406 ACCOUNT_LOGE("Write remote object for eventListener failed.");
407 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
408 }
409
410 ErrCode result = SendRequest(AccountMgrInterfaceCode::UNSUBSCRIBE_DISTRIBUTED_ACCOUNT_EVENT, data, reply);
411 if (result != ERR_OK) {
412 ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
413 return result;
414 }
415
416 if (!reply.ReadInt32(result)) {
417 ACCOUNT_LOGE("Read reply failed.");
418 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
419 }
420 if (result != ERR_OK) {
421 ACCOUNT_LOGE("Unsubscribe distributed account failed, result=%{public}d.", result);
422 }
423
424 return result;
425 }
426
GetAppAccountService()427 sptr<IRemoteObject> AccountProxy::GetAppAccountService()
428 {
429 MessageParcel data;
430 if (!data.WriteInterfaceToken(GetDescriptor())) {
431 ACCOUNT_LOGE("Write descriptor failed");
432 return nullptr;
433 }
434 MessageParcel reply;
435 auto ret = SendRequest(AccountMgrInterfaceCode::GET_APP_ACCOUNT_SERVICE, data, reply);
436 if (ret != ERR_NONE) {
437 return nullptr;
438 }
439 return reply.ReadRemoteObject();
440 }
441
GetOsAccountService()442 sptr<IRemoteObject> AccountProxy::GetOsAccountService()
443 {
444 MessageParcel data;
445 if (!data.WriteInterfaceToken(GetDescriptor())) {
446 ACCOUNT_LOGE("Write descriptor failed");
447 return nullptr;
448 }
449 MessageParcel reply;
450 auto ret = SendRequest(AccountMgrInterfaceCode::GET_OS_ACCOUNT_SERVICE, data, reply);
451 if (ret != ERR_NONE) {
452 return nullptr;
453 }
454
455 return reply.ReadRemoteObject();
456 }
457
GetAccountIAMService()458 sptr<IRemoteObject> AccountProxy::GetAccountIAMService()
459 {
460 MessageParcel data;
461 if (!data.WriteInterfaceToken(GetDescriptor())) {
462 ACCOUNT_LOGE("Write descriptor failed");
463 return nullptr;
464 }
465 MessageParcel reply;
466 auto ret = SendRequest(AccountMgrInterfaceCode::GET_ACCOUNT_IAM_SERVICE, data, reply);
467 if (ret != ERR_NONE) {
468 return nullptr;
469 }
470
471 return reply.ReadRemoteObject();
472 }
473
GetDomainAccountService()474 sptr<IRemoteObject> AccountProxy::GetDomainAccountService()
475 {
476 MessageParcel data;
477 if (!data.WriteInterfaceToken(GetDescriptor())) {
478 ACCOUNT_LOGE("Write descriptor failed");
479 return nullptr;
480 }
481 MessageParcel reply;
482 auto ret = SendRequest(AccountMgrInterfaceCode::GET_DOMAIN_ACCOUNT_SERVICE, data, reply);
483 if (ret != ERR_NONE) {
484 return nullptr;
485 }
486 return reply.ReadRemoteObject();
487 }
488 } // namespace AccountSA
489 } // namespace OHOS
490