1 /*
2 * Copyright (c) 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 "os_account_domain_account_callback.h"
17
18 #include "account_error_no.h"
19 #include "account_event_provider.h"
20 #include "account_log_wrapper.h"
21 #ifdef HAS_CES_PART
22 #include "common_event_support.h"
23 #endif // HAS_CES_PART
24 #include "iinner_os_account_manager.h"
25 #include "ios_account_control.h"
26 #include "os_account_constants.h"
27 #include "os_account_control_file_manager.h"
28
29 namespace OHOS {
30 namespace AccountSA {
CheckAndCreateDomainAccountCallback(std::shared_ptr<IOsAccountControl> & osAccountControl,const OsAccountType & type,const sptr<IDomainAccountCallback> & callback,const CreateOsAccountForDomainOptions & accountOptions)31 CheckAndCreateDomainAccountCallback::CheckAndCreateDomainAccountCallback(
32 std::shared_ptr<IOsAccountControl> &osAccountControl, const OsAccountType &type,
33 const sptr<IDomainAccountCallback> &callback, const CreateOsAccountForDomainOptions &accountOptions)
34 : type_(type), osAccountControl_(osAccountControl), accountOptions_(accountOptions), innerCallback_(callback)
35 {}
36
OnResult(int32_t errCode,Parcel & parcel)37 void CheckAndCreateDomainAccountCallback::OnResult(int32_t errCode, Parcel &parcel)
38 {
39 if (innerCallback_ == nullptr) {
40 ACCOUNT_LOGE("innerPlugin_ is nullptr");
41 return;
42 }
43 OsAccountInfo osAccountInfo;
44 Parcel resultParcel;
45 osAccountInfo.Marshalling(resultParcel);
46 if (errCode != ERR_OK) {
47 ACCOUNT_LOGE("check domain account failed");
48 return innerCallback_->OnResult(errCode, resultParcel);
49 }
50 std::shared_ptr<AAFwk::WantParams> parameters(AAFwk::WantParams::Unmarshalling(parcel));
51 if (parameters == nullptr) {
52 ACCOUNT_LOGE("parameters unmarshalling error");
53 return innerCallback_->OnResult(ERR_JS_SYSTEM_SERVICE_EXCEPTION, resultParcel);
54 }
55 DomainAccountInfo domainAccountInfo;
56 domainAccountInfo.accountName_ = parameters->GetStringParam("accountName");
57 domainAccountInfo.domain_ = parameters->GetStringParam("domain");
58 domainAccountInfo.accountId_ = parameters->GetStringParam("accountId");
59 domainAccountInfo.serverConfigId_ = parameters->GetStringParam("serverConfigId");
60 if ((domainAccountInfo.accountName_.empty()) || (domainAccountInfo.domain_.empty())) {
61 ACCOUNT_LOGE("domain account not found");
62 return innerCallback_->OnResult(ERR_JS_ACCOUNT_NOT_FOUND, resultParcel);
63 }
64 errCode = IInnerOsAccountManager::GetInstance().BindDomainAccount(type_, domainAccountInfo,
65 osAccountInfo, accountOptions_);
66 if (errCode != ERR_OK) {
67 return innerCallback_->OnResult(errCode, resultParcel);
68 }
69 auto callbackWrapper =
70 std::make_shared<BindDomainAccountCallback>(osAccountControl_, osAccountInfo, innerCallback_);
71 if (callbackWrapper == nullptr) {
72 ACCOUNT_LOGE("Create BindDomainAccountCallback failed");
73 return innerCallback_->OnResult(ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR, resultParcel);
74 }
75 errCode = InnerDomainAccountManager::GetInstance().OnAccountBound(domainAccountInfo,
76 osAccountInfo.GetLocalId(), callbackWrapper);
77 if (errCode != ERR_OK) {
78 return innerCallback_->OnResult(errCode, resultParcel);
79 }
80 }
81
BindDomainAccountCallback(std::shared_ptr<IOsAccountControl> & osAccountControl,const OsAccountInfo & osAccountInfo,const sptr<IDomainAccountCallback> & callback)82 BindDomainAccountCallback::BindDomainAccountCallback(
83 std::shared_ptr<IOsAccountControl> &osAccountControl, const OsAccountInfo &osAccountInfo,
84 const sptr<IDomainAccountCallback> &callback)
85 : osAccountControl_(osAccountControl), osAccountInfo_(osAccountInfo), innerCallback_(callback)
86 {}
87
OnResult(int32_t errCode,Parcel & parcel)88 void BindDomainAccountCallback::OnResult(int32_t errCode, Parcel &parcel)
89 {
90 if (innerCallback_ == nullptr) {
91 ACCOUNT_LOGE("inner callback is nullptr");
92 return;
93 }
94 if (errCode != ERR_OK) {
95 ACCOUNT_LOGE("failed to bind domain account");
96 if (osAccountInfo_.GetLocalId() != Constants::START_USER_ID) {
97 (void)osAccountControl_->DelOsAccount(osAccountInfo_.GetLocalId());
98 }
99 return innerCallback_->OnResult(errCode, parcel);
100 }
101 Parcel resultParcel;
102 if (osAccountInfo_.GetLocalId() != Constants::START_USER_ID) {
103 errCode = IInnerOsAccountManager::GetInstance().SendMsgForAccountCreate(osAccountInfo_);
104 osAccountInfo_.Marshalling(resultParcel);
105 return innerCallback_->OnResult(errCode, resultParcel);
106 }
107 if ((osAccountInfo_.GetLocalId() == Constants::START_USER_ID) && (errCode == ERR_OK)) {
108 #ifdef HAS_CES_PART
109 AccountEventProvider::EventPublish(
110 EventFwk::CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED, Constants::START_USER_ID, nullptr);
111 #else // HAS_CES_PART
112 ACCOUNT_LOGI("No common event part! Publish nothing!");
113 #endif // HAS_CES_PART
114 }
115 osAccountInfo_.Marshalling(resultParcel);
116 innerCallback_->OnResult(errCode, resultParcel);
117 }
118 } // namespace AccountSA
119 } // namespace OHOS