[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 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 | |||||
[email protected] | d7dc6add | 2014-03-20 05:59:58 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 11 | #include "base/message_loop/message_pump.h" |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 12 | #include "base/time/time.h" |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 13 | #include "mojo/common/mojo_common_export.h" |
[email protected] | 5dddd19 | 2014-03-28 00:53:27 | [diff] [blame^] | 14 | #include "mojo/public/cpp/system/core.h" |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 15 | |
16 | namespace mojo { | ||||
17 | namespace common { | ||||
18 | |||||
19 | class MessagePumpMojoHandler; | ||||
20 | |||||
21 | // Mojo implementation of MessagePump. | ||||
22 | class MOJO_COMMON_EXPORT MessagePumpMojo : public base::MessagePump { | ||||
23 | public: | ||||
24 | MessagePumpMojo(); | ||||
25 | virtual ~MessagePumpMojo(); | ||||
26 | |||||
[email protected] | d7dc6add | 2014-03-20 05:59:58 | [diff] [blame] | 27 | // Static factory function (for using with |base::Thread::Options|, wrapped |
28 | // using |base::Bind()|). | ||||
29 | static scoped_ptr<base::MessagePump> Create(); | ||||
30 | |||||
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 31 | // Registers a MessagePumpMojoHandler for the specified handle. Only one |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 32 | // handler can be registered for a specified handle. |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 33 | void AddHandler(MessagePumpMojoHandler* handler, |
[email protected] | bd6cb4bc | 2013-11-25 23:57:16 | [diff] [blame] | 34 | const Handle& handle, |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 35 | MojoWaitFlags wait_flags, |
36 | base::TimeTicks deadline); | ||||
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 37 | |
[email protected] | bd6cb4bc | 2013-11-25 23:57:16 | [diff] [blame] | 38 | void RemoveHandler(const Handle& handle); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 39 | |
40 | // MessagePump: | ||||
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; | ||||
46 | |||||
47 | private: | ||||
48 | struct RunState; | ||||
49 | struct WaitState; | ||||
50 | |||||
[email protected] | 273e217 | 2013-12-10 07:24:16 | [diff] [blame] | 51 | // Contains the data needed to track a request to AddHandler(). |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 52 | struct Handler { |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 53 | Handler() : handler(NULL), wait_flags(MOJO_WAIT_FLAG_NONE), id(0) {} |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 54 | |
55 | MessagePumpMojoHandler* handler; | ||||
56 | MojoWaitFlags wait_flags; | ||||
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 57 | base::TimeTicks deadline; |
58 | // See description of |MessagePumpMojo::next_handler_id_| for details. | ||||
59 | int id; | ||||
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 60 | }; |
61 | |||||
[email protected] | bd6cb4bc | 2013-11-25 23:57:16 | [diff] [blame] | 62 | typedef std::map<Handle, Handler> HandleToHandler; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 63 | |
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); | ||||
67 | |||||
68 | // Removes the first invalid handle. This is called if MojoWaitMany finds an | ||||
69 | // invalid handle. | ||||
70 | void RemoveFirstInvalidHandle(const WaitState& wait_state); | ||||
71 | |||||
72 | void SignalControlPipe(); | ||||
73 | |||||
74 | WaitState GetWaitState() const; | ||||
75 | |||||
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 76 | // Returns the deadline for the call to MojoWaitMany(). |
77 | MojoDeadline GetDeadlineForWait() const; | ||||
78 | |||||
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 79 | // If non-NULL we're running (inside Run()). Member is reference to value on |
80 | // stack. | ||||
81 | RunState* run_state_; | ||||
82 | |||||
83 | HandleToHandler handlers_; | ||||
84 | |||||
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 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 | ||||
89 | // notify it. | ||||
90 | int next_handler_id_; | ||||
91 | |||||
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 92 | DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo); |
93 | }; | ||||
94 | |||||
95 | } // namespace common | ||||
96 | } // namespace mojo | ||||
97 | |||||
98 | #endif // MOJO_COMMON_MESSAGE_PUMP_MOJO_H_ |