[PeerConnection] Implement asynchronous version of AddIceCandidate().

This is the same as the existing version, except it uses the Operations
Chain. As such, if an asynchronous operation that uses the chain is
currently pending, such as CreateOffer() or CreateAnswer(),
AddIceCandidate() will not happen until the previous operation
completes.

Bug: chromium:1019222
Change-Id: Ie6e5fc386fa9c29b5e2f8e3f65bfbaf9837d351c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/158741
Commit-Queue: Henrik Boström <hbos@webrtc.org>
Reviewed-by: Steve Anton <steveanton@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29704}
diff --git a/rtc_base/operations_chain.h b/rtc_base/operations_chain.h
index 94ff57b..b6ec46e 100644
--- a/rtc_base/operations_chain.h
+++ b/rtc_base/operations_chain.h
@@ -56,7 +56,14 @@
 #ifdef RTC_DCHECK_IS_ON
     has_run_ = true;
 #endif  // RTC_DCHECK_IS_ON
-    functor_(std::move(callback_));
+    // The functor being executed may invoke the callback synchronously,
+    // marking the operation as complete. As such, |this| OperationWithFunctor
+    // object may get deleted here, including destroying |functor_|. To
+    // protect the functor from self-destruction while running, it is moved to
+    // a local variable.
+    auto functor = std::move(functor_);
+    functor(std::move(callback_));
+    // |this| may now be deleted; don't touch any member variables.
   }
 
  private: