rAc - revert invalid suggestions to edit mode
[chromium-blink-merge.git] / mojo / common / message_pump_mojo.h
blobe4508e07470507ee0c9aedb23f5974a363d4ecb4
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_
8 #include <map>
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"
15 namespace mojo {
16 namespace common {
18 class MessagePumpMojoHandler;
20 // Mojo implementation of MessagePump.
21 class MOJO_COMMON_EXPORT MessagePumpMojo : public base::MessagePump {
22 public:
23 MessagePumpMojo();
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,
29 const Handle& handle,
30 MojoWaitFlags wait_flags,
31 base::TimeTicks deadline);
33 void RemoveHandler(const Handle& handle);
35 // MessagePump:
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;
42 private:
43 struct RunState;
44 struct WaitState;
46 // Contains the data needed to track a request to AddHandler().
47 struct Handler {
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.
54 int id;
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
64 // invalid handle.
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
75 // stack.
76 RunState* run_state_;
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
84 // notify it.
85 int next_handler_id_;
87 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo);
90 } // namespace common
91 } // namespace mojo
93 #endif // MOJO_COMMON_MESSAGE_PUMP_MOJO_H_