summaryrefslogtreecommitdiffstats
path: root/base/message_loop.cc
diff options
context:
space:
mode:
authordarin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-26 05:53:57 +0000
committerdarin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-26 05:53:57 +0000
commit4d9bdfafcd1393385860bc9fe947e0c07719c0f4 (patch)
tree67c91c3fa4c658352995e22b894535e60b04f282 /base/message_loop.cc
parent3c17b9c00d5fc28f5523ab60b0df502dd7c2a596 (diff)
downloadchromium_src-4d9bdfafcd1393385860bc9fe947e0c07719c0f4.zip
chromium_src-4d9bdfafcd1393385860bc9fe947e0c07719c0f4.tar.gz
chromium_src-4d9bdfafcd1393385860bc9fe947e0c07719c0f4.tar.bz2
Allow consumers of MessageLoop to specify the type of MessageLoop they want.
This CL introduces a Type enum to MessageLoop, and I also created subclasses of MessageLoop corresponding to the non-default types: MessageLoopForIO and MessageLoopForUI. I moved all of the platform-specific MessageLoop APIs onto either MessageLoopForIO or MessageLoopForUI. MessageLoopForIO gets the Watcher API, and MessageLoopForUI gets the Observer and Dispatcher APIs. Under the hood, both are implemented in terms of MessagePumpWin, but that will change in a future CL. The Thread class is changed to allow the consumer to specify the Type of MessageLoop they want to have setup on the created thread. I re-organized message_loop_unittest.cc and timer_unittest.cc so that I could exercise all (or most) of the tests against each type of MessageLoop. Note: I know that "explicit MessageLoop(Type type = TYPE_DEFAULT);" is in violation to the style-guide's restriction against default arguments. I'm working on finding a decent solution to that problem. Please ignore this issue for now. The corresponding chrome/ changes are coming in a separate CL due to Reitveld data size limitations. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1362 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/message_loop.cc')
-rw-r--r--base/message_loop.cc72
1 files changed, 58 insertions, 14 deletions
diff --git a/base/message_loop.cc b/base/message_loop.cc
index a84b3b5..4528b93 100644
--- a/base/message_loop.cc
+++ b/base/message_loop.cc
@@ -55,19 +55,30 @@ static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() {
//------------------------------------------------------------------------------
-MessageLoop::MessageLoop()
- : ALLOW_THIS_IN_INITIALIZER_LIST(timer_manager_(this)),
+MessageLoop::MessageLoop(Type type)
+ : type_(type),
+ ALLOW_THIS_IN_INITIALIZER_LIST(timer_manager_(this)),
nestable_tasks_allowed_(true),
exception_restoration_(false),
state_(NULL) {
- DCHECK(!current()) << "should only have one message loop per thread";
+ DCHECK(!tls_index_.Get()) << "should only have one message loop per thread";
tls_index_.Set(this);
- // TODO(darin): Generalize this to support instantiating different pumps.
+
+ switch (type) {
+ case TYPE_DEFAULT:
+ pump_ = new base::MessagePumpDefault();
+ break;
+ case TYPE_UI:
+ case TYPE_IO:
+ // TODO(darin): Use a special pumps for UI and IO, and figure out what to
+ // do for non-Windows platforms.
#if defined(OS_WIN)
- pump_ = new base::MessagePumpWin();
+ pump_ = new base::MessagePumpWin();
#else
- pump_ = new base::MessagePumpDefault();
+ pump_ = new base::MessagePumpDefault();
#endif
+ break;
+ }
}
MessageLoop::~MessageLoop() {
@@ -106,14 +117,6 @@ void MessageLoop::Run() {
RunHandler();
}
-#if defined(OS_WIN)
-void MessageLoop::Run(base::MessagePumpWin::Dispatcher* dispatcher) {
- AutoRunState save_state(this);
- state_->dispatcher = dispatcher;
- RunHandler();
-}
-#endif
-
void MessageLoop::RunAllPending() {
AutoRunState save_state(this);
state_->quit_received = true; // Means run until we would otherwise block.
@@ -560,3 +563,44 @@ const LinearHistogram::DescriptionPair MessageLoop::event_descriptions_[] = {
{-1, NULL} // The list must be null terminated, per API to histogram.
};
+//------------------------------------------------------------------------------
+// MessageLoopForUI
+
+#if defined(OS_WIN)
+
+void MessageLoopForUI::Run(Dispatcher* dispatcher) {
+ AutoRunState save_state(this);
+ state_->dispatcher = dispatcher;
+ RunHandler();
+}
+
+void MessageLoopForUI::AddObserver(Observer* observer) {
+ pump_win()->AddObserver(observer);
+}
+
+void MessageLoopForUI::RemoveObserver(Observer* observer) {
+ pump_win()->RemoveObserver(observer);
+}
+
+void MessageLoopForUI::WillProcessMessage(const MSG& message) {
+ pump_win()->WillProcessMessage(message);
+}
+void MessageLoopForUI::DidProcessMessage(const MSG& message) {
+ pump_win()->DidProcessMessage(message);
+}
+void MessageLoopForUI::PumpOutPendingPaintMessages() {
+ pump_win()->PumpOutPendingPaintMessages();
+}
+
+#endif // defined(OS_WIN)
+
+//------------------------------------------------------------------------------
+// MessageLoopForIO
+
+#if defined(OS_WIN)
+
+void MessageLoopForIO::WatchObject(HANDLE object, Watcher* watcher) {
+ pump_win()->WatchObject(object, watcher);
+}
+
+#endif // defined(OS_WIN)