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