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<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, sptr<IRemoteObject> callerToken); 61 void NotifyProcessForeground(int32_t pid); 62 void NotifyProcessBackground(int32_t pid); 63 void NotifyProcessDied(int32_t pid); 64 void DumpSecComp(std::string& dumpStr); 65 bool Initialize(); 66 void ExitSaProcess(); 67 void ExitWhenAppMgrDied(); 68 int32_t AddSecurityComponentProcess(const SecCompCallerInfo& caller); 69 70 private: 71 SecCompManager(); 72 bool IsForegroundCompExist(); 73 bool IsCompExist(); 74 int32_t AddSecurityComponentToList(int32_t pid, 75 AccessToken::AccessTokenID tokenId, const SecCompEntity& newEntity); 76 int32_t DeleteSecurityComponentFromList(int32_t pid, int32_t scId); 77 SecCompEntity* GetSecurityComponentFromList(int32_t pid, int32_t scId); 78 int32_t CheckClickSecurityComponentInfo(SecCompEntity* sc, int32_t scId, 79 const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller); 80 void SendCheckInfoEnhanceSysEvent(int32_t scId, 81 SecCompType type, const std::string& scene, int32_t res); 82 int32_t CreateScId(); 83 84 OHOS::Utils::RWLock componentInfoLock_; 85 std::mutex scIdMtx_; 86 std::unordered_map<int32_t, ProcessCompInfos> componentMap_; 87 int32_t scIdStart_; 88 bool isSaExit_ = false; 89 90 std::shared_ptr<AppExecFwk::EventRunner> secRunner_; 91 std::shared_ptr<SecEventHandler> secHandler_; 92 FirstUseDialog firstUseDialog_; 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