• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_recipient.h"
26 #include "app_status_change_callback.h"
27 #include "audio_global_switch_change_stub.h"
28 #include "camera_service_callback_stub.h"
29 #include "hap_token_info.h"
30 #include "nocopyable.h"
31 #include "on_permission_used_record_callback.h"
32 #include "permission_record.h"
33 #include "permission_used_request.h"
34 #include "permission_used_result.h"
35 #include "rwlock.h"
36 #include "thread_pool.h"
37 #include "window_manager_privacy_agent.h"
38 
39 namespace OHOS {
40 namespace Security {
41 namespace AccessToken {
42 
43 class PermissionRecordManager final {
44 public:
45     static PermissionRecordManager& GetInstance();
46     virtual ~PermissionRecordManager();
47 
48     void Init();
49     int32_t AddPermissionUsedRecord(
50         AccessTokenID tokenId, const std::string& permissionName, int32_t successCount, int32_t failCount);
51     void RemovePermissionUsedRecords(AccessTokenID tokenId, const std::string& deviceID);
52     int32_t GetPermissionUsedRecords(const PermissionUsedRequest& request, PermissionUsedResult& result);
53     int32_t GetPermissionUsedRecordsAsync(
54         const PermissionUsedRequest& request, const sptr<OnPermissionUsedRecordCallback>& callback);
55     int32_t StartUsingPermission(AccessTokenID tokenId, const std::string& permissionName);
56     int32_t StartUsingPermission(AccessTokenID tokenId, const std::string& permissionName,
57         const sptr<IRemoteObject>& callback);
58     int32_t StopUsingPermission(AccessTokenID tokenId, const std::string& permissionName);
59     int32_t RegisterPermActiveStatusCallback(
60         const std::vector<std::string>& permList, const sptr<IRemoteObject>& callback);
61     int32_t UnRegisterPermActiveStatusCallback(const sptr<IRemoteObject>& callback);
62 
63     void CallbackExecute(AccessTokenID tokenId, const std::string& permissionName, int32_t status);
64     int32_t PermissionListFilter(const std::vector<std::string>& listSrc, std::vector<std::string>& listRes);
65     bool IsAllowedUsingPermission(AccessTokenID tokenId, const std::string& permissionName);
66 
67     void NotifyMicChange(bool switchStatus);
68     void NotifyCameraChange(bool switchStatus);
69     void NotifyAppStateChange(AccessTokenID tokenId, ActiveChangeType status);
70     void NotifyCameraFloatWindowChange(AccessTokenID tokenId, bool isShowing);
71     void OnAppMgrRemoteDiedHandle();
72     void OnAudioMgrRemoteDiedHandle();
73     void OnCameraMgrRemoteDiedHandle();
74     void OnWindowMgrRemoteDiedHandle();
75 
76 private:
77     PermissionRecordManager();
78     DISALLOW_COPY_AND_MOVE(PermissionRecordManager);
79 
80     void GetLocalRecordTokenIdList(std::set<AccessTokenID>& tokenIdList);
81     void AddRecord(const PermissionRecord& record);
82     int32_t GetPermissionRecord(AccessTokenID tokenId, const std::string& permissionName,
83         int32_t successCount, int32_t failCount, PermissionRecord& record);
84     bool CreateBundleUsedRecord(const AccessTokenID tokenId, BundleUsedRecord& bundleRecord);
85     void ExecuteDeletePermissionRecordTask();
86     int32_t DeletePermissionRecord(int64_t days);
87     bool GetRecordsFromLocalDB(const PermissionUsedRequest& request, PermissionUsedResult& result);
88     void GetRecords(int32_t flag, std::vector<GenericValues> recordValues,
89         BundleUsedRecord& bundleRecord, PermissionUsedResult& result);
90     void UpdateRecords(int32_t flag, const PermissionUsedRecord& inBundleRecord, PermissionUsedRecord& outBundleRecord);
91 
92     void FindRecordsToUpdateAndExecuted(uint32_t tokenId, ActiveChangeType status);
93     void RemoveRecordFromStartList(const PermissionRecord& record);
94     void UpdateRecord(const PermissionRecord& record);
95     bool GetRecordFromStartList(uint32_t tokenId,  int32_t opCode, PermissionRecord& record);
96     bool AddRecordIfNotStarted(const PermissionRecord& record);
97 
98     std::string GetDeviceId(AccessTokenID tokenId);
99     void PermListToString(const std::vector<std::string>& permList);
100     bool GetGlobalSwitchStatus(const std::string& permissionName);
101     void SavePermissionRecords(const std::string& permissionName, PermissionRecord& record, bool switchStatus);
102     bool ShowGlobalDialog(const std::string& permissionName);
103 
104     void ExecuteCameraCallbackAsync(AccessTokenID tokenId);
105     void SetCameraCallback(sptr<IRemoteObject>);
106 
107     bool IsFlowWindowShow(AccessTokenID tokenId);
108     int32_t GetAppStatus(AccessTokenID tokenId);
109 
110     bool Register();
111     void Unregister();
112 private:
113     OHOS::ThreadPool deleteTaskWorker_;
114     bool hasInited_;
115     OHOS::Utils::RWLock rwLock_;
116     std::mutex startRecordListMutex_;
117     std::vector<PermissionRecord> startRecordList_;
118     std::mutex cameraMutex_;
119     sptr<IRemoteObject> cameraCallback_ = nullptr;
120 
121     // microphone
122     std::mutex micMuteMutex_;
123     sptr<AudioRoutingManagerListenerStub> micMuteCallback_ = nullptr;
124 
125     // camera
126     std::mutex camMuteMutex_;
127     sptr<CameraServiceCallbackStub> camMuteCallback_ = nullptr;
128 
129     // appState
130     std::mutex appStateMutex_;
131     sptr<ApplicationStateObserverStub> appStateCallback_ = nullptr;
132 
133     // camera float window
134     AccessTokenID floatWindowTokenId_ = 0;
135     bool camFloatWindowShowing_ = false;
136     std::mutex floatWinMutex_;
137     sptr<WindowManagerPrivacyAgent> floatWindowCallback_ = nullptr;
138 };
139 } // namespace AccessToken
140 } // namespace Security
141 } // namespace OHOS
142 #endif // PERMISSION_RECORD_MANAGER_H