blob: cc21565b65588155ade6caeafb8d2dfeeb2236b2 [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]a6881fd2013-11-23 19:04:1912#include "base/time/time.h"
[email protected]3fee57b2013-11-12 16:35:0213#include "mojo/common/mojo_common_export.h"
[email protected]5dddd192014-03-28 00:53:2714#include "mojo/public/cpp/system/core.h"
[email protected]3fee57b2013-11-12 16:35:0215
16namespace mojo {
17namespace common {
18
19class MessagePumpMojoHandler;
20
21// Mojo implementation of MessagePump.
22class MOJO_COMMON_EXPORT MessagePumpMojo : public base::MessagePump {
23 public:
24 MessagePumpMojo();
25 virtual ~MessagePumpMojo();
26
[email protected]d7dc6add2014-03-20 05:59:5827 // Static factory function (for using with |base::Thread::Options|, wrapped
28 // using |base::Bind()|).
29 static scoped_ptr<base::MessagePump> Create();
30
[email protected]3fee57b2013-11-12 16:35:0231 // Registers a MessagePumpMojoHandler for the specified handle. Only one
[email protected]a6881fd2013-11-23 19:04:1932 // handler can be registered for a specified handle.
[email protected]3fee57b2013-11-12 16:35:0233 void AddHandler(MessagePumpMojoHandler* handler,
[email protected]bd6cb4bc2013-11-25 23:57:1634 const Handle& handle,
[email protected]a6881fd2013-11-23 19:04:1935 MojoWaitFlags wait_flags,
36 base::TimeTicks deadline);
[email protected]3fee57b2013-11-12 16:35:0237
[email protected]bd6cb4bc2013-11-25 23:57:1638 void RemoveHandler(const Handle& handle);
[email protected]3fee57b2013-11-12 16:35:0239
40 // MessagePump:
41 virtual void Run(Delegate* delegate) OVERRIDE;
42 virtual void Quit() OVERRIDE;
43 virtual void ScheduleWork() OVERRIDE;
44 virtual void ScheduleDelayedWork(
45 const base::TimeTicks& delayed_work_time) OVERRIDE;
46
47 private:
48 struct RunState;
49 struct WaitState;
50
[email protected]273e2172013-12-10 07:24:1651 // Contains the data needed to track a request to AddHandler().
[email protected]3fee57b2013-11-12 16:35:0252 struct Handler {
[email protected]a6881fd2013-11-23 19:04:1953 Handler() : handler(NULL), wait_flags(MOJO_WAIT_FLAG_NONE), id(0) {}
[email protected]3fee57b2013-11-12 16:35:0254
55 MessagePumpMojoHandler* handler;
56 MojoWaitFlags wait_flags;
[email protected]a6881fd2013-11-23 19:04:1957 base::TimeTicks deadline;
58 // See description of |MessagePumpMojo::next_handler_id_| for details.
59 int id;
[email protected]3fee57b2013-11-12 16:35:0260 };
61
[email protected]bd6cb4bc2013-11-25 23:57:1662 typedef std::map<Handle, Handler> HandleToHandler;
[email protected]3fee57b2013-11-12 16:35:0263
64 // Services the set of handles ready. If |block| is true this waits for a
65 // handle to become ready, otherwise this does not block.
66 void DoInternalWork(bool block);
67
68 // Removes the first invalid handle. This is called if MojoWaitMany finds an
69 // invalid handle.
70 void RemoveFirstInvalidHandle(const WaitState& wait_state);
71
72 void SignalControlPipe();
73
74 WaitState GetWaitState() const;
75
[email protected]a6881fd2013-11-23 19:04:1976 // Returns the deadline for the call to MojoWaitMany().
77 MojoDeadline GetDeadlineForWait() const;
78
[email protected]3fee57b2013-11-12 16:35:0279 // If non-NULL we're running (inside Run()). Member is reference to value on
80 // stack.
81 RunState* run_state_;
82
83 HandleToHandler handlers_;
84
[email protected]a6881fd2013-11-23 19:04:1985 // An ever increasing value assigned to each Handler::id. Used to detect
86 // uniqueness while notifying. That is, while notifying expired timers we copy
87 // |handlers_| and only notify handlers whose id match. If the id does not
88 // match it means the handler was removed then added so that we shouldn't
89 // notify it.
90 int next_handler_id_;
91
[email protected]3fee57b2013-11-12 16:35:0292 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo);
93};
94
95} // namespace common
96} // namespace mojo
97
98#endif // MOJO_COMMON_MESSAGE_PUMP_MOJO_H_