flutter_flutter/shell/platform/embedder/embedder_task_runner.h
Chinmay Garde cb8eb801a4
Allow embedders to specify their own task runner interfaces. (#8273)
Currently, all Flutter threads are managed by the engine itself. This works for
all threads except the platform thread. On this thread, the engine cannot see
the underlying event multiplexing mechanism. Using the new task runner
interfaces, the engine can relinquish the task of setting up the event
multiplexing mechanism and instead have the embedder provide one for it during
setup.

This scheme is only wired up for the platform thread. But, the eventual goal
is to expose this message loop interoperability for all threads.
2019-03-27 16:16:59 -07:00

58 lines
1.6 KiB
C++

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_TASK_RUNNER_H_
#define FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_TASK_RUNNER_H_
#include <mutex>
#include <unordered_map>
#include "flutter/fml/macros.h"
#include "flutter/fml/synchronization/thread_annotations.h"
#include "flutter/fml/task_runner.h"
namespace shell {
class EmbedderTaskRunner final : public fml::TaskRunner {
public:
struct DispatchTable {
std::function<void(EmbedderTaskRunner* task_runner,
uint64_t task_baton,
fml::TimePoint target_time)>
post_task_callback;
std::function<bool(void)> runs_task_on_current_thread_callback;
};
EmbedderTaskRunner(DispatchTable table);
~EmbedderTaskRunner() override;
bool PostTask(uint64_t baton);
// |fml::TaskRunner|
void PostTask(fml::closure task) override;
// |fml::TaskRunner|
void PostTaskForTime(fml::closure task, fml::TimePoint target_time) override;
// |fml::TaskRunner|
void PostDelayedTask(fml::closure task, fml::TimeDelta delay) override;
// |fml::TaskRunner|
bool RunsTasksOnCurrentThread() override;
private:
DispatchTable dispatch_table_;
std::mutex tasks_mutex_;
uint64_t last_baton_ FML_GUARDED_BY(tasks_mutex_);
std::unordered_map<uint64_t, fml::closure> pending_tasks_
FML_GUARDED_BY(tasks_mutex_);
FML_DISALLOW_COPY_AND_ASSIGN(EmbedderTaskRunner);
};
} // namespace shell
#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_TASK_RUNNER_H_