// Copyright 2017 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #define FML_USED_ON_EMBEDDER #include "flutter/fml/task_runner.h" #include #include "flutter/fml/message_loop.h" #include "flutter/fml/message_loop_impl.h" namespace fml { TaskRunner::TaskRunner(fxl::RefPtr loop) : loop_(std::move(loop)) { FXL_CHECK(loop_); } TaskRunner::~TaskRunner() = default; void TaskRunner::PostTask(fxl::Closure task) { loop_->PostTask(std::move(task), fxl::TimePoint::Now()); } void TaskRunner::PostTaskForTime(fxl::Closure task, fxl::TimePoint target_time) { loop_->PostTask(std::move(task), target_time); } void TaskRunner::PostDelayedTask(fxl::Closure task, fxl::TimeDelta delay) { loop_->PostTask(std::move(task), fxl::TimePoint::Now() + delay); } bool TaskRunner::RunsTasksOnCurrentThread() { if (!fml::MessageLoop::IsInitializedForCurrentThread()) { return false; } return MessageLoop::GetCurrent().GetLoopImpl() == loop_; } void TaskRunner::RunNowOrPostTask(fxl::RefPtr runner, fxl::Closure task) { FXL_DCHECK(runner); if (runner->RunsTasksOnCurrentThread()) { task(); } else { runner->PostTask(std::move(task)); } } } // namespace fml