• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 
HandleErrorWithEmptyResult(ErrCode errorCode,const Parcel & resultParcel)37 ErrCode CheckAndCreateDomainAccountCallback::HandleErrorWithEmptyResult(
38     ErrCode errorCode, const Parcel& resultParcel)
39 {
40     DomainAccountParcel domainAccountResultParcel;
41     domainAccountResultParcel.SetParcelData(const_cast<Parcel&>(resultParcel));
42     return innerCallback_->OnResult(errorCode, domainAccountResultParcel);
43 }
44 
OnResult(int32_t errCode,const DomainAccountParcel & domainAccountParcel)45 ErrCode CheckAndCreateDomainAccountCallback::OnResult(int32_t errCode, const DomainAccountParcel &domainAccountParcel)
46 {
47     if (innerCallback_ == nullptr) {
48         ACCOUNT_LOGI("InnerPlugin_ is nullptr");
49         return ERR_OK;
50     }
51     Parcel parcel;
52     domainAccountParcel.GetParcelData(parcel);
53     OsAccountInfo osAccountInfo;
54     Parcel resultParcel;
55     osAccountInfo.Marshalling(resultParcel);
56     if (errCode != ERR_OK) {
57         ACCOUNT_LOGE("Check domain account failed");
58         return HandleErrorWithEmptyResult(errCode, resultParcel);
59     }
60     std::shared_ptr<AAFwk::WantParams> parameters(AAFwk::WantParams::Unmarshalling(parcel));
61     if (parameters == nullptr) {
62         ACCOUNT_LOGE("Parameters unmarshalling error");
63         return HandleErrorWithEmptyResult(ERR_JS_SYSTEM_SERVICE_EXCEPTION, resultParcel);
64     }
65     DomainAccountInfo domainAccountInfo;
66     domainAccountInfo.accountName_ = parameters->GetStringParam("accountName");
67     domainAccountInfo.domain_ = parameters->GetStringParam("domain");
68     domainAccountInfo.accountId_ = parameters->GetStringParam("accountId");
69     domainAccountInfo.serverConfigId_ = parameters->GetStringParam("serverConfigId");
70     if ((domainAccountInfo.accountName_.empty()) || (domainAccountInfo.domain_.empty())) {
71         ACCOUNT_LOGE("Domain account not found");
72         return HandleErrorWithEmptyResult(ERR_JS_ACCOUNT_NOT_FOUND, resultParcel);
73     }
74     errCode = IInnerOsAccountManager::GetInstance().BindDomainAccount(type_, domainAccountInfo,
75         osAccountInfo, accountOptions_);
76     if (errCode != ERR_OK) {
77         return HandleErrorWithEmptyResult(errCode, resultParcel);
78     }
79     auto callbackWrapper =
80         std::make_shared<BindDomainAccountCallback>(osAccountControl_, osAccountInfo, innerCallback_);
81     if (callbackWrapper == nullptr) {
82         ACCOUNT_LOGE("Create BindDomainAccountCallback failed");
83         return HandleErrorWithEmptyResult(ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR, resultParcel);
84     }
85     errCode = InnerDomainAccountManager::GetInstance().OnAccountBound(domainAccountInfo,
86         osAccountInfo.GetLocalId(), callbackWrapper);
87     if (errCode != ERR_OK) {
88         return HandleErrorWithEmptyResult(errCode, resultParcel);
89     }
90     return ERR_OK;
91 }
92 
BindDomainAccountCallback(std::shared_ptr<IOsAccountControl> & osAccountControl,const OsAccountInfo & osAccountInfo,const sptr<IDomainAccountCallback> & callback)93 BindDomainAccountCallback::BindDomainAccountCallback(
94     std::shared_ptr<IOsAccountControl> &osAccountControl, const OsAccountInfo &osAccountInfo,
95     const sptr<IDomainAccountCallback> &callback)
96     : osAccountControl_(osAccountControl), osAccountInfo_(osAccountInfo), innerCallback_(callback)
97 {}
98 
OnResult(int32_t errCode,Parcel & parcel)99 void BindDomainAccountCallback::OnResult(int32_t errCode, Parcel &parcel)
100 {
101     if (innerCallback_ == nullptr) {
102         ACCOUNT_LOGE("Inner callback is nullptr");
103         return;
104     }
105     if (errCode != ERR_OK) {
106         ACCOUNT_LOGE("Failed to bind domain account");
107         if (osAccountInfo_.GetLocalId() != Constants::START_USER_ID) {
108             (void)osAccountControl_->DelOsAccount(osAccountInfo_.GetLocalId());
109         }
110         DomainAccountParcel domainAccountResultParcel;
111         domainAccountResultParcel.SetParcelData(parcel);
112         innerCallback_->OnResult(errCode, domainAccountResultParcel);
113         return;
114     }
115     Parcel resultParcel;
116     if (osAccountInfo_.GetLocalId() != Constants::START_USER_ID) {
117         errCode = IInnerOsAccountManager::GetInstance().SendMsgForAccountCreate(osAccountInfo_);
118         if (errCode != ERR_OK) {
119             DomainAccountInfo curDomainInfo;
120             osAccountInfo_.GetDomainInfo(curDomainInfo);
121             if (InnerDomainAccountManager::GetInstance().OnAccountUnBound(curDomainInfo, nullptr,
122                 osAccountInfo_.GetLocalId()) == ERR_OK) {
123                 (void)osAccountControl_->DelOsAccount(osAccountInfo_.GetLocalId());
124             } else {
125                 ACCOUNT_LOGE("Failed to unbound domain account");
126             }
127         }
128         osAccountInfo_.Marshalling(resultParcel);
129         DomainAccountParcel domainAccountResultParcel;
130         domainAccountResultParcel.SetParcelData(resultParcel);
131         innerCallback_->OnResult(errCode, domainAccountResultParcel);
132         return;
133     }
134     if ((osAccountInfo_.GetLocalId() == Constants::START_USER_ID) && (errCode == ERR_OK)) {
135 #ifdef HAS_CES_PART
136     AccountEventProvider::EventPublish(
137         EventFwk::CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED, Constants::START_USER_ID, nullptr);
138 #else  // HAS_CES_PART
139     ACCOUNT_LOGI("No common event part! Publish nothing!");
140 #endif // HAS_CES_PART
141     }
142     osAccountInfo_.Marshalling(resultParcel);
143     DomainAccountParcel domainAccountResultParcel;
144     domainAccountResultParcel.SetParcelData(resultParcel);
145     innerCallback_->OnResult(errCode, domainAccountResultParcel);
146 }
147 } // namespace AccountSA
148 } // namespace OHOS