Fix the sigslot type of DtlsIdentityStore::WorkerTask.

BUG=4516
R=pthatcher@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/49619004

Cr-Commit-Position: refs/heads/master@{#8954}
diff --git a/talk/app/webrtc/dtlsidentitystore.cc b/talk/app/webrtc/dtlsidentitystore.cc
index 82285da..dd9bc5d 100644
--- a/talk/app/webrtc/dtlsidentitystore.cc
+++ b/talk/app/webrtc/dtlsidentitystore.cc
@@ -38,6 +38,7 @@
 namespace {
 
 enum {
+  MSG_DESTROY,
   MSG_GENERATE_IDENTITY,
   MSG_GENERATE_IDENTITY_RESULT,
   MSG_RETURN_FREE_IDENTITY
@@ -53,11 +54,12 @@
 class DtlsIdentityStore::WorkerTask : public sigslot::has_slots<>,
                                       public rtc::MessageHandler {
  public:
-  explicit WorkerTask(DtlsIdentityStore* store) : store_(store) {
+  explicit WorkerTask(DtlsIdentityStore* store)
+      : signaling_thread_(rtc::Thread::Current()), store_(store) {
     store_->SignalDestroyed.connect(this, &WorkerTask::OnStoreDestroyed);
   };
 
-  virtual ~WorkerTask() {}
+  virtual ~WorkerTask() { DCHECK(rtc::Thread::Current() == signaling_thread_); }
 
   void GenerateIdentity() {
     rtc::scoped_ptr<rtc::SSLIdentity> identity(
@@ -72,10 +74,20 @@
   }
 
   void OnMessage(rtc::Message* msg) override {
-    DCHECK(msg->message_id == MSG_GENERATE_IDENTITY);
-    GenerateIdentity();
-    // Deleting msg->pdata will destroy the WorkerTask.
-    delete msg->pdata;
+    switch (msg->message_id) {
+      case MSG_GENERATE_IDENTITY:
+        GenerateIdentity();
+
+        // Must delete |this|, owned by msg->pdata, on the signaling thread to
+        // avoid races on disconnecting the signal.
+        signaling_thread_->Post(this, MSG_DESTROY, msg->pdata);
+        break;
+      case MSG_DESTROY:
+        delete msg->pdata;
+        break;
+      default:
+        CHECK(false) << "Unexpected message type";
+    }
   }
 
  private:
@@ -84,6 +96,7 @@
     store_ = NULL;
   }
 
+  rtc::Thread* signaling_thread_;
   rtc::CriticalSection cs_;
   DtlsIdentityStore* store_;
 };
diff --git a/talk/app/webrtc/dtlsidentitystore.h b/talk/app/webrtc/dtlsidentitystore.h
index ffe8067..1ceaa82 100644
--- a/talk/app/webrtc/dtlsidentitystore.h
+++ b/talk/app/webrtc/dtlsidentitystore.h
@@ -68,7 +68,7 @@
   bool HasFreeIdentityForTesting() const;
 
  private:
-  sigslot::signal0<sigslot::multi_threaded_local> SignalDestroyed;
+  sigslot::signal0<> SignalDestroyed;
   class WorkerTask;
   typedef rtc::ScopedMessageData<DtlsIdentityStore::WorkerTask>
       IdentityTaskMessageData;