OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
6 #define BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 6 #define BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <queue> | 9 #include <queue> |
10 #include <string> | 10 #include <string> |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 }; | 155 }; |
156 | 156 |
157 // Add a DestructionObserver, which will start receiving notifications | 157 // Add a DestructionObserver, which will start receiving notifications |
158 // immediately. | 158 // immediately. |
159 void AddDestructionObserver(DestructionObserver* destruction_observer); | 159 void AddDestructionObserver(DestructionObserver* destruction_observer); |
160 | 160 |
161 // Remove a DestructionObserver. It is safe to call this method while a | 161 // Remove a DestructionObserver. It is safe to call this method while a |
162 // DestructionObserver is receiving a notification callback. | 162 // DestructionObserver is receiving a notification callback. |
163 void RemoveDestructionObserver(DestructionObserver* destruction_observer); | 163 void RemoveDestructionObserver(DestructionObserver* destruction_observer); |
164 | 164 |
165 // A NestingObserver is notified when a nested message loop begins. The | |
166 // observers are notified before the first task is processed. | |
167 class BASE_EXPORT NestingObserver { | |
168 public: | |
169 virtual void OnBeginNestedMessageLoop() = 0; | |
170 | |
171 protected: | |
172 virtual ~NestingObserver(); | |
173 }; | |
174 | |
175 void AddNestingObserver(NestingObserver* observer); | |
176 void RemoveNestingObserver(NestingObserver* observer); | |
177 | |
178 // Deprecated: use RunLoop instead. | 165 // Deprecated: use RunLoop instead. |
179 // | 166 // |
180 // Signals the Run method to return when it becomes idle. It will continue to | 167 // Signals the Run method to return when it becomes idle. It will continue to |
181 // process pending messages and future messages as long as they are enqueued. | 168 // process pending messages and future messages as long as they are enqueued. |
182 // Warning: if the MessageLoop remains busy, it may never quit. Only use this | 169 // Warning: if the MessageLoop remains busy, it may never quit. Only use this |
183 // Quit method when looping procedures (such as web pages) have been shut | 170 // Quit method when looping procedures (such as web pages) have been shut |
184 // down. | 171 // down. |
185 // | 172 // |
186 // This method may only be called on the same thread that called Run, and Run | 173 // This method may only be called on the same thread that called Run, and Run |
187 // must still be on the call stack. | 174 // must still be on the call stack. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 } | 257 } |
271 ~ScopedNestableTaskAllower() { | 258 ~ScopedNestableTaskAllower() { |
272 loop_->SetNestableTasksAllowed(old_state_); | 259 loop_->SetNestableTasksAllowed(old_state_); |
273 } | 260 } |
274 | 261 |
275 private: | 262 private: |
276 MessageLoop* loop_; | 263 MessageLoop* loop_; |
277 bool old_state_; | 264 bool old_state_; |
278 }; | 265 }; |
279 | 266 |
280 // Returns true if we are currently running a nested message loop. | |
281 bool IsNested(); | |
282 | |
283 // A TaskObserver is an object that receives task notifications from the | 267 // A TaskObserver is an object that receives task notifications from the |
284 // MessageLoop. | 268 // MessageLoop. |
285 // | 269 // |
286 // NOTE: A TaskObserver implementation should be extremely fast! | 270 // NOTE: A TaskObserver implementation should be extremely fast! |
287 class BASE_EXPORT TaskObserver { | 271 class BASE_EXPORT TaskObserver { |
288 public: | 272 public: |
289 TaskObserver(); | 273 TaskObserver(); |
290 | 274 |
291 // This method is called before processing a task. | 275 // This method is called before processing a task. |
292 virtual void WillProcessTask(const PendingTask& pending_task) = 0; | 276 virtual void WillProcessTask(const PendingTask& pending_task) = 0; |
293 | 277 |
294 // This method is called after processing a task. | 278 // This method is called after processing a task. |
295 virtual void DidProcessTask(const PendingTask& pending_task) = 0; | 279 virtual void DidProcessTask(const PendingTask& pending_task) = 0; |
296 | 280 |
297 protected: | 281 protected: |
298 virtual ~TaskObserver(); | 282 virtual ~TaskObserver(); |
299 }; | 283 }; |
300 | 284 |
301 // These functions can only be called on the same thread that |this| is | 285 // These functions can only be called on the same thread that |this| is |
302 // running on. | 286 // running on. |
303 void AddTaskObserver(TaskObserver* task_observer); | 287 void AddTaskObserver(TaskObserver* task_observer); |
304 void RemoveTaskObserver(TaskObserver* task_observer); | 288 void RemoveTaskObserver(TaskObserver* task_observer); |
305 | 289 |
306 // Can only be called from the thread that owns the MessageLoop. | |
307 bool is_running() const; | |
308 | |
309 // Returns true if the message loop has high resolution timers enabled. | 290 // Returns true if the message loop has high resolution timers enabled. |
310 // Provided for testing. | 291 // Provided for testing. |
311 bool HasHighResolutionTasks(); | 292 bool HasHighResolutionTasks(); |
312 | 293 |
313 // Returns true if the message loop is "idle". Provided for testing. | 294 // Returns true if the message loop is "idle". Provided for testing. |
314 bool IsIdleForTesting(); | 295 bool IsIdleForTesting(); |
315 | 296 |
316 // Returns the TaskAnnotator which is used to add debug information to posted | 297 // Returns the TaskAnnotator which is used to add debug information to posted |
317 // tasks. | 298 // tasks. |
318 debug::TaskAnnotator* task_annotator() { return &task_annotator_; } | 299 debug::TaskAnnotator* task_annotator() { return &task_annotator_; } |
319 | 300 |
320 // Runs the specified PendingTask. | 301 // Runs the specified PendingTask. |
321 void RunTask(PendingTask* pending_task); | 302 void RunTask(PendingTask* pending_task); |
322 | 303 |
323 bool nesting_allowed() const { return allow_nesting_; } | |
324 | |
325 // Disallow nesting. After this is called, running a nested RunLoop or calling | |
326 // Add/RemoveNestingObserver() on this MessageLoop will crash. | |
327 void DisallowNesting() { allow_nesting_ = false; } | |
328 | |
329 // Disallow task observers. After this is called, calling | 304 // Disallow task observers. After this is called, calling |
330 // Add/RemoveTaskObserver() on this MessageLoop will crash. | 305 // Add/RemoveTaskObserver() on this MessageLoop will crash. |
331 void DisallowTaskObservers() { allow_task_observers_ = false; } | 306 void DisallowTaskObservers() { allow_task_observers_ = false; } |
332 | 307 |
333 //---------------------------------------------------------------------------- | 308 //---------------------------------------------------------------------------- |
334 protected: | 309 protected: |
335 std::unique_ptr<MessagePump> pump_; | 310 std::unique_ptr<MessagePump> pump_; |
336 | 311 |
337 using MessagePumpFactoryCallback = | 312 using MessagePumpFactoryCallback = |
338 OnceCallback<std::unique_ptr<MessagePump>()>; | 313 OnceCallback<std::unique_ptr<MessagePump>()>; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 bool DeletePendingTasks(); | 370 bool DeletePendingTasks(); |
396 | 371 |
397 // Loads tasks from the incoming queue to |work_queue_| if the latter is | 372 // Loads tasks from the incoming queue to |work_queue_| if the latter is |
398 // empty. | 373 // empty. |
399 void ReloadWorkQueue(); | 374 void ReloadWorkQueue(); |
400 | 375 |
401 // Wakes up the message pump. Can be called on any thread. The caller is | 376 // Wakes up the message pump. Can be called on any thread. The caller is |
402 // responsible for synchronizing ScheduleWork() calls. | 377 // responsible for synchronizing ScheduleWork() calls. |
403 void ScheduleWork(); | 378 void ScheduleWork(); |
404 | 379 |
405 // Notify observers that a nested message loop is starting. | |
406 void NotifyBeginNestedLoop(); | |
407 | |
408 // MessagePump::Delegate methods: | 380 // MessagePump::Delegate methods: |
409 bool DoWork() override; | 381 bool DoWork() override; |
410 bool DoDelayedWork(TimeTicks* next_delayed_work_time) override; | 382 bool DoDelayedWork(TimeTicks* next_delayed_work_time) override; |
411 bool DoIdleWork() override; | 383 bool DoIdleWork() override; |
412 | 384 |
413 const Type type_; | 385 const Type type_; |
414 | 386 |
415 // A list of tasks that need to be processed by this instance. Note that | 387 // A list of tasks that need to be processed by this instance. Note that |
416 // this queue is only accessed (push/pop) by our current thread. | 388 // this queue is only accessed (push/pop) by our current thread. |
417 TaskQueue work_queue_; | 389 TaskQueue work_queue_; |
(...skipping 14 matching lines...) Expand all Loading... |
432 // A recent snapshot of Time::Now(), used to check delayed_work_queue_. | 404 // A recent snapshot of Time::Now(), used to check delayed_work_queue_. |
433 TimeTicks recent_time_; | 405 TimeTicks recent_time_; |
434 | 406 |
435 // A queue of non-nestable tasks that we had to defer because when it came | 407 // A queue of non-nestable tasks that we had to defer because when it came |
436 // time to execute them we were in a nested message loop. They will execute | 408 // time to execute them we were in a nested message loop. They will execute |
437 // once we're out of nested message loops. | 409 // once we're out of nested message loops. |
438 TaskQueue deferred_non_nestable_work_queue_; | 410 TaskQueue deferred_non_nestable_work_queue_; |
439 | 411 |
440 ObserverList<DestructionObserver> destruction_observers_; | 412 ObserverList<DestructionObserver> destruction_observers_; |
441 | 413 |
442 ObserverList<NestingObserver> nesting_observers_; | |
443 | |
444 // A recursion block that prevents accidentally running additional tasks when | 414 // A recursion block that prevents accidentally running additional tasks when |
445 // insider a (accidentally induced?) nested message pump. | 415 // insider a (accidentally induced?) nested message pump. |
446 bool nestable_tasks_allowed_; | 416 bool nestable_tasks_allowed_; |
447 | 417 |
448 // pump_factory_.Run() is called to create a message pump for this loop | 418 // pump_factory_.Run() is called to create a message pump for this loop |
449 // if type_ is TYPE_CUSTOM and pump_ is null. | 419 // if type_ is TYPE_CUSTOM and pump_ is null. |
450 MessagePumpFactoryCallback pump_factory_; | 420 MessagePumpFactoryCallback pump_factory_; |
451 | 421 |
452 RunLoop* run_loop_; | 422 RunLoop* run_loop_; |
453 | 423 |
(...skipping 14 matching lines...) Expand all Loading... |
468 scoped_refptr<internal::MessageLoopTaskRunner> unbound_task_runner_; | 438 scoped_refptr<internal::MessageLoopTaskRunner> unbound_task_runner_; |
469 | 439 |
470 // The task runner associated with this message loop. | 440 // The task runner associated with this message loop. |
471 scoped_refptr<SingleThreadTaskRunner> task_runner_; | 441 scoped_refptr<SingleThreadTaskRunner> task_runner_; |
472 std::unique_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; | 442 std::unique_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; |
473 | 443 |
474 // Id of the thread this message loop is bound to. Initialized once when the | 444 // Id of the thread this message loop is bound to. Initialized once when the |
475 // MessageLoop is bound to its thread and constant forever after. | 445 // MessageLoop is bound to its thread and constant forever after. |
476 PlatformThreadId thread_id_; | 446 PlatformThreadId thread_id_; |
477 | 447 |
478 // Whether nesting is allowed. | 448 // Whether this MessageLoop is currently running in nested RunLoops. |
479 bool allow_nesting_ = true; | 449 bool is_nested_ = false; |
480 | 450 |
481 // Whether task observers are allowed. | 451 // Whether task observers are allowed. |
482 bool allow_task_observers_ = true; | 452 bool allow_task_observers_ = true; |
483 | 453 |
484 DISALLOW_COPY_AND_ASSIGN(MessageLoop); | 454 DISALLOW_COPY_AND_ASSIGN(MessageLoop); |
485 }; | 455 }; |
486 | 456 |
487 #if !defined(OS_NACL) | 457 #if !defined(OS_NACL) |
488 | 458 |
489 //----------------------------------------------------------------------------- | 459 //----------------------------------------------------------------------------- |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
621 | 591 |
622 // Do not add any member variables to MessageLoopForIO! This is important b/c | 592 // Do not add any member variables to MessageLoopForIO! This is important b/c |
623 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 593 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
624 // data that you need should be stored on the MessageLoop's pump_ instance. | 594 // data that you need should be stored on the MessageLoop's pump_ instance. |
625 static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 595 static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
626 "MessageLoopForIO should not have extra member variables"); | 596 "MessageLoopForIO should not have extra member variables"); |
627 | 597 |
628 } // namespace base | 598 } // namespace base |
629 | 599 |
630 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 600 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
OLD | NEW |