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 &dvid) {
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     OhosAccountInfo accountInfo;
144     ErrCode result = QueryOhosAccountInfo(accountInfo);
145     return std::make_pair(result == ERR_OK, accountInfo);
146 }
147 
QueryDistributedVirtualDeviceId(std::string & dvid)148 ErrCode OhosAccountKitsImpl::QueryDistributedVirtualDeviceId(std::string &dvid)
149 {
150     auto accountProxy = GetService();
151     if (accountProxy == nullptr) {
152         ACCOUNT_LOGE("Get proxy failed");
153         return ERR_ACCOUNT_COMMON_GET_PROXY;
154     }
155 
156     return accountProxy->QueryDistributedVirtualDeviceId(dvid);
157 }
158 
QueryOhosAccountInfo(OhosAccountInfo & accountInfo)159 ErrCode OhosAccountKitsImpl::QueryOhosAccountInfo(OhosAccountInfo &accountInfo)
160 {
161     auto accountProxy = GetService();
162     if (accountProxy == nullptr) {
163         ACCOUNT_LOGE("Get proxy failed");
164         return ERR_ACCOUNT_COMMON_GET_PROXY;
165     }
166 
167     return accountProxy->QueryOhosAccountInfo(accountInfo);
168 }
169 
GetOhosAccountInfo(OhosAccountInfo & accountInfo)170 ErrCode OhosAccountKitsImpl::GetOhosAccountInfo(OhosAccountInfo &accountInfo)
171 {
172     auto accountProxy = GetService();
173     if (accountProxy == nullptr) {
174         ACCOUNT_LOGE("Get proxy failed");
175         return ERR_ACCOUNT_COMMON_GET_PROXY;
176     }
177 
178     return accountProxy->GetOhosAccountInfo(accountInfo);
179 }
180 
GetOhosAccountInfoByUserId(int32_t userId,OhosAccountInfo & accountInfo)181 ErrCode OhosAccountKitsImpl::GetOhosAccountInfoByUserId(int32_t userId, OhosAccountInfo &accountInfo)
182 {
183     auto accountProxy = GetService();
184     if (accountProxy == nullptr) {
185         ACCOUNT_LOGE("Get proxy failed");
186         return ERR_ACCOUNT_COMMON_GET_PROXY;
187     }
188 
189     return accountProxy->GetOhosAccountInfoByUserId(userId, accountInfo);
190 }
191 
QueryOhosAccountInfoByUserId(std::int32_t userId)192 std::pair<bool, OhosAccountInfo> OhosAccountKitsImpl::QueryOhosAccountInfoByUserId(std::int32_t userId)
193 {
194     OhosAccountInfo accountInfo;
195     ErrCode result = QueryOhosAccountInfoByUserId(userId, accountInfo);
196     return std::make_pair(result == ERR_OK, accountInfo);
197 }
198 
QueryOhosAccountInfoByUserId(std::int32_t userId,OhosAccountInfo & accountInfo)199 ErrCode OhosAccountKitsImpl::QueryOhosAccountInfoByUserId(std::int32_t userId, OhosAccountInfo &accountInfo)
200 {
201     auto accountProxy = GetService();
202     if (accountProxy == nullptr) {
203         ACCOUNT_LOGE("Get proxy failed");
204         return ERR_ACCOUNT_COMMON_GET_PROXY;
205     }
206 
207     return accountProxy->QueryOhosAccountInfoByUserId(userId, accountInfo);
208 }
209 
QueryDeviceAccountId(std::int32_t & accountId)210 ErrCode OhosAccountKitsImpl::QueryDeviceAccountId(std::int32_t& accountId)
211 {
212     auto accountProxy = GetService();
213     if (accountProxy == nullptr) {
214         ACCOUNT_LOGE("Get proxy failed");
215         return ERR_ACCOUNT_COMMON_GET_PROXY;
216     }
217 
218     return accountProxy->QueryDeviceAccountId(accountId);
219 }
220 
SubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,const std::shared_ptr<DistributedAccountSubscribeCallback> & callback)221 ErrCode OhosAccountKitsImpl::SubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
222     const std::shared_ptr<DistributedAccountSubscribeCallback> &callback)
223 {
224     ACCOUNT_LOGI("Subscribe distributed account event in client.");
225     if (callback == nullptr) {
226         ACCOUNT_LOGE("Callback is nullptr.");
227         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
228     }
229 
230     auto accountProxy = GetService();
231     if (accountProxy == nullptr) {
232         ACCOUNT_LOGE("Get proxy failed.");
233         return ERR_ACCOUNT_COMMON_GET_PROXY;
234     }
235 
236     sptr<IRemoteObject> listener = nullptr;
237     ErrCode result = CreateDistributedAccountEventService(type, callback, listener);
238 
239     if (listener == nullptr) {
240         ACCOUNT_LOGE("Create event service failed.");
241         return ERR_OHOSACCOUNT_KIT_SUBSCRIBE_ERROR;
242     }
243     if (result == ERR_OHOSACCOUNT_KIT_CALLBACK_ALREADY_REGISTERED_ERROR) {
244         ACCOUNT_LOGE("Callback already registered.");
245         return ERR_OK;
246     }
247 
248     result = accountProxy->SubscribeDistributedAccountEvent(type, listener);
249     if (result != ERR_OK) {
250         std::lock_guard<std::mutex> lock(eventListenersMutex_);
251         eventListeners_.erase(callback);
252     }
253     return result;
254 }
255 
UnsubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,const std::shared_ptr<DistributedAccountSubscribeCallback> & callback)256 ErrCode OhosAccountKitsImpl::UnsubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
257     const std::shared_ptr<DistributedAccountSubscribeCallback> &callback)
258 {
259     ACCOUNT_LOGI("Unsubscribe distributed account event in client.");
260     if (callback == nullptr) {
261         ACCOUNT_LOGE("Callback is nullptr.");
262         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
263     }
264 
265     auto accountProxy = GetService();
266     if (accountProxy == nullptr) {
267         ACCOUNT_LOGE("Get proxy failed.");
268         return ERR_ACCOUNT_COMMON_GET_PROXY;
269     }
270 
271     std::lock_guard<std::mutex> lock(eventListenersMutex_);
272     auto eventListener = eventListeners_.find(callback);
273     if (eventListener == eventListeners_.end()) {
274         ACCOUNT_LOGE("No specified callback has been registered.");
275         return ERR_OHOSACCOUNT_KIT_NO_SPECIFIED_CALLBACK_HAS_BEEN_REGISTERED;
276     }
277 
278     if (!(eventListener->second->IsTypeExist(type))) {
279         ACCOUNT_LOGE("No specified callback has been registered.");
280         return ERR_OHOSACCOUNT_KIT_NO_SPECIFIED_CALLBACK_HAS_BEEN_REGISTERED;
281     }
282     ErrCode result = accountProxy->UnsubscribeDistributedAccountEvent(type, eventListener->second->AsObject());
283     if (result == ERR_OK) {
284         eventListener->second->DeleteType(type);
285         if (eventListener->second->GetTypeSize() == 0) {
286             eventListeners_.erase(eventListener);
287         }
288     }
289     return result;
290 }
291 
CreateDistributedAccountEventService(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,const std::shared_ptr<DistributedAccountSubscribeCallback> & callback,sptr<IRemoteObject> & subscribeListener)292 ErrCode OhosAccountKitsImpl::CreateDistributedAccountEventService(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
293     const std::shared_ptr<DistributedAccountSubscribeCallback> &callback,
294     sptr<IRemoteObject> &subscribeListener)
295 {
296     if (callback == nullptr) {
297         ACCOUNT_LOGE("Callback is nullptr");
298         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
299     }
300 
301     std::lock_guard<std::mutex> lock(eventListenersMutex_);
302     auto eventListener = eventListeners_.find(callback);
303     if (eventListener != eventListeners_.end()) {
304         subscribeListener = eventListener->second->AsObject();
305         if (eventListener->second->IsTypeExist(type)) {
306             ACCOUNT_LOGI("Callback already has distributed account event listener.");
307             return ERR_OHOSACCOUNT_KIT_CALLBACK_ALREADY_REGISTERED_ERROR;
308         }
309         eventListener->second->AddType(type);
310         return ERR_OK;
311     }
312     if (eventListeners_.size() == Constants::DISTRIBUTED_SUBSCRIBER_MAX_SIZE) {
313         ACCOUNT_LOGE("The maximum number of eventListeners has been reached.");
314         return ERR_OHOSACCOUNT_KIT_SUBSCRIBE_MAX_SIZE_ERROR;
315     }
316     sptr<DistributedAccountEventService> listener = new (std::nothrow) DistributedAccountEventService(
317         type, callback);
318     if (listener == nullptr) {
319         ACCOUNT_LOGE("Memory allocation for listener failed!");
320         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
321     }
322     subscribeListener = listener->AsObject();
323     eventListeners_[callback] = listener;
324     return ERR_OK;
325 }
326 
RestoreSubscribe()327 void OhosAccountKitsImpl::RestoreSubscribe()
328 {
329     auto accountProxy = GetService();
330     if (accountProxy == nullptr) {
331         ACCOUNT_LOGE("Get proxy failed.");
332         return ;
333     }
334 
335     std::lock_guard<std::mutex> lock(eventListenersMutex_);
336     for (auto it = eventListeners_.begin(); it != eventListeners_.end(); ++it) {
337         std::vector<DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE> typeList;
338         it->second->GetAllType(typeList);
339         for (auto type : typeList) {
340             ErrCode subscribeState = accountProxy->SubscribeDistributedAccountEvent(type, it->second);
341             if (subscribeState != ERR_OK) {
342                 ACCOUNT_LOGE("Restore subscribe failed, res=%{public}d.", subscribeState);
343             }
344         }
345     }
346 }
347 
GetDeviceAccountIdByUID(std::int32_t & uid)348 std::int32_t OhosAccountKitsImpl::GetDeviceAccountIdByUID(std::int32_t& uid)
349 {
350     std::int32_t accountID = uid / UID_TRANSFORM_DIVISOR;
351     return accountID;
352 }
353 
SubscribeSystemAbility(const DomainAccountSubscribeSACallbackFunc & callbackFunc)354 ErrCode OhosAccountKitsImpl::SubscribeSystemAbility(const DomainAccountSubscribeSACallbackFunc& callbackFunc)
355 {
356     sptr<ISystemAbilityStatusChange> statusChangeListener =
357         new (std::nothrow) SystemAbilityStatusChangeListener(callbackFunc);
358     if (statusChangeListener == nullptr) {
359         ACCOUNT_LOGE("statusChangeListener is nullptr");
360         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
361     }
362     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
363     if (samgrProxy == NULL) {
364         ACCOUNT_LOGE("samgrProxy is NULL");
365         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
366     }
367     int32_t ret = samgrProxy->SubscribeSystemAbility(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN, statusChangeListener);
368     if (ret != ERR_OK) {
369         ACCOUNT_LOGE("SubscribeSystemAbility is failed");
370         return ret;
371     }
372     return ERR_OK;
373 }
374 
GetDomainAccountService()375 sptr<IRemoteObject> OhosAccountKitsImpl::GetDomainAccountService()
376 {
377     auto accountProxy = GetService();
378     if (accountProxy == nullptr) {
379         ACCOUNT_LOGE("Get proxy failed");
380         return nullptr;
381     }
382     return accountProxy->GetDomainAccountService();
383 }
384 
GetOsAccountService()385 sptr<IRemoteObject> OhosAccountKitsImpl::GetOsAccountService()
386 {
387     auto accountProxy = GetService();
388     if (accountProxy == nullptr) {
389         ACCOUNT_LOGE("Get proxy failed");
390         return nullptr;
391     }
392     return accountProxy->GetOsAccountService();
393 }
394 
GetAppAccountService()395 sptr<IRemoteObject> OhosAccountKitsImpl::GetAppAccountService()
396 {
397     auto accountProxy = GetService();
398     if (accountProxy == nullptr) {
399         ACCOUNT_LOGE("Get proxy failed");
400         return nullptr;
401     }
402     return accountProxy->GetAppAccountService();
403 }
404 
GetAccountIAMService()405 sptr<IRemoteObject> OhosAccountKitsImpl::GetAccountIAMService()
406 {
407     auto accountProxy = GetService();
408     if (accountProxy == nullptr) {
409         ACCOUNT_LOGE("Get proxy failed");
410         return nullptr;
411     }
412     return accountProxy->GetAccountIAMService();
413 }
414 } // namespace AccountSA
415 } // namespace OHOS
416