• 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 #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 HandleExtensionSuspended(const ConnectionData &data);
45     void HandleExtensionResumed(const ConnectionData &data);
46     void HandleRemoteDied(const wptr<IRemoteObject> &remote);
47 
48 #ifdef WITH_DLP
49     int32_t GetDlpConnectionInfos(std::vector<DlpConnectionInfo> &infos);
50     void HandleDlpAbilityOpened(const DlpStateData &data);
51     void HandleDlpAbilityClosed(const DlpStateData &data);
52 #endif // WITH_DLP
53 
54 private:
55     class ServiceDeathRecipient : public IRemoteObject::DeathRecipient {
56     public:
ServiceDeathRecipient(const std::shared_ptr<ConnectionObserverClientImpl> & owner)57         explicit ServiceDeathRecipient(const std::shared_ptr<ConnectionObserverClientImpl>& owner) : owner_(owner) {}
58 
59         virtual ~ServiceDeathRecipient() = default;
60 
61         void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
62 
63     private:
64         std::weak_ptr<ConnectionObserverClientImpl> owner_;
65     };
66 
67     std::unordered_set<std::shared_ptr<ConnectionObserver>> GetObservers();
68     std::shared_ptr<ServiceProxyAdapter> GetServiceProxy();
69     void ConnectLocked();
70     bool RegisterObserverToServiceLocked(const std::shared_ptr<ServiceProxyAdapter> &proxy);
71     void UnregisterFromServiceLocked(const std::shared_ptr<ServiceProxyAdapter> &proxy);
72     int32_t AddObserversLocked(const std::shared_ptr<ConnectionObserver> &observer);
73     int32_t RemoveObserversLocked(const std::shared_ptr<ConnectionObserver> &observer);
74     bool ResetProxy(const wptr<IRemoteObject> &remote);
75     void ResetStatus();
76     void NotifyServiceDiedToObservers();
77 
78     std::mutex observerLock_; // observer lock
79     bool isRegistered_ = false; // mark whether register observer to abilityms.
80     sptr<IConnectionObserver> observer_; // observer stub
81     std::unordered_set<std::shared_ptr<ConnectionObserver>> userObservers_; // all registered observers.
82 
83     std::mutex proxyLock_; // proxy lock.
84     std::shared_ptr<ServiceProxyAdapter> serviceAdapter_; // abilityms proxy adapter, send request code.
85     sptr<IRemoteObject::DeathRecipient> deathRecipient_; // abilityms death recipient.
86 };
87 } // namespace AbilityRuntime
88 } // namespace OHOS
89 #endif // ABILITY_RUNTIME_CONNECTION_OBSERVER_CLIENT_IMPL_H
90