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