Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3530)

Unified Diff: webrtc/p2p/base/stun.cc

Issue 2757893003: Add MakeUnique from chromium and change StunMessage::AddAttribute to take a unique_ptr. (Closed)
Patch Set: add ptr_util.h to rtc_base_approved build target Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webrtc/p2p/base/stun.h ('k') | webrtc/p2p/base/stun_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/p2p/base/stun.cc
diff --git a/webrtc/p2p/base/stun.cc b/webrtc/p2p/base/stun.cc
index af870d12d5975c9d7330daf92e5b1cb39541c048..4d6b21ba5465b0769f847aca5d984fd80166c581 100644
--- a/webrtc/p2p/base/stun.cc
+++ b/webrtc/p2p/base/stun.cc
@@ -19,6 +19,7 @@
#include "webrtc/base/crc32.h"
#include "webrtc/base/logging.h"
#include "webrtc/base/messagedigest.h"
+#include "webrtc/base/ptr_util.h"
#include "webrtc/base/stringencode.h"
using rtc::ByteBufferReader;
@@ -66,17 +67,18 @@ bool StunMessage::SetTransactionID(const std::string& str) {
return true;
}
-void StunMessage::AddAttribute(StunAttribute* attr) {
+void StunMessage::AddAttribute(std::unique_ptr<StunAttribute> attr) {
// Fail any attributes that aren't valid for this type of message.
RTC_DCHECK_EQ(attr->value_type(), GetAttributeValueType(attr->type()));
- attrs_.emplace_back(attr);
attr->SetOwner(this);
size_t attr_length = attr->length();
if (attr_length % 4 != 0) {
attr_length += (4 - (attr_length % 4));
}
length_ += static_cast<uint16_t>(attr_length + 4);
+
+ attrs_.push_back(std::move(attr));
}
const StunAddressAttribute* StunMessage::GetAddress(int type) const {
@@ -207,10 +209,10 @@ bool StunMessage::AddMessageIntegrity(const char* key,
size_t keylen) {
// Add the attribute with a dummy value. Since this is a known attribute, it
// can't fail.
- StunByteStringAttribute* msg_integrity_attr =
- new StunByteStringAttribute(STUN_ATTR_MESSAGE_INTEGRITY,
- std::string(kStunMessageIntegritySize, '0'));
- AddAttribute(msg_integrity_attr);
+ auto msg_integrity_attr_ptr = rtc::MakeUnique<StunByteStringAttribute>(
+ STUN_ATTR_MESSAGE_INTEGRITY, std::string(kStunMessageIntegritySize, '0'));
+ auto* msg_integrity_attr = msg_integrity_attr_ptr.get();
+ AddAttribute(std::move(msg_integrity_attr_ptr));
// Calculate the HMAC for the message.
ByteBufferWriter buf;
@@ -269,9 +271,10 @@ bool StunMessage::ValidateFingerprint(const char* data, size_t size) {
bool StunMessage::AddFingerprint() {
// Add the attribute with a dummy value. Since this is a known attribute,
// it can't fail.
- StunUInt32Attribute* fingerprint_attr =
- new StunUInt32Attribute(STUN_ATTR_FINGERPRINT, 0);
- AddAttribute(fingerprint_attr);
+ auto fingerprint_attr_ptr =
+ rtc::MakeUnique<StunUInt32Attribute>(STUN_ATTR_FINGERPRINT, 0);
+ auto fingerprint_attr = fingerprint_attr_ptr.get();
+ AddAttribute(std::move(fingerprint_attr_ptr));
// Calculate the CRC-32 for the message and insert it.
ByteBufferWriter buf;
@@ -453,33 +456,40 @@ StunAttribute* StunAttribute::Create(StunAttributeValueType value_type,
}
}
-StunAddressAttribute* StunAttribute::CreateAddress(uint16_t type) {
- return new StunAddressAttribute(type, 0);
+std::unique_ptr<StunAddressAttribute> StunAttribute::CreateAddress(
+ uint16_t type) {
+ return rtc::MakeUnique<StunAddressAttribute>(type, 0);
}
-StunXorAddressAttribute* StunAttribute::CreateXorAddress(uint16_t type) {
- return new StunXorAddressAttribute(type, 0, NULL);
+std::unique_ptr<StunXorAddressAttribute> StunAttribute::CreateXorAddress(
+ uint16_t type) {
+ return rtc::MakeUnique<StunXorAddressAttribute>(type, 0, nullptr);
}
-StunUInt64Attribute* StunAttribute::CreateUInt64(uint16_t type) {
- return new StunUInt64Attribute(type);
+std::unique_ptr<StunUInt64Attribute> StunAttribute::CreateUInt64(
+ uint16_t type) {
+ return rtc::MakeUnique<StunUInt64Attribute>(type);
}
-StunUInt32Attribute* StunAttribute::CreateUInt32(uint16_t type) {
- return new StunUInt32Attribute(type);
+std::unique_ptr<StunUInt32Attribute> StunAttribute::CreateUInt32(
+ uint16_t type) {
+ return rtc::MakeUnique<StunUInt32Attribute>(type);
}
-StunByteStringAttribute* StunAttribute::CreateByteString(uint16_t type) {
- return new StunByteStringAttribute(type, 0);
+std::unique_ptr<StunByteStringAttribute> StunAttribute::CreateByteString(
+ uint16_t type) {
+ return rtc::MakeUnique<StunByteStringAttribute>(type, 0);
}
-StunErrorCodeAttribute* StunAttribute::CreateErrorCode() {
- return new StunErrorCodeAttribute(
+std::unique_ptr<StunErrorCodeAttribute> StunAttribute::CreateErrorCode() {
+ return rtc::MakeUnique<StunErrorCodeAttribute>(
STUN_ATTR_ERROR_CODE, StunErrorCodeAttribute::MIN_SIZE);
}
-StunUInt16ListAttribute* StunAttribute::CreateUnknownAttributes() {
- return new StunUInt16ListAttribute(STUN_ATTR_UNKNOWN_ATTRIBUTES, 0);
+std::unique_ptr<StunUInt16ListAttribute>
+StunAttribute::CreateUnknownAttributes() {
+ return rtc::MakeUnique<StunUInt16ListAttribute>(STUN_ATTR_UNKNOWN_ATTRIBUTES,
+ 0);
}
StunAddressAttribute::StunAddressAttribute(uint16_t type,
@@ -756,6 +766,8 @@ void StunByteStringAttribute::SetBytes(char* bytes, size_t length) {
SetLength(static_cast<uint16_t>(length));
}
+const uint16_t StunErrorCodeAttribute::MIN_SIZE = 4;
+
StunErrorCodeAttribute::StunErrorCodeAttribute(uint16_t type,
int code,
const std::string& reason)
« no previous file with comments | « webrtc/p2p/base/stun.h ('k') | webrtc/p2p/base/stun_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698