1 /*
2 * Copyright (c) 2021-2022 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 #include "os_account_user_callback.h"
16 #include <chrono>
17 #include <thread>
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20 #include "os_account_manager.h"
21 #include "os_account_interface.h"
22
23 namespace OHOS {
24 namespace AccountSA {
OsAccountUserCallback()25 OsAccountUserCallback::OsAccountUserCallback()
26 {}
27
OsAccountUserCallback(const OsAccountStartCallbackFunc & callbackFunc)28 OsAccountUserCallback::OsAccountUserCallback(const OsAccountStartCallbackFunc &callbackFunc)
29 :startUserCallbackFunc_(callbackFunc)
30 {}
31
OnStopUserDoneInner(MessageParcel & data,MessageParcel & reply)32 int OsAccountUserCallback::OnStopUserDoneInner(MessageParcel &data, MessageParcel &reply)
33 {
34 auto accountId = data.ReadInt32();
35 auto errCode = data.ReadInt32();
36 OnStopUserDone(accountId, errCode);
37 return ERR_OK;
38 }
39
OnStartUserDoneInner(MessageParcel & data,MessageParcel & reply)40 int OsAccountUserCallback::OnStartUserDoneInner(MessageParcel &data, MessageParcel &reply)
41 {
42 auto accountId = data.ReadInt32();
43 auto errCode = data.ReadInt32();
44 OnStartUserDone(accountId, errCode);
45 return ERR_OK;
46 }
47
OnLogoutUserDoneInner(MessageParcel & data,MessageParcel & reply)48 int OsAccountUserCallback::OnLogoutUserDoneInner(MessageParcel &data, MessageParcel &reply)
49 {
50 int32_t accountId;
51 if (!data.ReadInt32(accountId)) {
52 ACCOUNT_LOGE("Read accountId from reply failed.");
53 OnLogoutUserDone(-1, ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR);
54 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
55 }
56 int32_t errCode;
57 if (!data.ReadInt32(errCode)) {
58 ACCOUNT_LOGE("Read errCode from reply failed.");
59 OnLogoutUserDone(accountId, ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR);
60 return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
61 }
62 OnLogoutUserDone(accountId, errCode);
63 return ERR_OK;
64 }
65
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)66 int OsAccountUserCallback::OnRemoteRequest(
67 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
68 {
69 std::u16string descriptor = OsAccountUserCallback::GetDescriptor();
70 std::u16string remoteDescriptor = data.ReadInterfaceToken();
71 if (descriptor != remoteDescriptor) {
72 ACCOUNT_LOGI("Local descriptor is not equal to remote");
73 return ERR_INVALID_STATE;
74 }
75
76 switch (code) {
77 case UserCallbackCmd::ON_STOP_USER_DONE:
78 return OnStopUserDoneInner(data, reply);
79 case UserCallbackCmd::ON_START_USER_DONE:
80 return OnStartUserDoneInner(data, reply);
81 case UserCallbackCmd::ON_LOGOUT_USER_DONE:
82 return OnLogoutUserDoneInner(data, reply);
83 default:
84 break;
85 }
86
87 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
88 }
89
OnStopUserDone(int userId,int errcode)90 void OsAccountUserCallback::OnStopUserDone(int userId, int errcode)
91 {
92 std::unique_lock<std::mutex> lock(mutex_);
93 ACCOUNT_LOGI("In call back account, OnStopUserDone id is %{public}d, errcode is %{public}d.",
94 userId, errcode);
95 isCalled_ = true;
96 resultCode_ = errcode;
97 onStopCondition_.notify_one();
98 }
99
OnStartUserDone(int userId,int errcode)100 void OsAccountUserCallback::OnStartUserDone(int userId, int errcode)
101 {
102 std::unique_lock<std::mutex> lock(mutex_);
103 ACCOUNT_LOGI("In call back account, OnStartUserDone id is %{public}d, errcode is %{public}d.",
104 userId, errcode);
105 if (errcode == ERR_OK && startUserCallbackFunc_ != nullptr) {
106 startUserCallbackFunc_(userId);
107 }
108 isCalled_ = true;
109 resultCode_ = errcode;
110 onStartCondition_.notify_one();
111 }
112
OnLogoutUserDone(int userId,int errcode)113 void OsAccountUserCallback::OnLogoutUserDone(int userId, int errcode)
114 {
115 std::unique_lock<std::mutex> lock(mutex_);
116 ACCOUNT_LOGI("In call back account, OnLogoutUserDone id is %{public}d, errcode is %{public}d.",
117 userId, errcode);
118 isCalled_ = true;
119 resultCode_ = errcode;
120 onLogoutCondition_.notify_one();
121 }
122 } // namespace AccountSA
123 } // namespace OHOS
124