Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / mojo / common / message_pump_mojo.h
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.
4
5 #ifndef MOJO_COMMON_MESSAGE_PUMP_MOJO_H_
6 #define MOJO_COMMON_MESSAGE_PUMP_MOJO_H_
7
8 #include <map>
9
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"
18
19 namespace mojo {
20 namespace common {
21
22 class MessagePumpMojoHandler;
23
24 // Mojo implementation of MessagePump.
25 class MOJO_COMMON_EXPORT MessagePumpMojo : public base::MessagePump {
26  public:
27   class Observer {
28    public:
29     Observer() {}
30
31     virtual void WillSignalHandler() = 0;
32     virtual void DidSignalHandler() = 0;
33
34    protected:
35     virtual ~Observer() {}
36   };
37
38   MessagePumpMojo();
39   ~MessagePumpMojo() override;
40
41   // Static factory function (for using with |base::Thread::Options|, wrapped
42   // using |base::Bind()|).
43   static scoped_ptr<base::MessagePump> Create();
44
45   // Returns the MessagePumpMojo instance of the current thread, if it exists.
46   static MessagePumpMojo* current();
47
48   static bool IsCurrent() { return !!current(); }
49
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);
57
58   void RemoveHandler(const Handle& handle);
59
60   void AddObserver(Observer*);
61   void RemoveObserver(Observer*);
62
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;
68
69  private:
70   struct RunState;
71   struct WaitState;
72
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) {}
76
77     MessagePumpMojoHandler* handler;
78     MojoHandleSignals wait_signals;
79     base::TimeTicks deadline;
80     // See description of |MessagePumpMojo::next_handler_id_| for details.
81     int id;
82   };
83
84   typedef std::map<Handle, Handler> HandleToHandler;
85
86   // Implementation of Run().
87   void DoRunLoop(RunState* run_state, Delegate* delegate);
88
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);
93
94   // Removes the first invalid handle. This is called if MojoWaitMany finds an
95   // invalid handle.
96   void RemoveFirstInvalidHandle(const WaitState& wait_state);
97
98   void SignalControlPipe(const RunState& run_state);
99
100   WaitState GetWaitState(const RunState& run_state) const;
101
102   // Returns the deadline for the call to MojoWaitMany().
103   MojoDeadline GetDeadlineForWait(const RunState& run_state) const;
104
105   void WillSignalHandler();
106   void DidSignalHandler();
107
108   // If non-NULL we're running (inside Run()). Member is reference to value on
109   // stack.
110   RunState* run_state_;
111
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
114   // thread.
115   base::Lock run_state_lock_;
116
117   HandleToHandler handlers_;
118
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
123   // notify it.
124   int next_handler_id_;
125
126   ObserverList<Observer> observers_;
127
128   DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo);
129 };
130
131 }  // namespace common
132 }  // namespace mojo
133
134 #endif  // MOJO_COMMON_MESSAGE_PUMP_MOJO_H_