1 /* 2 * Copyright (c) 2022 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 FILE_ACCESS_NOTIFY_MANAGER_H 17 #define FILE_ACCESS_NOTIFY_MANAGER_H 18 19 #include <memory> 20 #include <mutex> 21 #include <vector> 22 23 #include "event_handler.h" 24 #include "ifile_access_notify.h" 25 #include "iremote_object.h" 26 27 namespace OHOS { 28 namespace FileAccessFwk { 29 using EventHandler = OHOS::AppExecFwk::EventHandler; 30 class FileAccessNotifyManager final : public std::enable_shared_from_this<FileAccessNotifyManager> { 31 public: 32 using NotifyMapType = std::map<sptr<IFileAccessNotify>, sptr<IRemoteObject::DeathRecipient>>; 33 34 FileAccessNotifyManager(); 35 ~FileAccessNotifyManager(); 36 37 int RegisterNotify(sptr<IFileAccessNotify> ¬ify); 38 int UnregisterNotify(sptr<IFileAccessNotify> ¬ify); 39 int Notify(const NotifyMessage &message); 40 41 private: 42 void OnCallBackDied(const wptr<IRemoteObject> &remote); 43 bool AddNotifyToMap(const sptr<IFileAccessNotify> ¬ify); 44 bool RemoveNotifyFromMap(const sptr<IRemoteObject> &remote); 45 NotifyMapType notifyMap_; 46 static std::mutex notifyMapMutex_; 47 }; 48 49 class FileAccessNotifyCallbackRecipient : public IRemoteObject::DeathRecipient { 50 public: 51 using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>; 52 FileAccessNotifyCallbackRecipient(RemoteDiedHandler handler); 53 virtual ~FileAccessNotifyCallbackRecipient(); 54 virtual void OnRemoteDied(const wptr<IRemoteObject> &remote); 55 56 private: 57 RemoteDiedHandler handler_; 58 }; 59 } // namespace FileAccessFwk 60 } // namespace OHOS 61 #endif // FILE_ACCESS_NOTIFY_MANAGER_H