Preventive clean-up: ensure RTCPeerConnection stays valid when calling postTask
https://bugs.webkit.org/show_bug.cgi?id=169661

Patch by Youenn Fablet <youenn@apple.com> on 2017-03-15
Reviewed by Alex Christensen.

Source/WebCore:

Protecting the RTCPeerConnection object when calling postTask since it might get collected between the task post
and task run. Also do not send negotiationNeeded event if RTCPeerConnection is closed (covered by added test).

* Modules/mediastream/RTCPeerConnection.cpp:
(WebCore::RTCPeerConnection::updateIceGatheringState):
(WebCore::RTCPeerConnection::updateIceConnectionState):
(WebCore::RTCPeerConnection::scheduleNegotiationNeededEvent):

LayoutTests:

* webrtc/negotiatedneeded-event-addStream-expected.txt:
* webrtc/negotiatedneeded-event-addStream.html:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@213982 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/webrtc/negotiatedneeded-event-addStream.html b/LayoutTests/webrtc/negotiatedneeded-event-addStream.html
index 4f92a22..6ea66b9 100644
--- a/LayoutTests/webrtc/negotiatedneeded-event-addStream.html
+++ b/LayoutTests/webrtc/negotiatedneeded-event-addStream.html
@@ -26,6 +26,23 @@
         });
     });
 }, "on negotiation needed in case of adding a stream");
+
+promise_test((test) => {
+    if (window.testRunner)
+        testRunner.setUserMediaPermission(true);
+
+    return navigator.mediaDevices.getUserMedia({ video: true}).then((stream) => {
+        return new Promise((resolve, reject) => {
+            if (window.internals)
+                internals.useMockRTCPeerConnectionFactory("OneRealPeerConnection");
+            var pc = new RTCPeerConnection();
+            pc.onnegotiationneeded = () => { reject(); };
+            pc.addStream(stream);
+            pc.close();
+            setTimeout(resolve, 500);
+        });
+    });
+}, "on negotiation needed not called if pc is closed");
         </script>
     </body>
 </html>