OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_H_ | 5 #ifndef BASE_MESSAGE_LOOP_H_ |
6 #define BASE_MESSAGE_LOOP_H_ | 6 #define BASE_MESSAGE_LOOP_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <queue> | 9 #include <queue> |
10 #include <string> | 10 #include <string> |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 // | 60 // |
61 // NOTE: MessageLoop has task reentrancy protection. This means that if a | 61 // NOTE: MessageLoop has task reentrancy protection. This means that if a |
62 // task is being processed, a second task cannot start until the first task is | 62 // task is being processed, a second task cannot start until the first task is |
63 // finished. Reentrancy can happen when processing a task, and an inner | 63 // finished. Reentrancy can happen when processing a task, and an inner |
64 // message pump is created. That inner pump then processes native messages | 64 // message pump is created. That inner pump then processes native messages |
65 // which could implicitly start an inner task. Inner message pumps are created | 65 // which could implicitly start an inner task. Inner message pumps are created |
66 // with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions | 66 // with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions |
67 // (DoDragDrop), printer functions (StartDoc) and *many* others. | 67 // (DoDragDrop), printer functions (StartDoc) and *many* others. |
68 // | 68 // |
69 // Sample workaround when inner task processing is needed: | 69 // Sample workaround when inner task processing is needed: |
70 // bool old_state = MessageLoop::current()->NestableTasksAllowed(); | 70 // HRESULT hr; |
71 // MessageLoop::current()->SetNestableTasksAllowed(true); | 71 // { |
72 // HRESULT hr = DoDragDrop(...); // Implicitly runs a modal message loop here. | 72 // MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
73 // MessageLoop::current()->SetNestableTasksAllowed(old_state); | 73 // hr = DoDragDrop(...); // Implicitly runs a modal message loop. |
74 // // Process hr (the result returned by DoDragDrop(). | 74 // } |
| 75 // // Process |hr| (the result returned by DoDragDrop()). |
75 // | 76 // |
76 // Please be SURE your task is reentrant (nestable) and all global variables | 77 // Please be SURE your task is reentrant (nestable) and all global variables |
77 // are stable and accessible before calling SetNestableTasksAllowed(true). | 78 // are stable and accessible before calling SetNestableTasksAllowed(true). |
78 // | 79 // |
79 class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate { | 80 class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate { |
80 public: | 81 public: |
81 #if defined(OS_WIN) | 82 #if defined(OS_WIN) |
82 typedef base::MessagePumpWin::Dispatcher Dispatcher; | 83 typedef base::MessagePumpWin::Dispatcher Dispatcher; |
83 typedef base::MessagePumpObserver Observer; | 84 typedef base::MessagePumpObserver Observer; |
84 #elif !defined(OS_MACOSX) && !defined(OS_ANDROID) | 85 #elif !defined(OS_MACOSX) && !defined(OS_ANDROID) |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 // Gets the message loop proxy associated with this message loop. | 258 // Gets the message loop proxy associated with this message loop. |
258 scoped_refptr<base::MessageLoopProxy> message_loop_proxy() { | 259 scoped_refptr<base::MessageLoopProxy> message_loop_proxy() { |
259 return message_loop_proxy_.get(); | 260 return message_loop_proxy_.get(); |
260 } | 261 } |
261 | 262 |
262 // Enables or disables the recursive task processing. This happens in the case | 263 // Enables or disables the recursive task processing. This happens in the case |
263 // of recursive message loops. Some unwanted message loop may occurs when | 264 // of recursive message loops. Some unwanted message loop may occurs when |
264 // using common controls or printer functions. By default, recursive task | 265 // using common controls or printer functions. By default, recursive task |
265 // processing is disabled. | 266 // processing is disabled. |
266 // | 267 // |
| 268 // Please utilize |ScopedNestableTaskAllower| instead of calling these methods |
| 269 // directly. In general nestable message loops are to be avoided. They are |
| 270 // dangerous and difficult to get right, so please use with extreme caution. |
| 271 // |
267 // The specific case where tasks get queued is: | 272 // The specific case where tasks get queued is: |
268 // - The thread is running a message loop. | 273 // - The thread is running a message loop. |
269 // - It receives a task #1 and execute it. | 274 // - It receives a task #1 and execute it. |
270 // - The task #1 implicitly start a message loop, like a MessageBox in the | 275 // - The task #1 implicitly start a message loop, like a MessageBox in the |
271 // unit test. This can also be StartDoc or GetSaveFileName. | 276 // unit test. This can also be StartDoc or GetSaveFileName. |
272 // - The thread receives a task #2 before or while in this second message | 277 // - The thread receives a task #2 before or while in this second message |
273 // loop. | 278 // loop. |
274 // - With NestableTasksAllowed set to true, the task #2 will run right away. | 279 // - With NestableTasksAllowed set to true, the task #2 will run right away. |
275 // Otherwise, it will get executed right after task #1 completes at "thread | 280 // Otherwise, it will get executed right after task #1 completes at "thread |
276 // message loop level". | 281 // message loop level". |
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
659 #endif // defined(OS_POSIX) | 664 #endif // defined(OS_POSIX) |
660 }; | 665 }; |
661 | 666 |
662 // Do not add any member variables to MessageLoopForIO! This is important b/c | 667 // Do not add any member variables to MessageLoopForIO! This is important b/c |
663 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 668 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
664 // data that you need should be stored on the MessageLoop's pump_ instance. | 669 // data that you need should be stored on the MessageLoop's pump_ instance. |
665 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 670 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
666 MessageLoopForIO_should_not_have_extra_member_variables); | 671 MessageLoopForIO_should_not_have_extra_member_variables); |
667 | 672 |
668 #endif // BASE_MESSAGE_LOOP_H_ | 673 #endif // BASE_MESSAGE_LOOP_H_ |
OLD | NEW |