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_MANAGER_H
16 #define SECURITY_COMPONENT_MANAGER_H
17 
18 #include <map>
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 #include "accesstoken_kit.h"
24 #include "app_state_observer.h"
25 #include "first_use_dialog.h"
26 #include "nocopyable.h"
27 #include "rwlock.h"
28 #include "sec_comp_base.h"
29 #include "sec_comp_entity.h"
30 #include "sec_comp_info.h"
31 #include "sec_comp_malicious_apps.h"
32 #include "sec_event_handler.h"
33 
34 namespace OHOS {
35 namespace Security {
36 namespace SecurityComponent {
37 struct SecCompCallerInfo {
38     AccessToken::AccessTokenID tokenId;
39     int32_t uid;
40     int32_t pid;
41 };
42 
43 struct ProcessCompInfos {
44     std::vector<std::shared_ptr<SecCompEntity>> compList;
45     bool isForeground = false;
46     AccessToken::AccessTokenID tokenId;
47 };
48 
49 class SecCompManager {
50 public:
51     static SecCompManager& GetInstance();
52     virtual ~SecCompManager() = default;
53 
54     int32_t RegisterSecurityComponent(SecCompType type, const nlohmann::json& jsonComponent,
55         const SecCompCallerInfo& caller, int32_t& scId);
56     int32_t UpdateSecurityComponent(int32_t scId, const nlohmann::json& jsonComponent,
57         const SecCompCallerInfo& caller);
58     int32_t UnregisterSecurityComponent(int32_t scId, const SecCompCallerInfo& caller);
59     int32_t ReportSecurityComponentClickEvent(int32_t scId, const nlohmann::json& jsonComponent,
60         const SecCompCallerInfo& caller, const SecCompClickEvent& clickInfo,
61         const std::vector<sptr<IRemoteObject>>& remote);
62     void NotifyProcessForeground(int32_t pid);
63     void NotifyProcessBackground(int32_t pid);
64     void NotifyProcessDied(int32_t pid);
65     void DumpSecComp(std::string& dumpStr);
66     bool Initialize();
67     void ExitSaProcess();
68     void ExitWhenAppMgrDied();
69     int32_t AddSecurityComponentProcess(const SecCompCallerInfo& caller);
70 
71 private:
72     SecCompManager();
73     bool IsForegroundCompExist();
74     bool IsCompExist();
75     int32_t AddSecurityComponentToList(int32_t pid,
76         AccessToken::AccessTokenID tokenId, std::shared_ptr<SecCompEntity> newEntity);
77     int32_t DeleteSecurityComponentFromList(int32_t pid, int32_t scId);
78     std::shared_ptr<SecCompEntity> GetSecurityComponentFromList(int32_t pid, int32_t scId);
79     int32_t CheckClickSecurityComponentInfo(std::shared_ptr<SecCompEntity> sc, int32_t scId,
80         const nlohmann::json& jsonComponent,  const SecCompCallerInfo& caller);
81     void SendCheckInfoEnhanceSysEvent(int32_t scId,
82         SecCompType type, const std::string& scene, int32_t res);
83     int32_t CreateScId();
84 
85     OHOS::Utils::RWLock componentInfoLock_;
86     std::mutex scIdMtx_;
87     std::unordered_map<int32_t, ProcessCompInfos> componentMap_;
88     int32_t scIdStart_;
89     bool isSaExit_ = false;
90 
91     std::shared_ptr<AppExecFwk::EventRunner> secRunner_;
92     std::shared_ptr<SecEventHandler> secHandler_;
93     SecCompMaliciousApps malicious_;
94 
95     DISALLOW_COPY_AND_MOVE(SecCompManager);
96 };
97 }  // namespace SecurityComponent
98 }  // namespace Security
99 }  // namespace OHOS
100 #endif  // SECURITY_COMPONENT_MANAGER_H
101