1 /*
2 * Copyright (c) 2021-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 "app_account_common_event_observer.h"
17
18 #include "account_log_wrapper.h"
19 #include "bundle_constants.h"
20 #ifdef HAS_CES_PART
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #endif // HAS_CES_PART
24
25 #ifdef HAS_CES_PART
26 using namespace OHOS::EventFwk;
27 #endif // HAS_CES_PART
28
29 namespace OHOS {
30 namespace AccountSA {
31 #ifdef HAS_CES_PART
AppAccountCommonEventObserver(const CommonEventCallback & callback)32 AppAccountCommonEventObserver::AppAccountCommonEventObserver(const CommonEventCallback &callback)
33 : callback_(callback)
34 {
35 counter_ = 0;
36 MatchingSkills matchingSkills;
37 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
38 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
39 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED);
40
41 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
42 subscriber_ = std::make_shared<AppAccountCommonEventSubscriber>(
43 subscribeInfo, std::bind(&AppAccountCommonEventObserver::OnReceiveEvent, this, std::placeholders::_1));
44
45 if (GetEventHandler() != ERR_OK) {
46 ACCOUNT_LOGE("failed to get event handler");
47 } else {
48 Callback callbackTemp = std::bind(&AppAccountCommonEventObserver::SubscribeCommonEvent, this);
49 handler_->PostTask(callbackTemp, DELAY_FOR_COMMON_EVENT_SERVICE);
50 }
51 }
52
~AppAccountCommonEventObserver()53 AppAccountCommonEventObserver::~AppAccountCommonEventObserver()
54 {
55 if (handler_) {
56 handler_.reset();
57 }
58
59 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
60 }
61
GetEventHandler(void)62 ErrCode AppAccountCommonEventObserver::GetEventHandler(void)
63 {
64 if (!handler_) {
65 handler_ = std::make_shared<EventHandler>(EventRunner::Create());
66 if (handler_ == nullptr) {
67 ACCOUNT_LOGE("failed to create event handler");
68 return ERR_APPACCOUNT_SERVICE_CREATE_EVENT_HANDLER;
69 }
70 }
71
72 return ERR_OK;
73 }
74
SubscribeCommonEvent(void)75 void AppAccountCommonEventObserver::SubscribeCommonEvent(void)
76 {
77 bool result = CommonEventManager::SubscribeCommonEvent(subscriber_);
78 if (result) {
79 counter_ = 0;
80 } else {
81 counter_++;
82 if (counter_ == MAX_TRY_TIMES) {
83 ACCOUNT_LOGE("failed to subscribe common event and tried %{public}d times", counter_);
84 } else {
85 Callback callback = std::bind(&AppAccountCommonEventObserver::SubscribeCommonEvent, this);
86 handler_->PostTask(callback, DELAY_FOR_TIME_INTERVAL);
87 }
88 }
89 }
90
OnReceiveEvent(const CommonEventData & data)91 void AppAccountCommonEventObserver::OnReceiveEvent(const CommonEventData &data)
92 {
93 auto want = data.GetWant();
94 std::string action = want.GetAction();
95 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED ||
96 action == CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) {
97 DealWithRemoveEvent(want, action);
98 return;
99 }
100 if ((action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) && (callback_.OnUserRemoved != nullptr)) {
101 callback_.OnUserRemoved(data.GetCode());
102 }
103 }
104
DealWithRemoveEvent(const AAFwk::Want & want,const std::string action)105 void AppAccountCommonEventObserver::DealWithRemoveEvent(const AAFwk::Want &want, const std::string action)
106 {
107 if (callback_.OnPackageRemoved == nullptr) {
108 return;
109 }
110 auto element = want.GetElement();
111 std::string bundleName = element.GetBundleName();
112 auto uid = want.GetIntParam(AppExecFwk::Constants::UID, -1);
113 int32_t appIndex = 0;
114 if (action == CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) {
115 appIndex = want.GetIntParam(AppExecFwk::Constants::SANDBOX_APP_INDEX, -1);
116 if (appIndex < 0) {
117 ACCOUNT_LOGW("appIndex = %{public}d is invalid.", appIndex);
118 return;
119 }
120 }
121 ACCOUNT_LOGD("uid = %{public}d, bundleName = %{public}s. appIndex = %{public}d",
122 uid, bundleName.c_str(), appIndex);
123 callback_.OnPackageRemoved(uid, bundleName, appIndex);
124 }
125 #endif // HAS_CES_PART
126 } // namespace AccountSA
127 } // namespace OHOS