blob: 345bdb6bbd26b378b6a8736c6a7cc8d52454279a [file] [log] [blame]
[email protected]3fee57b2013-11-12 16:35:021// 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
skyadd030b2015-08-07 01:06:185#ifndef MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_
6#define MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_
[email protected]3fee57b2013-11-12 16:35:027
Avi Drissman2e88ac372015-12-21 18:14:578#include <stdint.h>
9
[email protected]3fee57b2013-11-12 16:35:0210#include <map>
amistrye8de0bc02015-11-24 08:42:4911#include <set>
[email protected]3fee57b2013-11-12 16:35:0212
viettrungluu473e9132014-09-30 05:04:5313#include "base/macros.h"
[email protected]d7dc6add2014-03-20 05:59:5814#include "base/memory/scoped_ptr.h"
[email protected]3fee57b2013-11-12 16:35:0215#include "base/message_loop/message_pump.h"
abarth4ba3cad2014-10-17 22:02:1516#include "base/observer_list.h"
[email protected]5273d8ec2014-04-16 05:34:1817#include "base/synchronization/lock.h"
[email protected]a6881fd2013-11-23 19:04:1918#include "base/time/time.h"
skyadd030b2015-08-07 01:06:1819#include "mojo/message_pump/mojo_message_pump_export.h"
rockot85dce0862015-11-13 01:33:5920#include "mojo/public/cpp/system/core.h"
[email protected]3fee57b2013-11-12 16:35:0221
22namespace mojo {
23namespace common {
24
25class MessagePumpMojoHandler;
26
27// Mojo implementation of MessagePump.
skyadd030b2015-08-07 01:06:1828class MOJO_MESSAGE_PUMP_EXPORT MessagePumpMojo : public base::MessagePump {
[email protected]3fee57b2013-11-12 16:35:0229 public:
skyadd030b2015-08-07 01:06:1830 class MOJO_MESSAGE_PUMP_EXPORT Observer {
abarth4ba3cad2014-10-17 22:02:1531 public:
32 Observer() {}
33
34 virtual void WillSignalHandler() = 0;
35 virtual void DidSignalHandler() = 0;
36
37 protected:
38 virtual ~Observer() {}
39 };
40
[email protected]3fee57b2013-11-12 16:35:0241 MessagePumpMojo();
dcheng3fc12db2014-10-21 12:16:2842 ~MessagePumpMojo() override;
[email protected]3fee57b2013-11-12 16:35:0243
[email protected]d7dc6add2014-03-20 05:59:5844 // Static factory function (for using with |base::Thread::Options|, wrapped
45 // using |base::Bind()|).
46 static scoped_ptr<base::MessagePump> Create();
47
yzshend928bdd2014-08-29 22:51:4248 // Returns the MessagePumpMojo instance of the current thread, if it exists.
49 static MessagePumpMojo* current();
50
51 static bool IsCurrent() { return !!current(); }
52
[email protected]3fee57b2013-11-12 16:35:0253 // Registers a MessagePumpMojoHandler for the specified handle. Only one
[email protected]a6881fd2013-11-23 19:04:1954 // handler can be registered for a specified handle.
[email protected]9a412922014-08-14 15:48:2455 // NOTE: a value of 0 for |deadline| indicates an indefinite timeout.
rockot2efa33852015-10-12 17:52:2856 void AddHandler(MessagePumpMojoHandler* handler,
[email protected]bd6cb4bc2013-11-25 23:57:1657 const Handle& handle,
[email protected]e3da5f92014-06-18 10:18:1158 MojoHandleSignals wait_signals,
[email protected]a6881fd2013-11-23 19:04:1959 base::TimeTicks deadline);
[email protected]3fee57b2013-11-12 16:35:0260
[email protected]bd6cb4bc2013-11-25 23:57:1661 void RemoveHandler(const Handle& handle);
[email protected]3fee57b2013-11-12 16:35:0262
abarth4ba3cad2014-10-17 22:02:1563 void AddObserver(Observer*);
64 void RemoveObserver(Observer*);
65
[email protected]3fee57b2013-11-12 16:35:0266 // MessagePump:
dcheng3fc12db2014-10-21 12:16:2867 void Run(Delegate* delegate) override;
68 void Quit() override;
69 void ScheduleWork() override;
70 void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time) override;
[email protected]3fee57b2013-11-12 16:35:0271
72 private:
73 struct RunState;
[email protected]3fee57b2013-11-12 16:35:0274
[email protected]273e2172013-12-10 07:24:1675 // Contains the data needed to track a request to AddHandler().
[email protected]3fee57b2013-11-12 16:35:0276 struct Handler {
rockot2efa33852015-10-12 17:52:2877 Handler() : handler(NULL), wait_signals(MOJO_HANDLE_SIGNAL_NONE), id(0) {}
[email protected]3fee57b2013-11-12 16:35:0278
79 MessagePumpMojoHandler* handler;
[email protected]e3da5f92014-06-18 10:18:1180 MojoHandleSignals wait_signals;
[email protected]a6881fd2013-11-23 19:04:1981 base::TimeTicks deadline;
82 // See description of |MessagePumpMojo::next_handler_id_| for details.
83 int id;
[email protected]3fee57b2013-11-12 16:35:0284 };
85
[email protected]bd6cb4bc2013-11-25 23:57:1686 typedef std::map<Handle, Handler> HandleToHandler;
[email protected]3fee57b2013-11-12 16:35:0287
[email protected]5273d8ec2014-04-16 05:34:1888 // Implementation of Run().
89 void DoRunLoop(RunState* run_state, Delegate* delegate);
90
[email protected]3fee57b2013-11-12 16:35:0291 // Services the set of handles ready. If |block| is true this waits for a
qsrd0a9ca12014-10-06 16:33:5892 // handle to become ready, otherwise this does not block. Returns |true| if a
93 // handle has become ready, |false| otherwise.
94 bool DoInternalWork(const RunState& run_state, bool block);
[email protected]3fee57b2013-11-12 16:35:0295
amistry6aebd412016-01-06 00:29:1796 // Waits for handles in the wait set to become ready. Returns |true| if ready
97 // handles may be available, or |false| if the wait's deadline was exceeded.
98 // Note, ready handles may be unavailable, even though |true| was returned.
99 bool WaitForReadyHandles(const RunState& run_state) const;
100
101 // Retrieves any 'ready' handles from the wait set, and runs the handler's
102 // OnHandleReady() or OnHandleError() functions as necessary. Returns |true|
103 // if any handles were ready and processed.
104 bool ProcessReadyHandles();
105
106 // Removes the given invalid handle. This is called if MojoGetReadyHandles
107 // finds an invalid or closed handle.
108 void RemoveInvalidHandle(MojoResult result, Handle handle);
109
110 // Removes any handles that have expired their deadline. Runs the handler's
111 // OnHandleError() function with |MOJO_RESULT_DEADLINE_EXCEEDED| as the
112 // result. Returns |true| if any handles were removed.
113 bool RemoveExpiredHandles();
[email protected]3fee57b2013-11-12 16:35:02114
amistry240e4e52015-11-18 00:38:40115 void SignalControlPipe();
[email protected]3fee57b2013-11-12 16:35:02116
amistry6aebd412016-01-06 00:29:17117 // Returns the deadline for the call to MojoWait().
[email protected]5273d8ec2014-04-16 05:34:18118 MojoDeadline GetDeadlineForWait(const RunState& run_state) const;
[email protected]a6881fd2013-11-23 19:04:19119
amistry6aebd412016-01-06 00:29:17120 // Run |OnHandleReady()| for the handler registered with |handle|. |handle|
121 // must be registered.
122 void SignalHandleReady(Handle handle);
123
abarth4ba3cad2014-10-17 22:02:15124 void WillSignalHandler();
125 void DidSignalHandler();
126
[email protected]3fee57b2013-11-12 16:35:02127 // If non-NULL we're running (inside Run()). Member is reference to value on
128 // stack.
129 RunState* run_state_;
130
[email protected]5273d8ec2014-04-16 05:34:18131 // Lock for accessing |run_state_|. In general the only method that we have to
132 // worry about is ScheduleWork(). All other methods are invoked on the same
133 // thread.
134 base::Lock run_state_lock_;
135
[email protected]3fee57b2013-11-12 16:35:02136 HandleToHandler handlers_;
amistrye8de0bc02015-11-24 08:42:49137 // Set of handles that have a deadline set. Avoids iterating over all elements
138 // in |handles_| in the common case (no deadline set).
139 // TODO(amistry): Make this better and avoid special-casing deadlines.
140 std::set<Handle> deadline_handles_;
[email protected]3fee57b2013-11-12 16:35:02141
[email protected]a6881fd2013-11-23 19:04:19142 // An ever increasing value assigned to each Handler::id. Used to detect
143 // uniqueness while notifying. That is, while notifying expired timers we copy
144 // |handlers_| and only notify handlers whose id match. If the id does not
145 // match it means the handler was removed then added so that we shouldn't
146 // notify it.
147 int next_handler_id_;
148
brettw236d3172015-06-03 16:31:43149 base::ObserverList<Observer> observers_;
abarth4ba3cad2014-10-17 22:02:15150
amistry6aebd412016-01-06 00:29:17151 // Mojo handle for the wait set.
152 ScopedHandle wait_set_handle_;
amistry240e4e52015-11-18 00:38:40153 // Used to wake up run loop from |SignalControlPipe()|.
154 ScopedMessagePipeHandle read_handle_;
155 ScopedMessagePipeHandle write_handle_;
156
[email protected]3fee57b2013-11-12 16:35:02157 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo);
158};
159
160} // namespace common
161} // namespace mojo
162
skyadd030b2015-08-07 01:06:18163#endif // MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_