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/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" 14 15 namespace mojo { 16 namespace common { 17 18 class MessagePumpMojoHandler; 19 20 // Mojo implementation of MessagePump. 21 class MOJO_COMMON_EXPORT MessagePumpMojo : public base::MessagePump { 22 public: 23 MessagePumpMojo(); 24 virtual ~MessagePumpMojo(); 25 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); 32 33 void RemoveHandler(const Handle& handle); 34 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; 41 42 private: 43 struct RunState; 44 struct WaitState; 45 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) {} 49 50 MessagePumpMojoHandler* handler; 51 MojoWaitFlags wait_flags; 52 base::TimeTicks deadline; 53 // See description of |MessagePumpMojo::next_handler_id_| for details. 54 int id; 55 }; 56 57 typedef std::map<Handle, Handler> HandleToHandler; 58 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); 62 63 // Removes the first invalid handle. This is called if MojoWaitMany finds an 64 // invalid handle. 65 void RemoveFirstInvalidHandle(const WaitState& wait_state); 66 67 void SignalControlPipe(); 68 69 WaitState GetWaitState() const; 70 71 // Returns the deadline for the call to MojoWaitMany(). 72 MojoDeadline GetDeadlineForWait() const; 73 74 // If non-NULL we're running (inside Run()). Member is reference to value on 75 // stack. 76 RunState* run_state_; 77 78 HandleToHandler handlers_; 79 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_; 86 87 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo); 88 }; 89 90 } // namespace common 91 } // namespace mojo 92 93 #endif // MOJO_COMMON_MESSAGE_PUMP_MOJO_H_ 94