Revert 187554 "Implement IPC::ChannelFactory, a class that accep..."
[chromium-blink-merge.git] / remoting / base / plugin_thread_task_runner.h
blobbfae1ce757294968b2605f85f03f0f8e08095c99
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef REMOTING_BASE_PLUGIN_THREAD_TASK_RUNNER_H_
6 #define REMOTING_BASE_PLUGIN_THREAD_TASK_RUNNER_H_
8 #include <set>
10 #include "base/callback_forward.h"
11 #include "base/compiler_specific.h"
12 #include "base/pending_task.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/synchronization/lock.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "base/threading/platform_thread.h"
17 #include "base/time.h"
19 class MessageLoop;
21 namespace remoting {
23 // SingleThreadTaskRunner for plugin main threads.
24 class PluginThreadTaskRunner : public base::SingleThreadTaskRunner {
25 public:
26 class Delegate {
27 public:
28 virtual ~Delegate();
30 virtual bool RunOnPluginThread(
31 base::TimeDelta delay, void(function)(void*), void* data) = 0;
34 // Caller keeps ownership of delegate.
35 PluginThreadTaskRunner(Delegate* delegate);
37 // Detaches the PluginThreadTaskRunner from the underlying Delegate and
38 // processes posted tasks until Quit() is called. This is used during plugin
39 // shutdown, when the plugin environment has stopped accepting new tasks to
40 // run, to process cleanup tasks posted to the plugin thread.
41 // This method must be called on the plugin thread.
42 void DetachAndRunShutdownLoop();
44 // Makes DetachAndRunShutdownLoop() stop processing tasks and return control
45 // to the caller. Calling Quit() before DetachAndRunShutdownLoop() causes
46 // the latter to exit immediately when called, without processing any delayed
47 // shutdown tasks. This method can be called from any thread.
48 void Quit();
50 // base::SingleThreadTaskRunner interface.
51 virtual bool PostDelayedTask(
52 const tracked_objects::Location& from_here,
53 const base::Closure& task,
54 base::TimeDelta delay) OVERRIDE;
55 virtual bool PostNonNestableDelayedTask(
56 const tracked_objects::Location& from_here,
57 const base::Closure& task,
58 base::TimeDelta delay) OVERRIDE;
59 virtual bool RunsTasksOnCurrentThread() const OVERRIDE;
61 protected:
62 virtual ~PluginThreadTaskRunner();
64 private:
65 // Methods that can be called from any thread.
67 // Schedules RunTasks to be called on the plugin thread.
68 void PostRunTasks();
70 // Methods that are always called on the plugin thread.
72 // Schedules RunDelayedTasks() to be called on the plugin thread. |when|
73 // specifies the time when RunDelayedTasks() should be called.
74 void PostDelayedRunTasks(base::TimeTicks when);
76 // Processes the incoming task queue: runs all non delayed tasks and posts all
77 // delayed tasks to |delayed_queue_|.
78 void ProcessIncomingTasks();
80 // Called in response to PostDelayedRunTasks().
81 void RunDelayedTasks(base::TimeTicks when);
83 // Runs all tasks that are due.
84 void RunDueTasks(base::TimeTicks now);
86 // Called in response to PostRunTasks().
87 void RunTasks();
89 static void TaskSpringboard(void* data);
91 const base::PlatformThreadId plugin_thread_id_;
93 // Used by the shutdown loop to block the thread until there is a task ready
94 // to run.
95 base::WaitableEvent event_;
97 base::Lock lock_;
99 // The members below are protected by |lock_|.
101 // Pointer to the delegate that implements scheduling tasks via the plugin
102 // API.
103 Delegate* delegate_;
105 // Contains all posted tasks that haven't been sorted yet.
106 base::TaskQueue incoming_queue_;
108 // The next sequence number to use for delayed tasks.
109 int next_sequence_num_;
111 // True if Quit() has been called.
112 bool quit_received_;
114 // The members below are accessed only on the plugin thread.
116 // Contains delayed tasks, sorted by their 'delayed_run_time' property.
117 base::DelayedTaskQueue delayed_queue_;
119 // The list of timestamps when scheduled timers are expected to fire.
120 std::set<base::TimeTicks> scheduled_timers_;
122 // True if the shutdown task loop was been stopped.
123 bool stopped_;
125 DISALLOW_COPY_AND_ASSIGN(PluginThreadTaskRunner);
128 } // namespace remoting
130 #endif // REMOTING_BASE_PLUGIN_THREAD_TASK_RUNNER_H_