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/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_pump.h"
13 #include "base/observer_list.h"
14 #include "base/synchronization/lock.h"
15 #include "base/time/time.h"
16 #include "mojo/common/mojo_common_export.h"
17 #include "mojo/public/cpp/system/core.h"
22 class MessagePumpMojoHandler
;
24 // Mojo implementation of MessagePump.
25 class MOJO_COMMON_EXPORT MessagePumpMojo
: public base::MessagePump
{
31 virtual void WillSignalHandler() = 0;
32 virtual void DidSignalHandler() = 0;
35 virtual ~Observer() {}
39 ~MessagePumpMojo() override
;
41 // Static factory function (for using with |base::Thread::Options|, wrapped
42 // using |base::Bind()|).
43 static scoped_ptr
<base::MessagePump
> Create();
45 // Returns the MessagePumpMojo instance of the current thread, if it exists.
46 static MessagePumpMojo
* current();
48 static bool IsCurrent() { return !!current(); }
50 // Registers a MessagePumpMojoHandler for the specified handle. Only one
51 // handler can be registered for a specified handle.
52 // NOTE: a value of 0 for |deadline| indicates an indefinite timeout.
53 void AddHandler(MessagePumpMojoHandler
* handler
,
55 MojoHandleSignals wait_signals
,
56 base::TimeTicks deadline
);
58 void RemoveHandler(const Handle
& handle
);
60 void AddObserver(Observer
*);
61 void RemoveObserver(Observer
*);
64 void Run(Delegate
* delegate
) override
;
66 void ScheduleWork() override
;
67 void ScheduleDelayedWork(const base::TimeTicks
& delayed_work_time
) override
;
73 // Contains the data needed to track a request to AddHandler().
75 Handler() : handler(NULL
), wait_signals(MOJO_HANDLE_SIGNAL_NONE
), id(0) {}
77 MessagePumpMojoHandler
* handler
;
78 MojoHandleSignals wait_signals
;
79 base::TimeTicks deadline
;
80 // See description of |MessagePumpMojo::next_handler_id_| for details.
84 typedef std::map
<Handle
, Handler
> HandleToHandler
;
86 // Implementation of Run().
87 void DoRunLoop(RunState
* run_state
, Delegate
* delegate
);
89 // Services the set of handles ready. If |block| is true this waits for a
90 // handle to become ready, otherwise this does not block. Returns |true| if a
91 // handle has become ready, |false| otherwise.
92 bool DoInternalWork(const RunState
& run_state
, bool block
);
94 // Removes the first invalid handle. This is called if MojoWaitMany finds an
96 void RemoveFirstInvalidHandle(const WaitState
& wait_state
);
98 void SignalControlPipe(const RunState
& run_state
);
100 WaitState
GetWaitState(const RunState
& run_state
) const;
102 // Returns the deadline for the call to MojoWaitMany().
103 MojoDeadline
GetDeadlineForWait(const RunState
& run_state
) const;
105 void WillSignalHandler();
106 void DidSignalHandler();
108 // If non-NULL we're running (inside Run()). Member is reference to value on
110 RunState
* run_state_
;
112 // Lock for accessing |run_state_|. In general the only method that we have to
113 // worry about is ScheduleWork(). All other methods are invoked on the same
115 base::Lock run_state_lock_
;
117 HandleToHandler handlers_
;
119 // An ever increasing value assigned to each Handler::id. Used to detect
120 // uniqueness while notifying. That is, while notifying expired timers we copy
121 // |handlers_| and only notify handlers whose id match. If the id does not
122 // match it means the handler was removed then added so that we shouldn't
124 int next_handler_id_
;
126 ObserverList
<Observer
> observers_
;
128 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo
);
131 } // namespace common
134 #endif // MOJO_COMMON_MESSAGE_PUMP_MOJO_H_