blob: 8fcc48eaa5f85eadd5211e756d6fd05e7ac46788 [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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
James Robinson94ade6b2015-08-25 13:02:06 -07005#ifndef MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_
6#define MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_
James Robinson646469d2014-10-03 15:33:28 -07007
8#include <map>
Anand K. Mistryecf67172015-06-03 13:07:31 +10009#include <utility>
10#include <vector>
James Robinson646469d2014-10-03 15:33:28 -070011
12#include "base/macros.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/message_loop/message_pump.h"
James Robinsond4531882014-10-17 16:14:32 -070015#include "base/observer_list.h"
James Robinson646469d2014-10-03 15:33:28 -070016#include "base/synchronization/lock.h"
17#include "base/time/time.h"
Viet-Trung Luuadf6aa82016-03-15 11:28:19 -070018#include "mojo/public/c/system/result.h"
19#include "mojo/public/c/system/time.h"
20#include "mojo/public/cpp/system/handle.h"
James Robinson646469d2014-10-03 15:33:28 -070021
22namespace mojo {
23namespace common {
24
25class MessagePumpMojoHandler;
26
27// Mojo implementation of MessagePump.
Viet-Trung Luu77ebb732015-04-02 14:30:05 -070028class MessagePumpMojo : public base::MessagePump {
James Robinson646469d2014-10-03 15:33:28 -070029 public:
James Robinsond4531882014-10-17 16:14:32 -070030 class Observer {
31 public:
32 Observer() {}
33
34 virtual void WillSignalHandler() = 0;
35 virtual void DidSignalHandler() = 0;
36
37 protected:
38 virtual ~Observer() {}
39 };
40
James Robinson646469d2014-10-03 15:33:28 -070041 MessagePumpMojo();
James Robinsone1b30cf2014-10-21 12:25:40 -070042 ~MessagePumpMojo() override;
James Robinson646469d2014-10-03 15:33:28 -070043
44 // Static factory function (for using with |base::Thread::Options|, wrapped
45 // using |base::Bind()|).
46 static scoped_ptr<base::MessagePump> Create();
47
48 // Returns the MessagePumpMojo instance of the current thread, if it exists.
49 static MessagePumpMojo* current();
50
51 static bool IsCurrent() { return !!current(); }
52
53 // Registers a MessagePumpMojoHandler for the specified handle. Only one
54 // handler can be registered for a specified handle.
55 // NOTE: a value of 0 for |deadline| indicates an indefinite timeout.
56 void AddHandler(MessagePumpMojoHandler* handler,
57 const Handle& handle,
58 MojoHandleSignals wait_signals,
59 base::TimeTicks deadline);
60
61 void RemoveHandler(const Handle& handle);
62
Scott Violetef1cb8c2015-03-13 14:01:39 -070063 void AddObserver(Observer* observer);
64 void RemoveObserver(Observer* observer);
James Robinsond4531882014-10-17 16:14:32 -070065
James Robinson646469d2014-10-03 15:33:28 -070066 // MessagePump:
James Robinsone1b30cf2014-10-21 12:25:40 -070067 void Run(Delegate* delegate) override;
68 void Quit() override;
69 void ScheduleWork() override;
70 void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time) override;
James Robinson646469d2014-10-03 15:33:28 -070071
72 private:
73 struct RunState;
74 struct WaitState;
75
76 // Contains the data needed to track a request to AddHandler().
77 struct Handler {
78 Handler() : handler(NULL), wait_signals(MOJO_HANDLE_SIGNAL_NONE), id(0) {}
79
80 MessagePumpMojoHandler* handler;
81 MojoHandleSignals wait_signals;
82 base::TimeTicks deadline;
83 // See description of |MessagePumpMojo::next_handler_id_| for details.
84 int id;
85 };
86
87 typedef std::map<Handle, Handler> HandleToHandler;
Anand K. Mistryecf67172015-06-03 13:07:31 +100088 typedef std::vector<std::pair<Handle, Handler>> HandleToHandlerList;
James Robinson646469d2014-10-03 15:33:28 -070089
90 // Implementation of Run().
91 void DoRunLoop(RunState* run_state, Delegate* delegate);
92
93 // Services the set of handles ready. If |block| is true this waits for a
James Robinsona9763132014-10-06 11:18:13 -070094 // handle to become ready, otherwise this does not block. Returns |true| if a
95 // handle has become ready, |false| otherwise.
96 bool DoInternalWork(const RunState& run_state, bool block);
James Robinson646469d2014-10-03 15:33:28 -070097
Jim Beveridgea4dc2732014-12-10 11:05:01 -080098 // Removes the given invalid handle. This is called if MojoWaitMany finds an
James Robinson646469d2014-10-03 15:33:28 -070099 // invalid handle.
Jim Beveridgea4dc2732014-12-10 11:05:01 -0800100 void RemoveInvalidHandle(const WaitState& wait_state,
101 MojoResult result,
102 uint32_t result_index);
James Robinson646469d2014-10-03 15:33:28 -0700103
104 void SignalControlPipe(const RunState& run_state);
105
Anand K. Mistryecf67172015-06-03 13:07:31 +1000106 void GetWaitState(const RunState& run_state, WaitState* wait_state) const;
James Robinson646469d2014-10-03 15:33:28 -0700107
108 // Returns the deadline for the call to MojoWaitMany().
109 MojoDeadline GetDeadlineForWait(const RunState& run_state) const;
110
James Robinsond4531882014-10-17 16:14:32 -0700111 void WillSignalHandler();
112 void DidSignalHandler();
113
James Robinson646469d2014-10-03 15:33:28 -0700114 // If non-NULL we're running (inside Run()). Member is reference to value on
115 // stack.
116 RunState* run_state_;
117
118 // Lock for accessing |run_state_|. In general the only method that we have to
119 // worry about is ScheduleWork(). All other methods are invoked on the same
120 // thread.
121 base::Lock run_state_lock_;
122
123 HandleToHandler handlers_;
124
125 // An ever increasing value assigned to each Handler::id. Used to detect
126 // uniqueness while notifying. That is, while notifying expired timers we copy
127 // |handlers_| and only notify handlers whose id match. If the id does not
128 // match it means the handler was removed then added so that we shouldn't
129 // notify it.
130 int next_handler_id_;
131
Viet-Trung Luu235cf3d2015-06-11 10:01:25 -0700132 base::ObserverList<Observer> observers_;
James Robinsond4531882014-10-17 16:14:32 -0700133
James Robinson646469d2014-10-03 15:33:28 -0700134 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo);
135};
136
137} // namespace common
138} // namespace mojo
139
James Robinson94ade6b2015-08-25 13:02:06 -0700140#endif // MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_