1 /*
2 * Copyright (c) 2021 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 "ohos_account_kits_impl.h"
17 #include "account_error_no.h"
18 #include "account_log_wrapper.h"
19 #include "account_proxy.h"
20 #include "if_system_ability_manager.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23
24 namespace OHOS {
25 namespace AccountSA {
OhosAccountKitsImpl()26 OhosAccountKitsImpl::OhosAccountKitsImpl() {}
~OhosAccountKitsImpl()27 OhosAccountKitsImpl::~OhosAccountKitsImpl() {}
28
ResetService(const wptr<IRemoteObject> & remote)29 void OhosAccountKitsImpl::ResetService(const wptr<IRemoteObject>& remote)
30 {
31 ACCOUNT_LOGI("Remote is dead, reset service instance");
32
33 std::lock_guard<std::mutex> lock(accountProxyLock_);
34 if (accountProxy_ != nullptr) {
35 sptr<IRemoteObject> object = accountProxy_->AsObject();
36 if ((object != nullptr) && (remote == object)) {
37 object->RemoveDeathRecipient(deathRecipient_);
38 accountProxy_ = nullptr;
39 }
40 }
41 }
42
GetService()43 sptr<IAccount> OhosAccountKitsImpl::GetService()
44 {
45 std::lock_guard<std::mutex> lock(accountProxyLock_);
46 if (accountProxy_ != nullptr) {
47 return accountProxy_;
48 }
49
50 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
51 if (samgr == nullptr) {
52 ACCOUNT_LOGE("Get samgr failed");
53 return nullptr;
54 }
55 sptr<IRemoteObject> object = samgr->GetSystemAbility(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN);
56 if (object == nullptr) {
57 ACCOUNT_LOGE("Get account object from samgr failed");
58 return nullptr;
59 }
60
61 if (deathRecipient_ == nullptr) {
62 deathRecipient_ = new (std::nothrow) DeathRecipient();
63 if (deathRecipient_ == nullptr) {
64 ACCOUNT_LOGE("deathRecipient_ is nullptr.");
65 return nullptr;
66 }
67 }
68
69 if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
70 ACCOUNT_LOGE("Failed to add death recipient");
71 }
72
73 accountProxy_ = iface_cast<AccountProxy>(object);
74 if (accountProxy_ == nullptr) {
75 ACCOUNT_LOGE("account iface_cast failed");
76 }
77 return accountProxy_;
78 }
79
OnRemoteDied(const wptr<IRemoteObject> & remote)80 void OhosAccountKitsImpl::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
81 {
82 DelayedRefSingleton<OhosAccountKitsImpl>::GetInstance().ResetService(remote);
83 }
84
UpdateOhosAccountInfo(const std::string & accountName,const std::string & uid,const std::string & eventStr)85 bool OhosAccountKitsImpl::UpdateOhosAccountInfo(const std::string& accountName, const std::string& uid,
86 const std::string& eventStr)
87 {
88 auto accountProxy = GetService();
89 if (accountProxy == nullptr) {
90 ACCOUNT_LOGE("Get proxy failed");
91 return false;
92 }
93 return accountProxy->UpdateOhosAccountInfo(accountName, uid, eventStr);
94 }
95
SetOhosAccountInfo(const OhosAccountInfo & ohosAccountInfo,const std::string & eventStr)96 std::int32_t OhosAccountKitsImpl::SetOhosAccountInfo(
97 const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr)
98 {
99 auto accountProxy = GetService();
100 if (accountProxy == nullptr) {
101 ACCOUNT_LOGE("Get proxy failed");
102 return ERR_ACCOUNT_ZIDL_ACCOUNT_PROXY_ERROR;
103 }
104 if (!ohosAccountInfo.IsValid()) {
105 ACCOUNT_LOGE("OhosAccountInfo check failed");
106 return ERR_OHOSACCOUNT_KIT_INVALID_PARAMETER;
107 }
108 return accountProxy->SetOhosAccountInfo(ohosAccountInfo, eventStr);
109 }
110
QueryOhosAccountInfo()111 std::pair<bool, OhosAccountInfo> OhosAccountKitsImpl::QueryOhosAccountInfo()
112 {
113 auto accountProxy = GetService();
114 if (accountProxy == nullptr) {
115 ACCOUNT_LOGE("Get proxy failed");
116 return std::make_pair(false, OhosAccountInfo());
117 }
118
119 return accountProxy->QueryOhosAccountInfo();
120 }
121
GetOhosAccountInfo(OhosAccountInfo & accountInfo)122 ErrCode OhosAccountKitsImpl::GetOhosAccountInfo(OhosAccountInfo &accountInfo)
123 {
124 auto accountProxy = GetService();
125 if (accountProxy == nullptr) {
126 ACCOUNT_LOGE("Get proxy failed");
127 return ERR_ACCOUNT_ZIDL_ACCOUNT_PROXY_ERROR;
128 }
129
130 return accountProxy->GetOhosAccountInfo(accountInfo);
131 }
132
GetOhosAccountInfoByUserId(int32_t userId,OhosAccountInfo & accountInfo)133 ErrCode OhosAccountKitsImpl::GetOhosAccountInfoByUserId(int32_t userId, OhosAccountInfo &accountInfo)
134 {
135 auto accountProxy = GetService();
136 if (accountProxy == nullptr) {
137 ACCOUNT_LOGE("Get proxy failed");
138 return ERR_ACCOUNT_ZIDL_ACCOUNT_PROXY_ERROR;
139 }
140
141 return accountProxy->GetOhosAccountInfoByUserId(userId, accountInfo);
142 }
143
QueryOhosAccountInfoByUserId(std::int32_t userId)144 std::pair<bool, OhosAccountInfo> OhosAccountKitsImpl::QueryOhosAccountInfoByUserId(std::int32_t userId)
145 {
146 auto accountProxy = GetService();
147 if (accountProxy == nullptr) {
148 ACCOUNT_LOGE("Get proxy failed");
149 return std::make_pair(false, OhosAccountInfo());
150 }
151
152 return accountProxy->QueryOhosAccountInfoByUserId(userId);
153 }
154
QueryDeviceAccountId(std::int32_t & accountId)155 ErrCode OhosAccountKitsImpl::QueryDeviceAccountId(std::int32_t& accountId)
156 {
157 auto accountProxy = GetService();
158 if (accountProxy == nullptr) {
159 ACCOUNT_LOGE("Get proxy failed");
160 return ERR_ACCOUNT_ZIDL_ACCOUNT_PROXY_ERROR;
161 }
162
163 return accountProxy->QueryDeviceAccountId(accountId);
164 }
165
GetDeviceAccountIdByUID(std::int32_t & uid)166 std::int32_t OhosAccountKitsImpl::GetDeviceAccountIdByUID(std::int32_t& uid)
167 {
168 std::int32_t accountID = uid / UID_TRANSFORM_DIVISOR;
169 return accountID;
170 }
171
GetDomainAccountService()172 sptr<IRemoteObject> OhosAccountKitsImpl::GetDomainAccountService()
173 {
174 auto accountProxy = GetService();
175 if (accountProxy == nullptr) {
176 ACCOUNT_LOGE("Get proxy failed");
177 return nullptr;
178 }
179 return accountProxy->GetDomainAccountService();
180 }
181 } // namespace AccountSA
182 } // namespace OHOS
183