1 /*
2 * Copyright (c) 2022 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 "account_observer.h"
17 #include "memmgr_log.h"
18
19 #include "os_account_manager.h"
20 #include "os_account_info.h"
21
22 namespace OHOS {
23 namespace Memory {
24 namespace {
25 const std::string TAG = "AccountObserver";
26 const int MAX_RETRY_TIMES = 10;
27 }
28
AccountObserver(const AccountCallback & callback)29 AccountObserver::AccountObserver(const AccountCallback &callback) : callback_(callback)
30 {
31 HILOGI("called");
32 if (!GetEventHandler()) {
33 return;
34 }
35 Register();
36 }
37
GetEventHandler()38 bool AccountObserver::GetEventHandler()
39 {
40 if (!handler_) {
41 handler_ = std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::Create());
42 if (handler_ == nullptr) {
43 HILOGI("failed to create event handler");
44 return false;
45 }
46 }
47 return true;
48 }
49
Register()50 void AccountObserver::Register()
51 {
52 retryTimes_++;
53
54 std::vector<AccountSA::OsAccountInfo> osAccountInfos;
55 ErrCode errCode = AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos);
56 if (errCode == ERR_OK) {
57 HILOGI("--------------------");
58 for (size_t i = 0; i < osAccountInfos.size(); ++i) {
59 AccountSA::OsAccountInfo &osAccountInfo = osAccountInfos[i];
60 HILOGI("OsAccount: id=%{public}d, name=%{public}s, toString=%{public}s",
61 osAccountInfo.GetLocalId(), osAccountInfo.GetLocalName().c_str(), osAccountInfo.ToString().c_str());
62 }
63 HILOGI("--------------------");
64 }
65
66 AccountSA::OsAccountSubscribeInfo osAccountSubscribeInfo;
67 osAccountSubscribeInfo.SetOsAccountSubscribeType(AccountSA::OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVED);
68 osAccountSubscribeInfo.SetName("MemMgrAccountActivedSubscriber");
69
70 subscriber_ = std::make_shared<AccountSubscriber>(osAccountSubscribeInfo,
71 std::bind(&AccountObserver::OnAccountsChanged, this, std::placeholders::_1));
72 ErrCode errCode2 = AccountSA::OsAccountManager::SubscribeOsAccount(subscriber_);
73 HILOGI("SubscribeOsAccount errCode=%{public}d", errCode2);
74
75 if (errCode2 == ERR_OK)
76 return;
77
78 if (retryTimes_ < MAX_RETRY_TIMES) {
79 std::function<void()> RegisterEventListenerFunc = std::bind(&AccountObserver::Register, this);
80 HILOGE("failed to SubscribeOsAccount, try again after 3s!, retryTimes=%{public}d/10", retryTimes_);
81 handler_->PostTask(RegisterEventListenerFunc, 3000, AppExecFwk::EventQueue::Priority::LOW); // 3000 means 3s
82 }
83 }
84
~AccountObserver()85 AccountObserver::~AccountObserver()
86 {
87 if (subscriber_) {
88 AccountSA::OsAccountManager::UnsubscribeOsAccount(subscriber_);
89 }
90 }
91
OnAccountsChanged(const int & id)92 void AccountObserver::OnAccountsChanged(const int &id)
93 {
94 HILOGI("called");
95 if (callback_.OnOsAccountsChanged != nullptr) {
96 callback_.OnOsAccountsChanged(id);
97 }
98 }
99 } // namespace Memory
100 } // namespace OHOS
101