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 int32_t CheckClickEventParams(const SecCompCallerInfo& caller, const std::vector<sptr<IRemoteObject>>& remote); 63 void NotifyProcessForeground(int32_t pid); 64 void NotifyProcessBackground(int32_t pid); 65 void NotifyProcessDied(int32_t pid, bool isProcessCached); 66 void DumpSecComp(std::string& dumpStr); 67 bool Initialize(); 68 void ExitSaProcess(); 69 void ExitWhenAppMgrDied(); 70 int32_t AddSecurityComponentProcess(const SecCompCallerInfo& caller); 71 bool HasCustomPermissionForSecComp(); 72 73 private: 74 SecCompManager(); 75 bool IsForegroundCompExist(); 76 bool IsCompExist(); 77 bool IsScIdExist(int32_t scId); 78 int32_t AddSecurityComponentToList(int32_t pid, 79 AccessToken::AccessTokenID tokenId, std::shared_ptr<SecCompEntity> newEntity); 80 int32_t DeleteSecurityComponentFromList(int32_t pid, int32_t scId); 81 std::shared_ptr<SecCompEntity> GetSecurityComponentFromList(int32_t pid, int32_t scId); 82 int32_t CheckClickSecurityComponentInfo(std::shared_ptr<SecCompEntity> sc, int32_t scId, 83 const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller, std::string& message); 84 void SendCheckInfoEnhanceSysEvent(int32_t scId, 85 SecCompType type, const std::string& scene, int32_t res); 86 int32_t CreateScId(); 87 void GetFoldOffsetY(const CrossAxisState crossAxisState); 88 89 OHOS::Utils::RWLock componentInfoLock_; 90 std::mutex scIdMtx_; 91 std::unordered_map<int32_t, ProcessCompInfos> componentMap_; 92 int32_t scIdStart_; 93 bool isSaExit_ = false; 94 int32_t superFoldOffsetY_ = 0; 95 96 std::shared_ptr<AppExecFwk::EventRunner> secRunner_; 97 std::shared_ptr<SecEventHandler> secHandler_; 98 SecCompMaliciousApps malicious_; 99 100 std::function<void ()> exitSaProcessFunc_ = []() { return; }; 101 DISALLOW_COPY_AND_MOVE(SecCompManager); 102 }; 103 } // namespace SecurityComponent 104 } // namespace Security 105 } // namespace OHOS 106 #endif // SECURITY_COMPONENT_MANAGER_H 107