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 DLP_PERMISSION_CALLBACK_MANAGER_H
17 #define DLP_PERMISSION_CALLBACK_MANAGER_H
18 
19 #include <mutex>
20 #include <map>
21 
22 #include "dlp_sandbox_info.h"
23 #include "iremote_broker.h"
24 
25 namespace OHOS {
26 namespace Security {
27 namespace DlpPermission {
28 struct DlpSandboxChangeCallbackRecord {
DlpSandboxChangeCallbackRecordDlpSandboxChangeCallbackRecord29     DlpSandboxChangeCallbackRecord() : callbackObject_(nullptr) {}
DlpSandboxChangeCallbackRecordDlpSandboxChangeCallbackRecord30     explicit DlpSandboxChangeCallbackRecord(sptr<IRemoteObject> callback) : callbackObject_(callback) {}
31 
32     sptr<IRemoteObject> callbackObject_;
33     int32_t pid = 0;
34 };
35 
36 class DlpSandboxChangeCallbackManager {
37 public:
38     virtual ~DlpSandboxChangeCallbackManager();
39     DlpSandboxChangeCallbackManager();
40     static DlpSandboxChangeCallbackManager &GetInstance();
41 
42     int32_t AddCallback(int32_t pid, const sptr<IRemoteObject> &callback);
43     int32_t RemoveCallback(const sptr<IRemoteObject>& callback);
44     int32_t RemoveCallback(int32_t pid, bool &result);
45     void ExecuteCallbackAsync(const DlpSandboxInfo &dlpSandboxInfo);
46 
47 private:
48     std::mutex mutex_;
49     std::map<int32_t, DlpSandboxChangeCallbackRecord> callbackInfoMap_;
50     sptr<IRemoteObject::DeathRecipient> callbackDeathRecipient_;
51 };
52 } // namespace DlpPermission
53 } // namespace Security
54 } // namespace OHOS
55 #endif // DLP_PERMISSION_CALLBACK_MANAGER_H
56