| 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 = Callback<std::unique_ptr<MessagePump>()>; | 312 using MessagePumpFactoryCallback = Callback<std::unique_ptr<MessagePump>()>; |
| 338 | 313 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 bool DeletePendingTasks(); | 369 bool DeletePendingTasks(); |
| 395 | 370 |
| 396 // Loads tasks from the incoming queue to |work_queue_| if the latter is | 371 // Loads tasks from the incoming queue to |work_queue_| if the latter is |
| 397 // empty. | 372 // empty. |
| 398 void ReloadWorkQueue(); | 373 void ReloadWorkQueue(); |
| 399 | 374 |
| 400 // Wakes up the message pump. Can be called on any thread. The caller is | 375 // Wakes up the message pump. Can be called on any thread. The caller is |
| 401 // responsible for synchronizing ScheduleWork() calls. | 376 // responsible for synchronizing ScheduleWork() calls. |
| 402 void ScheduleWork(); | 377 void ScheduleWork(); |
| 403 | 378 |
| 404 // Notify observers that a nested message loop is starting. | |
| 405 void NotifyBeginNestedLoop(); | |
| 406 | |
| 407 // MessagePump::Delegate methods: | 379 // MessagePump::Delegate methods: |
| 408 bool DoWork() override; | 380 bool DoWork() override; |
| 409 bool DoDelayedWork(TimeTicks* next_delayed_work_time) override; | 381 bool DoDelayedWork(TimeTicks* next_delayed_work_time) override; |
| 410 bool DoIdleWork() override; | 382 bool DoIdleWork() override; |
| 411 | 383 |
| 412 const Type type_; | 384 const Type type_; |
| 413 | 385 |
| 414 // A list of tasks that need to be processed by this instance. Note that | 386 // A list of tasks that need to be processed by this instance. Note that |
| 415 // this queue is only accessed (push/pop) by our current thread. | 387 // this queue is only accessed (push/pop) by our current thread. |
| 416 TaskQueue work_queue_; | 388 TaskQueue work_queue_; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 431 // A recent snapshot of Time::Now(), used to check delayed_work_queue_. | 403 // A recent snapshot of Time::Now(), used to check delayed_work_queue_. |
| 432 TimeTicks recent_time_; | 404 TimeTicks recent_time_; |
| 433 | 405 |
| 434 // A queue of non-nestable tasks that we had to defer because when it came | 406 // A queue of non-nestable tasks that we had to defer because when it came |
| 435 // time to execute them we were in a nested message loop. They will execute | 407 // time to execute them we were in a nested message loop. They will execute |
| 436 // once we're out of nested message loops. | 408 // once we're out of nested message loops. |
| 437 TaskQueue deferred_non_nestable_work_queue_; | 409 TaskQueue deferred_non_nestable_work_queue_; |
| 438 | 410 |
| 439 ObserverList<DestructionObserver> destruction_observers_; | 411 ObserverList<DestructionObserver> destruction_observers_; |
| 440 | 412 |
| 441 ObserverList<NestingObserver> nesting_observers_; | |
| 442 | |
| 443 // A recursion block that prevents accidentally running additional tasks when | 413 // A recursion block that prevents accidentally running additional tasks when |
| 444 // insider a (accidentally induced?) nested message pump. | 414 // insider a (accidentally induced?) nested message pump. |
| 445 bool nestable_tasks_allowed_; | 415 bool nestable_tasks_allowed_; |
| 446 | 416 |
| 447 // pump_factory_.Run() is called to create a message pump for this loop | 417 // pump_factory_.Run() is called to create a message pump for this loop |
| 448 // if type_ is TYPE_CUSTOM and pump_ is null. | 418 // if type_ is TYPE_CUSTOM and pump_ is null. |
| 449 MessagePumpFactoryCallback pump_factory_; | 419 MessagePumpFactoryCallback pump_factory_; |
| 450 | 420 |
| 451 RunLoop* run_loop_; | 421 RunLoop* run_loop_; |
| 452 | 422 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 467 scoped_refptr<internal::MessageLoopTaskRunner> unbound_task_runner_; | 437 scoped_refptr<internal::MessageLoopTaskRunner> unbound_task_runner_; |
| 468 | 438 |
| 469 // The task runner associated with this message loop. | 439 // The task runner associated with this message loop. |
| 470 scoped_refptr<SingleThreadTaskRunner> task_runner_; | 440 scoped_refptr<SingleThreadTaskRunner> task_runner_; |
| 471 std::unique_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; | 441 std::unique_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; |
| 472 | 442 |
| 473 // Id of the thread this message loop is bound to. Initialized once when the | 443 // Id of the thread this message loop is bound to. Initialized once when the |
| 474 // MessageLoop is bound to its thread and constant forever after. | 444 // MessageLoop is bound to its thread and constant forever after. |
| 475 PlatformThreadId thread_id_; | 445 PlatformThreadId thread_id_; |
| 476 | 446 |
| 477 // Whether nesting is allowed. | 447 // Whether this MessageLoop is currently running in nested RunLoops. |
| 478 bool allow_nesting_ = true; | 448 bool is_nested_ = false; |
| 479 | 449 |
| 480 // Whether task observers are allowed. | 450 // Whether task observers are allowed. |
| 481 bool allow_task_observers_ = true; | 451 bool allow_task_observers_ = true; |
| 482 | 452 |
| 483 DISALLOW_COPY_AND_ASSIGN(MessageLoop); | 453 DISALLOW_COPY_AND_ASSIGN(MessageLoop); |
| 484 }; | 454 }; |
| 485 | 455 |
| 486 #if !defined(OS_NACL) | 456 #if !defined(OS_NACL) |
| 487 | 457 |
| 488 //----------------------------------------------------------------------------- | 458 //----------------------------------------------------------------------------- |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 620 | 590 |
| 621 // Do not add any member variables to MessageLoopForIO! This is important b/c | 591 // Do not add any member variables to MessageLoopForIO! This is important b/c |
| 622 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 592 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
| 623 // data that you need should be stored on the MessageLoop's pump_ instance. | 593 // data that you need should be stored on the MessageLoop's pump_ instance. |
| 624 static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 594 static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
| 625 "MessageLoopForIO should not have extra member variables"); | 595 "MessageLoopForIO should not have extra member variables"); |
| 626 | 596 |
| 627 } // namespace base | 597 } // namespace base |
| 628 | 598 |
| 629 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 599 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
| OLD | NEW |