1 /*
2  * Copyright (c) 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_jump_interceptor_event_subscriber.h"
17 #include "app_jump_interceptor_manager_rdb.h"
18 #include "app_log_tag_wrapper.h"
19 #include "bundle_memory_guard.h"
20 #include "ffrt.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
24 const std::string WANT_PARAM_USER_ID = "userId";
AppJumpInterceptorEventSubscriber(const std::shared_ptr<IAppJumpInterceptorlManagerDb> & appJumpDb)25 AppJumpInterceptorEventSubscriber::AppJumpInterceptorEventSubscriber(
26     const std::shared_ptr<IAppJumpInterceptorlManagerDb> &appJumpDb)
27 {
28     appJumpDb_ = appJumpDb;
29 }
30 
~AppJumpInterceptorEventSubscriber()31 AppJumpInterceptorEventSubscriber::~AppJumpInterceptorEventSubscriber()
32 {
33 }
34 
OnReceiveEvent(const EventFwk::CommonEventData eventData)35 void AppJumpInterceptorEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData eventData)
36 {
37     const AAFwk::Want& want = eventData.GetWant();
38     std::string action = want.GetAction();
39     std::string bundleName = want.GetElement().GetBundleName();
40     int32_t userId = want.GetIntParam(WANT_PARAM_USER_ID, -1);
41     std::shared_ptr<IAppJumpInterceptorlManagerDb> db = appJumpDb_;
42     if (action.empty() || userId < 0 || db == nullptr) {
43         LOG_E(BMS_TAG_DEFAULT, "empty action: %{public}s, userId:%{public}d", action.c_str(), userId);
44         return;
45     }
46     if (bundleName.empty() && action != EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
47         LOG_E(BMS_TAG_DEFAULT, "invalid param action: %{public}s, bundleName: %{public}s",
48             action.c_str(), bundleName.c_str());
49         return;
50     }
51     LOG_I(BMS_TAG_DEFAULT, "action:%{public}s", action.c_str());
52     std::weak_ptr<AppJumpInterceptorEventSubscriber> weakThis = shared_from_this();
53     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
54         LOG_I(BMS_TAG_DEFAULT, "bundle remove, bundleName: %{public}s", bundleName.c_str());
55         auto task = [weakThis, bundleName, db, userId]() {
56             BundleMemoryGuard memoryGuard;
57             if (db == nullptr) {
58                 LOG_E(BMS_TAG_DEFAULT, "Get invalid db");
59                 return;
60             }
61             std::shared_ptr<AppJumpInterceptorEventSubscriber> sharedThis = weakThis.lock();
62             if (sharedThis) {
63                 LOG_I(BMS_TAG_DEFAULT, "delete rule bundleName:%{public}s userId:%{public}d",
64                     bundleName.c_str(), userId);
65                 db->DeleteRuleByCallerBundleName(bundleName, userId);
66                 db->DeleteRuleByTargetBundleName(bundleName, userId);
67             }
68         };
69         ffrt::submit(task);
70     } else {
71         LOG_W(BMS_TAG_DEFAULT, "invalid action");
72     }
73 }
74 } // AppExecFwk
75 } // OHOS