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 #include "privacy_common_event_subscriber.h"
16 
17 #include <unistd.h>
18 #include "accesstoken_log.h"
19 
20 #include "common_event_subscribe_info.h"
21 #include "permission_record_manager.h"
22 
23 #include "want.h"
24 
25 namespace OHOS {
26 namespace Security {
27 namespace AccessToken {
28 #ifdef COMMON_EVENT_SERVICE_ENABLE
29 namespace {
30 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
31     LOG_CORE, SECURITY_DOMAIN_PRIVACY, "PrivacyCommonEventSubscriber"
32 };
33 
34 static bool g_isRegistered = false;
35 
36 static std::shared_ptr<PrivacyCommonEventSubscriber> g_subscriber = nullptr;
37 }
38 
RegisterEvent()39 void PrivacyCommonEventSubscriber::RegisterEvent()
40 {
41     ACCESSTOKEN_LOG_INFO(LABEL, "RegisterEvent start");
42     if (g_isRegistered) {
43         ACCESSTOKEN_LOG_DEBUG(LABEL, "Status observer already registered");
44         return;
45     }
46 
47     auto skill = std::make_shared<EventFwk::MatchingSkills>();
48     skill->AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
49     skill->AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED);
50     skill->AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
51     skill->AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
52     skill->AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED);
53     skill->AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SHUTDOWN);
54     auto info = std::make_shared<EventFwk::CommonEventSubscribeInfo>(*skill);
55     g_subscriber = std::make_shared<PrivacyCommonEventSubscriber>(*info);
56     const auto result = EventFwk::CommonEventManager::SubscribeCommonEvent(g_subscriber);
57     if (!result) {
58         ACCESSTOKEN_LOG_ERROR(LABEL, "RegisterEvent result is err");
59         return;
60     }
61     g_isRegistered = true;
62 }
63 
UnRegisterEvent()64 void PrivacyCommonEventSubscriber::UnRegisterEvent()
65 {
66     ACCESSTOKEN_LOG_INFO(LABEL, "UnregisterEvent start");
67     const auto result = EventFwk::CommonEventManager::UnSubscribeCommonEvent(g_subscriber);
68     if (!result) {
69         ACCESSTOKEN_LOG_ERROR(LABEL, "UnregisterEvent result is err");
70         return;
71     }
72     g_isRegistered = false;
73 }
74 
OnReceiveEvent(const EventFwk::CommonEventData & event)75 void PrivacyCommonEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& event)
76 {
77     const auto want = event.GetWant();
78     const auto action = want.GetAction();
79     ACCESSTOKEN_LOG_INFO(LABEL, "Receive event(%{public}s)", action.c_str());
80     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) {
81         PermissionRecordManager::GetInstance()
82             .SetLockScreenStatus(LockScreenStatusChangeType::PERM_ACTIVE_IN_UNLOCKED);
83     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) {
84         PermissionRecordManager::GetInstance()
85             .SetLockScreenStatus(LockScreenStatusChangeType::PERM_ACTIVE_IN_LOCKED);
86     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
87         PermissionRecordManager::GetInstance().ExecuteAllCameraExecuteCallback();
88     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
89         PermissionRecordManager::GetInstance().ExecuteDeletePermissionRecordTask();
90     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED ||
91         action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED) {
92         uint32_t tokenId = static_cast<uint32_t>(want.GetParams().GetIntParam("accessTokenId", 0));
93         ACCESSTOKEN_LOG_INFO(LABEL, "Receive package uninstall: tokenId=%{public}d.", tokenId);
94         PermissionRecordManager::GetInstance().RemovePermissionUsedRecords(tokenId, "");
95     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SHUTDOWN) {
96         // when receive shut down power event, store the cache data to database immediately
97         PermissionRecordManager::GetInstance().UpdatePermRecImmediately();
98     } else {
99         ACCESSTOKEN_LOG_ERROR(LABEL, "Action is invalid.");
100     }
101 }
102 #endif
103 } // namespace AccessToken
104 } // namespace Security
105 } // namespace OHOS