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