[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 | |
| 5 | #ifndef MOJO_COMMON_MESSAGE_PUMP_MOJO_H_ |
| 6 | #define MOJO_COMMON_MESSAGE_PUMP_MOJO_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | |
[email protected] | d7dc6add | 2014-03-20 05:59:58 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 11 | #include "base/message_loop/message_pump.h" |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 12 | #include "base/synchronization/lock.h" |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 13 | #include "base/time/time.h" |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 14 | #include "mojo/common/mojo_common_export.h" |
[email protected] | 5dddd19 | 2014-03-28 00:53:27 | [diff] [blame] | 15 | #include "mojo/public/cpp/system/core.h" |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 16 | |
| 17 | namespace mojo { |
| 18 | namespace common { |
| 19 | |
| 20 | class MessagePumpMojoHandler; |
| 21 | |
| 22 | // Mojo implementation of MessagePump. |
| 23 | class MOJO_COMMON_EXPORT MessagePumpMojo : public base::MessagePump { |
| 24 | public: |
| 25 | MessagePumpMojo(); |
| 26 | virtual ~MessagePumpMojo(); |
| 27 | |
[email protected] | d7dc6add | 2014-03-20 05:59:58 | [diff] [blame] | 28 | // Static factory function (for using with |base::Thread::Options|, wrapped |
| 29 | // using |base::Bind()|). |
| 30 | static scoped_ptr<base::MessagePump> Create(); |
| 31 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 32 | // Registers a MessagePumpMojoHandler for the specified handle. Only one |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 33 | // handler can be registered for a specified handle. |
[email protected] | 9a41292 | 2014-08-14 15:48:24 | [diff] [blame^] | 34 | // NOTE: a value of 0 for |deadline| indicates an indefinite timeout. |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 35 | void AddHandler(MessagePumpMojoHandler* handler, |
[email protected] | bd6cb4bc | 2013-11-25 23:57:16 | [diff] [blame] | 36 | const Handle& handle, |
[email protected] | e3da5f9 | 2014-06-18 10:18:11 | [diff] [blame] | 37 | MojoHandleSignals wait_signals, |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 38 | base::TimeTicks deadline); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 39 | |
[email protected] | bd6cb4bc | 2013-11-25 23:57:16 | [diff] [blame] | 40 | void RemoveHandler(const Handle& handle); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 41 | |
| 42 | // MessagePump: |
| 43 | virtual void Run(Delegate* delegate) OVERRIDE; |
| 44 | virtual void Quit() OVERRIDE; |
| 45 | virtual void ScheduleWork() OVERRIDE; |
| 46 | virtual void ScheduleDelayedWork( |
| 47 | const base::TimeTicks& delayed_work_time) OVERRIDE; |
| 48 | |
| 49 | private: |
| 50 | struct RunState; |
| 51 | struct WaitState; |
| 52 | |
[email protected] | 273e217 | 2013-12-10 07:24:16 | [diff] [blame] | 53 | // Contains the data needed to track a request to AddHandler(). |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 54 | struct Handler { |
[email protected] | 9a067d5 | 2014-06-18 15:25:03 | [diff] [blame] | 55 | Handler() : handler(NULL), wait_signals(MOJO_HANDLE_SIGNAL_NONE), id(0) {} |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 56 | |
| 57 | MessagePumpMojoHandler* handler; |
[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; |
| 60 | // See description of |MessagePumpMojo::next_handler_id_| for details. |
| 61 | int id; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 62 | }; |
| 63 | |
[email protected] | bd6cb4bc | 2013-11-25 23:57:16 | [diff] [blame] | 64 | typedef std::map<Handle, Handler> HandleToHandler; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 65 | |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 66 | // Implementation of Run(). |
| 67 | void DoRunLoop(RunState* run_state, Delegate* delegate); |
| 68 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 69 | // Services the set of handles ready. If |block| is true this waits for a |
| 70 | // handle to become ready, otherwise this does not block. |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 71 | void DoInternalWork(const RunState& run_state, bool block); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 72 | |
| 73 | // Removes the first invalid handle. This is called if MojoWaitMany finds an |
| 74 | // invalid handle. |
| 75 | void RemoveFirstInvalidHandle(const WaitState& wait_state); |
| 76 | |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 77 | void SignalControlPipe(const RunState& run_state); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 78 | |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 79 | WaitState GetWaitState(const RunState& run_state) const; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 80 | |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 81 | // Returns the deadline for the call to MojoWaitMany(). |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 82 | MojoDeadline GetDeadlineForWait(const RunState& run_state) const; |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 83 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 84 | // If non-NULL we're running (inside Run()). Member is reference to value on |
| 85 | // stack. |
| 86 | RunState* run_state_; |
| 87 | |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 88 | // Lock for accessing |run_state_|. In general the only method that we have to |
| 89 | // worry about is ScheduleWork(). All other methods are invoked on the same |
| 90 | // thread. |
| 91 | base::Lock run_state_lock_; |
| 92 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 93 | HandleToHandler handlers_; |
| 94 | |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 95 | // An ever increasing value assigned to each Handler::id. Used to detect |
| 96 | // uniqueness while notifying. That is, while notifying expired timers we copy |
| 97 | // |handlers_| and only notify handlers whose id match. If the id does not |
| 98 | // match it means the handler was removed then added so that we shouldn't |
| 99 | // notify it. |
| 100 | int next_handler_id_; |
| 101 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 102 | DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo); |
| 103 | }; |
| 104 | |
| 105 | } // namespace common |
| 106 | } // namespace mojo |
| 107 | |
| 108 | #endif // MOJO_COMMON_MESSAGE_PUMP_MOJO_H_ |