• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "file_access_notify_manager.h"
17 
18 #include "file_access_framework_errno.h"
19 #include "hilog_wrapper.h"
20 
21 namespace OHOS {
22 namespace FileAccessFwk {
23 std::mutex FileAccessNotifyManager::notifyMapMutex_;
24 
FileAccessNotifyManager()25 FileAccessNotifyManager::FileAccessNotifyManager()
26 {
27 }
28 
~FileAccessNotifyManager()29 FileAccessNotifyManager::~FileAccessNotifyManager()
30 {
31 }
32 
RegisterNotify(sptr<IFileAccessNotify> & notify)33 int FileAccessNotifyManager::RegisterNotify(sptr<IFileAccessNotify> &notify)
34 {
35     if (!AddNotifyToMap(notify)) {
36         HILOG_ERROR("the remote obj is nullptr.");
37         return E_REGISTER;
38     }
39     HILOG_INFO("FileAccessNotifyManager::RegisterNotify success.");
40     return ERR_OK;
41 }
42 
UnregisterNotify(sptr<IFileAccessNotify> & notify)43 int FileAccessNotifyManager::UnregisterNotify(sptr<IFileAccessNotify> &notify)
44 {
45     if (notify == nullptr || notify->AsObject() == nullptr) {
46         HILOG_ERROR("the remote obj is nullptr.");
47         return E_NOTIFY;
48     }
49 
50     if (!RemoveNotifyFromMap(notify->AsObject())) {
51         HILOG_ERROR("remove remote obj from map fail.");
52         return E_REMOVE;
53     }
54     HILOG_INFO("FileAccessNotifyManager::UnregisterNotify success.");
55     return ERR_OK;
56 }
57 
Notify(const NotifyMessage & message)58 int FileAccessNotifyManager::Notify(const NotifyMessage &message)
59 {
60     std::lock_guard<std::mutex> lock(notifyMapMutex_);
61     for (auto iter = notifyMap_.begin(); iter != notifyMap_.end(); iter++) {
62         if (iter->first != nullptr) {
63             iter->first->Notify(message);
64         }
65     }
66     return ERR_OK;
67 }
68 
AddNotifyToMap(const sptr<IFileAccessNotify> & notify)69 bool FileAccessNotifyManager::AddNotifyToMap(const sptr<IFileAccessNotify> &notify)
70 {
71     if (notify == nullptr || notify->AsObject() == nullptr) {
72         HILOG_ERROR("the death notify obj is nullptr.");
73         return false;
74     }
75 
76     std::lock_guard<std::mutex> lock(notifyMapMutex_);
77     for (auto iter = notifyMap_.begin(); iter != notifyMap_.end(); iter++) {
78         if (iter->first == notify) {
79             iter->first->AsObject()->RemoveDeathRecipient(iter->second);
80             notifyMap_.erase(iter);
81         }
82     }
83     sptr<IRemoteObject::DeathRecipient> deathRecipient = new FileAccessNotifyCallbackRecipient(
84         std::bind(&FileAccessNotifyManager::OnCallBackDied, this, std::placeholders::_1));
85     notify->AsObject()->AddDeathRecipient(deathRecipient);
86     notifyMap_.emplace(notify, deathRecipient);
87     return true;
88 }
89 
RemoveNotifyFromMap(const sptr<IRemoteObject> & remote)90 bool FileAccessNotifyManager::RemoveNotifyFromMap(const sptr<IRemoteObject> &remote)
91 {
92     if (remote == nullptr) {
93         HILOG_ERROR("the death remote obj is nullptr.");
94         return false;
95     }
96 
97     std::lock_guard<std::mutex> lock(notifyMapMutex_);
98     for (auto iter = notifyMap_.begin(); iter != notifyMap_.end(); iter++) {
99         if (iter->first->AsObject() == remote) {
100             iter->first->AsObject()->RemoveDeathRecipient(iter->second);
101             notifyMap_.erase(iter);
102             return true;
103         }
104     }
105     HILOG_ERROR("find remote object fail.");
106     return false;
107 }
108 
OnCallBackDied(const wptr<IRemoteObject> & remote)109 void FileAccessNotifyManager::OnCallBackDied(const wptr<IRemoteObject> &remote)
110 {
111     auto object = remote.promote();
112     if (object == nullptr) {
113         HILOG_INFO("the death remote obj is nullptr");
114         return;
115     }
116 
117     if (!RemoveNotifyFromMap(object)) {
118         HILOG_ERROR("remove remote obj from map fail.");
119         return;
120     }
121 }
122 
FileAccessNotifyCallbackRecipient(RemoteDiedHandler handler)123 FileAccessNotifyCallbackRecipient::FileAccessNotifyCallbackRecipient(RemoteDiedHandler handler) : handler_(handler)
124 {
125 }
126 
~FileAccessNotifyCallbackRecipient()127 FileAccessNotifyCallbackRecipient::~FileAccessNotifyCallbackRecipient()
128 {
129 }
130 
OnRemoteDied(const wptr<IRemoteObject> & remote)131 void FileAccessNotifyCallbackRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
132 {
133     if (handler_ != nullptr) {
134         handler_(remote);
135     }
136 }
137 } // namespace FileAccessFwk
138 } // namespace OHOS