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