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 <functional> 19 #include <map> 20 #include <memory> 21 #include <mutex> 22 #include <string> 23 #include <vector> 24 #include "accesstoken_kit.h" 25 #include "app_state_observer.h" 26 #include "first_use_dialog.h" 27 #include "nocopyable.h" 28 #include "rwlock.h" 29 #include "sec_comp_base.h" 30 #include "sec_comp_entity.h" 31 #include "sec_comp_info.h" 32 #include "sec_comp_malicious_apps.h" 33 #include "sec_event_handler.h" 34 35 namespace OHOS { 36 namespace Security { 37 namespace SecurityComponent { 38 struct SecCompCallerInfo { 39 AccessToken::AccessTokenID tokenId; 40 int32_t uid; 41 int32_t pid; 42 }; 43 44 struct ProcessCompInfos { 45 std::vector<std::shared_ptr<SecCompEntity>> compList; 46 bool isForeground = false; 47 AccessToken::AccessTokenID tokenId; 48 }; 49 50 class SecCompManager { 51 public: 52 static SecCompManager& GetInstance(); 53 virtual ~SecCompManager() = default; 54 55 int32_t RegisterSecurityComponent(SecCompType type, const nlohmann::json& jsonComponent, 56 const SecCompCallerInfo& caller, int32_t& scId); 57 int32_t UpdateSecurityComponent(int32_t scId, const nlohmann::json& jsonComponent, 58 const SecCompCallerInfo& caller); 59 int32_t UnregisterSecurityComponent(int32_t scId, const SecCompCallerInfo& caller); 60 int32_t ReportSecurityComponentClickEvent(SecCompInfo& secCompInfo, const nlohmann::json& jsonComponent, 61 const SecCompCallerInfo& caller, const std::vector<sptr<IRemoteObject>>& remote, std::string& message); 62 void NotifyProcessForeground(int32_t pid); 63 void NotifyProcessBackground(int32_t pid); 64 void NotifyProcessDied(int32_t pid, bool isProcessCached); 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, std::string& message); 81 void SendCheckInfoEnhanceSysEvent(int32_t scId, 82 SecCompType type, const std::string& scene, int32_t res); 83 int32_t CreateScId(); 84 void GetFoldOffsetY(const CrossAxisState crossAxisState); 85 86 OHOS::Utils::RWLock componentInfoLock_; 87 std::mutex scIdMtx_; 88 std::unordered_map<int32_t, ProcessCompInfos> componentMap_; 89 int32_t scIdStart_; 90 bool isSaExit_ = false; 91 int32_t superFoldOffsetY_ = 0; 92 93 std::shared_ptr<AppExecFwk::EventRunner> secRunner_; 94 std::shared_ptr<SecEventHandler> secHandler_; 95 SecCompMaliciousApps malicious_; 96 97 std::function<void ()> exitSaProcessFunc_ = []() { return; }; 98 DISALLOW_COPY_AND_MOVE(SecCompManager); 99 }; 100 } // namespace SecurityComponent 101 } // namespace Security 102 } // namespace OHOS 103 #endif // SECURITY_COMPONENT_MANAGER_H 104