blob: 06869730eec7a0e1fa83622b3639432e905a744e [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
8#include <map>
9
viettrungluu473e9132014-09-30 05:04:5310#include "base/macros.h"
[email protected]d7dc6add2014-03-20 05:59:5811#include "base/memory/scoped_ptr.h"
[email protected]3fee57b2013-11-12 16:35:0212#include "base/message_loop/message_pump.h"
abarth4ba3cad2014-10-17 22:02:1513#include "base/observer_list.h"
[email protected]5273d8ec2014-04-16 05:34:1814#include "base/synchronization/lock.h"
[email protected]a6881fd2013-11-23 19:04:1915#include "base/time/time.h"
skyadd030b2015-08-07 01:06:1816#include "mojo/message_pump/mojo_message_pump_export.h"
rockot85dce0862015-11-13 01:33:5917#include "mojo/public/cpp/system/core.h"
[email protected]3fee57b2013-11-12 16:35:0218
19namespace mojo {
20namespace common {
21
22class MessagePumpMojoHandler;
23
24// Mojo implementation of MessagePump.
skyadd030b2015-08-07 01:06:1825class MOJO_MESSAGE_PUMP_EXPORT MessagePumpMojo : public base::MessagePump {
[email protected]3fee57b2013-11-12 16:35:0226 public:
skyadd030b2015-08-07 01:06:1827 class MOJO_MESSAGE_PUMP_EXPORT Observer {
abarth4ba3cad2014-10-17 22:02:1528 public:
29 Observer() {}
30
31 virtual void WillSignalHandler() = 0;
32 virtual void DidSignalHandler() = 0;
33
34 protected:
35 virtual ~Observer() {}
36 };
37
[email protected]3fee57b2013-11-12 16:35:0238 MessagePumpMojo();
dcheng3fc12db2014-10-21 12:16:2839 ~MessagePumpMojo() override;
[email protected]3fee57b2013-11-12 16:35:0240
[email protected]d7dc6add2014-03-20 05:59:5841 // Static factory function (for using with |base::Thread::Options|, wrapped
42 // using |base::Bind()|).
43 static scoped_ptr<base::MessagePump> Create();
44
yzshend928bdd2014-08-29 22:51:4245 // Returns the MessagePumpMojo instance of the current thread, if it exists.
46 static MessagePumpMojo* current();
47
48 static bool IsCurrent() { return !!current(); }
49
[email protected]3fee57b2013-11-12 16:35:0250 // Registers a MessagePumpMojoHandler for the specified handle. Only one
[email protected]a6881fd2013-11-23 19:04:1951 // handler can be registered for a specified handle.
[email protected]9a412922014-08-14 15:48:2452 // NOTE: a value of 0 for |deadline| indicates an indefinite timeout.
rockot2efa33852015-10-12 17:52:2853 void AddHandler(MessagePumpMojoHandler* handler,
[email protected]bd6cb4bc2013-11-25 23:57:1654 const Handle& handle,
[email protected]e3da5f92014-06-18 10:18:1155 MojoHandleSignals wait_signals,
[email protected]a6881fd2013-11-23 19:04:1956 base::TimeTicks deadline);
[email protected]3fee57b2013-11-12 16:35:0257
[email protected]bd6cb4bc2013-11-25 23:57:1658 void RemoveHandler(const Handle& handle);
[email protected]3fee57b2013-11-12 16:35:0259
abarth4ba3cad2014-10-17 22:02:1560 void AddObserver(Observer*);
61 void RemoveObserver(Observer*);
62
[email protected]3fee57b2013-11-12 16:35:0263 // MessagePump:
dcheng3fc12db2014-10-21 12:16:2864 void Run(Delegate* delegate) override;
65 void Quit() override;
66 void ScheduleWork() override;
67 void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time) override;
[email protected]3fee57b2013-11-12 16:35:0268
69 private:
70 struct RunState;
71 struct WaitState;
72
[email protected]273e2172013-12-10 07:24:1673 // Contains the data needed to track a request to AddHandler().
[email protected]3fee57b2013-11-12 16:35:0274 struct Handler {
rockot2efa33852015-10-12 17:52:2875 Handler() : handler(NULL), wait_signals(MOJO_HANDLE_SIGNAL_NONE), id(0) {}
[email protected]3fee57b2013-11-12 16:35:0276
[email protected]3fee57b2013-11-12 16:35:0277 MessagePumpMojoHandler* handler;
[email protected]e3da5f92014-06-18 10:18:1178 MojoHandleSignals wait_signals;
[email protected]a6881fd2013-11-23 19:04:1979 base::TimeTicks deadline;
80 // See description of |MessagePumpMojo::next_handler_id_| for details.
81 int id;
[email protected]3fee57b2013-11-12 16:35:0282 };
83
[email protected]bd6cb4bc2013-11-25 23:57:1684 typedef std::map<Handle, Handler> HandleToHandler;
[email protected]3fee57b2013-11-12 16:35:0285
[email protected]5273d8ec2014-04-16 05:34:1886 // Implementation of Run().
87 void DoRunLoop(RunState* run_state, Delegate* delegate);
88
[email protected]3fee57b2013-11-12 16:35:0289 // Services the set of handles ready. If |block| is true this waits for a
qsrd0a9ca12014-10-06 16:33:5890 // 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);
[email protected]3fee57b2013-11-12 16:35:0293
rockot6dcbf7c2015-01-16 00:41:1394 // Removes the given invalid handle. This is called if MojoWaitMany finds an
[email protected]3fee57b2013-11-12 16:35:0295 // invalid handle.
rockot6dcbf7c2015-01-16 00:41:1396 void RemoveInvalidHandle(const WaitState& wait_state,
97 MojoResult result,
98 uint32_t result_index);
[email protected]3fee57b2013-11-12 16:35:0299
[email protected]5273d8ec2014-04-16 05:34:18100 void SignalControlPipe(const RunState& run_state);
[email protected]3fee57b2013-11-12 16:35:02101
[email protected]5273d8ec2014-04-16 05:34:18102 WaitState GetWaitState(const RunState& run_state) const;
[email protected]3fee57b2013-11-12 16:35:02103
[email protected]a6881fd2013-11-23 19:04:19104 // Returns the deadline for the call to MojoWaitMany().
[email protected]5273d8ec2014-04-16 05:34:18105 MojoDeadline GetDeadlineForWait(const RunState& run_state) const;
[email protected]a6881fd2013-11-23 19:04:19106
abarth4ba3cad2014-10-17 22:02:15107 void WillSignalHandler();
108 void DidSignalHandler();
109
[email protected]3fee57b2013-11-12 16:35:02110 // If non-NULL we're running (inside Run()). Member is reference to value on
111 // stack.
112 RunState* run_state_;
113
[email protected]5273d8ec2014-04-16 05:34:18114 // 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
[email protected]3fee57b2013-11-12 16:35:02119 HandleToHandler handlers_;
120
[email protected]a6881fd2013-11-23 19:04:19121 // 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
brettw236d3172015-06-03 16:31:43128 base::ObserverList<Observer> observers_;
abarth4ba3cad2014-10-17 22:02:15129
[email protected]3fee57b2013-11-12 16:35:02130 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo);
131};
132
133} // namespace common
134} // namespace mojo
135
skyadd030b2015-08-07 01:06:18136#endif // MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_