1 /*
2 * Copyright (c) 2021-2023 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 "app_account_common_event_observer.h"
17 #include <pthread.h>
18 #include <thread>
19 #include <unistd.h>
20 #include "account_log_wrapper.h"
21 #include "app_account_control_manager.h"
22 #include "bundle_constants.h"
23 #ifdef HAS_CES_PART
24 #include "common_event_manager.h"
25 #include "common_event_support.h"
26 #endif // HAS_CES_PART
27 #include "os_account_state_subscriber.h"
28 #include "os_account_subscribe_manager.h"
29
30 #ifdef HAS_CES_PART
31 using namespace OHOS::EventFwk;
32 #endif // HAS_CES_PART
33
34 namespace OHOS {
35 namespace AccountSA {
36 #ifdef HAS_CES_PART
37 namespace {
38 const char THREAD_COMMON_EVENT[] = "commonEvent";
39 constexpr int32_t DELAY_FOR_TIME_INTERVAL = 1 * 1000;
40 constexpr int32_t MAX_TRY_TIMES = 10;
41 }
42
GetInstance()43 AppAccountCommonEventObserver &AppAccountCommonEventObserver::GetInstance()
44 {
45 static AppAccountCommonEventObserver *instance = new (std::nothrow) AppAccountCommonEventObserver();
46 return *instance;
47 }
48
AppAccountCommonEventObserver()49 AppAccountCommonEventObserver::AppAccountCommonEventObserver()
50 {
51 ACCOUNT_LOGI("Constructed");
52 counter_ = 0;
53 MatchingSkills matchingSkills;
54 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
55 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
56 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED);
57 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED);
58
59 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
60 subscriber_ = std::make_shared<AppAccountCommonEventSubscriber>(
61 subscribeInfo, [this] (const CommonEventData &data) { this->OnReceiveEvent(data); });
62
63 auto task = [this] {
64 this->SubscribeCommonEvent();
65 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
66 this->SubscribeOsAccountEvent();
67 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
68 };
69 std::thread taskThread(task);
70 pthread_setname_np(taskThread.native_handle(), THREAD_COMMON_EVENT);
71 taskThread.detach();
72 }
73
~AppAccountCommonEventObserver()74 AppAccountCommonEventObserver::~AppAccountCommonEventObserver()
75 {
76 ACCOUNT_LOGI("Destroyed");
77 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
78 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
79 OsAccountSubscribeManager::GetInstance().UnsubscribeOsAccount(subscriberOsAccountPtr_);
80 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
81 }
82
SubscribeOsAccountEvent(void)83 void AppAccountCommonEventObserver::SubscribeOsAccountEvent(void)
84 {
85 std::set<OsAccountState> states = { OsAccountState::STOPPING };
86 OsAccountSubscribeInfo subscribeInfo(states, true);
87 subscriberOsAccountPtr_ = (new (std::nothrow) OsAccountStateSubscriber());
88 auto subscribeInfoPtr = std::make_shared<OsAccountSubscribeInfo>(subscribeInfo);
89 if (subscribeInfoPtr == nullptr) {
90 ACCOUNT_LOGE("SubscribeInfoPtr is nullptr");
91 }
92 OsAccountSubscribeManager::GetInstance().SubscribeOsAccount(subscribeInfoPtr, subscriberOsAccountPtr_);
93 }
94
SubscribeCommonEvent(void)95 void AppAccountCommonEventObserver::SubscribeCommonEvent(void)
96 {
97 while (counter_ != MAX_TRY_TIMES) {
98 if (CommonEventManager::SubscribeCommonEvent(subscriber_)) {
99 ACCOUNT_LOGI("Successfully");
100 counter_ = 0;
101 break;
102 }
103 if (++counter_ == MAX_TRY_TIMES) {
104 ACCOUNT_LOGE("failed to subscribe common event and tried %{public}d times", counter_);
105 }
106 sleep(DELAY_FOR_TIME_INTERVAL / 1000); // 1000: 1s
107 }
108 }
109
OnReceiveEvent(const CommonEventData & data)110 void AppAccountCommonEventObserver::OnReceiveEvent(const CommonEventData &data)
111 {
112 auto want = data.GetWant();
113 std::string action = want.GetAction();
114 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED ||
115 action == CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) {
116 DealWithRemoveEvent(want, action);
117 return;
118 }
119 if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
120 AppAccountControlManager::GetInstance().OnUserRemoved(data.GetCode());
121 return;
122 }
123 if (action == CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
124 AppAccountControlManager::GetInstance().AddMigratedAccount(data.GetCode());
125 }
126 }
127
DealWithRemoveEvent(const AAFwk::Want & want,const std::string action)128 void AppAccountCommonEventObserver::DealWithRemoveEvent(const AAFwk::Want &want, const std::string action)
129 {
130 auto element = want.GetElement();
131 std::string bundleName = element.GetBundleName();
132 auto uid = want.GetIntParam(AppExecFwk::Constants::UID, -1);
133 int32_t appIndex = 0;
134 if (action == CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) {
135 appIndex = want.GetIntParam(AppExecFwk::Constants::SANDBOX_APP_INDEX, -1);
136 } else {
137 appIndex = want.GetIntParam(AppExecFwk::Constants::APP_INDEX, -1);
138 }
139 if (appIndex < 0) {
140 ACCOUNT_LOGW("appIndex = %{public}d is invalid.", appIndex);
141 return;
142 }
143 AppAccountControlManager::GetInstance().OnPackageRemoved(uid, bundleName, appIndex);
144 }
145 #endif // HAS_CES_PART
146 } // namespace AccountSA
147 } // namespace OHOS