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 #include "ui_service_mgr_xcollie.h"
17 
18 #include "ui_service_hilog.h"
19 
20 #ifdef UISERVICE_MGR_XCOLLIE_ENABLE
21 #include "xcollie/xcollie.h"
22 #endif
23 
24 namespace OHOS::Ace {
UIServiceMgrXCollie(const std::string & tag,uint32_t timeoutSeconds)25 UIServiceMgrXCollie::UIServiceMgrXCollie(const std::string& tag, uint32_t timeoutSeconds)
26 {
27     tag_ = tag;
28 #ifdef UISERVICE_MGR_XCOLLIE_ENABLE
29     id_ = HiviewDFX::XCollie::GetInstance().SetTimer(tag_, timeoutSeconds, nullptr, nullptr,
30         OHOS::HiviewDFX::XCOLLIE_FLAG_LOG | OHOS::HiviewDFX::XCOLLIE_FLAG_RECOVERY);
31 #else
32     id_ = -1;
33 #endif
34     isCanceled_ = false;
35     LOGI("start UIServiceMgrXCollie, tag:%{public}s, timeout: %{public}u, id: %{public}d", tag_.c_str(),
36         timeoutSeconds, id_);
37 }
38 
~UIServiceMgrXCollie()39 UIServiceMgrXCollie::~UIServiceMgrXCollie()
40 {
41     CancelUIServiceMgrXCollie();
42 }
43 
CancelUIServiceMgrXCollie()44 void UIServiceMgrXCollie::CancelUIServiceMgrXCollie()
45 {
46     if (!isCanceled_) {
47 #ifdef UISERVICE_MGR_XCOLLIE_ENABLE
48         HiviewDFX::XCollie::GetInstance().CancelTimer(id_);
49 #endif
50         isCanceled_ = true;
51         LOGI("cancel UIServiceMgrXCollie, tag: %{public}s, id: %{public}d", tag_.c_str(), id_);
52     }
53 }
54 }  // namespace OHOS::Ace