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 
OnStopUserDoneInner(MessageParcel &data, MessageParcel &reply)28 int OsAccountUserCallback::OnStopUserDoneInner(MessageParcel &data, MessageParcel &reply)
29 {
30     auto accountId = data.ReadInt32();
31     auto errCode = data.ReadInt32();
32     OnStopUserDone(accountId, errCode);
33     return ERR_OK;
34 }
35 
OnStartUserDoneInner(MessageParcel &data, MessageParcel &reply)36 int OsAccountUserCallback::OnStartUserDoneInner(MessageParcel &data, MessageParcel &reply)
37 {
38     auto accountId = data.ReadInt32();
39     auto errCode = data.ReadInt32();
40     OnStartUserDone(accountId, errCode);
41     return ERR_OK;
42 }
43 
OnRemoteRequest( uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)44 int OsAccountUserCallback::OnRemoteRequest(
45     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
46 {
47     std::u16string descriptor = OsAccountUserCallback::GetDescriptor();
48     std::u16string remoteDescriptor = data.ReadInterfaceToken();
49     if (descriptor != remoteDescriptor) {
50         ACCOUNT_LOGI("Local descriptor is not equal to remote");
51         return ERR_INVALID_STATE;
52     }
53 
54     switch (code) {
55         case UserCallbackCmd::ON_STOP_USER_DONE:
56             return OnStopUserDoneInner(data, reply);
57         case UserCallbackCmd::ON_START_USER_DONE:
58             return OnStartUserDoneInner(data, reply);
59         default:
60             break;
61     }
62 
63     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
64 }
65 
OnStopUserDone(int userId, int errcode)66 void OsAccountUserCallback::OnStopUserDone(int userId, int errcode)
67 {
68     std::unique_lock<std::mutex> lock(mutex_);
69     ACCOUNT_LOGI("in call back account, OnStopUserDone id is %{public}d, errcode is %{public}d.",
70         userId, errcode);
71     isCalled_ = true;
72     resultCode_ = errcode;
73     onStopCondition_.notify_one();
74 }
75 
OnStartUserDone(int userId, int errcode)76 void OsAccountUserCallback::OnStartUserDone(int userId, int errcode)
77 {
78     std::unique_lock<std::mutex> lock(mutex_);
79     ACCOUNT_LOGI("in call back account, OnStartUserDone id is %{public}d, errcode is %{public}d.",
80         userId, errcode);
81     isCalled_ = true;
82     resultCode_ = errcode;
83     onStartCondition_.notify_one();
84 }
85 }  // namespace AccountSA
86 }  // namespace OHOS
87