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 #ifdef WITH_DLP 66 /** 67 * notify observers that dlp ability was opened. 68 * 69 * @param data dlp state data. 70 */ 71 void NotifyDlpAbilityOpened(const AbilityRuntime::DlpStateData& data); 72 73 /** 74 * notify observers that dlp ability was closed. 75 * 76 * @param data dlp state data. 77 */ 78 void NotifyDlpAbilityClosed(const AbilityRuntime::DlpStateData& data); 79 #endif // WITH_DLP 80 81 private: 82 std::vector<sptr<AbilityRuntime::IConnectionObserver>> GetObservers(); 83 void HandleRemoteDied(const wptr<IRemoteObject> &remote); 84 85 template<typename F, typename... Args> CallObservers(F func,Args &&...args)86 void CallObservers(F func, Args&&... args) 87 { 88 auto observers = GetObservers(); 89 for (auto& observer : observers) { 90 if (observer) { 91 (observer->*func)(std::forward<Args>(args)...); 92 } 93 } 94 } 95 96 class ObserverDeathRecipient : public IRemoteObject::DeathRecipient { 97 public: 98 using ObserverDeathHandler = std::function<void(const wptr<IRemoteObject> &)>; 99 explicit ObserverDeathRecipient(ObserverDeathHandler handler); 100 ~ObserverDeathRecipient() = default; 101 void OnRemoteDied(const wptr<IRemoteObject> &remote) final; 102 103 private: 104 ObserverDeathHandler deathHandler_; 105 }; 106 107 private: 108 ffrt::mutex observerLock_; 109 std::vector<sptr<AbilityRuntime::IConnectionObserver>> observers_; 110 sptr<IRemoteObject::DeathRecipient> observerDeathRecipient_; 111 }; 112 } // namespace AAFwk 113 } // namespace OHOS 114 #endif // OHOS_AAFWK_CONNECTION_OBSERVER_CONTROLLER_H 115