1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#ifndef OHOS_AAFWK_CONNECTION_OBSERVER_CONTROLLER_H
17eace7efcSopenharmony_ci#define OHOS_AAFWK_CONNECTION_OBSERVER_CONTROLLER_H
18eace7efcSopenharmony_ci
19eace7efcSopenharmony_ci#include <mutex>
20eace7efcSopenharmony_ci#include <vector>
21eace7efcSopenharmony_ci#include "cpp/mutex.h"
22eace7efcSopenharmony_ci
23eace7efcSopenharmony_ci#include "iconnection_observer.h"
24eace7efcSopenharmony_ci
25eace7efcSopenharmony_cinamespace OHOS {
26eace7efcSopenharmony_cinamespace AAFwk {
27eace7efcSopenharmony_ci/**
28eace7efcSopenharmony_ci * @class ConnectionObserverController
29eace7efcSopenharmony_ci * ConnectionObserverController manage connection observers.
30eace7efcSopenharmony_ci */
31eace7efcSopenharmony_ciclass ConnectionObserverController : public std::enable_shared_from_this<ConnectionObserverController> {
32eace7efcSopenharmony_cipublic:
33eace7efcSopenharmony_ci    ConnectionObserverController() = default;
34eace7efcSopenharmony_ci    ~ConnectionObserverController() = default;
35eace7efcSopenharmony_ci
36eace7efcSopenharmony_ci    /**
37eace7efcSopenharmony_ci     * add connection observer.
38eace7efcSopenharmony_ci     *
39eace7efcSopenharmony_ci     * @param observer the observer callback.
40eace7efcSopenharmony_ci     * @return Returns ERR_OK on success, others on failure.
41eace7efcSopenharmony_ci     */
42eace7efcSopenharmony_ci    int AddObserver(const sptr<AbilityRuntime::IConnectionObserver> &observer);
43eace7efcSopenharmony_ci
44eace7efcSopenharmony_ci    /**
45eace7efcSopenharmony_ci     * delete a callback.
46eace7efcSopenharmony_ci     *
47eace7efcSopenharmony_ci     * @param observer the observer callback.
48eace7efcSopenharmony_ci     */
49eace7efcSopenharmony_ci    void RemoveObserver(const sptr<AbilityRuntime::IConnectionObserver> &observer);
50eace7efcSopenharmony_ci
51eace7efcSopenharmony_ci    /**
52eace7efcSopenharmony_ci     * notify observers that extension was connected.
53eace7efcSopenharmony_ci     *
54eace7efcSopenharmony_ci     * @param data connection data.
55eace7efcSopenharmony_ci     */
56eace7efcSopenharmony_ci    void NotifyExtensionConnected(const AbilityRuntime::ConnectionData& data);
57eace7efcSopenharmony_ci
58eace7efcSopenharmony_ci    /**
59eace7efcSopenharmony_ci     * notify observers that extension was disconnected.
60eace7efcSopenharmony_ci     *
61eace7efcSopenharmony_ci     * @param data connection data.
62eace7efcSopenharmony_ci     */
63eace7efcSopenharmony_ci    void NotifyExtensionDisconnected(const AbilityRuntime::ConnectionData& data);
64eace7efcSopenharmony_ci
65eace7efcSopenharmony_ci#ifdef WITH_DLP
66eace7efcSopenharmony_ci    /**
67eace7efcSopenharmony_ci     * notify observers that dlp ability was opened.
68eace7efcSopenharmony_ci     *
69eace7efcSopenharmony_ci     * @param data dlp state data.
70eace7efcSopenharmony_ci     */
71eace7efcSopenharmony_ci    void NotifyDlpAbilityOpened(const AbilityRuntime::DlpStateData& data);
72eace7efcSopenharmony_ci
73eace7efcSopenharmony_ci    /**
74eace7efcSopenharmony_ci     * notify observers that dlp ability was closed.
75eace7efcSopenharmony_ci     *
76eace7efcSopenharmony_ci     * @param data dlp state data.
77eace7efcSopenharmony_ci     */
78eace7efcSopenharmony_ci    void NotifyDlpAbilityClosed(const AbilityRuntime::DlpStateData& data);
79eace7efcSopenharmony_ci#endif // WITH_DLP
80eace7efcSopenharmony_ci
81eace7efcSopenharmony_ciprivate:
82eace7efcSopenharmony_ci    std::vector<sptr<AbilityRuntime::IConnectionObserver>> GetObservers();
83eace7efcSopenharmony_ci    void HandleRemoteDied(const wptr<IRemoteObject> &remote);
84eace7efcSopenharmony_ci
85eace7efcSopenharmony_ci    template<typename F, typename... Args>
86eace7efcSopenharmony_ci    void CallObservers(F func, Args&&... args)
87eace7efcSopenharmony_ci    {
88eace7efcSopenharmony_ci        auto observers = GetObservers();
89eace7efcSopenharmony_ci        for (auto& observer : observers) {
90eace7efcSopenharmony_ci            if (observer) {
91eace7efcSopenharmony_ci                (observer->*func)(std::forward<Args>(args)...);
92eace7efcSopenharmony_ci            }
93eace7efcSopenharmony_ci        }
94eace7efcSopenharmony_ci    }
95eace7efcSopenharmony_ci
96eace7efcSopenharmony_ci    class ObserverDeathRecipient : public IRemoteObject::DeathRecipient {
97eace7efcSopenharmony_ci    public:
98eace7efcSopenharmony_ci        using ObserverDeathHandler = std::function<void(const wptr<IRemoteObject> &)>;
99eace7efcSopenharmony_ci        explicit ObserverDeathRecipient(ObserverDeathHandler handler);
100eace7efcSopenharmony_ci        ~ObserverDeathRecipient() = default;
101eace7efcSopenharmony_ci        void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
102eace7efcSopenharmony_ci
103eace7efcSopenharmony_ci    private:
104eace7efcSopenharmony_ci        ObserverDeathHandler deathHandler_;
105eace7efcSopenharmony_ci    };
106eace7efcSopenharmony_ci
107eace7efcSopenharmony_ciprivate:
108eace7efcSopenharmony_ci    ffrt::mutex observerLock_;
109eace7efcSopenharmony_ci    std::vector<sptr<AbilityRuntime::IConnectionObserver>> observers_;
110eace7efcSopenharmony_ci    sptr<IRemoteObject::DeathRecipient> observerDeathRecipient_;
111eace7efcSopenharmony_ci};
112eace7efcSopenharmony_ci}  // namespace AAFwk
113eace7efcSopenharmony_ci}  // namespace OHOS
114eace7efcSopenharmony_ci#endif  // OHOS_AAFWK_CONNECTION_OBSERVER_CONTROLLER_H
115