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