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 "timer_manager.h"
17
18 #include "background_task_mgr_service.h"
19 #include "transient_task_log.h"
20
21 namespace OHOS {
22 namespace BackgroundTaskMgr {
23 const std::string BACKGROUND_TASK_TIMER = "BgTaskTimer";
24
TimerManager(const wptr<BackgroundTaskMgrService> & service)25 TimerManager::TimerManager(const wptr<BackgroundTaskMgrService>& service) : service_(service)
26 {
27 std::shared_ptr<AppExecFwk::EventRunner> eventRunner = AppExecFwk::EventRunner::Create(BACKGROUND_TASK_TIMER);
28 if (eventRunner.get() != nullptr) {
29 SetEventRunner(eventRunner);
30 }
31 }
32
AddTimer(int32_t requestId,int32_t interval)33 bool TimerManager::AddTimer(int32_t requestId, int32_t interval)
34 {
35 BGTASK_LOGD("Add request id: %{public}d", requestId);
36 return SendEvent(requestId, 0, interval);
37 }
38
RemoveTimer(int32_t requestId)39 void TimerManager::RemoveTimer(int32_t requestId)
40 {
41 BGTASK_LOGD("Remove request id: %{public}d", requestId);
42 return RemoveEvent(requestId);
43 }
44
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)45 void TimerManager::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
46 {
47 if (event == nullptr) {
48 return;
49 }
50 int32_t requestId = event->GetInnerEventId();
51 auto bgTask = service_.promote();
52 if (bgTask == nullptr) {
53 return;
54 }
55 bgTask->HandleRequestExpired(requestId);
56 }
57 } // namespace BackgroundTaskMgr
58 } // namespace OHOS