blob: 192438f869119cedbd9a6a80866df35eba88c2a9 [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;
74 struct WaitState;
75
[email protected]273e2172013-12-10 07:24:1676 // Contains the data needed to track a request to AddHandler().
[email protected]3fee57b2013-11-12 16:35:0277 struct Handler {
rockot2efa33852015-10-12 17:52:2878 Handler() : handler(NULL), wait_signals(MOJO_HANDLE_SIGNAL_NONE), id(0) {}
[email protected]3fee57b2013-11-12 16:35:0279
80 MessagePumpMojoHandler* handler;
[email protected]e3da5f92014-06-18 10:18:1181 MojoHandleSignals wait_signals;
[email protected]a6881fd2013-11-23 19:04:1982 base::TimeTicks deadline;
83 // See description of |MessagePumpMojo::next_handler_id_| for details.
84 int id;
[email protected]3fee57b2013-11-12 16:35:0285 };
86
[email protected]bd6cb4bc2013-11-25 23:57:1687 typedef std::map<Handle, Handler> HandleToHandler;
[email protected]3fee57b2013-11-12 16:35:0288
[email protected]5273d8ec2014-04-16 05:34:1889 // Implementation of Run().
90 void DoRunLoop(RunState* run_state, Delegate* delegate);
91
[email protected]3fee57b2013-11-12 16:35:0292 // Services the set of handles ready. If |block| is true this waits for a
qsrd0a9ca12014-10-06 16:33:5893 // handle to become ready, otherwise this does not block. Returns |true| if a
94 // handle has become ready, |false| otherwise.
95 bool DoInternalWork(const RunState& run_state, bool block);
[email protected]3fee57b2013-11-12 16:35:0296
rockot6dcbf7c2015-01-16 00:41:1397 // Removes the given invalid handle. This is called if MojoWaitMany finds an
[email protected]3fee57b2013-11-12 16:35:0298 // invalid handle.
rockot6dcbf7c2015-01-16 00:41:1399 void RemoveInvalidHandle(const WaitState& wait_state,
100 MojoResult result,
101 uint32_t result_index);
[email protected]3fee57b2013-11-12 16:35:02102
amistry240e4e52015-11-18 00:38:40103 void SignalControlPipe();
[email protected]3fee57b2013-11-12 16:35:02104
amistry240e4e52015-11-18 00:38:40105 WaitState GetWaitState() const;
[email protected]3fee57b2013-11-12 16:35:02106
[email protected]a6881fd2013-11-23 19:04:19107 // Returns the deadline for the call to MojoWaitMany().
[email protected]5273d8ec2014-04-16 05:34:18108 MojoDeadline GetDeadlineForWait(const RunState& run_state) const;
[email protected]a6881fd2013-11-23 19:04:19109
abarth4ba3cad2014-10-17 22:02:15110 void WillSignalHandler();
111 void DidSignalHandler();
112
[email protected]3fee57b2013-11-12 16:35:02113 // If non-NULL we're running (inside Run()). Member is reference to value on
114 // stack.
115 RunState* run_state_;
116
[email protected]5273d8ec2014-04-16 05:34:18117 // Lock for accessing |run_state_|. In general the only method that we have to
118 // worry about is ScheduleWork(). All other methods are invoked on the same
119 // thread.
120 base::Lock run_state_lock_;
121
[email protected]3fee57b2013-11-12 16:35:02122 HandleToHandler handlers_;
amistrye8de0bc02015-11-24 08:42:49123 // Set of handles that have a deadline set. Avoids iterating over all elements
124 // in |handles_| in the common case (no deadline set).
125 // TODO(amistry): Make this better and avoid special-casing deadlines.
126 std::set<Handle> deadline_handles_;
[email protected]3fee57b2013-11-12 16:35:02127
[email protected]a6881fd2013-11-23 19:04:19128 // An ever increasing value assigned to each Handler::id. Used to detect
129 // uniqueness while notifying. That is, while notifying expired timers we copy
130 // |handlers_| and only notify handlers whose id match. If the id does not
131 // match it means the handler was removed then added so that we shouldn't
132 // notify it.
133 int next_handler_id_;
134
brettw236d3172015-06-03 16:31:43135 base::ObserverList<Observer> observers_;
abarth4ba3cad2014-10-17 22:02:15136
amistry240e4e52015-11-18 00:38:40137 // Used to wake up run loop from |SignalControlPipe()|.
138 ScopedMessagePipeHandle read_handle_;
139 ScopedMessagePipeHandle write_handle_;
140
[email protected]3fee57b2013-11-12 16:35:02141 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo);
142};
143
144} // namespace common
145} // namespace mojo
146
skyadd030b2015-08-07 01:06:18147#endif // MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_