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_COMMON_MESSAGE_PUMP_MOJO_H_ | |
6 #define MOJO_COMMON_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 "mojo/common/mojo_common_export.h" | |
17 #include "third_party/mojo/src/mojo/public/cpp/system/core.h" | |
18 | |
19 namespace mojo { | |
20 namespace common { | |
21 | |
22 class MessagePumpMojoHandler; | |
23 | |
24 // Mojo implementation of MessagePump. | |
25 class MOJO_COMMON_EXPORT MessagePumpMojo : public base::MessagePump { | |
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 | |
38 MessagePumpMojo(); | |
39 ~MessagePumpMojo() override; | |
40 | |
41 // Static factory function (for using with |base::Thread::Options|, wrapped | |
42 // using |base::Bind()|). | |
43 static scoped_ptr<base::MessagePump> Create(); | |
44 | |
45 // Returns the MessagePumpMojo instance of the current thread, if it exists. | |
46 static MessagePumpMojo* current(); | |
47 | |
48 static bool IsCurrent() { return !!current(); } | |
49 | |
50 // Registers a MessagePumpMojoHandler for the specified handle. Only one | |
51 // handler can be registered for a specified handle. | |
52 // NOTE: a value of 0 for |deadline| indicates an indefinite timeout. | |
53 void AddHandler(MessagePumpMojoHandler* handler, | |
54 const Handle& handle, | |
55 MojoHandleSignals wait_signals, | |
56 base::TimeTicks deadline); | |
57 | |
58 void RemoveHandler(const Handle& handle); | |
59 | |
60 void AddObserver(Observer*); | |
61 void RemoveObserver(Observer*); | |
62 | |
63 // MessagePump: | |
64 void Run(Delegate* delegate) override; | |
65 void Quit() override; | |
66 void ScheduleWork() override; | |
67 void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time) override; | |
68 | |
69 private: | |
70 struct RunState; | |
71 struct WaitState; | |
72 | |
73 // Contains the data needed to track a request to AddHandler(). | |
74 struct Handler { | |
75 Handler() : handler(NULL), wait_signals(MOJO_HANDLE_SIGNAL_NONE), id(0) {} | |
76 | |
77 MessagePumpMojoHandler* handler; | |
78 MojoHandleSignals wait_signals; | |
79 base::TimeTicks deadline; | |
80 // See description of |MessagePumpMojo::next_handler_id_| for details. | |
81 int id; | |
82 }; | |
83 | |
84 typedef std::map<Handle, Handler> HandleToHandler; | |
85 | |
86 // Implementation of Run(). | |
87 void DoRunLoop(RunState* run_state, Delegate* delegate); | |
88 | |
89 // 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 | |
91 // handle has become ready, |false| otherwise. | |
92 bool DoInternalWork(const RunState& run_state, bool block); | |
93 | |
94 // Removes the given invalid handle. This is called if MojoWaitMany finds an | |
95 // invalid handle. | |
96 void RemoveInvalidHandle(const WaitState& wait_state, | |
97 MojoResult result, | |
98 uint32_t result_index); | |
99 | |
100 void SignalControlPipe(const RunState& run_state); | |
101 | |
102 WaitState GetWaitState(const RunState& run_state) const; | |
103 | |
104 // Returns the deadline for the call to MojoWaitMany(). | |
105 MojoDeadline GetDeadlineForWait(const RunState& run_state) const; | |
106 | |
107 void WillSignalHandler(); | |
108 void DidSignalHandler(); | |
109 | |
110 // If non-NULL we're running (inside Run()). Member is reference to value on | |
111 // stack. | |
112 RunState* run_state_; | |
113 | |
114 // 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 | |
116 // thread. | |
117 base::Lock run_state_lock_; | |
118 | |
119 HandleToHandler handlers_; | |
120 | |
121 // An ever increasing value assigned to each Handler::id. Used to detect | |
122 // 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 | |
124 // match it means the handler was removed then added so that we shouldn't | |
125 // notify it. | |
126 int next_handler_id_; | |
127 | |
128 base::ObserverList<Observer> observers_; | |
129 | |
130 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo); | |
131 }; | |
132 | |
133 } // namespace common | |
134 } // namespace mojo | |
135 | |
136 #endif // MOJO_COMMON_MESSAGE_PUMP_MOJO_H_ | |
OLD | NEW |