| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_COMMON_MESSAGE_PUMP_MOJO_H_ | 5 #ifndef MOJO_COMMON_MESSAGE_PUMP_MOJO_H_ |
| 6 #define MOJO_COMMON_MESSAGE_PUMP_MOJO_H_ | 6 #define MOJO_COMMON_MESSAGE_PUMP_MOJO_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_pump.h" | 12 #include "base/message_loop/message_pump.h" |
| 13 #include "base/observer_list.h" |
| 13 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 15 #include "mojo/common/mojo_common_export.h" | 16 #include "mojo/common/mojo_common_export.h" |
| 16 #include "mojo/public/cpp/system/core.h" | 17 #include "mojo/public/cpp/system/core.h" |
| 17 | 18 |
| 18 namespace mojo { | 19 namespace mojo { |
| 19 namespace common { | 20 namespace common { |
| 20 | 21 |
| 21 class MessagePumpMojoHandler; | 22 class MessagePumpMojoHandler; |
| 22 | 23 |
| 23 // Mojo implementation of MessagePump. | 24 // Mojo implementation of MessagePump. |
| 24 class MOJO_COMMON_EXPORT MessagePumpMojo : public base::MessagePump { | 25 class MOJO_COMMON_EXPORT MessagePumpMojo : public base::MessagePump { |
| 25 public: | 26 public: |
| 27 class Observer { |
| 28 public: |
| 29 Observer() {} |
| 30 |
| 31 virtual void WillSignalHandler() = 0; |
| 32 virtual void DidSignalHandler() = 0; |
| 33 |
| 34 protected: |
| 35 virtual ~Observer() {} |
| 36 }; |
| 37 |
| 26 MessagePumpMojo(); | 38 MessagePumpMojo(); |
| 27 virtual ~MessagePumpMojo(); | 39 virtual ~MessagePumpMojo(); |
| 28 | 40 |
| 29 // Static factory function (for using with |base::Thread::Options|, wrapped | 41 // Static factory function (for using with |base::Thread::Options|, wrapped |
| 30 // using |base::Bind()|). | 42 // using |base::Bind()|). |
| 31 static scoped_ptr<base::MessagePump> Create(); | 43 static scoped_ptr<base::MessagePump> Create(); |
| 32 | 44 |
| 33 // Returns the MessagePumpMojo instance of the current thread, if it exists. | 45 // Returns the MessagePumpMojo instance of the current thread, if it exists. |
| 34 static MessagePumpMojo* current(); | 46 static MessagePumpMojo* current(); |
| 35 | 47 |
| 36 static bool IsCurrent() { return !!current(); } | 48 static bool IsCurrent() { return !!current(); } |
| 37 | 49 |
| 38 // Registers a MessagePumpMojoHandler for the specified handle. Only one | 50 // Registers a MessagePumpMojoHandler for the specified handle. Only one |
| 39 // handler can be registered for a specified handle. | 51 // handler can be registered for a specified handle. |
| 40 // NOTE: a value of 0 for |deadline| indicates an indefinite timeout. | 52 // NOTE: a value of 0 for |deadline| indicates an indefinite timeout. |
| 41 void AddHandler(MessagePumpMojoHandler* handler, | 53 void AddHandler(MessagePumpMojoHandler* handler, |
| 42 const Handle& handle, | 54 const Handle& handle, |
| 43 MojoHandleSignals wait_signals, | 55 MojoHandleSignals wait_signals, |
| 44 base::TimeTicks deadline); | 56 base::TimeTicks deadline); |
| 45 | 57 |
| 46 void RemoveHandler(const Handle& handle); | 58 void RemoveHandler(const Handle& handle); |
| 47 | 59 |
| 60 void AddObserver(Observer*); |
| 61 void RemoveObserver(Observer*); |
| 62 |
| 48 // MessagePump: | 63 // MessagePump: |
| 49 virtual void Run(Delegate* delegate) override; | 64 virtual void Run(Delegate* delegate) override; |
| 50 virtual void Quit() override; | 65 virtual void Quit() override; |
| 51 virtual void ScheduleWork() override; | 66 virtual void ScheduleWork() override; |
| 52 virtual void ScheduleDelayedWork( | 67 virtual void ScheduleDelayedWork( |
| 53 const base::TimeTicks& delayed_work_time) override; | 68 const base::TimeTicks& delayed_work_time) override; |
| 54 | 69 |
| 55 private: | 70 private: |
| 56 struct RunState; | 71 struct RunState; |
| 57 struct WaitState; | 72 struct WaitState; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 81 // invalid handle. | 96 // invalid handle. |
| 82 void RemoveFirstInvalidHandle(const WaitState& wait_state); | 97 void RemoveFirstInvalidHandle(const WaitState& wait_state); |
| 83 | 98 |
| 84 void SignalControlPipe(const RunState& run_state); | 99 void SignalControlPipe(const RunState& run_state); |
| 85 | 100 |
| 86 WaitState GetWaitState(const RunState& run_state) const; | 101 WaitState GetWaitState(const RunState& run_state) const; |
| 87 | 102 |
| 88 // Returns the deadline for the call to MojoWaitMany(). | 103 // Returns the deadline for the call to MojoWaitMany(). |
| 89 MojoDeadline GetDeadlineForWait(const RunState& run_state) const; | 104 MojoDeadline GetDeadlineForWait(const RunState& run_state) const; |
| 90 | 105 |
| 106 void WillSignalHandler(); |
| 107 void DidSignalHandler(); |
| 108 |
| 91 // If non-NULL we're running (inside Run()). Member is reference to value on | 109 // If non-NULL we're running (inside Run()). Member is reference to value on |
| 92 // stack. | 110 // stack. |
| 93 RunState* run_state_; | 111 RunState* run_state_; |
| 94 | 112 |
| 95 // Lock for accessing |run_state_|. In general the only method that we have to | 113 // Lock for accessing |run_state_|. In general the only method that we have to |
| 96 // worry about is ScheduleWork(). All other methods are invoked on the same | 114 // worry about is ScheduleWork(). All other methods are invoked on the same |
| 97 // thread. | 115 // thread. |
| 98 base::Lock run_state_lock_; | 116 base::Lock run_state_lock_; |
| 99 | 117 |
| 100 HandleToHandler handlers_; | 118 HandleToHandler handlers_; |
| 101 | 119 |
| 102 // An ever increasing value assigned to each Handler::id. Used to detect | 120 // An ever increasing value assigned to each Handler::id. Used to detect |
| 103 // uniqueness while notifying. That is, while notifying expired timers we copy | 121 // uniqueness while notifying. That is, while notifying expired timers we copy |
| 104 // |handlers_| and only notify handlers whose id match. If the id does not | 122 // |handlers_| and only notify handlers whose id match. If the id does not |
| 105 // match it means the handler was removed then added so that we shouldn't | 123 // match it means the handler was removed then added so that we shouldn't |
| 106 // notify it. | 124 // notify it. |
| 107 int next_handler_id_; | 125 int next_handler_id_; |
| 108 | 126 |
| 127 ObserverList<Observer> observers_; |
| 128 |
| 109 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo); | 129 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo); |
| 110 }; | 130 }; |
| 111 | 131 |
| 112 } // namespace common | 132 } // namespace common |
| 113 } // namespace mojo | 133 } // namespace mojo |
| 114 | 134 |
| 115 #endif // MOJO_COMMON_MESSAGE_PUMP_MOJO_H_ | 135 #endif // MOJO_COMMON_MESSAGE_PUMP_MOJO_H_ |
| OLD | NEW |