Allow directories for GN data lists.
[chromium-blink-merge.git] / mojo / common / message_pump_mojo.h
blob86dd8ac076cb3c389f8319d35fa18c7660b8be9c
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/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 "third_party/mojo/src/mojo/public/cpp/system/core.h"
19 namespace mojo {
20 namespace common {
22 class MessagePumpMojoHandler;
24 // Mojo implementation of MessagePump.
25 class MOJO_COMMON_EXPORT MessagePumpMojo : public base::MessagePump {
26 public:
27 class Observer {
28 public:
29 Observer() {}
31 virtual void WillSignalHandler() = 0;
32 virtual void DidSignalHandler() = 0;
34 protected:
35 virtual ~Observer() {}
38 MessagePumpMojo();
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,
54 const Handle& handle,
55 MojoHandleSignals wait_signals,
56 base::TimeTicks deadline);
58 void RemoveHandler(const Handle& handle);
60 void AddObserver(Observer*);
61 void RemoveObserver(Observer*);
63 // MessagePump:
64 void Run(Delegate* delegate) override;
65 void Quit() override;
66 void ScheduleWork() override;
67 void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time) override;
69 private:
70 struct RunState;
71 struct WaitState;
73 // Contains the data needed to track a request to AddHandler().
74 struct Handler {
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.
81 int id;
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 given invalid handle. This is called if MojoWaitMany finds an
95 // invalid handle.
96 void RemoveInvalidHandle(const WaitState& wait_state,
97 MojoResult result,
98 uint32_t result_index);
100 void SignalControlPipe(const RunState& run_state);
102 WaitState GetWaitState(const RunState& run_state) const;
104 // Returns the deadline for the call to MojoWaitMany().
105 MojoDeadline GetDeadlineForWait(const RunState& run_state) const;
107 void WillSignalHandler();
108 void DidSignalHandler();
110 // If non-NULL we're running (inside Run()). Member is reference to value on
111 // stack.
112 RunState* run_state_;
114 // Lock for accessing |run_state_|. In general the only method that we have to
115 // worry about is ScheduleWork(). All other methods are invoked on the same
116 // thread.
117 base::Lock run_state_lock_;
119 HandleToHandler handlers_;
121 // An ever increasing value assigned to each Handler::id. Used to detect
122 // uniqueness while notifying. That is, while notifying expired timers we copy
123 // |handlers_| and only notify handlers whose id match. If the id does not
124 // match it means the handler was removed then added so that we shouldn't
125 // notify it.
126 int next_handler_id_;
128 base::ObserverList<Observer> observers_;
130 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo);
133 } // namespace common
134 } // namespace mojo
136 #endif // MOJO_COMMON_MESSAGE_PUMP_MOJO_H_