| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef REMOTING_BASE_PLUGIN_THREAD_TASK_RUNNER_H_ | |
| 6 #define REMOTING_BASE_PLUGIN_THREAD_TASK_RUNNER_H_ | |
| 7 | |
| 8 #include <set> | |
| 9 | |
| 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" | |
| 18 | |
| 19 namespace remoting { | |
| 20 | |
| 21 // SingleThreadTaskRunner for plugin main threads. | |
| 22 class PluginThreadTaskRunner : public base::SingleThreadTaskRunner { | |
| 23 public: | |
| 24 class Delegate { | |
| 25 public: | |
| 26 virtual ~Delegate(); | |
| 27 | |
| 28 virtual bool RunOnPluginThread( | |
| 29 base::TimeDelta delay, void(function)(void*), void* data) = 0; | |
| 30 }; | |
| 31 | |
| 32 // Caller keeps ownership of delegate. | |
| 33 PluginThreadTaskRunner(Delegate* delegate); | |
| 34 | |
| 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(); | |
| 41 | |
| 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(); | |
| 47 | |
| 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; | |
| 56 | |
| 57 protected: | |
| 58 ~PluginThreadTaskRunner() override; | |
| 59 | |
| 60 private: | |
| 61 // Methods that can be called from any thread. | |
| 62 | |
| 63 // Schedules RunTasks to be called on the plugin thread. | |
| 64 void PostRunTasks(); | |
| 65 | |
| 66 // Methods that are always called on the plugin thread. | |
| 67 | |
| 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); | |
| 71 | |
| 72 // Processes the incoming task queue: runs all non delayed tasks and posts all | |
| 73 // delayed tasks to |delayed_queue_|. | |
| 74 void ProcessIncomingTasks(); | |
| 75 | |
| 76 // Called in response to PostDelayedRunTasks(). | |
| 77 void RunDelayedTasks(base::TimeTicks when); | |
| 78 | |
| 79 // Runs all tasks that are due. | |
| 80 void RunDueTasks(base::TimeTicks now); | |
| 81 | |
| 82 // Called in response to PostRunTasks(). | |
| 83 void RunTasks(); | |
| 84 | |
| 85 static void TaskSpringboard(void* data); | |
| 86 | |
| 87 const base::PlatformThreadId plugin_thread_id_; | |
| 88 | |
| 89 // Used by the shutdown loop to block the thread until there is a task ready | |
| 90 // to run. | |
| 91 base::WaitableEvent event_; | |
| 92 | |
| 93 base::Lock lock_; | |
| 94 | |
| 95 // The members below are protected by |lock_|. | |
| 96 | |
| 97 // Pointer to the delegate that implements scheduling tasks via the plugin | |
| 98 // API. | |
| 99 Delegate* delegate_; | |
| 100 | |
| 101 // Contains all posted tasks that haven't been sorted yet. | |
| 102 base::TaskQueue incoming_queue_; | |
| 103 | |
| 104 // The next sequence number to use for delayed tasks. | |
| 105 int next_sequence_num_; | |
| 106 | |
| 107 // True if Quit() has been called. | |
| 108 bool quit_received_; | |
| 109 | |
| 110 // The members below are accessed only on the plugin thread. | |
| 111 | |
| 112 // Contains delayed tasks, sorted by their 'delayed_run_time' property. | |
| 113 base::DelayedTaskQueue delayed_queue_; | |
| 114 | |
| 115 // The list of timestamps when scheduled timers are expected to fire. | |
| 116 std::set<base::TimeTicks> scheduled_timers_; | |
| 117 | |
| 118 // True if the shutdown task loop was been stopped. | |
| 119 bool stopped_; | |
| 120 | |
| 121 DISALLOW_COPY_AND_ASSIGN(PluginThreadTaskRunner); | |
| 122 }; | |
| 123 | |
| 124 } // namespace remoting | |
| 125 | |
| 126 #endif // REMOTING_BASE_PLUGIN_THREAD_TASK_RUNNER_H_ | |
| OLD | NEW |