1 /* 2 * Copyright (c) 2025 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 #ifndef PERMISSION_SEC_COMP_MONITOR_H 16 #define PERMISSION_SEC_COMP_MONITOR_H 17 18 #include <mutex> 19 #include <set> 20 #include <vector> 21 #include "app_manager_death_callback.h" 22 #include "app_status_change_callback.h" 23 #include "nocopyable.h" 24 25 namespace OHOS { 26 namespace Security { 27 namespace AccessToken { 28 class SecCompUsageObserver : public ApplicationStateObserverStub { 29 public: 30 SecCompUsageObserver() = default; 31 ~SecCompUsageObserver() = default; 32 33 void OnProcessDied(const ProcessData &processData) override; 34 void OnProcessStateChanged(const ProcessData &processData) override; 35 void OnAppCacheStateChanged(const AppStateData &appStateData) override; 36 DISALLOW_COPY_AND_MOVE(SecCompUsageObserver); 37 }; 38 39 class SecCompAppManagerDeathCallback : public AppManagerDeathCallback { 40 public: 41 SecCompAppManagerDeathCallback() = default; 42 ~SecCompAppManagerDeathCallback() = default; 43 44 void NotifyAppManagerDeath() override; 45 DISALLOW_COPY_AND_MOVE(SecCompAppManagerDeathCallback); 46 }; 47 48 class SecCompMonitor final { 49 public: 50 static SecCompMonitor& GetInstance(); 51 ~SecCompMonitor(); 52 53 void RemoveProcessFromForegroundList(int32_t pid); 54 bool IsToastShownNeeded(int32_t pid); 55 void OnAppMgrRemoteDiedHandle(); 56 57 private: 58 SecCompMonitor(); 59 void InitAppObserver(); 60 DISALLOW_COPY_AND_MOVE(SecCompMonitor); 61 std::mutex observerMutex_; 62 sptr<SecCompUsageObserver> observer_ = nullptr; 63 std::mutex appManagerDeathMutex_; 64 std::shared_ptr<SecCompAppManagerDeathCallback> appManagerDeathCallback_ = nullptr; 65 std::mutex appfgLock_; 66 std::set<int32_t> appsInForeground_; 67 }; 68 } // namespace AccessToken 69 } // namespace Security 70 } // namespace OHOS 71 #endif // PERMISSION_SEC_COMP_MONITOR_H 72