blob: 751311850ebb442e33e0963a728031baebe5baaf [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
5#ifndef MOJO_COMMON_MESSAGE_PUMP_MOJO_H_
6#define MOJO_COMMON_MESSAGE_PUMP_MOJO_H_
7
8#include <map>
9
[email protected]d7dc6add2014-03-20 05:59:5810#include "base/memory/scoped_ptr.h"
[email protected]3fee57b2013-11-12 16:35:0211#include "base/message_loop/message_pump.h"
[email protected]5273d8ec2014-04-16 05:34:1812#include "base/synchronization/lock.h"
[email protected]a6881fd2013-11-23 19:04:1913#include "base/time/time.h"
[email protected]3fee57b2013-11-12 16:35:0214#include "mojo/common/mojo_common_export.h"
[email protected]5dddd192014-03-28 00:53:2715#include "mojo/public/cpp/system/core.h"
[email protected]3fee57b2013-11-12 16:35:0216
17namespace mojo {
18namespace common {
19
20class MessagePumpMojoHandler;
21
22// Mojo implementation of MessagePump.
23class MOJO_COMMON_EXPORT MessagePumpMojo : public base::MessagePump {
24 public:
25 MessagePumpMojo();
26 virtual ~MessagePumpMojo();
27
[email protected]d7dc6add2014-03-20 05:59:5828 // Static factory function (for using with |base::Thread::Options|, wrapped
29 // using |base::Bind()|).
30 static scoped_ptr<base::MessagePump> Create();
31
[email protected]3fee57b2013-11-12 16:35:0232 // Registers a MessagePumpMojoHandler for the specified handle. Only one
[email protected]a6881fd2013-11-23 19:04:1933 // handler can be registered for a specified handle.
[email protected]3fee57b2013-11-12 16:35:0234 void AddHandler(MessagePumpMojoHandler* handler,
[email protected]bd6cb4bc2013-11-25 23:57:1635 const Handle& handle,
[email protected]e3da5f92014-06-18 10:18:1136 MojoHandleSignals wait_signals,
[email protected]a6881fd2013-11-23 19:04:1937 base::TimeTicks deadline);
[email protected]3fee57b2013-11-12 16:35:0238
[email protected]bd6cb4bc2013-11-25 23:57:1639 void RemoveHandler(const Handle& handle);
[email protected]3fee57b2013-11-12 16:35:0240
41 // MessagePump:
42 virtual void Run(Delegate* delegate) OVERRIDE;
43 virtual void Quit() OVERRIDE;
44 virtual void ScheduleWork() OVERRIDE;
45 virtual void ScheduleDelayedWork(
46 const base::TimeTicks& delayed_work_time) OVERRIDE;
47
48 private:
49 struct RunState;
50 struct WaitState;
51
[email protected]273e2172013-12-10 07:24:1652 // Contains the data needed to track a request to AddHandler().
[email protected]3fee57b2013-11-12 16:35:0253 struct Handler {
[email protected]9a067d52014-06-18 15:25:0354 Handler() : handler(NULL), wait_signals(MOJO_HANDLE_SIGNAL_NONE), id(0) {}
[email protected]3fee57b2013-11-12 16:35:0255
56 MessagePumpMojoHandler* handler;
[email protected]e3da5f92014-06-18 10:18:1157 MojoHandleSignals wait_signals;
[email protected]a6881fd2013-11-23 19:04:1958 base::TimeTicks deadline;
59 // See description of |MessagePumpMojo::next_handler_id_| for details.
60 int id;
[email protected]3fee57b2013-11-12 16:35:0261 };
62
[email protected]bd6cb4bc2013-11-25 23:57:1663 typedef std::map<Handle, Handler> HandleToHandler;
[email protected]3fee57b2013-11-12 16:35:0264
[email protected]5273d8ec2014-04-16 05:34:1865 // Implementation of Run().
66 void DoRunLoop(RunState* run_state, Delegate* delegate);
67
[email protected]3fee57b2013-11-12 16:35:0268 // Services the set of handles ready. If |block| is true this waits for a
69 // handle to become ready, otherwise this does not block.
[email protected]5273d8ec2014-04-16 05:34:1870 void DoInternalWork(const RunState& run_state, bool block);
[email protected]3fee57b2013-11-12 16:35:0271
72 // Removes the first invalid handle. This is called if MojoWaitMany finds an
73 // invalid handle.
74 void RemoveFirstInvalidHandle(const WaitState& wait_state);
75
[email protected]5273d8ec2014-04-16 05:34:1876 void SignalControlPipe(const RunState& run_state);
[email protected]3fee57b2013-11-12 16:35:0277
[email protected]5273d8ec2014-04-16 05:34:1878 WaitState GetWaitState(const RunState& run_state) const;
[email protected]3fee57b2013-11-12 16:35:0279
[email protected]a6881fd2013-11-23 19:04:1980 // Returns the deadline for the call to MojoWaitMany().
[email protected]5273d8ec2014-04-16 05:34:1881 MojoDeadline GetDeadlineForWait(const RunState& run_state) const;
[email protected]a6881fd2013-11-23 19:04:1982
[email protected]3fee57b2013-11-12 16:35:0283 // If non-NULL we're running (inside Run()). Member is reference to value on
84 // stack.
85 RunState* run_state_;
86
[email protected]5273d8ec2014-04-16 05:34:1887 // Lock for accessing |run_state_|. In general the only method that we have to
88 // worry about is ScheduleWork(). All other methods are invoked on the same
89 // thread.
90 base::Lock run_state_lock_;
91
[email protected]3fee57b2013-11-12 16:35:0292 HandleToHandler handlers_;
93
[email protected]a6881fd2013-11-23 19:04:1994 // An ever increasing value assigned to each Handler::id. Used to detect
95 // uniqueness while notifying. That is, while notifying expired timers we copy
96 // |handlers_| and only notify handlers whose id match. If the id does not
97 // match it means the handler was removed then added so that we shouldn't
98 // notify it.
99 int next_handler_id_;
100
[email protected]3fee57b2013-11-12 16:35:02101 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo);
102};
103
104} // namespace common
105} // namespace mojo
106
107#endif // MOJO_COMMON_MESSAGE_PUMP_MOJO_H_