1 /* 2 * Copyright (c) 2022-2025 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 PRIVACY_MANAGER_SERVICE_H 17 #define PRIVACY_MANAGER_SERVICE_H 18 19 #include <string> 20 21 #ifdef EVENTHANDLER_ENABLE 22 #include "access_event_handler.h" 23 #endif 24 #include "privacy_manager_stub.h" 25 #include "iremote_object.h" 26 #include "nocopyable.h" 27 #include "proxy_death_handler.h" 28 #include "singleton.h" 29 #include "system_ability.h" 30 31 namespace OHOS { 32 namespace Security { 33 namespace AccessToken { 34 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 35 class PrivacyManagerService final : public SystemAbility, public PrivacyManagerStub { 36 DECLARE_DELAYED_SINGLETON(PrivacyManagerService); 37 DECLEAR_SYSTEM_ABILITY(PrivacyManagerService); 38 39 public: 40 void OnStart() override; 41 void OnStop() override; 42 43 int32_t AddPermissionUsedRecord(const AddPermParamInfoParcel& infoParcel) override; 44 int32_t AddPermissionUsedRecordAsync(const AddPermParamInfoParcel& infoParcel) override; 45 int32_t StartUsingPermission(const PermissionUsedTypeInfoParcel &infoParcel, 46 const sptr<IRemoteObject>& anonyStub) override; 47 int32_t StartUsingPermissionCallback(const PermissionUsedTypeInfoParcel &infoParcel, 48 const sptr<IRemoteObject>& callback, const sptr<IRemoteObject>& anonyStub) override; 49 int32_t SetPermissionUsedRecordToggleStatus(int32_t userID, bool status) override; 50 int32_t GetPermissionUsedRecordToggleStatus(int32_t userID, bool& status) override; 51 int32_t StopUsingPermission(AccessTokenID tokenId, int32_t pid, const std::string& permissionName) override; 52 int32_t RemovePermissionUsedRecords(AccessTokenID tokenId) override; 53 int32_t GetPermissionUsedRecords( 54 const PermissionUsedRequestParcel& request, PermissionUsedResultParcel& resultParcel) override; 55 int32_t GetPermissionUsedRecordsAsync( 56 const PermissionUsedRequestParcel& request, const sptr<OnPermissionUsedRecordCallback>& callback) override; 57 int32_t RegisterPermActiveStatusCallback( 58 const std::vector<std::string>& permList, const sptr<IRemoteObject>& callback) override; 59 int32_t UnRegisterPermActiveStatusCallback(const sptr<IRemoteObject>& callback) override; 60 int32_t IsAllowedUsingPermission( 61 AccessTokenID tokenId, const std::string& permissionName, int32_t pid, bool& isAllowed) override; 62 int32_t GetPermissionUsedTypeInfos(const AccessTokenID tokenId, const std::string& permissionName, 63 std::vector<PermissionUsedTypeInfoParcel>& resultsParcel) override; 64 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 65 int32_t SetMutePolicy(uint32_t policyType, uint32_t callerType, bool isMute, AccessTokenID tokenID) override; 66 int32_t SetHapWithFGReminder(uint32_t tokenId, bool isAllowed) override; 67 private: 68 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 69 bool Initialize(); 70 int32_t ResponseDumpCommand(int32_t fd, const std::vector<std::u16string>& args); 71 std::shared_ptr<ProxyDeathHandler> GetProxyDeathHandler(); 72 void ProcessProxyDeathStub(const sptr<IRemoteObject>& anonyStub, int32_t callerPid); 73 void ReleaseDeathStub(int32_t callerPid); 74 75 bool IsPrivilegedCalling() const; 76 bool IsAccessTokenCalling() const; 77 bool IsSystemAppCalling() const; 78 bool VerifyPermission(const std::string& permission) const; 79 static const int32_t ACCESSTOKEN_UID = 3020; 80 AccessTokenID secCompTokenId_ = 0; 81 static const int32_t ROOT_UID = 0; 82 83 ServiceRunningState state_; 84 85 #ifdef EVENTHANDLER_ENABLE 86 std::shared_ptr<AppExecFwk::EventRunner> eventRunner_; 87 std::shared_ptr<AccessEventHandler> eventHandler_; 88 #endif 89 std::mutex deathHandlerMutex_; 90 std::shared_ptr<ProxyDeathHandler> proxyDeathHandler_; 91 }; 92 } // namespace AccessToken 93 } // namespace Security 94 } // namespace OHOS 95 #endif // PRIVACY_MANAGER_SERVICE_H 96