1 // 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.
5 #ifndef MOJO_COMMON_MESSAGE_PUMP_MOJO_H_
6 #define MOJO_COMMON_MESSAGE_PUMP_MOJO_H_
10 #include "base/message_loop/message_pump.h"
11 #include "base/time/time.h"
12 #include "mojo/common/mojo_common_export.h"
13 #include "mojo/public/system/core_cpp.h"
18 class MessagePumpMojoHandler
;
20 // Mojo implementation of MessagePump.
21 class MOJO_COMMON_EXPORT MessagePumpMojo
: public base::MessagePump
{
24 virtual ~MessagePumpMojo();
26 // Registers a MessagePumpMojoHandler for the specified handle. Only one
27 // handler can be registered for a specified handle.
28 void AddHandler(MessagePumpMojoHandler
* handler
,
30 MojoWaitFlags wait_flags
,
31 base::TimeTicks deadline
);
33 void RemoveHandler(const Handle
& handle
);
36 virtual void Run(Delegate
* delegate
) OVERRIDE
;
37 virtual void Quit() OVERRIDE
;
38 virtual void ScheduleWork() OVERRIDE
;
39 virtual void ScheduleDelayedWork(
40 const base::TimeTicks
& delayed_work_time
) OVERRIDE
;
46 // Contains the data needed to track a request to AddHandler().
48 Handler() : handler(NULL
), wait_flags(MOJO_WAIT_FLAG_NONE
), id(0) {}
50 MessagePumpMojoHandler
* handler
;
51 MojoWaitFlags wait_flags
;
52 base::TimeTicks deadline
;
53 // See description of |MessagePumpMojo::next_handler_id_| for details.
57 typedef std::map
<Handle
, Handler
> HandleToHandler
;
59 // Services the set of handles ready. If |block| is true this waits for a
60 // handle to become ready, otherwise this does not block.
61 void DoInternalWork(bool block
);
63 // Removes the first invalid handle. This is called if MojoWaitMany finds an
65 void RemoveFirstInvalidHandle(const WaitState
& wait_state
);
67 void SignalControlPipe();
69 WaitState
GetWaitState() const;
71 // Returns the deadline for the call to MojoWaitMany().
72 MojoDeadline
GetDeadlineForWait() const;
74 // If non-NULL we're running (inside Run()). Member is reference to value on
78 HandleToHandler handlers_
;
80 // An ever increasing value assigned to each Handler::id. Used to detect
81 // uniqueness while notifying. That is, while notifying expired timers we copy
82 // |handlers_| and only notify handlers whose id match. If the id does not
83 // match it means the handler was removed then added so that we shouldn't
87 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo
);
93 #endif // MOJO_COMMON_MESSAGE_PUMP_MOJO_H_