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 ABILITY_RUNTIME_CONNECTION_OBSERVER_CLIENT_IMPL_H 17 #define ABILITY_RUNTIME_CONNECTION_OBSERVER_CLIENT_IMPL_H 18 19 #include <mutex> 20 #include <unordered_set> 21 22 #include "connection_observer.h" 23 #include "dlp_connection_info.h" 24 #include "service_proxy_adapter.h" 25 26 namespace OHOS { 27 namespace AbilityRuntime { 28 /** 29 * @class ConnectionObserverClientImpl 30 * ConnectionObserverClientImpl is used to manage connection observer. 31 */ 32 class ConnectionObserverClientImpl : public std::enable_shared_from_this<ConnectionObserverClientImpl> { 33 public: 34 ConnectionObserverClientImpl() = default; 35 virtual ~ConnectionObserverClientImpl() = default; 36 37 int32_t RegisterObserver(const std::shared_ptr<ConnectionObserver> &observer); 38 int32_t UnregisterObserver(const std::shared_ptr<ConnectionObserver> &observer); 39 int32_t GetDlpConnectionInfos(std::vector<DlpConnectionInfo> &infos); 40 void HandleExtensionConnected(const ConnectionData &data); 41 void HandleExtensionDisconnected(const ConnectionData &data); 42 void HandleDlpAbilityOpened(const DlpStateData &data); 43 void HandleDlpAbilityClosed(const DlpStateData &data); 44 void HandleRemoteDied(const wptr<IRemoteObject> &remote); 45 46 private: 47 class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { 48 public: ServiceDeathRecipient(const std::shared_ptr<ConnectionObserverClientImpl> & owner)49 explicit ServiceDeathRecipient(const std::shared_ptr<ConnectionObserverClientImpl>& owner) : owner_(owner) {} 50 51 virtual ~ServiceDeathRecipient() = default; 52 53 void OnRemoteDied(const wptr<IRemoteObject>& remote) override; 54 55 private: 56 std::weak_ptr<ConnectionObserverClientImpl> owner_; 57 }; 58 59 std::unordered_set<std::shared_ptr<ConnectionObserver>> GetObservers(); 60 std::shared_ptr<ServiceProxyAdapter> GetServiceProxy(); 61 void ConnectLocked(); 62 bool RegisterObserverToServiceLocked(const std::shared_ptr<ServiceProxyAdapter> &proxy); 63 void UnregisterFromServiceLocked(const std::shared_ptr<ServiceProxyAdapter> &proxy); 64 int32_t AddObserversLocked(const std::shared_ptr<ConnectionObserver> &observer); 65 int32_t RemoveObserversLocked(const std::shared_ptr<ConnectionObserver> &observer); 66 bool ResetProxy(const wptr<IRemoteObject> &remote); 67 void ResetStatus(); 68 void NotifyServiceDiedToObservers(); 69 70 std::recursive_mutex observerLock_; // observer lock 71 bool isRegistered_ = false; // mark whether register observer to abilityms. 72 sptr<IConnectionObserver> observer_; // observer stub 73 std::unordered_set<std::shared_ptr<ConnectionObserver>> userObservers_; // all registered observers. 74 75 std::recursive_mutex proxyLock_; // proxy lock. 76 std::shared_ptr<ServiceProxyAdapter> serviceAdapter_; // abilityms proxy adapter, send request code. 77 sptr<IRemoteObject::DeathRecipient> deathRecipient_; // abilityms death recipient. 78 }; 79 } // namespace AbilityRuntime 80 } // namespace OHOS 81 #endif // ABILITY_RUNTIME_CONNECTION_OBSERVER_CLIENT_IMPL_H 82