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/memory/scoped_ptr.h"
11 #include "base/message_loop/message_pump.h"
12 #include "base/time/time.h"
13 #include "mojo/common/mojo_common_export.h"
14 #include "mojo/public/system/core_cpp.h"
19 class MessagePumpMojoHandler
;
21 // Mojo implementation of MessagePump.
22 class MOJO_COMMON_EXPORT MessagePumpMojo
: public base::MessagePump
{
25 virtual ~MessagePumpMojo();
27 // Static factory function (for using with |base::Thread::Options|, wrapped
28 // using |base::Bind()|).
29 static scoped_ptr
<base::MessagePump
> Create();
31 // Registers a MessagePumpMojoHandler for the specified handle. Only one
32 // handler can be registered for a specified handle.
33 void AddHandler(MessagePumpMojoHandler
* handler
,
35 MojoWaitFlags wait_flags
,
36 base::TimeTicks deadline
);
38 void RemoveHandler(const Handle
& handle
);
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
;
51 // Contains the data needed to track a request to AddHandler().
53 Handler() : handler(NULL
), wait_flags(MOJO_WAIT_FLAG_NONE
), id(0) {}
55 MessagePumpMojoHandler
* handler
;
56 MojoWaitFlags wait_flags
;
57 base::TimeTicks deadline
;
58 // See description of |MessagePumpMojo::next_handler_id_| for details.
62 typedef std::map
<Handle
, Handler
> HandleToHandler
;
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
);
68 // Removes the first invalid handle. This is called if MojoWaitMany finds an
70 void RemoveFirstInvalidHandle(const WaitState
& wait_state
);
72 void SignalControlPipe();
74 WaitState
GetWaitState() const;
76 // Returns the deadline for the call to MojoWaitMany().
77 MojoDeadline
GetDeadlineForWait() const;
79 // If non-NULL we're running (inside Run()). Member is reference to value on
83 HandleToHandler handlers_
;
85 // 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
92 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo
);
98 #endif // MOJO_COMMON_MESSAGE_PUMP_MOJO_H_