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 "system_ability_status_change_listener.h"
22
23 namespace OHOS {
24 namespace AccountSA {
ohosCallbackFunc()25 std::function<void(int32_t, const std::string &)> ohosCallbackFunc()
26 {
27 return [](int32_t systemAbilityId, const std::string &deviceId) {
28 if (systemAbilityId == SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN) {
29 OhosAccountKitsImpl::GetInstance().RestoreSubscribe();
30 }
31 };
32 }
33
GetInstance()34 OhosAccountKitsImpl &OhosAccountKitsImpl::GetInstance()
35 {
36 static OhosAccountKitsImpl *instance = new (std::nothrow) OhosAccountKitsImpl();
37 return *instance;
38 }
39
ResetService(const wptr<IRemoteObject> & remote)40 void OhosAccountKitsImpl::ResetService(const wptr<IRemoteObject>& remote)
41 {
42 ACCOUNT_LOGI("Remote is dead, reset service instance");
43
44 std::lock_guard<std::mutex> lock(accountProxyLock_);
45 if (accountProxy_ != nullptr) {
46 sptr<IRemoteObject> object = accountProxy_->AsObject();
47 if ((object != nullptr) && (remote == object)) {
48 object->RemoveDeathRecipient(deathRecipient_);
49 accountProxy_ = nullptr;
50 }
51 }
52 if (!isSubscribeSA_) {
53 isSubscribeSA_ = true;
54 SubscribeSystemAbility(ohosCallbackFunc());
55 }
56 }
57
GetService()58 sptr<IAccount> OhosAccountKitsImpl::GetService()
59 {
60 std::lock_guard<std::mutex> lock(accountProxyLock_);
61 if (accountProxy_ != nullptr) {
62 return accountProxy_;
63 }
64
65 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
66 if (samgr == nullptr) {
67 ACCOUNT_LOGE("Get samgr failed");
68 return nullptr;
69 }
70 sptr<IRemoteObject> object = samgr->GetSystemAbility(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN);
71 if (object == nullptr) {
72 ACCOUNT_LOGE("Get account object from samgr failed");
73 return nullptr;
74 }
75
76 if (deathRecipient_ == nullptr) {
77 deathRecipient_ = new (std::nothrow) DeathRecipient();
78 if (deathRecipient_ == nullptr) {
79 ACCOUNT_LOGE("deathRecipient_ is nullptr.");
80 return nullptr;
81 }
82 }
83
84 if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
85 ACCOUNT_LOGE("Failed to add death recipient");
86 }
87
88 accountProxy_ = iface_cast<AccountProxy>(object);
89 if (accountProxy_ == nullptr) {
90 ACCOUNT_LOGE("account iface_cast failed");
91 }
92 return accountProxy_;
93 }
94
OnRemoteDied(const wptr<IRemoteObject> & remote)95 void OhosAccountKitsImpl::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
96 {
97 OhosAccountKitsImpl::GetInstance().ResetService(remote);
98 }
99
UpdateOhosAccountInfo(const std::string & accountName,const std::string & uid,const std::string & eventStr)100 bool OhosAccountKitsImpl::UpdateOhosAccountInfo(const std::string& accountName, const std::string& uid,
101 const std::string& eventStr)
102 {
103 auto accountProxy = GetService();
104 if (accountProxy == nullptr) {
105 ACCOUNT_LOGE("Get proxy failed");
106 return false;
107 }
108 return accountProxy->UpdateOhosAccountInfo(accountName, uid, eventStr);
109 }
110
SetOhosAccountInfo(const OhosAccountInfo & ohosAccountInfo,const std::string & eventStr)111 std::int32_t OhosAccountKitsImpl::SetOhosAccountInfo(
112 const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr)
113 {
114 auto accountProxy = GetService();
115 if (accountProxy == nullptr) {
116 ACCOUNT_LOGE("Get proxy failed");
117 return ERR_ACCOUNT_COMMON_GET_PROXY;
118 }
119 if (!ohosAccountInfo.IsValid()) {
120 ACCOUNT_LOGE("OhosAccountInfo check failed");
121 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
122 }
123 return accountProxy->SetOhosAccountInfo(ohosAccountInfo, eventStr);
124 }
125
SetOhosAccountInfoByUserId(const int32_t userId,const OhosAccountInfo & ohosAccountInfo,const std::string & eventStr)126 ErrCode OhosAccountKitsImpl::SetOhosAccountInfoByUserId(
127 const int32_t userId, const OhosAccountInfo& ohosAccountInfo, const std::string& eventStr)
128 {
129 auto accountProxy = GetService();
130 if (accountProxy == nullptr) {
131 ACCOUNT_LOGE("Get proxy failed");
132 return ERR_ACCOUNT_COMMON_GET_PROXY;
133 }
134 if (!ohosAccountInfo.IsValid()) {
135 ACCOUNT_LOGE("OhosAccountInfo check failed");
136 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
137 }
138 return accountProxy->SetOhosAccountInfoByUserId(userId, ohosAccountInfo, eventStr);
139 }
140
QueryOhosAccountInfo()141 std::pair<bool, OhosAccountInfo> OhosAccountKitsImpl::QueryOhosAccountInfo()
142 {
143 auto accountProxy = GetService();
144 if (accountProxy == nullptr) {
145 ACCOUNT_LOGE("Get proxy failed");
146 return std::make_pair(false, OhosAccountInfo());
147 }
148
149 return accountProxy->QueryOhosAccountInfo();
150 }
151
GetOhosAccountInfo(OhosAccountInfo & accountInfo)152 ErrCode OhosAccountKitsImpl::GetOhosAccountInfo(OhosAccountInfo &accountInfo)
153 {
154 auto accountProxy = GetService();
155 if (accountProxy == nullptr) {
156 ACCOUNT_LOGE("Get proxy failed");
157 return ERR_ACCOUNT_COMMON_GET_PROXY;
158 }
159
160 return accountProxy->GetOhosAccountInfo(accountInfo);
161 }
162
GetOhosAccountInfoByUserId(int32_t userId,OhosAccountInfo & accountInfo)163 ErrCode OhosAccountKitsImpl::GetOhosAccountInfoByUserId(int32_t userId, OhosAccountInfo &accountInfo)
164 {
165 auto accountProxy = GetService();
166 if (accountProxy == nullptr) {
167 ACCOUNT_LOGE("Get proxy failed");
168 return ERR_ACCOUNT_COMMON_GET_PROXY;
169 }
170
171 return accountProxy->GetOhosAccountInfoByUserId(userId, accountInfo);
172 }
173
QueryOhosAccountInfoByUserId(std::int32_t userId)174 std::pair<bool, OhosAccountInfo> OhosAccountKitsImpl::QueryOhosAccountInfoByUserId(std::int32_t userId)
175 {
176 auto accountProxy = GetService();
177 if (accountProxy == nullptr) {
178 ACCOUNT_LOGE("Get proxy failed");
179 return std::make_pair(false, OhosAccountInfo());
180 }
181
182 return accountProxy->QueryOhosAccountInfoByUserId(userId);
183 }
184
QueryDeviceAccountId(std::int32_t & accountId)185 ErrCode OhosAccountKitsImpl::QueryDeviceAccountId(std::int32_t& accountId)
186 {
187 auto accountProxy = GetService();
188 if (accountProxy == nullptr) {
189 ACCOUNT_LOGE("Get proxy failed");
190 return ERR_ACCOUNT_COMMON_GET_PROXY;
191 }
192
193 return accountProxy->QueryDeviceAccountId(accountId);
194 }
195
SubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,const std::shared_ptr<DistributedAccountSubscribeCallback> & callback)196 ErrCode OhosAccountKitsImpl::SubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
197 const std::shared_ptr<DistributedAccountSubscribeCallback> &callback)
198 {
199 ACCOUNT_LOGI("Subscribe distributed account event in client.");
200 if (callback == nullptr) {
201 ACCOUNT_LOGE("Callback is nullptr.");
202 return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
203 }
204
205 auto accountProxy = GetService();
206 if (accountProxy == nullptr) {
207 ACCOUNT_LOGE("Get proxy failed.");
208 return ERR_ACCOUNT_COMMON_GET_PROXY;
209 }
210
211 sptr<IRemoteObject> listener = nullptr;
212 ErrCode result = CreateDistributedAccountEventService(type, callback, listener);
213
214 if (listener == nullptr) {
215 ACCOUNT_LOGE("Create event service failed.");
216 return ERR_OHOSACCOUNT_KIT_SUBSCRIBE_ERROR;
217 }
218 if (result == ERR_OHOSACCOUNT_KIT_CALLBACK_ALREADY_REGISTERED_ERROR) {
219 ACCOUNT_LOGE("Callback already registered.");
220 return ERR_OK;
221 }
222
223 result = accountProxy->SubscribeDistributedAccountEvent(type, listener);
224 if (result != ERR_OK) {
225 std::lock_guard<std::mutex> lock(eventListenersMutex_);
226 eventListeners_.erase(callback);
227 }
228 return result;
229 }
230
UnsubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,const std::shared_ptr<DistributedAccountSubscribeCallback> & callback)231 ErrCode OhosAccountKitsImpl::UnsubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
232 const std::shared_ptr<DistributedAccountSubscribeCallback> &callback)
233 {
234 ACCOUNT_LOGI("Unsubscribe distributed account event in client.");
235 if (callback == nullptr) {
236 ACCOUNT_LOGE("Callback is nullptr.");
237 return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
238 }
239
240 auto accountProxy = GetService();
241 if (accountProxy == nullptr) {
242 ACCOUNT_LOGE("Get proxy failed.");
243 return ERR_ACCOUNT_COMMON_GET_PROXY;
244 }
245
246 std::lock_guard<std::mutex> lock(eventListenersMutex_);
247 auto eventListener = eventListeners_.find(callback);
248 if (eventListener == eventListeners_.end()) {
249 ACCOUNT_LOGE("No specified callback has been registered.");
250 return ERR_OHOSACCOUNT_KIT_NO_SPECIFIED_CALLBACK_HAS_BEEN_REGISTERED;
251 }
252
253 if (!(eventListener->second->IsTypeExist(type))) {
254 ACCOUNT_LOGE("No specified callback has been registered.");
255 return ERR_OHOSACCOUNT_KIT_NO_SPECIFIED_CALLBACK_HAS_BEEN_REGISTERED;
256 }
257 ErrCode result = accountProxy->UnsubscribeDistributedAccountEvent(type, eventListener->second->AsObject());
258 if (result == ERR_OK) {
259 eventListener->second->DeleteType(type);
260 if (eventListener->second->GetTypeSize() == 0) {
261 eventListeners_.erase(eventListener);
262 }
263 }
264 return result;
265 }
266
CreateDistributedAccountEventService(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,const std::shared_ptr<DistributedAccountSubscribeCallback> & callback,sptr<IRemoteObject> & subscribeListener)267 ErrCode OhosAccountKitsImpl::CreateDistributedAccountEventService(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
268 const std::shared_ptr<DistributedAccountSubscribeCallback> &callback,
269 sptr<IRemoteObject> &subscribeListener)
270 {
271 if (callback == nullptr) {
272 ACCOUNT_LOGE("Callback is nullptr");
273 return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
274 }
275
276 std::lock_guard<std::mutex> lock(eventListenersMutex_);
277 auto eventListener = eventListeners_.find(callback);
278 if (eventListener != eventListeners_.end()) {
279 subscribeListener = eventListener->second->AsObject();
280 if (eventListener->second->IsTypeExist(type)) {
281 ACCOUNT_LOGI("Callback already has distributed account event listener.");
282 return ERR_OHOSACCOUNT_KIT_CALLBACK_ALREADY_REGISTERED_ERROR;
283 }
284 eventListener->second->AddType(type);
285 return ERR_OK;
286 }
287 if (eventListeners_.size() == Constants::DISTRIBUTED_SUBSCRIBER_MAX_SIZE) {
288 ACCOUNT_LOGE("The maximum number of eventListeners has been reached.");
289 return ERR_OHOSACCOUNT_KIT_SUBSCRIBE_MAX_SIZE_ERROR;
290 }
291 sptr<DistributedAccountEventService> listener = new (std::nothrow) DistributedAccountEventService(
292 type, callback);
293 if (listener == nullptr) {
294 ACCOUNT_LOGE("Memory allocation for listener failed!");
295 return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
296 }
297 subscribeListener = listener->AsObject();
298 eventListeners_[callback] = listener;
299 return ERR_OK;
300 }
301
RestoreSubscribe()302 void OhosAccountKitsImpl::RestoreSubscribe()
303 {
304 auto accountProxy = GetService();
305 if (accountProxy == nullptr) {
306 ACCOUNT_LOGE("Get proxy failed.");
307 return ;
308 }
309
310 std::lock_guard<std::mutex> lock(eventListenersMutex_);
311 for (auto it = eventListeners_.begin(); it != eventListeners_.end(); ++it) {
312 std::vector<DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE> typeList;
313 it->second->GetAllType(typeList);
314 for (auto type : typeList) {
315 ErrCode subscribeState = accountProxy->SubscribeDistributedAccountEvent(type, it->second);
316 if (subscribeState != ERR_OK) {
317 ACCOUNT_LOGE("Restore subscribe failed, res=%{public}d.", subscribeState);
318 }
319 }
320 }
321 }
322
GetDeviceAccountIdByUID(std::int32_t & uid)323 std::int32_t OhosAccountKitsImpl::GetDeviceAccountIdByUID(std::int32_t& uid)
324 {
325 std::int32_t accountID = uid / UID_TRANSFORM_DIVISOR;
326 return accountID;
327 }
328
SubscribeSystemAbility(const DomainAccountSubscribeSACallbackFunc & callbackFunc)329 ErrCode OhosAccountKitsImpl::SubscribeSystemAbility(const DomainAccountSubscribeSACallbackFunc& callbackFunc)
330 {
331 sptr<ISystemAbilityStatusChange> statusChangeListener =
332 new (std::nothrow) SystemAbilityStatusChangeListener(callbackFunc);
333 if (statusChangeListener == nullptr) {
334 ACCOUNT_LOGE("statusChangeListener is nullptr");
335 return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
336 }
337 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
338 if (samgrProxy == NULL) {
339 ACCOUNT_LOGE("samgrProxy is NULL");
340 return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
341 }
342 int32_t ret = samgrProxy->SubscribeSystemAbility(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN, statusChangeListener);
343 if (ret != ERR_OK) {
344 ACCOUNT_LOGE("SubscribeSystemAbility is failed");
345 return ret;
346 }
347 return ERR_OK;
348 }
349
GetDomainAccountService()350 sptr<IRemoteObject> OhosAccountKitsImpl::GetDomainAccountService()
351 {
352 auto accountProxy = GetService();
353 if (accountProxy == nullptr) {
354 ACCOUNT_LOGE("Get proxy failed");
355 return nullptr;
356 }
357 return accountProxy->GetDomainAccountService();
358 }
359
GetOsAccountService()360 sptr<IRemoteObject> OhosAccountKitsImpl::GetOsAccountService()
361 {
362 auto accountProxy = GetService();
363 if (accountProxy == nullptr) {
364 ACCOUNT_LOGE("Get proxy failed");
365 return nullptr;
366 }
367 return accountProxy->GetOsAccountService();
368 }
369
GetAppAccountService()370 sptr<IRemoteObject> OhosAccountKitsImpl::GetAppAccountService()
371 {
372 auto accountProxy = GetService();
373 if (accountProxy == nullptr) {
374 ACCOUNT_LOGE("Get proxy failed");
375 return nullptr;
376 }
377 return accountProxy->GetAppAccountService();
378 }
379
GetAccountIAMService()380 sptr<IRemoteObject> OhosAccountKitsImpl::GetAccountIAMService()
381 {
382 auto accountProxy = GetService();
383 if (accountProxy == nullptr) {
384 ACCOUNT_LOGE("Get proxy failed");
385 return nullptr;
386 }
387 return accountProxy->GetAccountIAMService();
388 }
389 } // namespace AccountSA
390 } // namespace OHOS
391