• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_uninstall_observer.h"
17 #include "dlp_permission.h"
18 #include "dlp_permission_log.h"
19 #include "retention_file_manager.h"
20 
21 namespace OHOS {
22 namespace Security {
23 namespace DlpPermission {
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "AppUninstallObserver" };
26 const int32_t INVALID_APPINDEX = -1;
27 }
28 
AppUninstallObserver(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)29 AppUninstallObserver::AppUninstallObserver(const EventFwk::CommonEventSubscribeInfo& subscribeInfo)
30     : CommonEventSubscriber(subscribeInfo)
31 {}
32 
OnReceiveEvent(const EventFwk::CommonEventData & data)33 void AppUninstallObserver::OnReceiveEvent(const EventFwk::CommonEventData& data)
34 {
35     std::string action = data.GetWant().GetAction();
36     std::string bundleName = data.GetWant().GetBundle();
37     DLP_LOG_DEBUG(LABEL, "action %{public}s %{public}s is uninstall", action.c_str(), bundleName.c_str());
38     if (action != EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED &&
39         action != EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED) {
40         return;
41     }
42     if (RetentionFileManager::GetInstance().HasRetentionSandboxInfo(bundleName)) {
43         RetentionFileManager::GetInstance().RemoveRetentionState(bundleName, INVALID_APPINDEX);
44     }
45 }
46 
DlpEventSubSubscriber()47 DlpEventSubSubscriber::DlpEventSubSubscriber()
48 {
49     if (subscriber_ == nullptr) {
50         EventFwk::MatchingSkills matchingSkills;
51         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
52         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED);
53         EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
54         subscriber_ = std::make_shared<AppUninstallObserver>(subscribeInfo);
55         EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
56     }
57 }
58 
~DlpEventSubSubscriber()59 DlpEventSubSubscriber::~DlpEventSubSubscriber()
60 {
61     if (subscriber_ != nullptr) {
62         EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
63         subscriber_ = nullptr;
64     }
65 }
66 } // namespace DlpPermission
67 } // namespace Security
68 } // namespace OHOS
69