[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 | |
| 8 | #include <map> |
| 9 | |
viettrungluu | 473e913 | 2014-09-30 05:04:53 | [diff] [blame] | 10 | #include "base/macros.h" |
[email protected] | d7dc6add | 2014-03-20 05:59:58 | [diff] [blame] | 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 12 | #include "base/message_loop/message_pump.h" |
abarth | 4ba3cad | 2014-10-17 22:02:15 | [diff] [blame] | 13 | #include "base/observer_list.h" |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 14 | #include "base/synchronization/lock.h" |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 15 | #include "base/time/time.h" |
sky | add030b | 2015-08-07 01:06:18 | [diff] [blame] | 16 | #include "mojo/message_pump/mojo_message_pump_export.h" |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame^] | 17 | #include "mojo/public/cpp/system/core.h" |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 18 | |
| 19 | namespace mojo { |
| 20 | namespace common { |
| 21 | |
| 22 | class MessagePumpMojoHandler; |
| 23 | |
| 24 | // Mojo implementation of MessagePump. |
sky | add030b | 2015-08-07 01:06:18 | [diff] [blame] | 25 | class MOJO_MESSAGE_PUMP_EXPORT MessagePumpMojo : public base::MessagePump { |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 26 | public: |
sky | add030b | 2015-08-07 01:06:18 | [diff] [blame] | 27 | class MOJO_MESSAGE_PUMP_EXPORT Observer { |
abarth | 4ba3cad | 2014-10-17 22:02:15 | [diff] [blame] | 28 | public: |
| 29 | Observer() {} |
| 30 | |
| 31 | virtual void WillSignalHandler() = 0; |
| 32 | virtual void DidSignalHandler() = 0; |
| 33 | |
| 34 | protected: |
| 35 | virtual ~Observer() {} |
| 36 | }; |
| 37 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 38 | MessagePumpMojo(); |
dcheng | 3fc12db | 2014-10-21 12:16:28 | [diff] [blame] | 39 | ~MessagePumpMojo() override; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 40 | |
[email protected] | d7dc6add | 2014-03-20 05:59:58 | [diff] [blame] | 41 | // Static factory function (for using with |base::Thread::Options|, wrapped |
| 42 | // using |base::Bind()|). |
| 43 | static scoped_ptr<base::MessagePump> Create(); |
| 44 | |
yzshen | d928bdd | 2014-08-29 22:51:42 | [diff] [blame] | 45 | // Returns the MessagePumpMojo instance of the current thread, if it exists. |
| 46 | static MessagePumpMojo* current(); |
| 47 | |
| 48 | static bool IsCurrent() { return !!current(); } |
| 49 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 50 | // Registers a MessagePumpMojoHandler for the specified handle. Only one |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 51 | // handler can be registered for a specified handle. |
[email protected] | 9a41292 | 2014-08-14 15:48:24 | [diff] [blame] | 52 | // NOTE: a value of 0 for |deadline| indicates an indefinite timeout. |
rockot | 2efa3385 | 2015-10-12 17:52:28 | [diff] [blame] | 53 | void AddHandler(MessagePumpMojoHandler* handler, |
[email protected] | bd6cb4bc | 2013-11-25 23:57:16 | [diff] [blame] | 54 | const Handle& handle, |
[email protected] | e3da5f9 | 2014-06-18 10:18:11 | [diff] [blame] | 55 | MojoHandleSignals wait_signals, |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 56 | base::TimeTicks deadline); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 57 | |
[email protected] | bd6cb4bc | 2013-11-25 23:57:16 | [diff] [blame] | 58 | void RemoveHandler(const Handle& handle); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 59 | |
abarth | 4ba3cad | 2014-10-17 22:02:15 | [diff] [blame] | 60 | void AddObserver(Observer*); |
| 61 | void RemoveObserver(Observer*); |
| 62 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 63 | // MessagePump: |
dcheng | 3fc12db | 2014-10-21 12:16:28 | [diff] [blame] | 64 | void Run(Delegate* delegate) override; |
| 65 | void Quit() override; |
| 66 | void ScheduleWork() override; |
| 67 | void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time) override; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 68 | |
| 69 | private: |
| 70 | struct RunState; |
| 71 | struct WaitState; |
| 72 | |
[email protected] | 273e217 | 2013-12-10 07:24:16 | [diff] [blame] | 73 | // Contains the data needed to track a request to AddHandler(). |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 74 | struct Handler { |
rockot | 2efa3385 | 2015-10-12 17:52:28 | [diff] [blame] | 75 | Handler() : handler(NULL), wait_signals(MOJO_HANDLE_SIGNAL_NONE), id(0) {} |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 76 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 77 | MessagePumpMojoHandler* handler; |
[email protected] | e3da5f9 | 2014-06-18 10:18:11 | [diff] [blame] | 78 | MojoHandleSignals wait_signals; |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 79 | base::TimeTicks deadline; |
| 80 | // See description of |MessagePumpMojo::next_handler_id_| for details. |
| 81 | int id; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 82 | }; |
| 83 | |
[email protected] | bd6cb4bc | 2013-11-25 23:57:16 | [diff] [blame] | 84 | typedef std::map<Handle, Handler> HandleToHandler; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 85 | |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 86 | // Implementation of Run(). |
| 87 | void DoRunLoop(RunState* run_state, Delegate* delegate); |
| 88 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 89 | // Services the set of handles ready. If |block| is true this waits for a |
qsr | d0a9ca1 | 2014-10-06 16:33:58 | [diff] [blame] | 90 | // handle to become ready, otherwise this does not block. Returns |true| if a |
| 91 | // handle has become ready, |false| otherwise. |
| 92 | bool DoInternalWork(const RunState& run_state, bool block); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 93 | |
rockot | 6dcbf7c | 2015-01-16 00:41:13 | [diff] [blame] | 94 | // Removes the given invalid handle. This is called if MojoWaitMany finds an |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 95 | // invalid handle. |
rockot | 6dcbf7c | 2015-01-16 00:41:13 | [diff] [blame] | 96 | void RemoveInvalidHandle(const WaitState& wait_state, |
| 97 | MojoResult result, |
| 98 | uint32_t result_index); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 99 | |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 100 | void SignalControlPipe(const RunState& run_state); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 101 | |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 102 | WaitState GetWaitState(const RunState& run_state) const; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 103 | |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 104 | // Returns the deadline for the call to MojoWaitMany(). |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 105 | MojoDeadline GetDeadlineForWait(const RunState& run_state) const; |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 106 | |
abarth | 4ba3cad | 2014-10-17 22:02:15 | [diff] [blame] | 107 | void WillSignalHandler(); |
| 108 | void DidSignalHandler(); |
| 109 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 110 | // If non-NULL we're running (inside Run()). Member is reference to value on |
| 111 | // stack. |
| 112 | RunState* run_state_; |
| 113 | |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 114 | // Lock for accessing |run_state_|. In general the only method that we have to |
| 115 | // worry about is ScheduleWork(). All other methods are invoked on the same |
| 116 | // thread. |
| 117 | base::Lock run_state_lock_; |
| 118 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 119 | HandleToHandler handlers_; |
| 120 | |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 121 | // An ever increasing value assigned to each Handler::id. Used to detect |
| 122 | // uniqueness while notifying. That is, while notifying expired timers we copy |
| 123 | // |handlers_| and only notify handlers whose id match. If the id does not |
| 124 | // match it means the handler was removed then added so that we shouldn't |
| 125 | // notify it. |
| 126 | int next_handler_id_; |
| 127 | |
brettw | 236d317 | 2015-06-03 16:31:43 | [diff] [blame] | 128 | base::ObserverList<Observer> observers_; |
abarth | 4ba3cad | 2014-10-17 22:02:15 | [diff] [blame] | 129 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 130 | DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo); |
| 131 | }; |
| 132 | |
| 133 | } // namespace common |
| 134 | } // namespace mojo |
| 135 | |
sky | add030b | 2015-08-07 01:06:18 | [diff] [blame] | 136 | #endif // MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_ |