1 /* 2 * Copyright (c) 2023-2024 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_SERVICE_H 16 #define SECURITY_COMPONENT_SERVICE_H 17 18 #include <string> 19 #include <vector> 20 #include "access_token.h" 21 #include "app_state_observer.h" 22 #include "iremote_object.h" 23 #include "nlohmann/json.hpp" 24 #include "nocopyable.h" 25 #include "sec_comp_base.h" 26 #include "sec_comp_click_event_parcel.h" 27 #include "sec_comp_info.h" 28 #include "sec_comp_manager.h" 29 #include "sec_comp_service_stub.h" 30 #include "security_component_service_ipc_interface_code.h" 31 #include "singleton.h" 32 #include "system_ability.h" 33 34 namespace OHOS { 35 namespace Security { 36 namespace SecurityComponent { 37 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 38 class __attribute__((visibility("default"))) SecCompService final : public SystemAbility, public SecCompServiceStub { 39 DECLARE_DELAYED_SINGLETON(SecCompService); 40 DECLEAR_SYSTEM_ABILITY(SecCompService); 41 42 public: 43 SecCompService(int32_t saId, bool runOnCreate); 44 45 void OnStart() override; 46 void OnStop() override; 47 48 int32_t RegisterSecurityComponent(const SecCompRawdata& rawData, SecCompRawdata& rawReply) override; 49 int32_t UpdateSecurityComponent(const SecCompRawdata& rawData, SecCompRawdata& rawReply) override; 50 int32_t UnregisterSecurityComponent(const SecCompRawdata& rawData, SecCompRawdata& rawReply) override; 51 int32_t ReportSecurityComponentClickEvent(const sptr<IRemoteObject>& callerToken, 52 const sptr<IRemoteObject>& dialogCallback, const SecCompRawdata& rawData, SecCompRawdata& rawReply) override; 53 int32_t VerifySavePermission(AccessToken::AccessTokenID tokenId, bool& isGranted) override; 54 int32_t PreRegisterSecCompProcess(const SecCompRawdata& rawData, SecCompRawdata& rawReply) override; 55 56 int Dump(int fd, const std::vector<std::u16string>& args) override; 57 58 private: 59 int32_t WriteError(int32_t res, SecCompRawdata& rawReply); 60 int32_t RegisterReadFromRawdata(SecCompRawdata& rawData, SecCompType& type, std::string& componentInfo); 61 int32_t RegisterSecurityComponentBody(SecCompType type, const std::string& componentInfo, int32_t& scId); 62 int32_t RegisterWriteToRawdata(int32_t res, int32_t scId, SecCompRawdata& rawReply); 63 int32_t UpdateReadFromRawdata(SecCompRawdata& rawData, int32_t& scId, std::string& componentInfo); 64 int32_t UpdateSecurityComponentBody(int32_t scId, const std::string& componentInfo); 65 int32_t UpdateWriteToRawdata(int32_t res, SecCompRawdata& rawReply); 66 int32_t UnregisterReadFromRawdata(SecCompRawdata& rawData, int32_t& scId); 67 int32_t UnregisterSecurityComponentBody(int32_t scId); 68 int32_t UnregisterWriteToRawdata(int32_t res, SecCompRawdata& rawReply); 69 int32_t ReportSecurityComponentClickEventBody(SecCompInfo& secCompInfo, 70 sptr<IRemoteObject> callerToken, sptr<IRemoteObject> dialogCallback, std::string& message); 71 int32_t ReportWriteToRawdata(int32_t res, std::string message, SecCompRawdata& rawReply); 72 int32_t PreRegisterReadFromRawdata(SecCompRawdata& rawData); 73 int32_t PreRegisterSecCompProcessBody(); 74 int32_t PreRegisterWriteToRawdata(int32_t res, SecCompRawdata& rawReply); 75 int32_t ParseParams(const std::string& componentInfo, SecCompCallerInfo& caller, nlohmann::json& jsonRes); 76 bool Initialize() const; 77 bool RegisterAppStateObserver(); 78 void UnregisterAppStateObserver(); 79 bool GetCallerInfo(SecCompCallerInfo& caller); 80 bool IsMediaLibraryCalling(); 81 82 ServiceRunningState state_; 83 sptr<AppExecFwk::IAppMgr> iAppMgr_; 84 sptr<AppStateObserver> appStateObserver_; 85 AccessToken::AccessTokenID mediaLibraryTokenId_ = 0; 86 }; 87 } // namespace SecurityComponent 88 } // namespace Security 89 } // namespace OHOS 90 #endif // SECURITY_COMPONENT_SERVICE_H 91