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_hisysevent_adapter.h"
20 #include "account_log_wrapper.h"
21 #include "os_account_constants.h"
22 #include "os_account_manager.h"
23 #include "os_account_interface.h"
24 
25 namespace OHOS {
26 namespace AccountSA {
OsAccountUserCallback()27 OsAccountUserCallback::OsAccountUserCallback()
28 {}
29 
OsAccountUserCallback(const OsAccountStartCallbackFunc & callbackFunc)30 OsAccountUserCallback::OsAccountUserCallback(const OsAccountStartCallbackFunc &callbackFunc)
31     :startUserCallbackFunc_(callbackFunc)
32 {}
33 
OnStopUserDoneInner(MessageParcel & data,MessageParcel & reply)34 int OsAccountUserCallback::OnStopUserDoneInner(MessageParcel &data, MessageParcel &reply)
35 {
36     auto accountId = data.ReadInt32();
37     auto errCode = data.ReadInt32();
38     OnStopUserDone(accountId, errCode);
39     return ERR_OK;
40 }
41 
OnStartUserDoneInner(MessageParcel & data,MessageParcel & reply)42 int OsAccountUserCallback::OnStartUserDoneInner(MessageParcel &data, MessageParcel &reply)
43 {
44     auto accountId = data.ReadInt32();
45     auto errCode = data.ReadInt32();
46     OnStartUserDone(accountId, errCode);
47     return ERR_OK;
48 }
49 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)50 int OsAccountUserCallback::OnRemoteRequest(
51     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
52 {
53     std::u16string descriptor = OsAccountUserCallback::GetDescriptor();
54     std::u16string remoteDescriptor = data.ReadInterfaceToken();
55     if (descriptor != remoteDescriptor) {
56         ACCOUNT_LOGI("Local descriptor is not equal to remote");
57         return ERR_INVALID_STATE;
58     }
59 
60     switch (code) {
61         case UserCallbackCmd::ON_STOP_USER_DONE:
62             return OnStopUserDoneInner(data, reply);
63         case UserCallbackCmd::ON_START_USER_DONE:
64             return OnStartUserDoneInner(data, reply);
65         default:
66             break;
67     }
68 
69     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
70 }
71 
OnStopUserDone(int userId,int errcode)72 void OsAccountUserCallback::OnStopUserDone(int userId, int errcode)
73 {
74     std::unique_lock<std::mutex> lock(mutex_);
75     ACCOUNT_LOGI("in call back account, OnStopUserDone id is %{public}d, errcode is %{public}d.",
76         userId, errcode);
77     if (errcode != 0) {
78         ReportOsAccountOperationFail(
79             userId, Constants::OPERATION_STOP, errcode, "AbilityManager failed to stop user in callback");
80     }
81     isReturnOk_ = (errcode == 0);
82     onStopCondition_.notify_one();
83 }
84 
OnStartUserDone(int userId,int errcode)85 void OsAccountUserCallback::OnStartUserDone(int userId, int errcode)
86 {
87     std::unique_lock<std::mutex> lock(mutex_);
88     ACCOUNT_LOGI("in call back account, OnStartUserDone id is %{public}d, errcode is %{public}d.",
89         userId, errcode);
90     if (errcode == ERR_OK && startUserCallbackFunc_ != nullptr) {
91         startUserCallbackFunc_(userId);
92     }
93     if (errcode != 0) {
94         ReportOsAccountOperationFail(
95             userId, Constants::OPERATION_START, errcode, "AbilityManager failed to start user in callback");
96     }
97     isReturnOk_ = (errcode == 0);
98     onStartCondition_.notify_one();
99 }
100 }  // namespace AccountSA
101 }  // namespace OHOS
102