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 16 #ifndef SECURITY_COMPONENT_CLIENT_H 17 #define SECURITY_COMPONENT_CLIENT_H 18 19 #include <condition_variable> 20 #include <cstdint> 21 #include <mutex> 22 #include <string> 23 #include "access_token.h" 24 #include "isec_comp_service.h" 25 #include "sec_comp_death_recipient.h" 26 #include "sec_comp_enhance_adapter.h" 27 #include "sec_comp_err.h" 28 #include "sec_comp_info.h" 29 #include "security_component_service_ipc_interface_code.h" 30 31 namespace OHOS { 32 namespace Security { 33 namespace SecurityComponent { 34 class SecCompClient final { 35 public: 36 static SecCompClient& GetInstance(); 37 38 int32_t RegisterWriteToRawdata(SecCompType type, const std::string& componentInfo, SecCompRawdata& rawData); 39 int32_t UpdateWriteToRawdata(int32_t scId, const std::string& componentInfo, SecCompRawdata& rawData); 40 int32_t UnregisterWriteToRawdata(int32_t scId, SecCompRawdata& rawData); 41 int32_t ReportWriteToRawdata(SecCompInfo& secCompInfo, SecCompRawdata& rawData, std::string& message); 42 int32_t PreRegisterWriteToRawdata(SecCompRawdata& rawData); 43 int32_t RegisterSecurityComponent(SecCompType type, const std::string& componentInfo, int32_t& scId); 44 int32_t UpdateSecurityComponent(int32_t scId, const std::string& componentInfo); 45 int32_t UnregisterSecurityComponent(int32_t scId); 46 int32_t ReportSecurityComponentClickEvent(SecCompInfo& secCompInfo, 47 sptr<IRemoteObject> callerToken, sptr<IRemoteObject> dialogCallback, std::string& message); 48 bool VerifySavePermission(AccessToken::AccessTokenID tokenId); 49 int32_t PreRegisterSecCompProcess(); 50 bool IsServiceExist(); 51 bool LoadService(); 52 bool IsSystemAppCalling(); 53 54 void FinishStartSASuccess(const sptr<IRemoteObject>& remoteObject); 55 void FinishStartSAFail(); 56 void OnRemoteDiedHandle(); 57 std::mutex useIPCMutex_; 58 59 private: 60 SecCompClient(); 61 virtual ~SecCompClient(); 62 DISALLOW_COPY_AND_MOVE(SecCompClient); 63 64 bool TryToGetSecCompSa(); 65 bool StartLoadSecCompSa(); 66 void WaitForSecCompSa(); 67 void GetSecCompSa(); 68 void LoadSecCompSa(); 69 sptr<ISecCompService> GetProxy(bool doLoadSa); 70 void GetProxyFromRemoteObject(const sptr<IRemoteObject>& remoteObject); 71 int32_t TryRegisterSecurityComponent(SecCompType type, const std::string& componentInfo, 72 int32_t& scId, sptr<ISecCompService> proxy); 73 74 std::mutex cvLock_; 75 bool readyFlag_ = false; 76 std::condition_variable secComCon_; 77 std::mutex proxyMutex_; 78 bool serviceAbilityNeedLoadFlag_ = false; 79 std::condition_variable secCompSACon_; 80 std::mutex secCompSaMutex_; 81 sptr<ISecCompService> proxy_ = nullptr; 82 sptr<SecCompDeathRecipient> serviceDeathObserver_ = nullptr; 83 }; 84 } // namespace SecurityComponent 85 } // namespace Security 86 } // namespace OHOS 87 #endif // SECURITY_COMPONENT_CLIENT_H 88