[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 1 | // Copyright 2013 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 | |
sky | add030b | 2015-08-07 01:06:18 | [diff] [blame] | 5 | #ifndef MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_ |
| 6 | #define MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_ |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 7 | |
Avi Drissman | 2e88ac37 | 2015-12-21 18:14:57 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 10 | #include <map> |
amistry | e8de0bc0 | 2015-11-24 08:42:49 | [diff] [blame] | 11 | #include <set> |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 12 | |
viettrungluu | 473e913 | 2014-09-30 05:04:53 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | d7dc6add | 2014-03-20 05:59:58 | [diff] [blame] | 14 | #include "base/memory/scoped_ptr.h" |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 15 | #include "base/message_loop/message_pump.h" |
abarth | 4ba3cad | 2014-10-17 22:02:15 | [diff] [blame] | 16 | #include "base/observer_list.h" |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 17 | #include "base/synchronization/lock.h" |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 18 | #include "base/time/time.h" |
sky | add030b | 2015-08-07 01:06:18 | [diff] [blame] | 19 | #include "mojo/message_pump/mojo_message_pump_export.h" |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 20 | #include "mojo/public/cpp/system/core.h" |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 21 | |
| 22 | namespace mojo { |
| 23 | namespace common { |
| 24 | |
| 25 | class MessagePumpMojoHandler; |
| 26 | |
| 27 | // Mojo implementation of MessagePump. |
sky | add030b | 2015-08-07 01:06:18 | [diff] [blame] | 28 | class MOJO_MESSAGE_PUMP_EXPORT MessagePumpMojo : public base::MessagePump { |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 29 | public: |
sky | add030b | 2015-08-07 01:06:18 | [diff] [blame] | 30 | class MOJO_MESSAGE_PUMP_EXPORT Observer { |
abarth | 4ba3cad | 2014-10-17 22:02:15 | [diff] [blame] | 31 | public: |
| 32 | Observer() {} |
| 33 | |
| 34 | virtual void WillSignalHandler() = 0; |
| 35 | virtual void DidSignalHandler() = 0; |
| 36 | |
| 37 | protected: |
| 38 | virtual ~Observer() {} |
| 39 | }; |
| 40 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 41 | MessagePumpMojo(); |
dcheng | 3fc12db | 2014-10-21 12:16:28 | [diff] [blame] | 42 | ~MessagePumpMojo() override; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 43 | |
[email protected] | d7dc6add | 2014-03-20 05:59:58 | [diff] [blame] | 44 | // Static factory function (for using with |base::Thread::Options|, wrapped |
| 45 | // using |base::Bind()|). |
| 46 | static scoped_ptr<base::MessagePump> Create(); |
| 47 | |
yzshen | d928bdd | 2014-08-29 22:51:42 | [diff] [blame] | 48 | // Returns the MessagePumpMojo instance of the current thread, if it exists. |
| 49 | static MessagePumpMojo* current(); |
| 50 | |
| 51 | static bool IsCurrent() { return !!current(); } |
| 52 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 53 | // Registers a MessagePumpMojoHandler for the specified handle. Only one |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 54 | // handler can be registered for a specified handle. |
[email protected] | 9a41292 | 2014-08-14 15:48:24 | [diff] [blame] | 55 | // NOTE: a value of 0 for |deadline| indicates an indefinite timeout. |
rockot | 2efa3385 | 2015-10-12 17:52:28 | [diff] [blame] | 56 | void AddHandler(MessagePumpMojoHandler* handler, |
[email protected] | bd6cb4bc | 2013-11-25 23:57:16 | [diff] [blame] | 57 | const Handle& handle, |
[email protected] | e3da5f9 | 2014-06-18 10:18:11 | [diff] [blame] | 58 | MojoHandleSignals wait_signals, |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 59 | base::TimeTicks deadline); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 60 | |
[email protected] | bd6cb4bc | 2013-11-25 23:57:16 | [diff] [blame] | 61 | void RemoveHandler(const Handle& handle); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 62 | |
abarth | 4ba3cad | 2014-10-17 22:02:15 | [diff] [blame] | 63 | void AddObserver(Observer*); |
| 64 | void RemoveObserver(Observer*); |
| 65 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 66 | // MessagePump: |
dcheng | 3fc12db | 2014-10-21 12:16:28 | [diff] [blame] | 67 | void Run(Delegate* delegate) override; |
| 68 | void Quit() override; |
| 69 | void ScheduleWork() override; |
| 70 | void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time) override; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 71 | |
| 72 | private: |
| 73 | struct RunState; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 74 | |
[email protected] | 273e217 | 2013-12-10 07:24:16 | [diff] [blame] | 75 | // Contains the data needed to track a request to AddHandler(). |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 76 | struct Handler { |
rockot | 2efa3385 | 2015-10-12 17:52:28 | [diff] [blame] | 77 | Handler() : handler(NULL), wait_signals(MOJO_HANDLE_SIGNAL_NONE), id(0) {} |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 78 | |
| 79 | MessagePumpMojoHandler* handler; |
[email protected] | e3da5f9 | 2014-06-18 10:18:11 | [diff] [blame] | 80 | MojoHandleSignals wait_signals; |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 81 | base::TimeTicks deadline; |
| 82 | // See description of |MessagePumpMojo::next_handler_id_| for details. |
| 83 | int id; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 84 | }; |
| 85 | |
[email protected] | bd6cb4bc | 2013-11-25 23:57:16 | [diff] [blame] | 86 | typedef std::map<Handle, Handler> HandleToHandler; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 87 | |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 88 | // Implementation of Run(). |
| 89 | void DoRunLoop(RunState* run_state, Delegate* delegate); |
| 90 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 91 | // Services the set of handles ready. If |block| is true this waits for a |
qsr | d0a9ca1 | 2014-10-06 16:33:58 | [diff] [blame] | 92 | // handle to become ready, otherwise this does not block. Returns |true| if a |
| 93 | // handle has become ready, |false| otherwise. |
| 94 | bool DoInternalWork(const RunState& run_state, bool block); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 95 | |
amistry | 6aebd41 | 2016-01-06 00:29:17 | [diff] [blame^] | 96 | // Waits for handles in the wait set to become ready. Returns |true| if ready |
| 97 | // handles may be available, or |false| if the wait's deadline was exceeded. |
| 98 | // Note, ready handles may be unavailable, even though |true| was returned. |
| 99 | bool WaitForReadyHandles(const RunState& run_state) const; |
| 100 | |
| 101 | // Retrieves any 'ready' handles from the wait set, and runs the handler's |
| 102 | // OnHandleReady() or OnHandleError() functions as necessary. Returns |true| |
| 103 | // if any handles were ready and processed. |
| 104 | bool ProcessReadyHandles(); |
| 105 | |
| 106 | // Removes the given invalid handle. This is called if MojoGetReadyHandles |
| 107 | // finds an invalid or closed handle. |
| 108 | void RemoveInvalidHandle(MojoResult result, Handle handle); |
| 109 | |
| 110 | // Removes any handles that have expired their deadline. Runs the handler's |
| 111 | // OnHandleError() function with |MOJO_RESULT_DEADLINE_EXCEEDED| as the |
| 112 | // result. Returns |true| if any handles were removed. |
| 113 | bool RemoveExpiredHandles(); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 114 | |
amistry | 240e4e5 | 2015-11-18 00:38:40 | [diff] [blame] | 115 | void SignalControlPipe(); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 116 | |
amistry | 6aebd41 | 2016-01-06 00:29:17 | [diff] [blame^] | 117 | // Returns the deadline for the call to MojoWait(). |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 118 | MojoDeadline GetDeadlineForWait(const RunState& run_state) const; |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 119 | |
amistry | 6aebd41 | 2016-01-06 00:29:17 | [diff] [blame^] | 120 | // Run |OnHandleReady()| for the handler registered with |handle|. |handle| |
| 121 | // must be registered. |
| 122 | void SignalHandleReady(Handle handle); |
| 123 | |
abarth | 4ba3cad | 2014-10-17 22:02:15 | [diff] [blame] | 124 | void WillSignalHandler(); |
| 125 | void DidSignalHandler(); |
| 126 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 127 | // If non-NULL we're running (inside Run()). Member is reference to value on |
| 128 | // stack. |
| 129 | RunState* run_state_; |
| 130 | |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 131 | // Lock for accessing |run_state_|. In general the only method that we have to |
| 132 | // worry about is ScheduleWork(). All other methods are invoked on the same |
| 133 | // thread. |
| 134 | base::Lock run_state_lock_; |
| 135 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 136 | HandleToHandler handlers_; |
amistry | e8de0bc0 | 2015-11-24 08:42:49 | [diff] [blame] | 137 | // Set of handles that have a deadline set. Avoids iterating over all elements |
| 138 | // in |handles_| in the common case (no deadline set). |
| 139 | // TODO(amistry): Make this better and avoid special-casing deadlines. |
| 140 | std::set<Handle> deadline_handles_; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 141 | |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 142 | // An ever increasing value assigned to each Handler::id. Used to detect |
| 143 | // uniqueness while notifying. That is, while notifying expired timers we copy |
| 144 | // |handlers_| and only notify handlers whose id match. If the id does not |
| 145 | // match it means the handler was removed then added so that we shouldn't |
| 146 | // notify it. |
| 147 | int next_handler_id_; |
| 148 | |
brettw | 236d317 | 2015-06-03 16:31:43 | [diff] [blame] | 149 | base::ObserverList<Observer> observers_; |
abarth | 4ba3cad | 2014-10-17 22:02:15 | [diff] [blame] | 150 | |
amistry | 6aebd41 | 2016-01-06 00:29:17 | [diff] [blame^] | 151 | // Mojo handle for the wait set. |
| 152 | ScopedHandle wait_set_handle_; |
amistry | 240e4e5 | 2015-11-18 00:38:40 | [diff] [blame] | 153 | // Used to wake up run loop from |SignalControlPipe()|. |
| 154 | ScopedMessagePipeHandle read_handle_; |
| 155 | ScopedMessagePipeHandle write_handle_; |
| 156 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 157 | DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo); |
| 158 | }; |
| 159 | |
| 160 | } // namespace common |
| 161 | } // namespace mojo |
| 162 | |
sky | add030b | 2015-08-07 01:06:18 | [diff] [blame] | 163 | #endif // MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_ |