1 /*
2  * Copyright (c) 2024 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 FOUNDATION_ACE_ADAPTER_OHOS_CAPABILITY_EVENT_PROXY_PACKAGE_H
17 #define FOUNDATION_ACE_ADAPTER_OHOS_CAPABILITY_EVENT_PROXY_PACKAGE_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <set>
22 
23 #include "common_event_subscriber.h"
24 
25 #include "base/memory/referenced.h"
26 #include "base/utils/noncopyable.h"
27 #include "core/event/package/package_event_proxy.h"
28 
29 namespace OHOS::Ace {
30 using EventFwk::CommonEventData;
31 using EventFwk::CommonEventSubscribeInfo;
32 using EventFwk::CommonEventSubscriber;
33 
34 /**
35  * @brief The PackageEventSubscriber class is a subclass of CommonEventSubscriber.
36  * It represents a subscriber for package events.
37  */
38 class PackageEventSubscriber : public CommonEventSubscriber {
39 public:
PackageEventSubscriber(const CommonEventSubscribeInfo & subscribeInfo)40     explicit PackageEventSubscriber(const CommonEventSubscribeInfo& subscribeInfo)
41         : CommonEventSubscriber(subscribeInfo) {}
42     void OnReceiveEvent(const CommonEventData& data) override;
43 };
44 
45 class ACE_EXPORT PackageEventProxyOhos : public PackageEventProxy {
46 public:
47     PackageEventProxyOhos();
48     ~PackageEventProxyOhos() override;
49 
50     void Register(const WeakPtr<PackageChangeListener>& listener) override;
51 
52     void OnPackageChange() override;
53 
54 private:
55     /**
56      * @brief A set of package change listeners paired with their Container IDs.
57      *
58      * Need to switch to the correct container ID before notifying the listener.
59      */
60     std::set<std::pair<WeakPtr<PackageChangeListener>, int32_t>> listeners_;
61 
62     std::shared_ptr<PackageEventSubscriber> packageEventSubscriber_;
63 
64     ACE_DISALLOW_COPY_AND_MOVE(PackageEventProxyOhos);
65 };
66 } // namespace OHOS::Ace
67 
68 #endif // FOUNDATION_ACE_ADAPTER_OHOS_CAPABILITY_EVENT_PROXY_PACKAGE_H