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
28 #ifdef HAS_CES_PART
29 using namespace OHOS::EventFwk;
30 #endif // HAS_CES_PART
31
32 namespace OHOS {
33 namespace AccountSA {
34 #ifdef HAS_CES_PART
35 namespace {
36 const char THREAD_COMMON_EVENT[] = "commonEvent";
37 constexpr int32_t DELAY_FOR_TIME_INTERVAL = 1 * 1000;
38 constexpr int32_t MAX_TRY_TIMES = 10;
39 }
40
AppAccountCommonEventObserver(const CommonEventCallback & callback)41 AppAccountCommonEventObserver::AppAccountCommonEventObserver(const CommonEventCallback &callback)
42 : callback_(callback)
43 {
44 counter_ = 0;
45 MatchingSkills matchingSkills;
46 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
47 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
48 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED);
49 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED);
50
51 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
52 subscriber_ = std::make_shared<AppAccountCommonEventSubscriber>(
53 subscribeInfo, [this] (const CommonEventData &data) { this->OnReceiveEvent(data); });
54
55 auto task = [this] { this->SubscribeCommonEvent(); };
56 std::thread taskThread(task);
57 pthread_setname_np(taskThread.native_handle(), THREAD_COMMON_EVENT);
58 taskThread.detach();
59 }
60
~AppAccountCommonEventObserver()61 AppAccountCommonEventObserver::~AppAccountCommonEventObserver()
62 {
63 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
64 }
65
SubscribeCommonEvent(void)66 void AppAccountCommonEventObserver::SubscribeCommonEvent(void)
67 {
68 while (counter_ != MAX_TRY_TIMES) {
69 if (CommonEventManager::SubscribeCommonEvent(subscriber_)) {
70 counter_ = 0;
71 break;
72 }
73 if (++counter_ == MAX_TRY_TIMES) {
74 ACCOUNT_LOGE("failed to subscribe common event and tried %{public}d times", counter_);
75 }
76 sleep(DELAY_FOR_TIME_INTERVAL / 1000); // 1000: 1s
77 }
78 }
79
OnReceiveEvent(const CommonEventData & data)80 void AppAccountCommonEventObserver::OnReceiveEvent(const CommonEventData &data)
81 {
82 auto want = data.GetWant();
83 std::string action = want.GetAction();
84 ACCOUNT_LOGI("Receive event: %{public}s", action.c_str());
85 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED ||
86 action == CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) {
87 DealWithRemoveEvent(want, action);
88 return;
89 }
90 if ((action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) && (callback_.OnUserRemoved != nullptr)) {
91 callback_.OnUserRemoved(data.GetCode());
92 return;
93 }
94 if (action == CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
95 AppAccountControlManager::GetInstance().AddMigratedAccount(data.GetCode());
96 }
97 }
98
DealWithRemoveEvent(const AAFwk::Want & want,const std::string action)99 void AppAccountCommonEventObserver::DealWithRemoveEvent(const AAFwk::Want &want, const std::string action)
100 {
101 if (callback_.OnPackageRemoved == nullptr) {
102 return;
103 }
104 auto element = want.GetElement();
105 std::string bundleName = element.GetBundleName();
106 auto uid = want.GetIntParam(AppExecFwk::Constants::UID, -1);
107 int32_t appIndex = 0;
108 if (action == CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) {
109 appIndex = want.GetIntParam(AppExecFwk::Constants::SANDBOX_APP_INDEX, -1);
110 } else {
111 appIndex = want.GetIntParam(AppExecFwk::Constants::APP_INDEX, -1);
112 }
113 if (appIndex < 0) {
114 ACCOUNT_LOGW("appIndex = %{public}d is invalid.", appIndex);
115 return;
116 }
117 ACCOUNT_LOGD("uid = %{public}d, bundleName = %{public}s. appIndex = %{public}d",
118 uid, bundleName.c_str(), appIndex);
119 callback_.OnPackageRemoved(uid, bundleName, appIndex);
120 }
121 #endif // HAS_CES_PART
122 } // namespace AccountSA
123 } // namespace OHOS