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