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 #include "watchdog.h"
16 
17 #include "work_sched_hilog.h"
18 #include "work_policy_manager.h"
19 
20 using namespace std;
21 
22 namespace OHOS {
23 namespace WorkScheduler {
24 const std::string WORK_SCHEDULER_WATCHDOG = "WorkSchedulerWatchdog";
25 
Watchdog(const std::shared_ptr<WorkPolicyManager> & service,const std::shared_ptr<AppExecFwk::EventRunner> & runner)26 Watchdog::Watchdog(const std::shared_ptr<WorkPolicyManager>& service,
27     const std::shared_ptr<AppExecFwk::EventRunner>& runner) : service_(service)
28 {
29     if (runner != nullptr) {
30         SetEventRunner(runner);
31     }
32 }
33 
AddWatchdog(uint32_t watchdogId,int32_t interval)34 bool Watchdog::AddWatchdog(uint32_t watchdogId, int32_t interval)
35 {
36     WS_HILOGD("Add watchdog with Id:%{public}u", watchdogId);
37     return SendEvent(watchdogId, 0, interval);
38 }
39 
RemoveWatchdog(uint32_t watchdogId)40 void Watchdog::RemoveWatchdog(uint32_t watchdogId)
41 {
42     WS_HILOGD("Remove watchdog with Id:%{public}u", watchdogId);
43     RemoveEvent(watchdogId);
44 }
45 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)46 void Watchdog::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
47 {
48     if (event == nullptr) {
49         return;
50     }
51     uint32_t watchdogId = event->GetInnerEventId();
52     if (service_ != nullptr) {
53         service_->WatchdogTimeOut(watchdogId);
54     } else {
55         WS_HILOGE("service is null");
56     }
57 }
58 } // namespace WorkScheduler
59 } // namespace OHOS