1209bc2fbSopenharmony_ci/*
2209bc2fbSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3209bc2fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4209bc2fbSopenharmony_ci * you may not use this file except in compliance with the License.
5209bc2fbSopenharmony_ci * You may obtain a copy of the License at
6209bc2fbSopenharmony_ci *
7209bc2fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8209bc2fbSopenharmony_ci *
9209bc2fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10209bc2fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11209bc2fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12209bc2fbSopenharmony_ci * See the License for the specific language governing permissions and
13209bc2fbSopenharmony_ci * limitations under the License.
14209bc2fbSopenharmony_ci */
15209bc2fbSopenharmony_ci
16209bc2fbSopenharmony_ci#include "handler_checker.h"
17209bc2fbSopenharmony_ci#include "ipc_skeleton.h"
18209bc2fbSopenharmony_ci#include "xcollie_utils.h"
19209bc2fbSopenharmony_ci
20209bc2fbSopenharmony_cinamespace OHOS {
21209bc2fbSopenharmony_cinamespace HiviewDFX {
22209bc2fbSopenharmony_civoid HandlerChecker::ScheduleCheck()
23209bc2fbSopenharmony_ci{
24209bc2fbSopenharmony_ci    if (!isCompleted_ || handler_ == nullptr) {
25209bc2fbSopenharmony_ci        return;
26209bc2fbSopenharmony_ci    }
27209bc2fbSopenharmony_ci    if (name_.compare(IPC_FULL) == 0) {
28209bc2fbSopenharmony_ci        auto fb = [] {
29209bc2fbSopenharmony_ci            IPCDfx::BlockUntilThreadAvailable();
30209bc2fbSopenharmony_ci        };
31209bc2fbSopenharmony_ci        if (!handler_->PostTask(fb, "IpcCheck Task", 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) {
32209bc2fbSopenharmony_ci            XCOLLIE_LOGE("XCollie IpcCheck Task PostTask failed.");
33209bc2fbSopenharmony_ci        }
34209bc2fbSopenharmony_ci    }
35209bc2fbSopenharmony_ci
36209bc2fbSopenharmony_ci    isCompleted_.store(false);
37209bc2fbSopenharmony_ci    taskSlow = false;
38209bc2fbSopenharmony_ci    auto weak = weak_from_this();
39209bc2fbSopenharmony_ci    auto f = [weak] () {
40209bc2fbSopenharmony_ci        auto self = weak.lock();
41209bc2fbSopenharmony_ci        if (self) {
42209bc2fbSopenharmony_ci            self->isCompleted_.store(true);
43209bc2fbSopenharmony_ci        }
44209bc2fbSopenharmony_ci    };
45209bc2fbSopenharmony_ci    if (!handler_->PostTask(f, "XCollie Watchdog Task", 0, AppExecFwk::EventQueue::Priority::IMMEDIATE)) {
46209bc2fbSopenharmony_ci        XCOLLIE_LOGE("XCollie Watchdog Task PostTask False.");
47209bc2fbSopenharmony_ci    }
48209bc2fbSopenharmony_ci}
49209bc2fbSopenharmony_ci
50209bc2fbSopenharmony_ciint HandlerChecker::GetCheckState()
51209bc2fbSopenharmony_ci{
52209bc2fbSopenharmony_ci    if (isCompleted_) {
53209bc2fbSopenharmony_ci        taskSlow = false;
54209bc2fbSopenharmony_ci        return CheckStatus::COMPLETED;
55209bc2fbSopenharmony_ci    } else {
56209bc2fbSopenharmony_ci        if (!taskSlow) {
57209bc2fbSopenharmony_ci            taskSlow = true;
58209bc2fbSopenharmony_ci            return CheckStatus::WAITED_HALF;
59209bc2fbSopenharmony_ci        } else {
60209bc2fbSopenharmony_ci            return CheckStatus::WAITING;
61209bc2fbSopenharmony_ci        }
62209bc2fbSopenharmony_ci    }
63209bc2fbSopenharmony_ci}
64209bc2fbSopenharmony_ci
65209bc2fbSopenharmony_cistd::string HandlerChecker::GetDumpInfo()
66209bc2fbSopenharmony_ci{
67209bc2fbSopenharmony_ci    std::string ret;
68209bc2fbSopenharmony_ci    if (handler_ != nullptr) {
69209bc2fbSopenharmony_ci        HandlerDumper handlerDumper;
70209bc2fbSopenharmony_ci        handler_->Dump(handlerDumper);
71209bc2fbSopenharmony_ci        ret = handlerDumper.GetDumpInfo();
72209bc2fbSopenharmony_ci    }
73209bc2fbSopenharmony_ci    return ret;
74209bc2fbSopenharmony_ci}
75209bc2fbSopenharmony_ci
76209bc2fbSopenharmony_ci
77209bc2fbSopenharmony_cistd::shared_ptr<AppExecFwk::EventHandler> HandlerChecker::GetHandler() const
78209bc2fbSopenharmony_ci{
79209bc2fbSopenharmony_ci    return handler_;
80209bc2fbSopenharmony_ci}
81209bc2fbSopenharmony_ci
82209bc2fbSopenharmony_civoid HandlerDumper::Dump(const std::string &message)
83209bc2fbSopenharmony_ci{
84209bc2fbSopenharmony_ci    XCOLLIE_LOGD("message is %{public}s", message.c_str());
85209bc2fbSopenharmony_ci    dumpInfo_ += message;
86209bc2fbSopenharmony_ci}
87209bc2fbSopenharmony_ci
88209bc2fbSopenharmony_cistd::string HandlerDumper::GetTag()
89209bc2fbSopenharmony_ci{
90209bc2fbSopenharmony_ci    return "";
91209bc2fbSopenharmony_ci}
92209bc2fbSopenharmony_ci
93209bc2fbSopenharmony_cistd::string HandlerDumper::GetDumpInfo()
94209bc2fbSopenharmony_ci{
95209bc2fbSopenharmony_ci    return dumpInfo_;
96209bc2fbSopenharmony_ci}
97209bc2fbSopenharmony_ci} // end of namespace HiviewDFX
98209bc2fbSopenharmony_ci} // end of namespace OHOS
99