1 /*
2  * Copyright (c) 2023 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_FRAMEWORKS_CORE_EVENT_TIME_EVENT_PROXY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_TIME_EVENT_PROXY_H
18 
19 #include <mutex>
20 #include <set>
21 
22 #include "base/memory/referenced.h"
23 #include "base/utils/noncopyable.h"
24 #include "core/event/time/time_change_listener.h"
25 
26 namespace OHOS::Ace {
27 /**
28  * @class TimeEventProxy
29  * @brief The TimeEventProxy class is responsible for managing time event subscriptions and notifying subscribers.
30  *        It provides a singleton instance and allows subscribers to register for time events.
31  */
32 class TimeEventProxy {
33 public:
34     virtual ~TimeEventProxy() = default;
35 
36     /**
37      * @brief Get the singleton instance of TimeEventProxy.
38      * @return The singleton instance of TimeEventProxy.
39      */
40     ACE_FORCE_EXPORT static TimeEventProxy* GetInstance();
41 
42     /**
43      * @brief Register a time event listener.
44      * @param listener The time event listener to register.
45      */
46     virtual void Register(const WeakPtr<TimeChangeListener>& listener) = 0;
47 
48     virtual void OnTimeChange() = 0;
49 
50 protected:
51     TimeEventProxy() = default;
52 
53 private:
54 
55     static std::unique_ptr<TimeEventProxy> instance_; /**< The singleton instance of TimeEventProxy. */
56     static std::mutex mutex_;                         /**< Mutex for thread-safe creation of the singleton instance. */
57 
58     ACE_DISALLOW_COPY_AND_MOVE(TimeEventProxy);
59 };
60 } // namespace OHOS::Ace
61 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_TIME_EVENT_PROXY_H
62