brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [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 BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
| 6 | #define BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
| 7 | |
| 8 | #include <queue> |
| 9 | #include <string> |
| 10 | |
| 11 | #include "base/base_export.h" |
| 12 | #include "base/basictypes.h" |
| 13 | #include "base/callback_forward.h" |
skyostil@chromium.org | 2ca1bf3 | 2014-08-14 23:26:09 +0900 | [diff] [blame] | 14 | #include "base/debug/task_annotator.h" |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 15 | #include "base/location.h" |
| 16 | #include "base/memory/ref_counted.h" |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 17 | #include "base/memory/scoped_ptr.h" |
| 18 | #include "base/message_loop/incoming_task_queue.h" |
skyostil | 9368ac0 | 2015-06-20 02:22:54 +0900 | [diff] [blame] | 19 | #include "base/message_loop/message_loop_task_runner.h" |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 20 | #include "base/message_loop/message_pump.h" |
jeremy@chromium.org | 4d5a6b2 | 2014-06-06 04:36:20 +0900 | [diff] [blame] | 21 | #include "base/message_loop/timer_slack.h" |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 22 | #include "base/observer_list.h" |
| 23 | #include "base/pending_task.h" |
| 24 | #include "base/sequenced_task_runner_helpers.h" |
| 25 | #include "base/synchronization/lock.h" |
avi@chromium.org | b039e8b | 2013-06-28 09:49:07 +0900 | [diff] [blame] | 26 | #include "base/time/time.h" |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 27 | #include "base/tracking_info.h" |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 28 | |
sky@chromium.org | 66a00b3 | 2014-01-17 09:10:29 +0900 | [diff] [blame] | 29 | // TODO(sky): these includes should not be necessary. Nuke them. |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 30 | #if defined(OS_WIN) |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 31 | #include "base/message_loop/message_pump_win.h" |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 32 | #elif defined(OS_IOS) |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 33 | #include "base/message_loop/message_pump_io_ios.h" |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 34 | #elif defined(OS_POSIX) |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 35 | #include "base/message_loop/message_pump_libevent.h" |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 36 | #endif |
| 37 | |
| 38 | namespace base { |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 39 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 40 | class HistogramBase; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 41 | class RunLoop; |
| 42 | class ThreadTaskRunnerHandle; |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 43 | class WaitableEvent; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 44 | |
| 45 | // A MessageLoop is used to process events for a particular thread. There is |
| 46 | // at most one MessageLoop instance per thread. |
| 47 | // |
| 48 | // Events include at a minimum Task instances submitted to PostTask and its |
| 49 | // variants. Depending on the type of message pump used by the MessageLoop |
| 50 | // other events such as UI messages may be processed. On Windows APC calls (as |
| 51 | // time permits) and signals sent to a registered set of HANDLEs may also be |
| 52 | // processed. |
| 53 | // |
| 54 | // NOTE: Unless otherwise specified, a MessageLoop's methods may only be called |
| 55 | // on the thread where the MessageLoop's Run method executes. |
| 56 | // |
| 57 | // NOTE: MessageLoop has task reentrancy protection. This means that if a |
| 58 | // task is being processed, a second task cannot start until the first task is |
| 59 | // finished. Reentrancy can happen when processing a task, and an inner |
| 60 | // message pump is created. That inner pump then processes native messages |
| 61 | // which could implicitly start an inner task. Inner message pumps are created |
| 62 | // with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions |
| 63 | // (DoDragDrop), printer functions (StartDoc) and *many* others. |
| 64 | // |
| 65 | // Sample workaround when inner task processing is needed: |
| 66 | // HRESULT hr; |
| 67 | // { |
| 68 | // MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| 69 | // hr = DoDragDrop(...); // Implicitly runs a modal message loop. |
| 70 | // } |
| 71 | // // Process |hr| (the result returned by DoDragDrop()). |
| 72 | // |
| 73 | // Please be SURE your task is reentrant (nestable) and all global variables |
| 74 | // are stable and accessible before calling SetNestableTasksAllowed(true). |
| 75 | // |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 76 | class BASE_EXPORT MessageLoop : public MessagePump::Delegate { |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 77 | public: |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 78 | // A MessageLoop has a particular type, which indicates the set of |
| 79 | // asynchronous events it may process in addition to tasks and timers. |
| 80 | // |
| 81 | // TYPE_DEFAULT |
| 82 | // This type of ML only supports tasks and timers. |
| 83 | // |
| 84 | // TYPE_UI |
| 85 | // This type of ML also supports native UI events (e.g., Windows messages). |
| 86 | // See also MessageLoopForUI. |
| 87 | // |
| 88 | // TYPE_IO |
| 89 | // This type of ML also supports asynchronous IO. See also |
| 90 | // MessageLoopForIO. |
| 91 | // |
kristianm@chromium.org | a78bfa9 | 2013-08-08 10:31:52 +0900 | [diff] [blame] | 92 | // TYPE_JAVA |
| 93 | // This type of ML is backed by a Java message handler which is responsible |
| 94 | // for running the tasks added to the ML. This is only for use on Android. |
| 95 | // TYPE_JAVA behaves in essence like TYPE_UI, except during construction |
| 96 | // where it does not use the main thread specific pump factory. |
| 97 | // |
sky@chromium.org | ab45280 | 2013-11-08 15:16:53 +0900 | [diff] [blame] | 98 | // TYPE_CUSTOM |
| 99 | // MessagePump was supplied to constructor. |
| 100 | // |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 101 | enum Type { |
| 102 | TYPE_DEFAULT, |
| 103 | TYPE_UI, |
sky@chromium.org | ab45280 | 2013-11-08 15:16:53 +0900 | [diff] [blame] | 104 | TYPE_CUSTOM, |
kristianm@chromium.org | a78bfa9 | 2013-08-08 10:31:52 +0900 | [diff] [blame] | 105 | TYPE_IO, |
| 106 | #if defined(OS_ANDROID) |
| 107 | TYPE_JAVA, |
thestig | fa24e5b | 2015-01-22 16:09:45 +0900 | [diff] [blame] | 108 | #endif // defined(OS_ANDROID) |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | // Normally, it is not necessary to instantiate a MessageLoop. Instead, it |
| 112 | // is typical to make use of the current thread's MessageLoop instance. |
| 113 | explicit MessageLoop(Type type = TYPE_DEFAULT); |
sky@chromium.org | ab45280 | 2013-11-08 15:16:53 +0900 | [diff] [blame] | 114 | // Creates a TYPE_CUSTOM MessageLoop with the supplied MessagePump, which must |
| 115 | // be non-NULL. |
kinuko | f25405c | 2015-05-23 20:38:37 +0900 | [diff] [blame] | 116 | explicit MessageLoop(scoped_ptr<MessagePump> pump); |
| 117 | |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 118 | ~MessageLoop() override; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 119 | |
| 120 | // Returns the MessageLoop object for the current thread, or null if none. |
| 121 | static MessageLoop* current(); |
| 122 | |
| 123 | static void EnableHistogrammer(bool enable_histogrammer); |
| 124 | |
suyash.s@samsung.com | b5f1fc9 | 2014-03-08 02:03:54 +0900 | [diff] [blame] | 125 | typedef scoped_ptr<MessagePump> (MessagePumpFactory)(); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 126 | // Uses the given base::MessagePumpForUIFactory to override the default |
| 127 | // MessagePump implementation for 'TYPE_UI'. Returns true if the factory |
| 128 | // was successfully registered. |
| 129 | static bool InitMessagePumpForUIFactory(MessagePumpFactory* factory); |
| 130 | |
sky@chromium.org | 4f42682 | 2013-11-13 01:35:02 +0900 | [diff] [blame] | 131 | // Creates the default MessagePump based on |type|. Caller owns return |
| 132 | // value. |
suyash.s@samsung.com | b5f1fc9 | 2014-03-08 02:03:54 +0900 | [diff] [blame] | 133 | static scoped_ptr<MessagePump> CreateMessagePumpForType(Type type); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 134 | // A DestructionObserver is notified when the current MessageLoop is being |
| 135 | // destroyed. These observers are notified prior to MessageLoop::current() |
| 136 | // being changed to return NULL. This gives interested parties the chance to |
| 137 | // do final cleanup that depends on the MessageLoop. |
| 138 | // |
| 139 | // NOTE: Any tasks posted to the MessageLoop during this notification will |
| 140 | // not be run. Instead, they will be deleted. |
| 141 | // |
| 142 | class BASE_EXPORT DestructionObserver { |
| 143 | public: |
| 144 | virtual void WillDestroyCurrentMessageLoop() = 0; |
| 145 | |
| 146 | protected: |
| 147 | virtual ~DestructionObserver(); |
| 148 | }; |
| 149 | |
| 150 | // Add a DestructionObserver, which will start receiving notifications |
| 151 | // immediately. |
| 152 | void AddDestructionObserver(DestructionObserver* destruction_observer); |
| 153 | |
| 154 | // Remove a DestructionObserver. It is safe to call this method while a |
| 155 | // DestructionObserver is receiving a notification callback. |
| 156 | void RemoveDestructionObserver(DestructionObserver* destruction_observer); |
| 157 | |
skyostil | dd4cb91 | 2015-03-21 09:56:51 +0900 | [diff] [blame] | 158 | // NOTE: Deprecated; prefer task_runner() and the TaskRunner interfaces. |
| 159 | // TODO(skyostil): Remove these functions (crbug.com/465354). |
| 160 | // |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 161 | // The "PostTask" family of methods call the task's Run method asynchronously |
| 162 | // from within a message loop at some point in the future. |
| 163 | // |
| 164 | // With the PostTask variant, tasks are invoked in FIFO order, inter-mixed |
| 165 | // with normal UI or IO event processing. With the PostDelayedTask variant, |
| 166 | // tasks are called after at least approximately 'delay_ms' have elapsed. |
| 167 | // |
| 168 | // The NonNestable variants work similarly except that they promise never to |
| 169 | // dispatch the task from a nested invocation of MessageLoop::Run. Instead, |
| 170 | // such tasks get deferred until the top-most MessageLoop::Run is executing. |
| 171 | // |
| 172 | // The MessageLoop takes ownership of the Task, and deletes it after it has |
| 173 | // been Run(). |
| 174 | // |
| 175 | // PostTask(from_here, task) is equivalent to |
| 176 | // PostDelayedTask(from_here, task, 0). |
| 177 | // |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 178 | // NOTE: These methods may be called on any thread. The Task will be invoked |
| 179 | // on the thread that executes MessageLoop::Run(). |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 180 | void PostTask(const tracked_objects::Location& from_here, |
| 181 | const Closure& task); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 182 | |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 183 | void PostDelayedTask(const tracked_objects::Location& from_here, |
| 184 | const Closure& task, |
| 185 | TimeDelta delay); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 186 | |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 187 | void PostNonNestableTask(const tracked_objects::Location& from_here, |
| 188 | const Closure& task); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 189 | |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 190 | void PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
| 191 | const Closure& task, |
| 192 | TimeDelta delay); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 193 | |
| 194 | // A variant on PostTask that deletes the given object. This is useful |
| 195 | // if the object needs to live until the next run of the MessageLoop (for |
| 196 | // example, deleting a RenderProcessHost from within an IPC callback is not |
| 197 | // good). |
| 198 | // |
| 199 | // NOTE: This method may be called on any thread. The object will be deleted |
maniscalco | cba1be9 | 2014-10-03 03:35:33 +0900 | [diff] [blame] | 200 | // on the thread that executes MessageLoop::Run(). |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 201 | template <class T> |
| 202 | void DeleteSoon(const tracked_objects::Location& from_here, const T* object) { |
| 203 | base::subtle::DeleteHelperInternal<T, void>::DeleteViaSequencedTaskRunner( |
| 204 | this, from_here, object); |
| 205 | } |
| 206 | |
| 207 | // A variant on PostTask that releases the given reference counted object |
| 208 | // (by calling its Release method). This is useful if the object needs to |
| 209 | // live until the next run of the MessageLoop, or if the object needs to be |
| 210 | // released on a particular thread. |
| 211 | // |
maniscalco@chromium.org | 26ea8ac | 2014-05-28 07:37:50 +0900 | [diff] [blame] | 212 | // A common pattern is to manually increment the object's reference count |
vkuzkokov@chromium.org | 49e37bd | 2014-05-31 03:07:17 +0900 | [diff] [blame] | 213 | // (AddRef), clear the pointer, then issue a ReleaseSoon. The reference count |
maniscalco@chromium.org | 26ea8ac | 2014-05-28 07:37:50 +0900 | [diff] [blame] | 214 | // is incremented manually to ensure clearing the pointer does not trigger a |
| 215 | // delete and to account for the upcoming decrement (ReleaseSoon). For |
| 216 | // example: |
| 217 | // |
| 218 | // scoped_refptr<Foo> foo = ... |
vkuzkokov@chromium.org | 49e37bd | 2014-05-31 03:07:17 +0900 | [diff] [blame] | 219 | // foo->AddRef(); |
| 220 | // Foo* raw_foo = foo.get(); |
maniscalco@chromium.org | 26ea8ac | 2014-05-28 07:37:50 +0900 | [diff] [blame] | 221 | // foo = NULL; |
vkuzkokov@chromium.org | 49e37bd | 2014-05-31 03:07:17 +0900 | [diff] [blame] | 222 | // message_loop->ReleaseSoon(raw_foo); |
maniscalco@chromium.org | 26ea8ac | 2014-05-28 07:37:50 +0900 | [diff] [blame] | 223 | // |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 224 | // NOTE: This method may be called on any thread. The object will be |
| 225 | // released (and thus possibly deleted) on the thread that executes |
| 226 | // MessageLoop::Run(). If this is not the same as the thread that calls |
maniscalco | cba1be9 | 2014-10-03 03:35:33 +0900 | [diff] [blame] | 227 | // ReleaseSoon(FROM_HERE, ), then T MUST inherit from |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 228 | // RefCountedThreadSafe<T>! |
| 229 | template <class T> |
| 230 | void ReleaseSoon(const tracked_objects::Location& from_here, |
| 231 | const T* object) { |
| 232 | base::subtle::ReleaseHelperInternal<T, void>::ReleaseViaSequencedTaskRunner( |
| 233 | this, from_here, object); |
| 234 | } |
| 235 | |
| 236 | // Deprecated: use RunLoop instead. |
| 237 | // Run the message loop. |
| 238 | void Run(); |
| 239 | |
| 240 | // Deprecated: use RunLoop instead. |
| 241 | // Process all pending tasks, windows messages, etc., but don't wait/sleep. |
| 242 | // Return as soon as all items that can be run are taken care of. |
| 243 | void RunUntilIdle(); |
| 244 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 245 | // Deprecated: use RunLoop instead. |
| 246 | // |
| 247 | // Signals the Run method to return when it becomes idle. It will continue to |
| 248 | // process pending messages and future messages as long as they are enqueued. |
| 249 | // Warning: if the MessageLoop remains busy, it may never quit. Only use this |
| 250 | // Quit method when looping procedures (such as web pages) have been shut |
| 251 | // down. |
| 252 | // |
| 253 | // This method may only be called on the same thread that called Run, and Run |
| 254 | // must still be on the call stack. |
| 255 | // |
| 256 | // Use QuitClosure variants if you need to Quit another thread's MessageLoop, |
| 257 | // but note that doing so is fairly dangerous if the target thread makes |
| 258 | // nested calls to MessageLoop::Run. The problem being that you won't know |
| 259 | // which nested run loop you are quitting, so be careful! |
| 260 | void QuitWhenIdle(); |
| 261 | |
| 262 | // Deprecated: use RunLoop instead. |
| 263 | // |
| 264 | // This method is a variant of Quit, that does not wait for pending messages |
| 265 | // to be processed before returning from Run. |
| 266 | void QuitNow(); |
| 267 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 268 | // Deprecated: use RunLoop instead. |
| 269 | // Construct a Closure that will call QuitWhenIdle(). Useful to schedule an |
| 270 | // arbitrary MessageLoop to QuitWhenIdle. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 271 | static Closure QuitWhenIdleClosure(); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 272 | |
jeremy@chromium.org | 4d5a6b2 | 2014-06-06 04:36:20 +0900 | [diff] [blame] | 273 | // Set the timer slack for this message loop. |
| 274 | void SetTimerSlack(TimerSlack timer_slack) { |
| 275 | pump_->SetTimerSlack(timer_slack); |
| 276 | } |
| 277 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 278 | // Returns true if this loop is |type|. This allows subclasses (especially |
| 279 | // those in tests) to specialize how they are identified. |
| 280 | virtual bool IsType(Type type) const; |
| 281 | |
| 282 | // Returns the type passed to the constructor. |
| 283 | Type type() const { return type_; } |
| 284 | |
| 285 | // Optional call to connect the thread name with this loop. |
| 286 | void set_thread_name(const std::string& thread_name) { |
| 287 | DCHECK(thread_name_.empty()) << "Should not rename this thread!"; |
| 288 | thread_name_ = thread_name; |
| 289 | } |
| 290 | const std::string& thread_name() const { return thread_name_; } |
| 291 | |
rsleevi@chromium.org | c9ff378 | 2014-07-12 10:10:52 +0900 | [diff] [blame] | 292 | // Gets the TaskRunner associated with this message loop. |
skyostil | d1abbc1 | 2015-07-14 06:41:17 +0900 | [diff] [blame] | 293 | const scoped_refptr<SingleThreadTaskRunner>& task_runner() { |
| 294 | return task_runner_; |
| 295 | } |
| 296 | |
| 297 | // Sets a new TaskRunner for this message loop. The message loop must already |
| 298 | // have been bound to a thread prior to this call, and the task runner must |
| 299 | // belong to that thread. Note that changing the task runner will also affect |
| 300 | // the ThreadTaskRunnerHandle for the target thread. Must be called on the |
| 301 | // thread to which the message loop is bound. |
| 302 | void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner); |
rsleevi@chromium.org | c9ff378 | 2014-07-12 10:10:52 +0900 | [diff] [blame] | 303 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 304 | // Enables or disables the recursive task processing. This happens in the case |
qyearsley | b8cfa6d | 2015-07-25 09:33:31 +0900 | [diff] [blame] | 305 | // of recursive message loops. Some unwanted message loops may occur when |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 306 | // using common controls or printer functions. By default, recursive task |
| 307 | // processing is disabled. |
| 308 | // |
qyearsley | b8cfa6d | 2015-07-25 09:33:31 +0900 | [diff] [blame] | 309 | // Please use |ScopedNestableTaskAllower| instead of calling these methods |
| 310 | // directly. In general, nestable message loops are to be avoided. They are |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 311 | // dangerous and difficult to get right, so please use with extreme caution. |
| 312 | // |
| 313 | // The specific case where tasks get queued is: |
| 314 | // - The thread is running a message loop. |
qyearsley | b8cfa6d | 2015-07-25 09:33:31 +0900 | [diff] [blame] | 315 | // - It receives a task #1 and executes it. |
| 316 | // - The task #1 implicitly starts a message loop, like a MessageBox in the |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 317 | // unit test. This can also be StartDoc or GetSaveFileName. |
| 318 | // - The thread receives a task #2 before or while in this second message |
| 319 | // loop. |
| 320 | // - With NestableTasksAllowed set to true, the task #2 will run right away. |
| 321 | // Otherwise, it will get executed right after task #1 completes at "thread |
| 322 | // message loop level". |
| 323 | void SetNestableTasksAllowed(bool allowed); |
| 324 | bool NestableTasksAllowed() const; |
| 325 | |
| 326 | // Enables nestable tasks on |loop| while in scope. |
| 327 | class ScopedNestableTaskAllower { |
| 328 | public: |
| 329 | explicit ScopedNestableTaskAllower(MessageLoop* loop) |
| 330 | : loop_(loop), |
| 331 | old_state_(loop_->NestableTasksAllowed()) { |
| 332 | loop_->SetNestableTasksAllowed(true); |
| 333 | } |
| 334 | ~ScopedNestableTaskAllower() { |
| 335 | loop_->SetNestableTasksAllowed(old_state_); |
| 336 | } |
| 337 | |
| 338 | private: |
| 339 | MessageLoop* loop_; |
| 340 | bool old_state_; |
| 341 | }; |
| 342 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 343 | // Returns true if we are currently running a nested message loop. |
| 344 | bool IsNested(); |
| 345 | |
| 346 | // A TaskObserver is an object that receives task notifications from the |
| 347 | // MessageLoop. |
| 348 | // |
| 349 | // NOTE: A TaskObserver implementation should be extremely fast! |
| 350 | class BASE_EXPORT TaskObserver { |
| 351 | public: |
| 352 | TaskObserver(); |
| 353 | |
| 354 | // This method is called before processing a task. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 355 | virtual void WillProcessTask(const PendingTask& pending_task) = 0; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 356 | |
| 357 | // This method is called after processing a task. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 358 | virtual void DidProcessTask(const PendingTask& pending_task) = 0; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 359 | |
| 360 | protected: |
| 361 | virtual ~TaskObserver(); |
| 362 | }; |
| 363 | |
| 364 | // These functions can only be called on the same thread that |this| is |
| 365 | // running on. |
| 366 | void AddTaskObserver(TaskObserver* task_observer); |
| 367 | void RemoveTaskObserver(TaskObserver* task_observer); |
| 368 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 369 | #if defined(OS_WIN) |
| 370 | void set_os_modal_loop(bool os_modal_loop) { |
| 371 | os_modal_loop_ = os_modal_loop; |
| 372 | } |
| 373 | |
| 374 | bool os_modal_loop() const { |
| 375 | return os_modal_loop_; |
| 376 | } |
| 377 | #endif // OS_WIN |
| 378 | |
| 379 | // Can only be called from the thread that owns the MessageLoop. |
| 380 | bool is_running() const; |
| 381 | |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 382 | // Returns true if the message loop has high resolution timers enabled. |
| 383 | // Provided for testing. |
cpu | 410a98e | 2014-08-29 08:25:37 +0900 | [diff] [blame] | 384 | bool HasHighResolutionTasks(); |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 385 | |
| 386 | // Returns true if the message loop is "idle". Provided for testing. |
| 387 | bool IsIdleForTesting(); |
| 388 | |
jamesr | 8d73106 | 2014-09-30 06:12:44 +0900 | [diff] [blame] | 389 | // Returns the TaskAnnotator which is used to add debug information to posted |
| 390 | // tasks. |
| 391 | debug::TaskAnnotator* task_annotator() { return &task_annotator_; } |
| 392 | |
| 393 | // Runs the specified PendingTask. |
| 394 | void RunTask(const PendingTask& pending_task); |
| 395 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 396 | //---------------------------------------------------------------------------- |
| 397 | protected: |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 398 | scoped_ptr<MessagePump> pump_; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 399 | |
| 400 | private: |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 401 | friend class RunLoop; |
kinuko | f25405c | 2015-05-23 20:38:37 +0900 | [diff] [blame] | 402 | friend class internal::IncomingTaskQueue; |
| 403 | friend class ScheduleWorkTest; |
| 404 | friend class Thread; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 405 | |
kinuko | f25405c | 2015-05-23 20:38:37 +0900 | [diff] [blame] | 406 | using MessagePumpFactoryCallback = Callback<scoped_ptr<MessagePump>()>; |
| 407 | |
| 408 | // Creates a MessageLoop without binding to a thread. |
| 409 | // If |type| is TYPE_CUSTOM non-null |pump_factory| must be also given |
| 410 | // to create a message pump for this message loop. Otherwise a default |
| 411 | // message pump for the |type| is created. |
| 412 | // |
| 413 | // It is valid to call this to create a new message loop on one thread, |
| 414 | // and then pass it to the thread where the message loop actually runs. |
| 415 | // The message loop's BindToCurrentThread() method must be called on the |
| 416 | // thread the message loop runs on, before calling Run(). |
qyearsley | b8cfa6d | 2015-07-25 09:33:31 +0900 | [diff] [blame] | 417 | // Before BindToCurrentThread() is called, only Post*Task() functions can |
kinuko | f25405c | 2015-05-23 20:38:37 +0900 | [diff] [blame] | 418 | // be called on the message loop. |
kinuko | 9adf8ab | 2015-06-30 10:31:54 +0900 | [diff] [blame] | 419 | static scoped_ptr<MessageLoop> CreateUnbound( |
kinuko | f25405c | 2015-05-23 20:38:37 +0900 | [diff] [blame] | 420 | Type type, |
| 421 | MessagePumpFactoryCallback pump_factory); |
| 422 | |
| 423 | // Common private constructor. Other constructors delegate the initialization |
| 424 | // to this constructor. |
| 425 | MessageLoop(Type type, MessagePumpFactoryCallback pump_factory); |
| 426 | |
| 427 | // Configure various members and bind this message loop to the current thread. |
| 428 | void BindToCurrentThread(); |
sky@chromium.org | ab45280 | 2013-11-08 15:16:53 +0900 | [diff] [blame] | 429 | |
skyostil | d1abbc1 | 2015-07-14 06:41:17 +0900 | [diff] [blame] | 430 | // Sets the ThreadTaskRunnerHandle for the current thread to point to the |
| 431 | // task runner for this message loop. |
| 432 | void SetThreadTaskRunnerHandle(); |
| 433 | |
cpu@chromium.org | 33353ba | 2014-01-04 06:25:26 +0900 | [diff] [blame] | 434 | // Invokes the actual run loop using the message pump. |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 435 | void RunHandler(); |
| 436 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 437 | // Called to process any delayed non-nestable tasks. |
| 438 | bool ProcessNextDelayedNonNestableTask(); |
| 439 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 440 | // Calls RunTask or queues the pending_task on the deferred task list if it |
| 441 | // cannot be run right now. Returns true if the task was run. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 442 | bool DeferOrRunPendingTask(const PendingTask& pending_task); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 443 | |
| 444 | // Adds the pending task to delayed_work_queue_. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 445 | void AddToDelayedWorkQueue(const PendingTask& pending_task); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 446 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 447 | // Delete tasks that haven't run yet without running them. Used in the |
| 448 | // destructor to make sure all the task's destructors get called. Returns |
| 449 | // true if some work was done. |
| 450 | bool DeletePendingTasks(); |
| 451 | |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 452 | // Loads tasks from the incoming queue to |work_queue_| if the latter is |
| 453 | // empty. |
| 454 | void ReloadWorkQueue(); |
| 455 | |
kinuko | f25405c | 2015-05-23 20:38:37 +0900 | [diff] [blame] | 456 | // Wakes up the message pump. Can be called on any thread. The caller is |
| 457 | // responsible for synchronizing ScheduleWork() calls. |
| 458 | void ScheduleWork(); |
| 459 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 460 | // Start recording histogram info about events and action IF it was enabled |
| 461 | // and IF the statistics recorder can accept a registration of our histogram. |
| 462 | void StartHistogrammer(); |
| 463 | |
| 464 | // Add occurrence of event to our histogram, so that we can see what is being |
| 465 | // done in a specific MessageLoop instance (i.e., specific thread). |
| 466 | // If message_histogram_ is NULL, this is a no-op. |
| 467 | void HistogramEvent(int event); |
| 468 | |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 469 | // MessagePump::Delegate methods: |
dcheng | 7dc8df5 | 2014-10-21 19:54:51 +0900 | [diff] [blame] | 470 | bool DoWork() override; |
| 471 | bool DoDelayedWork(TimeTicks* next_delayed_work_time) override; |
| 472 | bool DoIdleWork() override; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 473 | |
sky@chromium.org | ab45280 | 2013-11-08 15:16:53 +0900 | [diff] [blame] | 474 | const Type type_; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 475 | |
| 476 | // A list of tasks that need to be processed by this instance. Note that |
| 477 | // this queue is only accessed (push/pop) by our current thread. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 478 | TaskQueue work_queue_; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 479 | |
thestig | fa24e5b | 2015-01-22 16:09:45 +0900 | [diff] [blame] | 480 | #if defined(OS_WIN) |
cpu | 410a98e | 2014-08-29 08:25:37 +0900 | [diff] [blame] | 481 | // How many high resolution tasks are in the pending task queue. This value |
| 482 | // increases by N every time we call ReloadWorkQueue() and decreases by 1 |
| 483 | // every time we call RunTask() if the task needs a high resolution timer. |
| 484 | int pending_high_res_tasks_; |
| 485 | // Tracks if we have requested high resolution timers. Its only use is to |
| 486 | // turn off the high resolution timer upon loop destruction. |
| 487 | bool in_high_res_mode_; |
thestig | fa24e5b | 2015-01-22 16:09:45 +0900 | [diff] [blame] | 488 | #endif |
cpu | 410a98e | 2014-08-29 08:25:37 +0900 | [diff] [blame] | 489 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 490 | // Contains delayed tasks, sorted by their 'delayed_run_time' property. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 491 | DelayedTaskQueue delayed_work_queue_; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 492 | |
| 493 | // A recent snapshot of Time::Now(), used to check delayed_work_queue_. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 494 | TimeTicks recent_time_; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 495 | |
| 496 | // A queue of non-nestable tasks that we had to defer because when it came |
| 497 | // time to execute them we were in a nested message loop. They will execute |
| 498 | // once we're out of nested message loops. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 499 | TaskQueue deferred_non_nestable_work_queue_; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 500 | |
| 501 | ObserverList<DestructionObserver> destruction_observers_; |
| 502 | |
| 503 | // A recursion block that prevents accidentally running additional tasks when |
| 504 | // insider a (accidentally induced?) nested message pump. |
| 505 | bool nestable_tasks_allowed_; |
| 506 | |
alexeypa@google.com | bb819d6 | 2013-07-23 05:06:56 +0900 | [diff] [blame] | 507 | #if defined(OS_WIN) |
qyearsley | b8cfa6d | 2015-07-25 09:33:31 +0900 | [diff] [blame] | 508 | // Should be set to true before calling Windows APIs like TrackPopupMenu, etc. |
alexeypa@google.com | bb819d6 | 2013-07-23 05:06:56 +0900 | [diff] [blame] | 509 | // which enter a modal message loop. |
| 510 | bool os_modal_loop_; |
| 511 | #endif |
| 512 | |
kinuko | f25405c | 2015-05-23 20:38:37 +0900 | [diff] [blame] | 513 | // pump_factory_.Run() is called to create a message pump for this loop |
| 514 | // if type_ is TYPE_CUSTOM and pump_ is null. |
| 515 | MessagePumpFactoryCallback pump_factory_; |
| 516 | |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 517 | std::string thread_name_; |
| 518 | // A profiling histogram showing the counts of various messages and events. |
| 519 | HistogramBase* message_histogram_; |
| 520 | |
| 521 | RunLoop* run_loop_; |
alexeypa@google.com | bb819d6 | 2013-07-23 05:06:56 +0900 | [diff] [blame] | 522 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 523 | ObserverList<TaskObserver> task_observers_; |
| 524 | |
skyostil@chromium.org | 2ca1bf3 | 2014-08-14 23:26:09 +0900 | [diff] [blame] | 525 | debug::TaskAnnotator task_annotator_; |
| 526 | |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 527 | scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_; |
| 528 | |
skyostil | d1abbc1 | 2015-07-14 06:41:17 +0900 | [diff] [blame] | 529 | // A task runner which we haven't bound to a thread yet. |
| 530 | scoped_refptr<internal::MessageLoopTaskRunner> unbound_task_runner_; |
| 531 | |
skyostil | 9368ac0 | 2015-06-20 02:22:54 +0900 | [diff] [blame] | 532 | // The task runner associated with this message loop. |
skyostil | d1abbc1 | 2015-07-14 06:41:17 +0900 | [diff] [blame] | 533 | scoped_refptr<SingleThreadTaskRunner> task_runner_; |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 534 | scoped_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 535 | |
| 536 | template <class T, class R> friend class base::subtle::DeleteHelperInternal; |
| 537 | template <class T, class R> friend class base::subtle::ReleaseHelperInternal; |
| 538 | |
| 539 | void DeleteSoonInternal(const tracked_objects::Location& from_here, |
| 540 | void(*deleter)(const void*), |
| 541 | const void* object); |
| 542 | void ReleaseSoonInternal(const tracked_objects::Location& from_here, |
| 543 | void(*releaser)(const void*), |
| 544 | const void* object); |
| 545 | |
| 546 | DISALLOW_COPY_AND_ASSIGN(MessageLoop); |
| 547 | }; |
| 548 | |
sadrul@chromium.org | b169e22 | 2014-04-23 09:55:07 +0900 | [diff] [blame] | 549 | #if !defined(OS_NACL) |
| 550 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 551 | //----------------------------------------------------------------------------- |
| 552 | // MessageLoopForUI extends MessageLoop with methods that are particular to a |
| 553 | // MessageLoop instantiated with TYPE_UI. |
| 554 | // |
| 555 | // This class is typically used like so: |
| 556 | // MessageLoopForUI::current()->...call some method... |
| 557 | // |
| 558 | class BASE_EXPORT MessageLoopForUI : public MessageLoop { |
| 559 | public: |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 560 | MessageLoopForUI() : MessageLoop(TYPE_UI) { |
| 561 | } |
| 562 | |
| 563 | // Returns the MessageLoopForUI of the current thread. |
| 564 | static MessageLoopForUI* current() { |
| 565 | MessageLoop* loop = MessageLoop::current(); |
| 566 | DCHECK(loop); |
| 567 | DCHECK_EQ(MessageLoop::TYPE_UI, loop->type()); |
| 568 | return static_cast<MessageLoopForUI*>(loop); |
| 569 | } |
| 570 | |
sky@chromium.org | 8a7aae7 | 2014-01-20 17:59:52 +0900 | [diff] [blame] | 571 | static bool IsCurrent() { |
| 572 | MessageLoop* loop = MessageLoop::current(); |
| 573 | return loop && loop->type() == MessageLoop::TYPE_UI; |
| 574 | } |
| 575 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 576 | #if defined(OS_IOS) |
| 577 | // On iOS, the main message loop cannot be Run(). Instead call Attach(), |
| 578 | // which connects this MessageLoop to the UI thread's CFRunLoop and allows |
| 579 | // PostTask() to work. |
| 580 | void Attach(); |
| 581 | #endif |
| 582 | |
| 583 | #if defined(OS_ANDROID) |
| 584 | // On Android, the UI message loop is handled by Java side. So Run() should |
| 585 | // never be called. Instead use Start(), which will forward all the native UI |
| 586 | // events to the Java message loop. |
| 587 | void Start(); |
sky@chromium.org | f0ef6b6 | 2014-01-12 08:12:06 +0900 | [diff] [blame] | 588 | #endif |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 589 | |
zhaoze.zhou@partner.samsung.com | 0137aa8 | 2014-07-17 04:12:40 +0900 | [diff] [blame] | 590 | #if defined(USE_OZONE) || (defined(USE_X11) && !defined(USE_GLIB)) |
sadrul@chromium.org | 7aff5f6 | 2014-04-11 02:19:38 +0900 | [diff] [blame] | 591 | // Please see MessagePumpLibevent for definition. |
| 592 | bool WatchFileDescriptor( |
| 593 | int fd, |
| 594 | bool persistent, |
| 595 | MessagePumpLibevent::Mode mode, |
| 596 | MessagePumpLibevent::FileDescriptorWatcher* controller, |
| 597 | MessagePumpLibevent::Watcher* delegate); |
| 598 | #endif |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 599 | }; |
| 600 | |
| 601 | // Do not add any member variables to MessageLoopForUI! This is important b/c |
| 602 | // MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra |
| 603 | // data that you need should be stored on the MessageLoop's pump_ instance. |
avi | 486c61f | 2015-11-24 23:26:24 +0900 | [diff] [blame] | 604 | static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForUI), |
| 605 | "MessageLoopForUI should not have extra member variables"); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 606 | |
sadrul@chromium.org | b169e22 | 2014-04-23 09:55:07 +0900 | [diff] [blame] | 607 | #endif // !defined(OS_NACL) |
| 608 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 609 | //----------------------------------------------------------------------------- |
| 610 | // MessageLoopForIO extends MessageLoop with methods that are particular to a |
| 611 | // MessageLoop instantiated with TYPE_IO. |
| 612 | // |
| 613 | // This class is typically used like so: |
| 614 | // MessageLoopForIO::current()->...call some method... |
| 615 | // |
| 616 | class BASE_EXPORT MessageLoopForIO : public MessageLoop { |
| 617 | public: |
sadrul@chromium.org | b169e22 | 2014-04-23 09:55:07 +0900 | [diff] [blame] | 618 | MessageLoopForIO() : MessageLoop(TYPE_IO) { |
| 619 | } |
| 620 | |
| 621 | // Returns the MessageLoopForIO of the current thread. |
| 622 | static MessageLoopForIO* current() { |
| 623 | MessageLoop* loop = MessageLoop::current(); |
| 624 | DCHECK_EQ(MessageLoop::TYPE_IO, loop->type()); |
| 625 | return static_cast<MessageLoopForIO*>(loop); |
| 626 | } |
| 627 | |
| 628 | static bool IsCurrent() { |
| 629 | MessageLoop* loop = MessageLoop::current(); |
| 630 | return loop && loop->type() == MessageLoop::TYPE_IO; |
| 631 | } |
| 632 | |
hidehiko | 58e1907 | 2014-11-06 08:51:52 +0900 | [diff] [blame] | 633 | #if !defined(OS_NACL_SFI) |
sadrul@chromium.org | b169e22 | 2014-04-23 09:55:07 +0900 | [diff] [blame] | 634 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 635 | #if defined(OS_WIN) |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 636 | typedef MessagePumpForIO::IOHandler IOHandler; |
| 637 | typedef MessagePumpForIO::IOContext IOContext; |
| 638 | typedef MessagePumpForIO::IOObserver IOObserver; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 639 | #elif defined(OS_IOS) |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 640 | typedef MessagePumpIOSForIO::Watcher Watcher; |
| 641 | typedef MessagePumpIOSForIO::FileDescriptorWatcher |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 642 | FileDescriptorWatcher; |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 643 | typedef MessagePumpIOSForIO::IOObserver IOObserver; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 644 | |
| 645 | enum Mode { |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 646 | WATCH_READ = MessagePumpIOSForIO::WATCH_READ, |
| 647 | WATCH_WRITE = MessagePumpIOSForIO::WATCH_WRITE, |
| 648 | WATCH_READ_WRITE = MessagePumpIOSForIO::WATCH_READ_WRITE |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 649 | }; |
| 650 | #elif defined(OS_POSIX) |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 651 | typedef MessagePumpLibevent::Watcher Watcher; |
| 652 | typedef MessagePumpLibevent::FileDescriptorWatcher |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 653 | FileDescriptorWatcher; |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 654 | typedef MessagePumpLibevent::IOObserver IOObserver; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 655 | |
| 656 | enum Mode { |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 657 | WATCH_READ = MessagePumpLibevent::WATCH_READ, |
| 658 | WATCH_WRITE = MessagePumpLibevent::WATCH_WRITE, |
| 659 | WATCH_READ_WRITE = MessagePumpLibevent::WATCH_READ_WRITE |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 660 | }; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 661 | #endif |
| 662 | |
sadrul@chromium.org | b169e22 | 2014-04-23 09:55:07 +0900 | [diff] [blame] | 663 | void AddIOObserver(IOObserver* io_observer); |
| 664 | void RemoveIOObserver(IOObserver* io_observer); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 665 | |
| 666 | #if defined(OS_WIN) |
| 667 | // Please see MessagePumpWin for definitions of these methods. |
| 668 | void RegisterIOHandler(HANDLE file, IOHandler* handler); |
| 669 | bool RegisterJobObject(HANDLE job, IOHandler* handler); |
| 670 | bool WaitForIOCompletion(DWORD timeout, IOHandler* filter); |
sadrul@chromium.org | b169e22 | 2014-04-23 09:55:07 +0900 | [diff] [blame] | 671 | #elif defined(OS_POSIX) |
| 672 | // Please see MessagePumpIOSForIO/MessagePumpLibevent for definition. |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 673 | bool WatchFileDescriptor(int fd, |
| 674 | bool persistent, |
| 675 | Mode mode, |
thestig | fa24e5b | 2015-01-22 16:09:45 +0900 | [diff] [blame] | 676 | FileDescriptorWatcher* controller, |
| 677 | Watcher* delegate); |
sadrul@chromium.org | b169e22 | 2014-04-23 09:55:07 +0900 | [diff] [blame] | 678 | #endif // defined(OS_IOS) || defined(OS_POSIX) |
hidehiko | 58e1907 | 2014-11-06 08:51:52 +0900 | [diff] [blame] | 679 | #endif // !defined(OS_NACL_SFI) |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 680 | }; |
| 681 | |
| 682 | // Do not add any member variables to MessageLoopForIO! This is important b/c |
| 683 | // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
| 684 | // data that you need should be stored on the MessageLoop's pump_ instance. |
avi | 486c61f | 2015-11-24 23:26:24 +0900 | [diff] [blame] | 685 | static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
| 686 | "MessageLoopForIO should not have extra member variables"); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 687 | |
| 688 | } // namespace base |
| 689 | |
| 690 | #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |