From ab820df141e6ab45fd8a095d2f57f91df44e6c9c Mon Sep 17 00:00:00 2001
From: "darin@google.com"
 <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98>
Date: Tue, 26 Aug 2008 05:55:10 +0000
Subject: Chrome changes corresponding to my message_loop_type CL.

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1363 0039d316-1c4b-4281-b951-d872f2087c98
---
 chrome/common/animation_unittest.cc        |  6 ++++--
 chrome/common/chrome_plugin_unittest.cc    |  2 ++
 chrome/common/chrome_plugin_util.cc        | 12 +++---------
 chrome/common/chrome_plugin_util.h         |  8 ++------
 chrome/common/ipc_channel.cc               | 28 +++++++++++++++++-----------
 chrome/common/ipc_channel.h                |  2 +-
 chrome/common/ipc_sync_channel_unittest.cc | 27 ++++++++++++++++++---------
 chrome/common/ipc_tests.cc                 |  8 +++++---
 chrome/common/pref_service.cc              |  4 ++--
 chrome/common/pref_service.h               |  7 +++++--
 chrome/common/process_watcher.cc           | 16 ++++++++--------
 11 files changed, 67 insertions(+), 53 deletions(-)

(limited to 'chrome/common')

diff --git a/chrome/common/animation_unittest.cc b/chrome/common/animation_unittest.cc
index e4429cd..3f9da3c 100644
--- a/chrome/common/animation_unittest.cc
+++ b/chrome/common/animation_unittest.cc
@@ -11,6 +11,8 @@ using namespace std;
 
 namespace {
   class AnimationTest: public testing::Test {
+   private:
+    MessageLoopForUI message_loop_;
   };
 };
 
@@ -81,7 +83,7 @@ class TestAnimationDelegate : public AnimationDelegate {
   bool canceled_;
 };
 
-TEST(AnimationTest, RunCase) {
+TEST_F(AnimationTest, RunCase) {
   TestAnimationDelegate ad;
   RunAnimation a1(150, &ad);
   a1.SetDuration(2000);
@@ -92,7 +94,7 @@ TEST(AnimationTest, RunCase) {
   EXPECT_FALSE(ad.canceled());
 }
 
-TEST(AnimationTest, CancelCase) {
+TEST_F(AnimationTest, CancelCase) {
   TestAnimationDelegate ad;
   CancelAnimation a2(2000, 150, &ad);
   a2.Start();
diff --git a/chrome/common/chrome_plugin_unittest.cc b/chrome/common/chrome_plugin_unittest.cc
index 139bcf4..f998755 100644
--- a/chrome/common/chrome_plugin_unittest.cc
+++ b/chrome/common/chrome_plugin_unittest.cc
@@ -62,6 +62,8 @@ class ChromePluginTest : public testing::Test, public URLRequest::Delegate {
     Profile::set_default_request_context(NULL);
   }
  protected:
+  MessageLoopForIO message_loop_;
+
   // Note: we use URLRequest (instead of URLFetcher) because this allows the
   // request to be intercepted.
   scoped_ptr<URLRequest> request_;
diff --git a/chrome/common/chrome_plugin_util.cc b/chrome/common/chrome_plugin_util.cc
index 1a33990..56b1f54 100644
--- a/chrome/common/chrome_plugin_util.cc
+++ b/chrome/common/chrome_plugin_util.cc
@@ -46,18 +46,14 @@ void PluginHelper::DestroyAllHelpersForPlugin(ChromePluginLib* plugin) {
 }
 
 PluginHelper::PluginHelper(ChromePluginLib* plugin) : plugin_(plugin) {
-#ifndef NDEBUG
-  message_loop_ = MessageLoop::current();
-#endif
+  DCHECK(CalledOnValidThread());
   NotificationService::current()->AddObserver(
       this, NOTIFY_CHROME_PLUGIN_UNLOADED,
       Source<ChromePluginLib>(plugin_));
 }
 
 PluginHelper::~PluginHelper() {
-#ifndef NDEBUG
-  DCHECK(MessageLoop::current() == message_loop_);
-#endif
+  DCHECK(CalledOnValidThread());
   NotificationService::current()->RemoveObserver(
       this, NOTIFY_CHROME_PLUGIN_UNLOADED,
       Source<ChromePluginLib>(plugin_));
@@ -66,9 +62,7 @@ PluginHelper::~PluginHelper() {
 void PluginHelper::Observe(NotificationType type,
                            const NotificationSource& source,
                            const NotificationDetails& details) {
-#ifndef NDEBUG
-  DCHECK(MessageLoop::current() == message_loop_);
-#endif
+  DCHECK(CalledOnValidThread());
   DCHECK(type == NOTIFY_CHROME_PLUGIN_UNLOADED);
   DCHECK(plugin_ == Source<ChromePluginLib>(source).ptr());
 
diff --git a/chrome/common/chrome_plugin_util.h b/chrome/common/chrome_plugin_util.h
index 847e096..5ca17fc 100644
--- a/chrome/common/chrome_plugin_util.h
+++ b/chrome/common/chrome_plugin_util.h
@@ -6,6 +6,7 @@
 #define CHROME_COMMON_CHROME_PLUGIN_UTIL_H__
 
 #include "base/basictypes.h"
+#include "base/non_thread_safe.h"
 #include "base/ref_counted.h"
 #include "chrome/common/chrome_plugin_api.h"
 #include "chrome/common/notification_service.h"
@@ -35,7 +36,7 @@ struct ScopableCPRequest : public CPRequest {
 // This is a base class for plugin-related objects that need to go away when
 // the plugin unloads.  This object also verifies that it is created and
 // destroyed on the same thread.
-class PluginHelper : public NotificationObserver {
+class PluginHelper : public NotificationObserver, public NonThreadSafe {
  public:
   static void DestroyAllHelpersForPlugin(ChromePluginLib* plugin);
 
@@ -49,11 +50,6 @@ class PluginHelper : public NotificationObserver {
 
  protected:
   scoped_refptr<ChromePluginLib> plugin_;
-#ifndef NDEBUG
-  // We keep track of the message loop of the thread we were created on, so
-  // we can verify that all other methods are called on the same thread.
-  MessageLoop* message_loop_;
-#endif
 
   DISALLOW_EVIL_CONSTRUCTORS(PluginHelper);
 };
diff --git a/chrome/common/ipc_channel.cc b/chrome/common/ipc_channel.cc
index 40a101b..c87d75f 100644
--- a/chrome/common/ipc_channel.cc
+++ b/chrome/common/ipc_channel.cc
@@ -49,8 +49,9 @@ Channel::Channel(const wstring& channel_id, Mode mode, Listener* listener)
 
 void Channel::Close() {
   // make sure we are no longer watching the pipe events
-  MessageLoop::current()->WatchObject(input_state_.overlapped.hEvent, NULL);
-  MessageLoop::current()->WatchObject(output_state_.overlapped.hEvent, NULL);
+  MessageLoopForIO* loop = MessageLoopForIO::current();
+  loop->WatchObject(input_state_.overlapped.hEvent, NULL);
+  loop->WatchObject(output_state_.overlapped.hEvent, NULL);
 
   if (pipe_ != INVALID_HANDLE_VALUE) {
     CloseHandle(pipe_);
@@ -168,7 +169,8 @@ bool Channel::Connect() {
     // to OnObjectSignaled that this is the special initialization signal.
 
     SetEvent(input_state_.overlapped.hEvent);
-    MessageLoop::current()->WatchObject(input_state_.overlapped.hEvent, this);
+    MessageLoopForIO::current()->WatchObject(
+        input_state_.overlapped.hEvent, this);
   }
 
   if (!waiting_connect_)
@@ -178,7 +180,8 @@ bool Channel::Connect() {
 
 bool Channel::ProcessConnection() {
   input_state_.is_pending = false;
-  MessageLoop::current()->WatchObject(input_state_.overlapped.hEvent, NULL);
+  MessageLoopForIO::current()->WatchObject(
+      input_state_.overlapped.hEvent, NULL);
 
   // Do we have a client connected to our pipe?
   DCHECK(pipe_ != INVALID_HANDLE_VALUE);
@@ -195,7 +198,8 @@ bool Channel::ProcessConnection() {
   switch (err) {
   case ERROR_IO_PENDING:
     input_state_.is_pending = true;
-    MessageLoop::current()->WatchObject(input_state_.overlapped.hEvent, this);
+    MessageLoopForIO::current()->WatchObject(
+        input_state_.overlapped.hEvent, this);
     break;
   case ERROR_PIPE_CONNECTED:
     waiting_connect_ = false;
@@ -211,7 +215,8 @@ bool Channel::ProcessConnection() {
 bool Channel::ProcessIncomingMessages() {
   DWORD bytes_read = 0;
 
-  MessageLoop::current()->WatchObject(input_state_.overlapped.hEvent, NULL);
+  MessageLoopForIO::current()->WatchObject(
+      input_state_.overlapped.hEvent, NULL);
 
   if (input_state_.is_pending) {
     input_state_.is_pending = false;
@@ -243,8 +248,8 @@ bool Channel::ProcessIncomingMessages() {
       if (!ok) {
         DWORD err = GetLastError();
         if (err == ERROR_IO_PENDING) {
-          MessageLoop::current()->WatchObject(input_state_.overlapped.hEvent,
-                                              this);
+          MessageLoopForIO::current()->WatchObject(
+              input_state_.overlapped.hEvent, this);
           input_state_.is_pending = true;
           return true;
         }
@@ -307,7 +312,8 @@ bool Channel::ProcessOutgoingMessages() {
   DWORD bytes_written;
 
   if (output_state_.is_pending) {
-    MessageLoop::current()->WatchObject(output_state_.overlapped.hEvent, NULL);
+    MessageLoopForIO::current()->WatchObject(
+        output_state_.overlapped.hEvent, NULL);
     output_state_.is_pending = false;
     BOOL ok = GetOverlappedResult(pipe_,
                                   &output_state_.overlapped,
@@ -336,8 +342,8 @@ bool Channel::ProcessOutgoingMessages() {
     if (!ok) {
       DWORD err = GetLastError();
       if (err == ERROR_IO_PENDING) {
-        MessageLoop::current()->WatchObject(output_state_.overlapped.hEvent,
-                                            this);
+        MessageLoopForIO::current()->WatchObject(
+            output_state_.overlapped.hEvent, this);
         output_state_.is_pending = true;
 
 #ifdef IPC_MESSAGE_DEBUG_EXTRA
diff --git a/chrome/common/ipc_channel.h b/chrome/common/ipc_channel.h
index 549e9cb..94d41a3 100644
--- a/chrome/common/ipc_channel.h
+++ b/chrome/common/ipc_channel.h
@@ -14,7 +14,7 @@ namespace IPC {
 
 //------------------------------------------------------------------------------
 
-class Channel : public MessageLoop::Watcher,
+class Channel : public MessageLoopForIO::Watcher,
                 public Message::Sender {
   // Security tests need access to the pipe handle.
   friend class ChannelTest;
diff --git a/chrome/common/ipc_sync_channel_unittest.cc b/chrome/common/ipc_sync_channel_unittest.cc
index f930790..3899204 100644
--- a/chrome/common/ipc_sync_channel_unittest.cc
+++ b/chrome/common/ipc_sync_channel_unittest.cc
@@ -97,12 +97,13 @@ class Worker : public Channel::Listener, public Message::Sender {
   void WaitForChannelCreation() { channel_created_.Wait(); }
   void CloseChannel() { channel_.reset(); }
   void Start() {
-    listener_thread_.Start();
-    Thread* thread = overrided_thread_ ? overrided_thread_ : &listener_thread_;
+    StartThread(&listener_thread_);
+    base::Thread* thread =
+        overrided_thread_ ? overrided_thread_ : &listener_thread_;
     thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
         this, &Worker::OnStart));
   }
-  void OverrideThread(Thread* overrided_thread) {
+  void OverrideThread(base::Thread* overrided_thread) {
     DCHECK(overrided_thread_ == NULL);
     overrided_thread_ = overrided_thread;
   }
@@ -133,8 +134,8 @@ class Worker : public Channel::Listener, public Message::Sender {
  private:
   // Called on the listener thread to create the sync channel.
   void OnStart() {
-    ipc_thread_.Start();
     // Link ipc_thread_, listener_thread_ and channel_ altogether.
+    StartThread(&ipc_thread_);
     channel_.reset(new SyncChannel(
         channel_name_, mode_, this, NULL, ipc_thread_.message_loop(), true,
         TestProcess::GetShutDownEvent()));
@@ -150,14 +151,20 @@ class Worker : public Channel::Listener, public Message::Sender {
     IPC_END_MESSAGE_MAP()
   }
 
+  void StartThread(base::Thread* thread) {
+    base::Thread::Options options;
+    options.message_loop_type = MessageLoop::TYPE_IO;
+    thread->StartWithOptions(options);
+  }
+
   Event done_;
   Event channel_created_;
   std::wstring channel_name_;
   Channel::Mode mode_;
   scoped_ptr<SyncChannel> channel_;
-  Thread ipc_thread_;
-  Thread listener_thread_;
-  Thread* overrided_thread_;
+  base::Thread ipc_thread_;
+  base::Thread listener_thread_;
+  base::Thread* overrided_thread_;
 
   DISALLOW_EVIL_CONSTRUCTORS(Worker);
 };
@@ -191,6 +198,8 @@ void RunTest(std::vector<Worker*> workers) {
   int count = static_cast<int>(done_handles.size());
   WaitForMultipleObjects(count, &done_handles.front(), TRUE, INFINITE);
   STLDeleteContainerPointers(workers.begin(), workers.end());
+  
+  TestProcess::GlobalCleanup();
 }
 
 
@@ -403,7 +412,7 @@ TEST(IPCSyncChannelTest, Multiple) {
   std::vector<Worker*> workers;
 
   // A shared worker thread so that server1 and server2 run on one thread.
-  Thread worker_thread("Multiple");
+  base::Thread worker_thread("Multiple");
   worker_thread.Start();
 
   // Server1 sends a sync msg to client1, which blocks the reply until
@@ -510,7 +519,7 @@ TEST(IPCSyncChannelTest, QueuedReply) {
   std::vector<Worker*> workers;
 
   // A shared worker thread so that server1 and server2 run on one thread.
-  Thread worker_thread("QueuedReply");
+  base::Thread worker_thread("QueuedReply");
   worker_thread.Start();
 
   Event client1_msg_received, server2_can_reply;
diff --git a/chrome/common/ipc_tests.cc b/chrome/common/ipc_tests.cc
index 316c397..67d7140 100644
--- a/chrome/common/ipc_tests.cc
+++ b/chrome/common/ipc_tests.cc
@@ -133,8 +133,10 @@ TEST(IPCChannelTest, ChannelTest) {
 
 TEST(IPCChannelTest, ChannelProxyTest) {
   // The thread needs to out-live the ChannelProxy.
-  Thread thread("ChannelProxyTestServer");
-  thread.Start();
+  base::Thread thread("ChannelProxyTestServer");
+  base::Thread::Options options;
+  options.message_loop_type = MessageLoop::TYPE_IO;
+  thread.StartWithOptions(options);
   {
     // setup IPC channel proxy
     IPC::ChannelProxy chan(kTestClientChannel, IPC::Channel::MODE_SERVER,
@@ -380,7 +382,7 @@ int main(int argc, char** argv) {
   // the AtExitManager or else we will leak objects.
   base::AtExitManager at_exit_manager;  
 
-  MessageLoop main_message_loop;
+  MessageLoopForIO main_message_loop;
 
   // suppress standard crash dialogs and such unless a debugger is present.
   if (!IsDebuggerPresent()) {
diff --git a/chrome/common/pref_service.cc b/chrome/common/pref_service.cc
index dd55bdc..60ac6f2 100644
--- a/chrome/common/pref_service.cc
+++ b/chrome/common/pref_service.cc
@@ -178,7 +178,7 @@ void PrefService::ReloadPersistentPrefs() {
   }
 }
 
-bool PrefService::SavePersistentPrefs(Thread* thread) const {
+bool PrefService::SavePersistentPrefs(base::Thread* thread) const {
   DCHECK(!pref_filename_.empty());
   DCHECK(CalledOnValidThread());
 
@@ -202,7 +202,7 @@ bool PrefService::SavePersistentPrefs(Thread* thread) const {
   return true;
 }
 
-void PrefService::ScheduleSavePersistentPrefs(Thread* thread) {
+void PrefService::ScheduleSavePersistentPrefs(base::Thread* thread) {
   if (!save_preferences_factory_.empty())
     return;
 
diff --git a/chrome/common/pref_service.h b/chrome/common/pref_service.h
index 9b552f2..32c012f 100644
--- a/chrome/common/pref_service.h
+++ b/chrome/common/pref_service.h
@@ -28,7 +28,10 @@
 
 class NotificationObserver;
 class Preference;
+
+namespace base {
 class Thread;
+}
 
 class PrefService : public NonThreadSafe {
  public:
@@ -88,12 +91,12 @@ class PrefService : public NonThreadSafe {
   // (since it's on a different thread).  This should only be used if we need
   // to save immediately (basically, during shutdown).  Otherwise, you should
   // use ScheduleSavePersistentPrefs.
-  bool SavePersistentPrefs(Thread* thread) const;
+  bool SavePersistentPrefs(base::Thread* thread) const;
 
   // Starts a timer that ends up saving the preferences.  This helps to batch
   // together save requests that happen in a close time frame so we don't write
   // to disk too frequently.
-  void ScheduleSavePersistentPrefs(Thread* thread);
+  void ScheduleSavePersistentPrefs(base::Thread* thread);
 
   DictionaryValue* transient() { return transient_.get(); }
 
diff --git a/chrome/common/process_watcher.cc b/chrome/common/process_watcher.cc
index 8a9fadc..b8d78bd2 100644
--- a/chrome/common/process_watcher.cc
+++ b/chrome/common/process_watcher.cc
@@ -5,6 +5,7 @@
 #include "chrome/common/process_watcher.h"
 
 #include "base/message_loop.h"
+#include "base/object_watcher.h"
 #include "chrome/app/result_codes.h"
 #include "chrome/common/env_util.h"
 #include "chrome/common/env_vars.h"
@@ -14,10 +15,10 @@ static const int kWaitInterval = 2000;
 
 namespace {
 
-class TimerExpiredTask : public Task, public MessageLoop::Watcher {
+class TimerExpiredTask : public Task, public base::ObjectWatcher::Delegate {
  public:
   explicit TimerExpiredTask(ProcessHandle process) : process_(process) {
-    MessageLoop::current()->WatchObject(process_, this);
+    watcher_.StartWatching(process_, this);
   }
 
   virtual ~TimerExpiredTask() {
@@ -37,12 +38,9 @@ class TimerExpiredTask : public Task, public MessageLoop::Watcher {
   // MessageLoop::Watcher -----------------------------------------------------
 
   virtual void OnObjectSignaled(HANDLE object) {
-    if (MessageLoop::current()) {
-      // When we're called from our destructor, the message loop is in the
-      // process of being torn down.  Only touch the message loop if it is
-      // still running.
-      MessageLoop::current()->WatchObject(process_, NULL);  // Stop watching.
-    }
+    // When we're called from KillProcess, the ObjectWatcher may still be
+    // watching.  the process handle, so make sure it has stopped.
+    watcher_.StopWatching();
 
     CloseHandle(process_);
     process_ = NULL;
@@ -72,6 +70,8 @@ class TimerExpiredTask : public Task, public MessageLoop::Watcher {
   // The process that we are watching.
   ProcessHandle process_;
 
+  base::ObjectWatcher watcher_;
+
   DISALLOW_EVIL_CONSTRUCTORS(TimerExpiredTask);
 };
 
-- 
cgit v1.1