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