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 #include "form_event_handler.h"
17 
18 #include <memory>
19 
20 #include "fms_log_wrapper.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
24 int64_t FormEventHandler::eventId_ = 0;
FormEventHandler(const std::shared_ptr<FormSerialQueue> & serialQueue)25 FormEventHandler::FormEventHandler(const std::shared_ptr<FormSerialQueue> &serialQueue)
26     : serialQueue_(serialQueue)
27 {
28 }
29 
ProcessEvent(int64_t msg,int64_t eventId,int64_t delayTime)30 void FormEventHandler::ProcessEvent(int64_t msg, int64_t eventId, int64_t delayTime)
31 {
32     if (serialQueue_ == nullptr) {
33         HILOG_ERROR("null serialQueue_");
34         return;
35     }
36 
37     auto task = [thisWeakPtr = weak_from_this(), msg, eventId]() {
38         auto sharedThis = thisWeakPtr.lock();
39         std::lock_guard<std::mutex> lock(sharedThis->observerMutex_);
40         for (auto &observer : sharedThis->observers_) {
41             if (observer == nullptr) {
42                 HILOG_ERROR("null observer");
43                 continue;
44             }
45             observer->OnEventTimeoutResponse(msg, eventId);
46         }
47     };
48     serialQueue_->ScheduleDelayTask(std::make_pair(msg, eventId), delayTime, task);
49 }
50 
GetEventId()51 int64_t FormEventHandler::GetEventId()
52 {
53     eventId_++;
54     return eventId_;
55 }
56 
RegisterEventTimeoutObserver(const std::shared_ptr<FormEventTimeoutObserver> & observer)57 void FormEventHandler::RegisterEventTimeoutObserver(const std::shared_ptr<FormEventTimeoutObserver> &observer)
58 {
59     HILOG_DEBUG("call");
60     std::lock_guard<std::mutex> lock(observerMutex_);
61     auto iter = observers_.find(observer);
62     if (iter != observers_.end()) {
63         HILOG_ERROR("observer repeat attach");
64         return;
65     }
66     observers_.emplace(observer);
67 }
68 
UnregisterEventTimeoutObserver(const std::shared_ptr<FormEventTimeoutObserver> & observer)69 void FormEventHandler::UnregisterEventTimeoutObserver(const std::shared_ptr<FormEventTimeoutObserver> &observer)
70 {
71     HILOG_DEBUG("call");
72     std::lock_guard<std::mutex> lock(observerMutex_);
73     auto iter = observers_.find(observer);
74     if (iter == observers_.end()) {
75         HILOG_ERROR("observer not exist");
76         return;
77     }
78 
79     observers_.erase(iter);
80 }
81 } // namespace AppExecFwk
82 } // namespace OHOS
83