From 8e967dfdfc371771cbc8ad6ece1a2bf8045b89d9 Mon Sep 17 00:00:00 2001
From: Steve Anton <steveanton@webrtc.org>
Date: Tue, 30 Jul 2019 18:07:40 -0700
Subject: [PATCH] Use unique_ptr in JsepCandidateCollection

Bug: None
Change-Id: I80ffacf3a355879b56a03b5cb59bffa32114dac1
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/147601
Commit-Queue: Steve Anton <steveanton@webrtc.org>
Reviewed-by: Seth Hampson <shampson@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28712}
---
 api/BUILD.gn              |  1 +
 api/jsep_ice_candidate.cc | 38 ++++++++++++++------------------------
 api/jsep_ice_candidate.h  |  4 ++--
 3 files changed, 17 insertions(+), 26 deletions(-)

diff --git a/api/BUILD.gn b/api/BUILD.gn
index ca2b5cb8ae..5c681bc15f 100644
--- a/api/BUILD.gn
+++ b/api/BUILD.gn
@@ -183,6 +183,7 @@ rtc_static_library("libjingle_peerconnection_api") {
     "video:video_frame",
     "video:video_rtp_headers",
     "//third_party/abseil-cpp/absl/algorithm:container",
+    "//third_party/abseil-cpp/absl/memory",
     "//third_party/abseil-cpp/absl/strings",
     "//third_party/abseil-cpp/absl/types:optional",
 
diff --git a/api/jsep_ice_candidate.cc b/api/jsep_ice_candidate.cc
index 33283868f6..e18e6e28af 100644
--- a/api/jsep_ice_candidate.cc
+++ b/api/jsep_ice_candidate.cc
@@ -10,9 +10,11 @@
 
 #include "api/jsep_ice_candidate.h"
 
+#include <memory>
 #include <utility>
 
 #include "absl/algorithm/container.h"
+#include "absl/memory/memory.h"
 
 namespace webrtc {
 
@@ -42,41 +44,29 @@ size_t JsepCandidateCollection::count() const {
 }
 
 void JsepCandidateCollection::add(JsepIceCandidate* candidate) {
-  candidates_.push_back(candidate);
+  candidates_.push_back(absl::WrapUnique(candidate));
 }
 
 const IceCandidateInterface* JsepCandidateCollection::at(size_t index) const {
-  return candidates_[index];
-}
-
-JsepCandidateCollection::~JsepCandidateCollection() {
-  for (std::vector<JsepIceCandidate*>::iterator it = candidates_.begin();
-       it != candidates_.end(); ++it) {
-    delete *it;
-  }
+  return candidates_[index].get();
 }
 
 bool JsepCandidateCollection::HasCandidate(
     const IceCandidateInterface* candidate) const {
-  bool ret = false;
-  for (std::vector<JsepIceCandidate*>::const_iterator it = candidates_.begin();
-       it != candidates_.end(); ++it) {
-    if ((*it)->sdp_mid() == candidate->sdp_mid() &&
-        (*it)->sdp_mline_index() == candidate->sdp_mline_index() &&
-        (*it)->candidate().IsEquivalent(candidate->candidate())) {
-      ret = true;
-      break;
-    }
-  }
-  return ret;
+  return absl::c_any_of(
+      candidates_, [&](const std::unique_ptr<JsepIceCandidate>& entry) {
+        return entry->sdp_mid() == candidate->sdp_mid() &&
+               entry->sdp_mline_index() == candidate->sdp_mline_index() &&
+               entry->candidate().IsEquivalent(candidate->candidate());
+      });
 }
 
 size_t JsepCandidateCollection::remove(const cricket::Candidate& candidate) {
-  auto iter = absl::c_find_if(candidates_, [&](JsepIceCandidate* c) {
-    return candidate.MatchesForRemoval(c->candidate());
-  });
+  auto iter = absl::c_find_if(
+      candidates_, [&](const std::unique_ptr<JsepIceCandidate>& c) {
+        return candidate.MatchesForRemoval(c->candidate());
+      });
   if (iter != candidates_.end()) {
-    delete *iter;
     candidates_.erase(iter);
     return 1;
   }
diff --git a/api/jsep_ice_candidate.h b/api/jsep_ice_candidate.h
index 651aa7c658..4ee84cf79c 100644
--- a/api/jsep_ice_candidate.h
+++ b/api/jsep_ice_candidate.h
@@ -16,6 +16,7 @@
 
 #include <stddef.h>
 
+#include <memory>
 #include <string>
 #include <vector>
 
@@ -63,7 +64,6 @@ class JsepCandidateCollection : public IceCandidateCollection {
   // Move constructor is defined so that a vector of JsepCandidateCollections
   // can be resized.
   JsepCandidateCollection(JsepCandidateCollection&& o);
-  ~JsepCandidateCollection() override;
   size_t count() const override;
   bool HasCandidate(const IceCandidateInterface* candidate) const override;
   // Adds and takes ownership of the JsepIceCandidate.
@@ -77,7 +77,7 @@ class JsepCandidateCollection : public IceCandidateCollection {
   size_t remove(const cricket::Candidate& candidate);
 
  private:
-  std::vector<JsepIceCandidate*> candidates_;
+  std::vector<std::unique_ptr<JsepIceCandidate>> candidates_;
 
   RTC_DISALLOW_COPY_AND_ASSIGN(JsepCandidateCollection);
 };