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 FOUNDATION_ABILITY_FORM_FWK_SERVICES_INCLUDE_FORM_EVENT_HANDLER_H
17 #define FOUNDATION_ABILITY_FORM_FWK_SERVICES_INCLUDE_FORM_EVENT_HANDLER_H
18 
19 #include <memory>
20 #include <set>
21 #include "event_handler.h"
22 #include "event_runner.h"
23 #include "form_serial_queue.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace MSG {
28 const int64_t FORM_SHARE_INFO_DELAY_MSG = 1;
29 const int64_t FORM_PACKAGE_FREE_INSTALL_DELAY_MSG = 2;
30 }
31 
32 class FormEventTimeoutObserver {
33 public:
34     FormEventTimeoutObserver() = default;
35     virtual ~FormEventTimeoutObserver() = default;
36 
37     virtual void OnEventTimeoutResponse(int64_t msg, int64_t eventId) = 0;
38 };
39 
40 /**
41  * @class FormEventHandler
42  * FormEventHandler handling the form event.
43  */
44 class FormEventHandler : public std::enable_shared_from_this<FormEventHandler> {
45 public:
46     FormEventHandler(const std::shared_ptr<FormSerialQueue> &serialQueue);
47     virtual ~FormEventHandler() = default;
48 
49     static int64_t GetEventId();
50 
51     /**
52      * Register event timeout observer.
53      *
54      * @param observer The observer of form event timeout.
55      */
56     void RegisterEventTimeoutObserver(const std::shared_ptr<FormEventTimeoutObserver> &observer);
57 
58     /**
59      * Unregister event timeout observer.
60      *
61      * @param observer The observer of form event timeout.
62      */
63     void UnregisterEventTimeoutObserver(const std::shared_ptr<FormEventTimeoutObserver> &observer);
64 
65     /**
66      * ProcessEvent with request.
67      *
68      * @param msg, The event message.
69      * @param eventId, The event Id.
70      * @param delayTime, The delay time.
71      */
72     void ProcessEvent(int64_t msg, int64_t eventId, int64_t delayTime = 0);
73 
74 private:
75     static int64_t eventId_;
76     std::shared_ptr<FormSerialQueue> serialQueue_ = nullptr;
77     std::set<std::shared_ptr<FormEventTimeoutObserver>> observers_;
78     mutable std::mutex observerMutex_;
79 };
80 } // namespace AppExecFwk
81 } // namespace OHOS
82 #endif // FOUNDATION_ABILITY_FORM_FWK_SERVICES_INCLUDE_FORM_EVENT_HANDLER_H
83