(1) Chrome data compression proxy should use QUIC only if
[chromium-blink-merge.git] / remoting / base / plugin_thread_task_runner.h
blob3ae0025c1490ea81dacb79ae5461107f29145be4
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/time.h"
19 namespace remoting {
21 // SingleThreadTaskRunner for plugin main threads.
22 class PluginThreadTaskRunner : public base::SingleThreadTaskRunner {
23 public:
24 class Delegate {
25 public:
26 virtual ~Delegate();
28 virtual bool RunOnPluginThread(
29 base::TimeDelta delay, void(function)(void*), void* data) = 0;
32 // Caller keeps ownership of delegate.
33 PluginThreadTaskRunner(Delegate* delegate);
35 // Detaches the PluginThreadTaskRunner from the underlying Delegate and
36 // processes posted tasks until Quit() is called. This is used during plugin
37 // shutdown, when the plugin environment has stopped accepting new tasks to
38 // run, to process cleanup tasks posted to the plugin thread.
39 // This method must be called on the plugin thread.
40 void DetachAndRunShutdownLoop();
42 // Makes DetachAndRunShutdownLoop() stop processing tasks and return control
43 // to the caller. Calling Quit() before DetachAndRunShutdownLoop() causes
44 // the latter to exit immediately when called, without processing any delayed
45 // shutdown tasks. This method can be called from any thread.
46 void Quit();
48 // base::SingleThreadTaskRunner interface.
49 bool PostDelayedTask(const tracked_objects::Location& from_here,
50 const base::Closure& task,
51 base::TimeDelta delay) override;
52 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
53 const base::Closure& task,
54 base::TimeDelta delay) override;
55 bool RunsTasksOnCurrentThread() const override;
57 protected:
58 ~PluginThreadTaskRunner() override;
60 private:
61 // Methods that can be called from any thread.
63 // Schedules RunTasks to be called on the plugin thread.
64 void PostRunTasks();
66 // Methods that are always called on the plugin thread.
68 // Schedules RunDelayedTasks() to be called on the plugin thread. |when|
69 // specifies the time when RunDelayedTasks() should be called.
70 void PostDelayedRunTasks(base::TimeTicks when);
72 // Processes the incoming task queue: runs all non delayed tasks and posts all
73 // delayed tasks to |delayed_queue_|.
74 void ProcessIncomingTasks();
76 // Called in response to PostDelayedRunTasks().
77 void RunDelayedTasks(base::TimeTicks when);
79 // Runs all tasks that are due.
80 void RunDueTasks(base::TimeTicks now);
82 // Called in response to PostRunTasks().
83 void RunTasks();
85 static void TaskSpringboard(void* data);
87 const base::PlatformThreadId plugin_thread_id_;
89 // Used by the shutdown loop to block the thread until there is a task ready
90 // to run.
91 base::WaitableEvent event_;
93 base::Lock lock_;
95 // The members below are protected by |lock_|.
97 // Pointer to the delegate that implements scheduling tasks via the plugin
98 // API.
99 Delegate* delegate_;
101 // Contains all posted tasks that haven't been sorted yet.
102 base::TaskQueue incoming_queue_;
104 // The next sequence number to use for delayed tasks.
105 int next_sequence_num_;
107 // True if Quit() has been called.
108 bool quit_received_;
110 // The members below are accessed only on the plugin thread.
112 // Contains delayed tasks, sorted by their 'delayed_run_time' property.
113 base::DelayedTaskQueue delayed_queue_;
115 // The list of timestamps when scheduled timers are expected to fire.
116 std::set<base::TimeTicks> scheduled_timers_;
118 // True if the shutdown task loop was been stopped.
119 bool stopped_;
121 DISALLOW_COPY_AND_ASSIGN(PluginThreadTaskRunner);
124 } // namespace remoting
126 #endif // REMOTING_BASE_PLUGIN_THREAD_TASK_RUNNER_H_