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 #ifndef SECURITY_COMPONENT_SA_APP_STATE_OBSERVER_APP_STATE_OBSERVER_H
16 #define SECURITY_COMPONENT_SA_APP_STATE_OBSERVER_APP_STATE_OBSERVER_H
17 
18 #include <set>
19 #include <vector>
20 #include "app_mgr_interface.h"
21 #include "application_state_observer_stub.h"
22 #include "iremote_object.h"
23 #include "rwlock.h"
24 
25 namespace OHOS {
26 namespace Security {
27 namespace SecurityComponent {
28 struct SecCompProcessData {
29     std::string bundleName;
30     int32_t pid = -1;
31     int32_t uid = -1;
32 };
33 
34 class AppStateObserver : public AppExecFwk::ApplicationStateObserverStub {
35 public:
36     explicit AppStateObserver();
37     virtual ~AppStateObserver();
38 
39     void OnProcessStateChanged(const AppExecFwk::ProcessData& processData) override;
40     void OnProcessDied(const AppExecFwk::ProcessData& processData) override;
41     bool IsProcessForeground(int32_t pid, int32_t uid);
42     void AddProcessToForegroundSet(int32_t pid, const SecCompProcessData& data);
43     void AddProcessToForegroundSet(const AppExecFwk::ProcessData& processData);
44     void AddProcessToForegroundSet(const AppExecFwk::AppStateData& stateData);
45     void DumpProcess(std::string& dumpStr);
46 
47 private:
48     void RemoveProcessFromForegroundSet(const AppExecFwk::ProcessData& processData);
49     std::vector<SecCompProcessData> foregrandProcList_;
50     OHOS::Utils::RWLock fgProcLock_;
51 };
52 }  // namespace SecurityComponent
53 }  // namespace Security
54 }  // namespace OHOS
55 
56 #endif  // SECURITY_COMPONENT_SA_APP_STATE_OBSERVER_APP_STATE_OBSERVER_H
57 
58