blob: 4eba660294630be80082c6c0fd5c558ca4b1ce66 [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
10#include "base/message_loop/message_pump.h"
11#include "mojo/common/mojo_common_export.h"
12#include "mojo/public/system/core.h"
13
14namespace mojo {
15namespace common {
16
17class MessagePumpMojoHandler;
18
19// Mojo implementation of MessagePump.
20class MOJO_COMMON_EXPORT MessagePumpMojo : public base::MessagePump {
21 public:
22 MessagePumpMojo();
23 virtual ~MessagePumpMojo();
24
25 // Registers a MessagePumpMojoHandler for the specified handle. Only one
26 // handler can be registered for a specified handle. If there is an existing
27 // handler registered it is clobbered and silently removed.
28 // The handler is notified either when the handle is ready, or when it becomes
29 // invalid. If the handle becomes invalid the handler is removed and notified.
30 void AddHandler(MessagePumpMojoHandler* handler,
31 MojoHandle handle,
32 MojoWaitFlags wait_flags);
33
34 void RemoveHandler(MojoHandle handle);
35
36 // MessagePump:
37 virtual void Run(Delegate* delegate) OVERRIDE;
38 virtual void Quit() OVERRIDE;
39 virtual void ScheduleWork() OVERRIDE;
40 virtual void ScheduleDelayedWork(
41 const base::TimeTicks& delayed_work_time) OVERRIDE;
42
43 private:
44 struct RunState;
45 struct WaitState;
46
47 // Creates a MessagePumpMojoHandler and the set of MojoWaitFlags it was
48 // registered with.
49 struct Handler {
50 Handler() : handler(NULL), wait_flags(MOJO_WAIT_FLAG_NONE) {}
51
52 MessagePumpMojoHandler* handler;
53 MojoWaitFlags wait_flags;
54 };
55
56 typedef std::map<MojoHandle, Handler> HandleToHandler;
57
58 // Services the set of handles ready. If |block| is true this waits for a
59 // handle to become ready, otherwise this does not block.
60 void DoInternalWork(bool block);
61
62 // Removes the first invalid handle. This is called if MojoWaitMany finds an
63 // invalid handle.
64 void RemoveFirstInvalidHandle(const WaitState& wait_state);
65
66 void SignalControlPipe();
67
68 WaitState GetWaitState() const;
69
70 // If non-NULL we're running (inside Run()). Member is reference to value on
71 // stack.
72 RunState* run_state_;
73
74 HandleToHandler handlers_;
75
76 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo);
77};
78
79} // namespace common
80} // namespace mojo
81
82#endif // MOJO_COMMON_MESSAGE_PUMP_MOJO_H_