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 OHOS_AAFWK_CONNECTION_OBSERVER_CONTROLLER_H 17 #define OHOS_AAFWK_CONNECTION_OBSERVER_CONTROLLER_H 18 19 #include <mutex> 20 #include <vector> 21 #include "cpp/mutex.h" 22 23 #include "iconnection_observer.h" 24 25 namespace OHOS { 26 namespace AAFwk { 27 /** 28 * @class ConnectionObserverController 29 * ConnectionObserverController manage connection observers. 30 */ 31 class ConnectionObserverController : public std::enable_shared_from_this<ConnectionObserverController> { 32 public: 33 ConnectionObserverController() = default; 34 ~ConnectionObserverController() = default; 35 36 /** 37 * add connection observer. 38 * 39 * @param observer the observer callback. 40 * @return Returns ERR_OK on success, others on failure. 41 */ 42 int AddObserver(const sptr<AbilityRuntime::IConnectionObserver> &observer); 43 44 /** 45 * delete a callback. 46 * 47 * @param observer the observer callback. 48 */ 49 void RemoveObserver(const sptr<AbilityRuntime::IConnectionObserver> &observer); 50 51 /** 52 * notify observers that extension was connected. 53 * 54 * @param data connection data. 55 */ 56 void NotifyExtensionConnected(const AbilityRuntime::ConnectionData& data); 57 58 /** 59 * notify observers that extension was disconnected. 60 * 61 * @param data connection data. 62 */ 63 void NotifyExtensionDisconnected(const AbilityRuntime::ConnectionData& data); 64 65 /** 66 * notify observers that dlp ability was opened. 67 * 68 * @param data dlp state data. 69 */ 70 void NotifyDlpAbilityOpened(const AbilityRuntime::DlpStateData& data); 71 72 /** 73 * notify observers that dlp ability was closed. 74 * 75 * @param data dlp state data. 76 */ 77 void NotifyDlpAbilityClosed(const AbilityRuntime::DlpStateData& data); 78 79 private: 80 std::vector<sptr<AbilityRuntime::IConnectionObserver>> GetObservers(); 81 void HandleRemoteDied(const wptr<IRemoteObject> &remote); 82 83 template<typename F, typename... Args> CallObservers(F func,Args &&...args)84 void CallObservers(F func, Args&&... args) 85 { 86 auto observers = GetObservers(); 87 for (auto& observer : observers) { 88 if (observer) { 89 (observer->*func)(std::forward<Args>(args)...); 90 } 91 } 92 } 93 94 class ObserverDeathRecipient : public IRemoteObject::DeathRecipient { 95 public: 96 using ObserverDeathHandler = std::function<void(const wptr<IRemoteObject> &)>; 97 explicit ObserverDeathRecipient(ObserverDeathHandler handler); 98 ~ObserverDeathRecipient() = default; 99 void OnRemoteDied(const wptr<IRemoteObject> &remote) final; 100 101 private: 102 ObserverDeathHandler deathHandler_; 103 }; 104 105 private: 106 ffrt::mutex observerLock_; 107 std::vector<sptr<AbilityRuntime::IConnectionObserver>> observers_; 108 sptr<IRemoteObject::DeathRecipient> observerDeathRecipient_; 109 }; 110 } // namespace AAFwk 111 } // namespace OHOS 112 #endif // OHOS_AAFWK_CONNECTION_OBSERVER_CONTROLLER_H 113