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_wrapper.h"
19 #include "bundle_memory_guard.h"
20 #include "ffrt.h"
21 #include "want.h"
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 APP_LOGE("%{public}s failed, empty action: %{public}s, userId:%d",
44 __func__, action.c_str(), userId);
45 return;
46 }
47 if (bundleName.empty() && action != EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
48 APP_LOGE("%{public}s failed, invalid param, action: %{public}s, bundleName: %{public}s",
49 __func__, action.c_str(), bundleName.c_str());
50 return;
51 }
52 APP_LOGI("%{public}s, action:%{public}s.", __func__, action.c_str());
53 std::weak_ptr<AppJumpInterceptorEventSubscriber> weakThis = shared_from_this();
54 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
55 APP_LOGI("bundle remove, bundleName: %{public}s", bundleName.c_str());
56 auto task = [weakThis, bundleName, db, userId]() {
57 BundleMemoryGuard memoryGuard;
58 if (db == nullptr) {
59 APP_LOGE("Get invalid db");
60 return;
61 }
62 std::shared_ptr<AppJumpInterceptorEventSubscriber> sharedThis = weakThis.lock();
63 if (sharedThis) {
64 APP_LOGI("start delete rule bundleName: %{public}s, userId:%d", bundleName.c_str(), userId);
65 db->DeleteRuleByCallerBundleName(bundleName, userId);
66 db->DeleteRuleByTargetBundleName(bundleName, userId);
67 }
68 };
69 ffrt::submit(task);
70 } else {
71 APP_LOGW("%{public}s warnning, invalid action.", __func__);
72 }
73 }
74 } // AppExecFwk
75 } // OHOS