Skip to content

Commit

Permalink
Use scoped_ptr<>.Pass() to pass ownership in the remoting protocol code.
Browse files Browse the repository at this point in the history
Review URL: https://chromiumcodereview.appspot.com/9240033

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@118407 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
sergeyu@chromium.org committed Jan 20, 2012
1 parent 0bc93ad commit 5bf5231
Show file tree
Hide file tree
Showing 41 changed files with 257 additions and 249 deletions.
5 changes: 3 additions & 2 deletions remoting/protocol/authenticator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ bool Authenticator::IsAuthenticatorMessage(const buzz::XmlElement* message) {
}

// static
buzz::XmlElement* Authenticator::CreateEmptyAuthenticatorMessage() {
return new buzz::XmlElement(kAuthenticationQName);
scoped_ptr<buzz::XmlElement> Authenticator::CreateEmptyAuthenticatorMessage() {
return scoped_ptr<buzz::XmlElement>(
new buzz::XmlElement(kAuthenticationQName));
}

// static
Expand Down
16 changes: 9 additions & 7 deletions remoting/protocol/authenticator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <string>

#include "base/memory/scoped_ptr.h"

namespace buzz {
class XmlElement;
} // namespace buzz
Expand Down Expand Up @@ -57,7 +59,7 @@ class Authenticator {
static bool IsAuthenticatorMessage(const buzz::XmlElement* message);

// Creates an empty Authenticator message, owned by the caller.
static buzz::XmlElement* CreateEmptyAuthenticatorMessage();
static scoped_ptr<buzz::XmlElement> CreateEmptyAuthenticatorMessage();

// Finds Authenticator message among child elements of |message|, or
// returns NULL otherwise.
Expand All @@ -77,12 +79,12 @@ class Authenticator {

// Must be called when in MESSAGE_READY state. Returns next
// authentication message that needs to be sent to the peer.
virtual buzz::XmlElement* GetNextMessage() = 0;
virtual scoped_ptr<buzz::XmlElement> GetNextMessage() = 0;

// Creates new authenticator for a channel. Caller must take
// ownership of the result. Can be called only in the ACCEPTED
// state.
virtual ChannelAuthenticator* CreateChannelAuthenticator() const = 0;
// Creates new authenticator for a channel. Can be called only in
// the ACCEPTED state.
virtual scoped_ptr<ChannelAuthenticator>
CreateChannelAuthenticator() const = 0;
};

// Factory for Authenticator instances.
Expand All @@ -99,7 +101,7 @@ class AuthenticatorFactory {
// if the |first_message| is invalid and the session should be
// rejected. ProcessMessage() should be called with |first_message|
// for the result of this method.
virtual Authenticator* CreateAuthenticator(
virtual scoped_ptr<Authenticator> CreateAuthenticator(
const std::string& remote_jid,
const buzz::XmlElement* first_message) = 0;
};
Expand Down
12 changes: 6 additions & 6 deletions remoting/protocol/connection_to_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ void ConnectionToHost::OnSessionManagerReady() {
DCHECK(message_loop_->BelongsToCurrentThread());

// After SessionManager is initialized we can try to connect to the host.
CandidateSessionConfig* candidate_config =
scoped_ptr<CandidateSessionConfig> candidate_config =
CandidateSessionConfig::CreateDefault();
V1ClientAuthenticator* authenticator =
new V1ClientAuthenticator(signal_strategy_->GetLocalJid(), access_code_);
session_.reset(session_manager_->Connect(
host_jid_, authenticator, candidate_config,
scoped_ptr<Authenticator> authenticator(
new V1ClientAuthenticator(signal_strategy_->GetLocalJid(), access_code_));
session_ = session_manager_->Connect(
host_jid_, authenticator.Pass(), candidate_config.Pass(),
base::Bind(&ConnectionToHost::OnSessionStateChange,
base::Unretained(this))));
base::Unretained(this)));
}

void ConnectionToHost::OnIncomingSession(
Expand Down
11 changes: 5 additions & 6 deletions remoting/protocol/content_description.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ bool ParseChannelConfig(const XmlElement* element, bool codec_required,
} // namespace

ContentDescription::ContentDescription(
const CandidateSessionConfig* candidate_config,
const buzz::XmlElement* authenticator_message)
: candidate_config_(candidate_config),
authenticator_message_(authenticator_message) {
scoped_ptr<CandidateSessionConfig> config,
scoped_ptr<buzz::XmlElement> authenticator_message)
: candidate_config_(config.Pass()),
authenticator_message_(authenticator_message.Pass()) {
}

ContentDescription::~ContentDescription() { }
Expand Down Expand Up @@ -247,8 +247,7 @@ ContentDescription* ContentDescription::ParseXml(
if (child)
authenticator_message.reset(new XmlElement(*child));

return new ContentDescription(
config.release(), authenticator_message.release());
return new ContentDescription(config.Pass(), authenticator_message.Pass());
}
LOG(ERROR) << "Invalid description: " << element->Str();
return NULL;
Expand Down
5 changes: 2 additions & 3 deletions remoting/protocol/content_description.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ class ContentDescription : public cricket::ContentDescription {
public:
static const char kChromotingContentName[];

// Takes ownership of |config| and |authenticator_message|.
ContentDescription(const CandidateSessionConfig* config,
const buzz::XmlElement* authenticator_message);
ContentDescription(scoped_ptr<CandidateSessionConfig> config,
scoped_ptr<buzz::XmlElement> authenticator_message);
virtual ~ContentDescription();

const CandidateSessionConfig* config() const {
Expand Down
19 changes: 10 additions & 9 deletions remoting/protocol/fake_authenticator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,25 @@ void FakeAuthenticator::ProcessMessage(const buzz::XmlElement* message) {
++messages_;
}

buzz::XmlElement* FakeAuthenticator::GetNextMessage() {
scoped_ptr<buzz::XmlElement> FakeAuthenticator::GetNextMessage() {
EXPECT_EQ(MESSAGE_READY, state());

buzz::XmlElement* result = new buzz::XmlElement(
buzz::QName(kChromotingXmlNamespace, "authentication"));
scoped_ptr<buzz::XmlElement> result(new buzz::XmlElement(
buzz::QName(kChromotingXmlNamespace, "authentication")));
buzz::XmlElement* id = new buzz::XmlElement(
buzz::QName(kChromotingXmlNamespace, "id"));
id->AddText(base::IntToString(messages_));
result->AddElement(id);

++messages_;
return result;
return result.Pass();
}

ChannelAuthenticator*
scoped_ptr<ChannelAuthenticator>
FakeAuthenticator::CreateChannelAuthenticator() const {
EXPECT_EQ(ACCEPTED, state());
return new FakeChannelAuthenticator(action_ != REJECT_CHANNEL, async_);
return scoped_ptr<ChannelAuthenticator>(
new FakeChannelAuthenticator(action_ != REJECT_CHANNEL, async_));
}

FakeHostAuthenticatorFactory::FakeHostAuthenticatorFactory(
Expand All @@ -126,11 +127,11 @@ FakeHostAuthenticatorFactory::FakeHostAuthenticatorFactory(
FakeHostAuthenticatorFactory::~FakeHostAuthenticatorFactory() {
}

Authenticator* FakeHostAuthenticatorFactory::CreateAuthenticator(
scoped_ptr<Authenticator> FakeHostAuthenticatorFactory::CreateAuthenticator(
const std::string& remote_jid,
const buzz::XmlElement* first_message) {
return new FakeAuthenticator(FakeAuthenticator::HOST, round_trips_,
action_, async_);
return scoped_ptr<Authenticator>(new FakeAuthenticator(
FakeAuthenticator::HOST, round_trips_, action_, async_));
}

} // namespace protocol
Expand Down
7 changes: 4 additions & 3 deletions remoting/protocol/fake_authenticator.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ class FakeAuthenticator : public Authenticator {
// Authenticator interface.
virtual State state() const OVERRIDE;
virtual void ProcessMessage(const buzz::XmlElement* message) OVERRIDE;
virtual buzz::XmlElement* GetNextMessage() OVERRIDE;
virtual ChannelAuthenticator* CreateChannelAuthenticator() const OVERRIDE;
virtual scoped_ptr<buzz::XmlElement> GetNextMessage() OVERRIDE;
virtual scoped_ptr<ChannelAuthenticator>
CreateChannelAuthenticator() const OVERRIDE;

protected:
Type type_;
Expand All @@ -76,7 +77,7 @@ class FakeHostAuthenticatorFactory : public AuthenticatorFactory {
virtual ~FakeHostAuthenticatorFactory();

// AuthenticatorFactory interface.
virtual Authenticator* CreateAuthenticator(
virtual scoped_ptr<Authenticator> CreateAuthenticator(
const std::string& remote_jid,
const buzz::XmlElement* first_message) OVERRIDE;

Expand Down
6 changes: 3 additions & 3 deletions remoting/protocol/it2me_host_authenticator_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ It2MeHostAuthenticatorFactory::It2MeHostAuthenticatorFactory(
It2MeHostAuthenticatorFactory::~It2MeHostAuthenticatorFactory() {
}

Authenticator* It2MeHostAuthenticatorFactory::CreateAuthenticator(
scoped_ptr<Authenticator> It2MeHostAuthenticatorFactory::CreateAuthenticator(
const std::string& remote_jid,
const buzz::XmlElement* first_message) {
// TODO(sergeyu): V2 authenticator is not finished yet. Enable it
Expand All @@ -35,8 +35,8 @@ Authenticator* It2MeHostAuthenticatorFactory::CreateAuthenticator(
// local_cert_, *local_private_key_, shared_secret_);
// }

return new V1HostAuthenticator(local_cert_, *local_private_key_,
shared_secret_, remote_jid);
return scoped_ptr<Authenticator>(new V1HostAuthenticator(
local_cert_, *local_private_key_, shared_secret_, remote_jid));
}

} // namespace protocol
Expand Down
2 changes: 1 addition & 1 deletion remoting/protocol/it2me_host_authenticator_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class It2MeHostAuthenticatorFactory : public AuthenticatorFactory {
virtual ~It2MeHostAuthenticatorFactory();

// AuthenticatorFactory interface.
virtual Authenticator* CreateAuthenticator(
virtual scoped_ptr<Authenticator> CreateAuthenticator(
const std::string& remote_jid,
const buzz::XmlElement* first_message) OVERRIDE;

Expand Down
5 changes: 2 additions & 3 deletions remoting/protocol/jingle_channel_connector.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ class JingleChannelConnector : public base::NonThreadSafe {
JingleChannelConnector() { }
virtual ~JingleChannelConnector() { }

// Starts the connection process for the channel. Takes ownership of
// |authenticator|.
virtual void Connect(ChannelAuthenticator* authenticator,
// Starts the connection process for the channel.
virtual void Connect(scoped_ptr<ChannelAuthenticator> authenticator,
cricket::TransportChannel* raw_channel) = 0;

protected:
Expand Down
4 changes: 2 additions & 2 deletions remoting/protocol/jingle_datagram_connector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ JingleDatagramConnector::~JingleDatagramConnector() {
}

void JingleDatagramConnector::Connect(
ChannelAuthenticator* authenticator,
scoped_ptr<ChannelAuthenticator> authenticator,
cricket::TransportChannel* raw_channel) {
DCHECK(CalledOnValidThread());

authenticator_.reset(authenticator);
authenticator_ = authenticator.Pass();

net::Socket* socket =
new jingle_glue::TransportChannelSocketAdapter(raw_channel);
Expand Down
2 changes: 1 addition & 1 deletion remoting/protocol/jingle_datagram_connector.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class JingleDatagramConnector : public JingleChannelConnector {
// TODO(sergeyu): In the current implementation ChannelAuthenticator
// cannot be used for datagram channels, so needs to be either
// extended or replaced with something else here.
virtual void Connect(ChannelAuthenticator* authenticator,
virtual void Connect(scoped_ptr<ChannelAuthenticator> authenticator,
cricket::TransportChannel* raw_channel) OVERRIDE;

private:
Expand Down
50 changes: 27 additions & 23 deletions remoting/protocol/jingle_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ namespace protocol {
JingleSession::JingleSession(
JingleSessionManager* jingle_session_manager,
cricket::Session* cricket_session,
Authenticator* authenticator)
scoped_ptr<Authenticator> authenticator)
: jingle_session_manager_(jingle_session_manager),
authenticator_(authenticator),
authenticator_(authenticator.Pass()),
state_(INITIALIZING),
error_(OK),
closing_(false),
Expand All @@ -62,8 +62,9 @@ JingleSession::~JingleSession() {
void JingleSession::SendSessionInitiate() {
DCHECK_EQ(authenticator_->state(), Authenticator::MESSAGE_READY);
cricket_session_->Initiate(
jid_, CreateSessionDescription(candidate_config()->Clone(),
authenticator_->GetNextMessage()));
jid_, CreateSessionDescription(
candidate_config()->Clone(),
authenticator_->GetNextMessage()).release());
}

void JingleSession::CloseInternal(int result, Error error) {
Expand Down Expand Up @@ -172,11 +173,11 @@ const CandidateSessionConfig* JingleSession::candidate_config() {
}

void JingleSession::set_candidate_config(
const CandidateSessionConfig* candidate_config) {
scoped_ptr<CandidateSessionConfig> candidate_config) {
DCHECK(CalledOnValidThread());
DCHECK(!candidate_config_.get());
DCHECK(candidate_config);
candidate_config_.reset(candidate_config);
DCHECK(candidate_config.get());
candidate_config_ = candidate_config.Pass();
}

const SessionConfig& JingleSession::config() {
Expand Down Expand Up @@ -384,7 +385,7 @@ void JingleSession::AcceptConnection() {
CHECK(content);
const ContentDescription* content_description =
static_cast<const ContentDescription*>(content->description);
candidate_config_.reset(content_description->config()->Clone());
candidate_config_ = content_description->config()->Clone();

SessionManager::IncomingSessionResponse response =
jingle_session_manager_->AcceptConnection(this);
Expand All @@ -411,8 +412,8 @@ void JingleSession::AcceptConnection() {
return;
}

authenticator_.reset(
jingle_session_manager_->CreateAuthenticator(jid(), auth_message));
authenticator_ =
jingle_session_manager_->CreateAuthenticator(jid(), auth_message);
if (!authenticator_.get()) {
CloseInternal(net::ERR_CONNECTION_FAILED, INCOMPATIBLE_PROTOCOL);
return;
Expand All @@ -426,24 +427,26 @@ void JingleSession::AcceptConnection() {
}

// Connection must be configured by the AcceptConnection() callback.
CandidateSessionConfig* candidate_config =
scoped_ptr<CandidateSessionConfig> candidate_config =
CandidateSessionConfig::CreateFrom(config());

buzz::XmlElement* auth_reply = NULL;
scoped_ptr<buzz::XmlElement> auth_reply;
if (authenticator_->state() == Authenticator::MESSAGE_READY)
auth_reply = authenticator_->GetNextMessage();
DCHECK_NE(authenticator_->state(), Authenticator::MESSAGE_READY);
cricket_session_->Accept(
CreateSessionDescription(candidate_config, auth_reply));
CreateSessionDescription(candidate_config.Pass(),
auth_reply.Pass()).release());
}

void JingleSession::ProcessAuthenticationStep() {
DCHECK_EQ(state_, CONNECTED);

if (authenticator_->state() == Authenticator::MESSAGE_READY) {
buzz::XmlElement* auth_message = authenticator_->GetNextMessage();
scoped_ptr<buzz::XmlElement> auth_message =
authenticator_->GetNextMessage();
cricket::XmlElements message;
message.push_back(auth_message);
message.push_back(auth_message.release());
cricket_session_->SendInfoMessage(message);
}
DCHECK_NE(authenticator_->state(), Authenticator::MESSAGE_READY);
Expand Down Expand Up @@ -471,9 +474,9 @@ void JingleSession::AddChannelConnector(
}

channel_connectors_[name] = connector;
ChannelAuthenticator* authenticator =
scoped_ptr<ChannelAuthenticator> authenticator =
authenticator_->CreateChannelAuthenticator();
connector->Connect(authenticator, raw_channel);
connector->Connect(authenticator.Pass(), raw_channel);

// Workaround bug in libjingle - it doesn't connect channels if they
// are created after the session is accepted. See crbug.com/89384.
Expand Down Expand Up @@ -517,14 +520,15 @@ void JingleSession::SetState(State new_state) {
}

// static
cricket::SessionDescription* JingleSession::CreateSessionDescription(
const CandidateSessionConfig* config,
const buzz::XmlElement* authenticator_message) {
cricket::SessionDescription* desc = new cricket::SessionDescription();
scoped_ptr<cricket::SessionDescription> JingleSession::CreateSessionDescription(
scoped_ptr<CandidateSessionConfig> config,
scoped_ptr<buzz::XmlElement> authenticator_message) {
scoped_ptr<cricket::SessionDescription> desc(
new cricket::SessionDescription());
desc->AddContent(
ContentDescription::kChromotingContentName, kChromotingXmlNamespace,
new ContentDescription(config, authenticator_message));
return desc;
new ContentDescription(config.Pass(), authenticator_message.Pass()));
return desc.Pass();
}

} // namespace protocol
Expand Down
Loading

0 comments on commit 5bf5231

Please sign in to comment.