1 /* 2 * Copyright (c) 2022-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 PERMISSION_RECORD_MANAGER_H 17 #define PERMISSION_RECORD_MANAGER_H 18 19 #include <vector> 20 #include <set> 21 #include <string> 22 23 #include "access_token.h" 24 #include "active_change_response_info.h" 25 #include "app_manager_death_callback.h" 26 #include "app_manager_death_recipient.h" 27 #include "app_status_change_callback.h" 28 #include "audio_global_switch_change_stub.h" 29 #include "camera_service_callback_stub.h" 30 #include "hap_token_info.h" 31 #include "nocopyable.h" 32 #include "on_permission_used_record_callback.h" 33 #include "permission_record.h" 34 #include "permission_used_request.h" 35 #include "permission_used_result.h" 36 #include "rwlock.h" 37 #include "thread_pool.h" 38 #ifdef CAMERA_FLOAT_WINDOW_ENABLE 39 #include "window_manager_privacy_agent.h" 40 #endif 41 #ifdef CUSTOMIZATION_CONFIG_POLICY_ENABLE 42 #include "nlohmann/json.hpp" 43 #include "permission_record_config.h" 44 #endif 45 46 namespace OHOS { 47 namespace Security { 48 namespace AccessToken { 49 class PrivacyAppStateObserver : public ApplicationStateObserverStub { 50 public: 51 PrivacyAppStateObserver() = default; 52 ~PrivacyAppStateObserver() = default; 53 54 void OnForegroundApplicationChanged(const AppStateData &appStateData) override; 55 DISALLOW_COPY_AND_MOVE(PrivacyAppStateObserver); 56 }; 57 58 class PrivacyAppManagerDeathCallback : public AppManagerDeathCallback { 59 public: 60 PrivacyAppManagerDeathCallback() = default; 61 ~PrivacyAppManagerDeathCallback() = default; 62 63 void NotifyAppManagerDeath() override; 64 DISALLOW_COPY_AND_MOVE(PrivacyAppManagerDeathCallback); 65 }; 66 67 class PermissionRecordManager final { 68 public: 69 static PermissionRecordManager& GetInstance(); 70 virtual ~PermissionRecordManager(); 71 72 void Init(); 73 int32_t AddPermissionUsedRecord( 74 AccessTokenID tokenId, const std::string& permissionName, int32_t successCount, int32_t failCount); 75 void RemovePermissionUsedRecords(AccessTokenID tokenId, const std::string& deviceID); 76 int32_t GetPermissionUsedRecords(const PermissionUsedRequest& request, PermissionUsedResult& result); 77 int32_t GetPermissionUsedRecordsAsync( 78 const PermissionUsedRequest& request, const sptr<OnPermissionUsedRecordCallback>& callback); 79 int32_t StartUsingPermission(AccessTokenID tokenId, const std::string& permissionName); 80 int32_t StartUsingPermission(AccessTokenID tokenId, const std::string& permissionName, 81 const sptr<IRemoteObject>& callback); 82 int32_t StopUsingPermission(AccessTokenID tokenId, const std::string& permissionName); 83 int32_t RegisterPermActiveStatusCallback( 84 const std::vector<std::string>& permList, const sptr<IRemoteObject>& callback); 85 int32_t UnRegisterPermActiveStatusCallback(const sptr<IRemoteObject>& callback); 86 87 void CallbackExecute(AccessTokenID tokenId, const std::string& permissionName, int32_t status); 88 int32_t PermissionListFilter(const std::vector<std::string>& listSrc, std::vector<std::string>& listRes); 89 bool IsAllowedUsingPermission(AccessTokenID tokenId, const std::string& permissionName); 90 91 void NotifyMicChange(bool switchStatus); 92 void NotifyCameraChange(bool switchStatus); 93 void NotifyAppStateChange(AccessTokenID tokenId, ActiveChangeType status); 94 void NotifyLockScreenStatusChange(LockScreenStatusChangeType lockScreenStatus); 95 96 #ifdef CAMERA_FLOAT_WINDOW_ENABLE 97 void NotifyCameraFloatWindowChange(AccessTokenID tokenId, bool isShowing); 98 void OnWindowMgrRemoteDiedHandle(); 99 #endif 100 void OnAppMgrRemoteDiedHandle(); 101 void OnAudioMgrRemoteDiedHandle(); 102 void OnCameraMgrRemoteDiedHandle(); 103 int32_t GetRecordSizeMaxImum(); 104 int32_t GetRecordAgingTime(); 105 106 private: 107 PermissionRecordManager(); 108 DISALLOW_COPY_AND_MOVE(PermissionRecordManager); 109 110 void GetLocalRecordTokenIdList(std::set<AccessTokenID>& tokenIdList); 111 void AddRecord(const PermissionRecord& record); 112 int32_t GetPermissionRecord(AccessTokenID tokenId, const std::string& permissionName, 113 int32_t successCount, int32_t failCount, PermissionRecord& record); 114 bool CreateBundleUsedRecord(const AccessTokenID tokenId, BundleUsedRecord& bundleRecord); 115 void ExecuteDeletePermissionRecordTask(); 116 int32_t DeletePermissionRecord(int32_t days); 117 bool GetRecordsFromLocalDB(const PermissionUsedRequest& request, PermissionUsedResult& result); 118 void GetRecords(int32_t flag, std::vector<GenericValues> recordValues, 119 BundleUsedRecord& bundleRecord, PermissionUsedResult& result); 120 void UpdateRecords(int32_t flag, const PermissionUsedRecord& inBundleRecord, PermissionUsedRecord& outBundleRecord); 121 122 void FindRecordsToUpdateAndExecuted(uint32_t tokenId, ActiveChangeType status); 123 void GenerateRecordsWhenScreenStatusChanged(LockScreenStatusChangeType lockScreenStatus); 124 void RemoveRecordFromStartList(const PermissionRecord& record); 125 void UpdateRecord(const PermissionRecord& record); 126 bool GetRecordFromStartList(uint32_t tokenId, int32_t opCode, PermissionRecord& record); 127 bool AddRecordToStartList(const PermissionRecord& record); 128 129 std::string GetDeviceId(AccessTokenID tokenId); 130 void PermListToString(const std::vector<std::string>& permList); 131 bool GetGlobalSwitchStatus(const std::string& permissionName); 132 void SavePermissionRecords(const std::string& permissionName, PermissionRecord& record, bool switchStatus); 133 bool ShowGlobalDialog(const std::string& permissionName); 134 135 void ExecuteCameraCallbackAsync(AccessTokenID tokenId); 136 void SetCameraCallback(sptr<IRemoteObject>); 137 138 #ifdef CAMERA_FLOAT_WINDOW_ENABLE 139 bool IsFlowWindowShow(AccessTokenID tokenId); 140 #endif 141 int32_t GetAppStatus(AccessTokenID tokenId); 142 143 bool RegisterAppStatusAndLockScreenStatusListener(); 144 bool Register(); 145 bool RegisterApplicationStateObserver(); 146 void Unregister(); 147 148 #ifdef CUSTOMIZATION_CONFIG_POLICY_ENABLE 149 void GetConfigFilePathList(std::vector<std::string> &pathList); 150 void from_json(const nlohmann::json& j, PermissionRecordConfig& p); 151 bool GetConfigValueFromFile(std::string& fileContent); 152 #endif 153 void SetDefaultConfigValue(); 154 void GetConfigValue(); 155 private: 156 OHOS::ThreadPool deleteTaskWorker_; 157 bool hasInited_; 158 OHOS::Utils::RWLock rwLock_; 159 std::mutex startRecordListMutex_; 160 std::vector<PermissionRecord> startRecordList_; 161 std::mutex cameraMutex_; 162 std::mutex cameraCallbackMutex_; 163 sptr<IRemoteObject> cameraCallback_ = nullptr; 164 165 // microphone 166 std::mutex micMuteMutex_; 167 std::mutex micCallbackMutex_; 168 bool isMicMute_ = false; 169 sptr<AudioRoutingManagerListenerStub> micMuteCallback_ = nullptr; 170 171 // camera 172 std::mutex camMuteMutex_; 173 bool isCameraMute_ = false; 174 sptr<CameraServiceCallbackStub> camMuteCallback_ = nullptr; 175 176 // appState 177 std::mutex appStateMutex_; 178 sptr<PrivacyAppStateObserver> appStateCallback_ = nullptr; 179 180 // app manager death 181 std::mutex appManagerDeathMutex_; 182 std::shared_ptr<PrivacyAppManagerDeathCallback> appManagerDeathCallback_ = nullptr; 183 184 // lockScreenState 185 std::mutex lockScreenStateMutex_; 186 187 // camera float window 188 #ifdef CAMERA_FLOAT_WINDOW_ENABLE 189 AccessTokenID floatWindowTokenId_ = 0; 190 bool camFloatWindowShowing_ = false; 191 std::mutex floatWinMutex_; 192 sptr<WindowManagerPrivacyAgent> floatWindowCallback_ = nullptr; 193 #endif 194 195 // record config 196 int32_t recordSizeMaximum_ = 0; 197 int32_t recordAgingTime_ = 0; 198 }; 199 } // namespace AccessToken 200 } // namespace Security 201 } // namespace OHOS 202 #endif // PERMISSION_RECORD_MANAGER_H