1 /*
2 * Copyright (c) 2023 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 "adapter/preview/external/flutter/platform_task_runner_adapter.h"
17
18 #include "flutter/fml/time/time_point.h"
19 #include "jsapp/rich/external/EventHandler.h"
20
21 namespace flutter {
22
PlatformTaskRunnerAdapter(bool useCurrentEventRunner)23 PlatformTaskRunnerAdapter::PlatformTaskRunnerAdapter(bool useCurrentEventRunner) : fml::TaskRunner(nullptr) {}
24
PostTask(fml::closure task)25 void PlatformTaskRunnerAdapter::PostTask(fml::closure task)
26 {
27 OHOS::AppExecFwk::EventHandler::PostTask(std::move(task));
28 }
29
PostTaskForTime(fml::closure task,fml::TimePoint target_time)30 void PlatformTaskRunnerAdapter::PostTaskForTime(fml::closure task, fml::TimePoint target_time)
31 {
32 OHOS::AppExecFwk::EventHandler::PostTask(std::move(task), target_time.ToEpochDelta().ToMilliseconds());
33 }
34
PostDelayedTask(fml::closure task,fml::TimeDelta delay)35 void PlatformTaskRunnerAdapter::PostDelayedTask(fml::closure task, fml::TimeDelta delay)
36 {
37 OHOS::AppExecFwk::EventHandler::PostTask(std::move(task), delay.ToMilliseconds());
38 }
39
RunsTasksOnCurrentThread()40 bool PlatformTaskRunnerAdapter::RunsTasksOnCurrentThread()
41 {
42 return OHOS::AppExecFwk::EventHandler::IsCurrentRunnerThread();
43 }
44
GetTaskQueueId()45 fml::TaskQueueId PlatformTaskRunnerAdapter::GetTaskQueueId()
46 {
47 return fml::_kUnmerged;
48 }
49
CurrentTaskRunner(bool useCurrentEventRunner)50 fml::RefPtr<fml::TaskRunner> PlatformTaskRunnerAdapter::CurrentTaskRunner(bool useCurrentEventRunner)
51 {
52 static fml::RefPtr<fml::TaskRunner> taskRunner_ =
53 fml::MakeRefCounted<PlatformTaskRunnerAdapter>(useCurrentEventRunner);
54 return taskRunner_;
55 }
56
57 } // namespace flutter
58