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 std::string 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 bool 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 false;
63 }
64 if (!data.WriteString16(Str8ToStr16(accountName))) {
65 ACCOUNT_LOGE("Write accountName failed!");
66 return false;
67 }
68 if (!data.WriteString16(Str8ToStr16(uid))) {
69 ACCOUNT_LOGE("Write uid failed!");
70 return false;
71 }
72 if (!data.WriteString16(Str8ToStr16(eventStr))) {
73 ACCOUNT_LOGE("Write eventStr failed!");
74 return false;
75 }
76 MessageParcel reply;
77 auto ret = SendRequest(AccountMgrInterfaceCode::UPDATE_OHOS_ACCOUNT_INFO, data, reply);
78 if (ret != ERR_NONE) {
79 return false;
80 }
81
82 std::int32_t result = ERR_OK;
83 if (!reply.ReadInt32(result)) {
84 ACCOUNT_LOGE("reply ReadInt32 failed");
85 return false;
86 }
87
88 if (result != ERR_OK) {
89 ACCOUNT_LOGE("UpdateOhosAccountInfo failed: %{public}d", result);
90 return false;
91 }
92
93 return true;
94 }
95
SetOhosAccountInfo(const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr)96 std::int32_t AccountProxy::SetOhosAccountInfo(const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr)
97 {
98 MessageParcel data;
99 if (!data.WriteInterfaceToken(GetDescriptor())) {
100 ACCOUNT_LOGE("Write descriptor failed!");
101 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
102 }
103 if (!WriteOhosAccountInfo(data, ohosAccountInfo)) {
104 ACCOUNT_LOGE("Write ohosAccountInfo failed!");
105 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
106 }
107 if (!data.WriteString16(Str8ToStr16(eventStr))) {
108 ACCOUNT_LOGE("Write eventStr failed!");
109 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
110 }
111 MessageParcel reply;
112 auto ret = SendRequest(AccountMgrInterfaceCode::SET_OHOS_ACCOUNT_INFO, data, reply);
113 if (ret != ERR_NONE) {
114 return ret;
115 }
116
117 std::int32_t result = ERR_OK;
118 if (!reply.ReadInt32(result)) {
119 ACCOUNT_LOGE("reply ReadInt32 failed");
120 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
121 }
122
123 if (result != ERR_OK) {
124 ACCOUNT_LOGE("SetOhosAccountInfo failed: %{public}d", result);
125 }
126
127 return result;
128 }
129
SetOhosAccountInfoByUserId( const int32_t userId, const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr)130 ErrCode AccountProxy::SetOhosAccountInfoByUserId(
131 const int32_t userId, const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr)
132 {
133 MessageParcel data;
134 if (!data.WriteInterfaceToken(GetDescriptor())) {
135 ACCOUNT_LOGE("Write descriptor failed!");
136 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
137 }
138 if (!data.WriteInt32(userId)) {
139 ACCOUNT_LOGE("failed to write userId failed");
140 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
141 }
142 if (!WriteOhosAccountInfo(data, ohosAccountInfo)) {
143 ACCOUNT_LOGE("Write ohosAccountInfo failed!");
144 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
145 }
146 if (!data.WriteString16(Str8ToStr16(eventStr))) {
147 ACCOUNT_LOGE("Write eventStr failed!");
148 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
149 }
150 MessageParcel reply;
151 auto ret = SendRequest(AccountMgrInterfaceCode::SET_OHOS_ACCOUNT_INFO_BY_USER_ID, data, reply);
152 if (ret != ERR_NONE) {
153 return ret;
154 }
155
156 std::int32_t result = ERR_OK;
157 if (!reply.ReadInt32(result)) {
158 ACCOUNT_LOGE("reply ReadInt32 failed");
159 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
160 }
161 return result;
162 }
163
QueryOhosAccountInfo(OhosAccountInfo &accountInfo)164 ErrCode AccountProxy::QueryOhosAccountInfo(OhosAccountInfo &accountInfo)
165 {
166 MessageParcel data;
167 if (!data.WriteInterfaceToken(GetDescriptor())) {
168 ACCOUNT_LOGE("Write descriptor failed");
169 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
170 }
171 MessageParcel reply;
172 auto ret = SendRequest(AccountMgrInterfaceCode::QUERY_OHOS_ACCOUNT_INFO, data, reply);
173 if (ret != ERR_NONE) {
174 return ret;
175 }
176
177 std::u16string name;
178 std::u16string uid;
179 std::int32_t status;
180 if ((!reply.ReadString16(name)) || (!reply.ReadString16(uid)) || (!reply.ReadInt32(status))) {
181 ACCOUNT_LOGE("failed to read from parcel");
182 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
183 }
184 accountInfo.name_ = Str16ToStr8(name);
185 accountInfo.uid_ = Str16ToStr8(uid);
186 accountInfo.status_ = status;
187 return ERR_OK;
188 }
189
GetOhosAccountInfo(OhosAccountInfo &ohosAccountInfo)190 ErrCode AccountProxy::GetOhosAccountInfo(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 MessageParcel reply;
198 auto ret = SendRequest(AccountMgrInterfaceCode::GET_OHOS_ACCOUNT_INFO, data, reply);
199 if (ret != ERR_NONE) {
200 return ret;
201 }
202 ret = ReadOhosAccountInfo(reply, ohosAccountInfo);
203 if (ret != ERR_OK) {
204 return ret;
205 }
206 ACCOUNT_LOGI("Get ohos account %{public}s.", AnonymizeNameStr(ohosAccountInfo.nickname_).c_str());
207 return ERR_OK;
208 }
209
GetOhosAccountInfoByUserId(int32_t userId, OhosAccountInfo &ohosAccountInfo)210 ErrCode AccountProxy::GetOhosAccountInfoByUserId(int32_t userId, OhosAccountInfo &ohosAccountInfo)
211 {
212 MessageParcel data;
213 if (!data.WriteInterfaceToken(GetDescriptor())) {
214 ACCOUNT_LOGE("Write descriptor failed");
215 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
216 }
217 if (!data.WriteInt32(userId)) {
218 ACCOUNT_LOGE("failed to write int for userId %{public}d.", userId);
219 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
220 }
221 MessageParcel reply;
222 auto ret = SendRequest(AccountMgrInterfaceCode::GET_OHOS_ACCOUNT_INFO_BY_USER_ID, data, reply);
223 if (ret != ERR_NONE) {
224 return ret;
225 }
226 ret = ReadOhosAccountInfo(reply, ohosAccountInfo);
227 if (ret != ERR_OK) {
228 return ret;
229 }
230 ACCOUNT_LOGI("Get ohos account %{public}s.", AnonymizeNameStr(ohosAccountInfo.nickname_).c_str());
231 return ERR_OK;
232 }
233
QueryOhosAccountInfoByUserId(std::int32_t userId, OhosAccountInfo &accountInfo)234 ErrCode AccountProxy::QueryOhosAccountInfoByUserId(std::int32_t userId, 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
242 if (!data.WriteInt32(userId)) {
243 ACCOUNT_LOGE("failed to write int for userId %{public}d.", userId);
244 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
245 }
246 MessageParcel reply;
247 auto ret = SendRequest(AccountMgrInterfaceCode::QUERY_OHOS_ACCOUNT_INFO_BY_USER_ID, data, reply);
248 if (ret != ERR_NONE) {
249 return ret;
250 }
251
252 std::u16string name;
253 std::u16string uid;
254 std::int32_t status;
255 if ((!reply.ReadString16(name)) || (!reply.ReadString16(uid)) || (!reply.ReadInt32(status))) {
256 ACCOUNT_LOGE("failed to read from parcel");
257 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
258 }
259 accountInfo.name_ = Str16ToStr8(name);
260 accountInfo.uid_ = Str16ToStr8(uid);
261 accountInfo.status_ = status;
262 return ERR_OK;
263 }
264
QueryDeviceAccountId(std::int32_t &accountId)265 std::int32_t AccountProxy::QueryDeviceAccountId(std::int32_t &accountId)
266 {
267 MessageParcel data;
268 if (!data.WriteInterfaceToken(GetDescriptor())) {
269 ACCOUNT_LOGE("Write descriptor failed");
270 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
271 }
272 MessageParcel reply;
273 auto ret = SendRequest(AccountMgrInterfaceCode::QUERY_DEVICE_ACCOUNT_ID, data, reply);
274 if (ret != ERR_NONE) {
275 return ret;
276 }
277 accountId = reply.ReadInt32();
278 return ERR_OK;
279 }
280
SubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type, const sptr<IRemoteObject> &eventListener)281 ErrCode AccountProxy::SubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
282 const sptr<IRemoteObject> &eventListener)
283 {
284 MessageParcel data;
285 MessageParcel reply;
286
287 if (!data.WriteInterfaceToken(GetDescriptor())) {
288 ACCOUNT_LOGE("Write descriptor failed.");
289 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
290 }
291
292 if (!data.WriteInt32(static_cast<int32_t>(type))) {
293 ACCOUNT_LOGE("Write type failed.");
294 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
295 }
296
297 if (!data.WriteRemoteObject(eventListener)) {
298 ACCOUNT_LOGE("Write remote object for eventListener failed.");
299 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
300 }
301
302 ErrCode result = SendRequest(AccountMgrInterfaceCode::SUBSCRIBE_DISTRIBUTED_ACCOUNT_EVENT, data, reply);
303 if (result != ERR_OK) {
304 ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
305 return result;
306 }
307
308 if (!reply.ReadInt32(result)) {
309 ACCOUNT_LOGE("Read reply failed.");
310 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
311 }
312 if (result != ERR_OK) {
313 ACCOUNT_LOGE("Subscribe distributed account event failed, result=%{public}d.", result);
314 return result;
315 }
316 return ERR_OK;
317 }
318
UnsubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type, const sptr<IRemoteObject> &eventListener)319 ErrCode AccountProxy::UnsubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
320 const sptr<IRemoteObject> &eventListener)
321 {
322 MessageParcel data;
323 MessageParcel reply;
324
325 if (!data.WriteInterfaceToken(GetDescriptor())) {
326 ACCOUNT_LOGE("Write descriptor failed.");
327 return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
328 }
329
330 if (!data.WriteInt32(static_cast<int32_t>(type))) {
331 ACCOUNT_LOGE("Write type failed.");
332 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
333 }
334
335 if (!data.WriteRemoteObject(eventListener)) {
336 ACCOUNT_LOGE("Write remote object for eventListener failed.");
337 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
338 }
339
340 ErrCode result = SendRequest(AccountMgrInterfaceCode::UNSUBSCRIBE_DISTRIBUTED_ACCOUNT_EVENT, data, reply);
341 if (result != ERR_OK) {
342 ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
343 return result;
344 }
345
346 if (!reply.ReadInt32(result)) {
347 ACCOUNT_LOGE("Read reply failed.");
348 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
349 }
350 if (result != ERR_OK) {
351 ACCOUNT_LOGE("Unsubscribe distributed account failed, result=%{public}d.", result);
352 }
353
354 return result;
355 }
356
GetAppAccountService()357 sptr<IRemoteObject> AccountProxy::GetAppAccountService()
358 {
359 MessageParcel data;
360 if (!data.WriteInterfaceToken(GetDescriptor())) {
361 ACCOUNT_LOGE("Write descriptor failed");
362 return nullptr;
363 }
364 MessageParcel reply;
365 auto ret = SendRequest(AccountMgrInterfaceCode::GET_APP_ACCOUNT_SERVICE, data, reply);
366 if (ret != ERR_NONE) {
367 return nullptr;
368 }
369 return reply.ReadRemoteObject();
370 }
371
GetOsAccountService()372 sptr<IRemoteObject> AccountProxy::GetOsAccountService()
373 {
374 MessageParcel data;
375 if (!data.WriteInterfaceToken(GetDescriptor())) {
376 ACCOUNT_LOGE("Write descriptor failed");
377 return nullptr;
378 }
379 MessageParcel reply;
380 auto ret = SendRequest(AccountMgrInterfaceCode::GET_OS_ACCOUNT_SERVICE, data, reply);
381 if (ret != ERR_NONE) {
382 return nullptr;
383 }
384
385 return reply.ReadRemoteObject();
386 }
387
GetAccountIAMService()388 sptr<IRemoteObject> AccountProxy::GetAccountIAMService()
389 {
390 MessageParcel data;
391 if (!data.WriteInterfaceToken(GetDescriptor())) {
392 ACCOUNT_LOGE("Write descriptor failed");
393 return nullptr;
394 }
395 MessageParcel reply;
396 auto ret = SendRequest(AccountMgrInterfaceCode::GET_ACCOUNT_IAM_SERVICE, data, reply);
397 if (ret != ERR_NONE) {
398 return nullptr;
399 }
400
401 return reply.ReadRemoteObject();
402 }
403
GetDomainAccountService()404 sptr<IRemoteObject> AccountProxy::GetDomainAccountService()
405 {
406 MessageParcel data;
407 if (!data.WriteInterfaceToken(GetDescriptor())) {
408 ACCOUNT_LOGE("Write descriptor failed");
409 return nullptr;
410 }
411 MessageParcel reply;
412 auto ret = SendRequest(AccountMgrInterfaceCode::GET_DOMAIN_ACCOUNT_SERVICE, data, reply);
413 if (ret != ERR_NONE) {
414 return nullptr;
415 }
416 return reply.ReadRemoteObject();
417 }
418 } // namespace AccountSA
419 } // namespace OHOS
420