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 PERMISSION_SEC_COMP_ENHANCE_AGENT_H
16 #define PERMISSION_SEC_COMP_ENHANCE_AGENT_H
17 
18 #include <mutex>
19 #include <vector>
20 #include "app_manager_death_callback.h"
21 #include "app_status_change_callback.h"
22 #include "nocopyable.h"
23 #include "sec_comp_enhance_data.h"
24 
25 namespace OHOS {
26 namespace Security {
27 namespace AccessToken {
28 class PrivacyAppUsingSecCompStateObserver : public ApplicationStateObserverStub {
29 public:
30     PrivacyAppUsingSecCompStateObserver() = default;
31     ~PrivacyAppUsingSecCompStateObserver() = default;
32 
33     void OnProcessDied(const ProcessData &processData) override;
34     DISALLOW_COPY_AND_MOVE(PrivacyAppUsingSecCompStateObserver);
35 };
36 
37 class PrivacySecCompAppManagerDeathCallback : public AppManagerDeathCallback {
38 public:
39     PrivacySecCompAppManagerDeathCallback() = default;
40     ~PrivacySecCompAppManagerDeathCallback() = default;
41 
42     void NotifyAppManagerDeath() override;
43     DISALLOW_COPY_AND_MOVE(PrivacySecCompAppManagerDeathCallback);
44 };
45 
46 class PrivacySecCompEnhanceAgent final {
47 public:
48     static PrivacySecCompEnhanceAgent& GetInstance();
49     virtual ~PrivacySecCompEnhanceAgent();
50 
51     int32_t RegisterSecCompEnhance(const SecCompEnhanceData& enhanceData);
52     int32_t UpdateSecCompEnhance(int32_t pid, uint32_t seqNum);
53     int32_t GetSecCompEnhance(int32_t pid, SecCompEnhanceData& enhanceData);
54     int32_t GetSpecialSecCompEnhance(const std::string& bundleName,
55         std::vector<SecCompEnhanceData>& enhanceList);
56     void RemoveSecCompEnhance(int pid);
57     void OnAppMgrRemoteDiedHandle();
58 
59 private:
60     PrivacySecCompEnhanceAgent();
61     void InitAppObserver();
62     DISALLOW_COPY_AND_MOVE(PrivacySecCompEnhanceAgent);
63 
64 private:
65     sptr<PrivacyAppUsingSecCompStateObserver> observer_ = nullptr;
66     std::shared_ptr<PrivacySecCompAppManagerDeathCallback> appManagerDeathCallback_ = nullptr;
67     std::mutex secCompEnhanceMutex_;
68     std::vector<SecCompEnhanceData> secCompEnhanceData_;
69 };
70 } // namespace AccessToken
71 } // namespace Security
72 } // namespace OHOS
73 #endif // PERMISSION_SEC_COMP_ENHANCE_AGENT_H
74