diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-04 01:00:49 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-04 01:00:49 +0000 |
commit | b39e182dca78d0c165ba75bb04878f4eb71b3625 (patch) | |
tree | c8f3d4f74b05cdb50b4d5244620595a758d8a996 /remoting/host | |
parent | dc01874febf63d62bd05178f168f5459fec05372 (diff) | |
download | chromium_src-b39e182dca78d0c165ba75bb04878f4eb71b3625.zip chromium_src-b39e182dca78d0c165ba75bb04878f4eb71b3625.tar.gz chromium_src-b39e182dca78d0c165ba75bb04878f4eb71b3625.tar.bz2 |
Refactor IqRequest.
Remove CreateIqRequest from SignalStrategy interface. Intead to send an Iq
stanza the new IqSender now need to be used. IqSender creats of IqRequest
objects and handling iq responses.
BUG=None
TEST=Unittests.
Review URL: http://codereview.chromium.org/8432009
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@108606 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/host')
-rw-r--r-- | remoting/host/heartbeat_sender.cc | 16 | ||||
-rw-r--r-- | remoting/host/heartbeat_sender.h | 11 | ||||
-rw-r--r-- | remoting/host/heartbeat_sender_unittest.cc | 30 | ||||
-rw-r--r-- | remoting/host/register_support_host_request.cc | 19 | ||||
-rw-r--r-- | remoting/host/register_support_host_request.h | 2 | ||||
-rw-r--r-- | remoting/host/register_support_host_request_unittest.cc | 27 |
6 files changed, 60 insertions, 45 deletions
diff --git a/remoting/host/heartbeat_sender.cc b/remoting/host/heartbeat_sender.cc index ecd35ca..c4e3a23 100644 --- a/remoting/host/heartbeat_sender.cc +++ b/remoting/host/heartbeat_sender.cc @@ -11,7 +11,7 @@ #include "base/time.h" #include "remoting/base/constants.h" #include "remoting/host/host_config.h" -#include "remoting/jingle_glue/iq_request.h" +#include "remoting/jingle_glue/iq_sender.h" #include "remoting/jingle_glue/jingle_thread.h" #include "remoting/jingle_glue/signal_strategy.h" #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" @@ -72,9 +72,8 @@ void HeartbeatSender::OnSignallingConnected(SignalStrategy* signal_strategy, state_ = STARTED; full_jid_ = full_jid; - request_.reset(signal_strategy->CreateIqRequest()); - request_->set_callback(base::Bind(&HeartbeatSender::ProcessResponse, - base::Unretained(this))); + + iq_sender_.reset(new IqSender(signal_strategy)); DoSendStanza(); timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(interval_ms_), this, @@ -84,7 +83,8 @@ void HeartbeatSender::OnSignallingConnected(SignalStrategy* signal_strategy, void HeartbeatSender::OnSignallingDisconnected() { DCHECK(message_loop_->BelongsToCurrentThread()); state_ = STOPPED; - request_.reset(NULL); + request_.reset(); + iq_sender_.reset(); } // Ignore any notifications other than signalling @@ -99,8 +99,10 @@ void HeartbeatSender::DoSendStanza() { DCHECK_EQ(state_, STARTED); VLOG(1) << "Sending heartbeat stanza to " << kChromotingBotJid; - request_->SendIq(IqRequest::MakeIqStanza( - buzz::STR_SET, kChromotingBotJid, CreateHeartbeatMessage())); + request_.reset(iq_sender_->SendIq( + buzz::STR_SET, kChromotingBotJid, CreateHeartbeatMessage(), + base::Bind(&HeartbeatSender::ProcessResponse, + base::Unretained(this)))); } void HeartbeatSender::ProcessResponse(const XmlElement* response) { diff --git a/remoting/host/heartbeat_sender.h b/remoting/host/heartbeat_sender.h index 2100b67..582bc28 100644 --- a/remoting/host/heartbeat_sender.h +++ b/remoting/host/heartbeat_sender.h @@ -13,17 +13,21 @@ #include "base/timer.h" #include "remoting/host/host_key_pair.h" #include "remoting/host/host_status_observer.h" -#include "remoting/jingle_glue/iq_request.h" -#include "testing/gtest/include/gtest/gtest_prod.h" +#include "base/gtest_prod_util.h" namespace base { class MessageLoopProxy; } // namespace base +namespace buzz { +class XmlElement; +} // namespace buzz + namespace remoting { -class IqRequest; class HostKeyPair; +class IqRequest; +class IqSender; class MutableHostConfig; // HeartbeatSender periodically sends heartbeat stanzas to the Chromoting Bot. @@ -106,6 +110,7 @@ class HeartbeatSender : public HostStatusObserver { std::string host_id_; HostKeyPair key_pair_; std::string full_jid_; + scoped_ptr<IqSender> iq_sender_; scoped_ptr<IqRequest> request_; int interval_ms_; base::RepeatingTimer<HeartbeatSender> timer_; diff --git a/remoting/host/heartbeat_sender_unittest.cc b/remoting/host/heartbeat_sender_unittest.cc index 49933c2..4c3390a 100644 --- a/remoting/host/heartbeat_sender_unittest.cc +++ b/remoting/host/heartbeat_sender_unittest.cc @@ -2,16 +2,17 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "remoting/host/heartbeat_sender.h" + #include "base/memory/ref_counted.h" #include "base/message_loop.h" #include "base/message_loop_proxy.h" #include "base/string_number_conversions.h" #include "remoting/base/constants.h" -#include "remoting/host/heartbeat_sender.h" #include "remoting/host/host_key_pair.h" #include "remoting/host/in_memory_host_config.h" #include "remoting/host/test_key_pair.h" -#include "remoting/jingle_glue/iq_request.h" +#include "remoting/jingle_glue/iq_sender.h" #include "remoting/jingle_glue/mock_objects.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -35,6 +36,7 @@ namespace { const char kHostId[] = "0"; const char kTestJid[] = "user@gmail.com/chromoting123"; const int64 kTestTime = 123123123; +const char kStanzaId[] = "123"; } // namespace class HeartbeatSenderTest : public testing::Test { @@ -53,23 +55,19 @@ class HeartbeatSenderTest : public testing::Test { // Call Start() followed by Stop(), and makes sure an Iq stanza is // being sent. TEST_F(HeartbeatSenderTest, DoSendStanza) { - // |iq_request| is freed by HeartbeatSender. - MockIqRequest* iq_request = new MockIqRequest(); - iq_request->Init(); - - EXPECT_CALL(*iq_request, set_callback(_)).Times(1); + SignalStrategy::Listener* listener; + EXPECT_CALL(signal_strategy_, AddListener(NotNull())) + .WillOnce(SaveArg<0>(&listener)); scoped_ptr<HeartbeatSender> heartbeat_sender( - new HeartbeatSender(base::MessageLoopProxy::current(), - config_)); + new HeartbeatSender(base::MessageLoopProxy::current(), config_)); ASSERT_TRUE(heartbeat_sender->Init()); - EXPECT_CALL(signal_strategy_, CreateIqRequest()) - .WillOnce(Return(iq_request)); - XmlElement* sent_iq = NULL; - EXPECT_CALL(*iq_request, SendIq(NotNull())) - .WillOnce(SaveArg<0>(&sent_iq)); + EXPECT_CALL(signal_strategy_, GetNextId()) + .WillOnce(Return(kStanzaId)); + EXPECT_CALL(signal_strategy_, SendStanza(NotNull())) + .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true))); heartbeat_sender->OnSignallingConnected(&signal_strategy_, kTestJid); message_loop_.RunAllPending(); @@ -81,6 +79,8 @@ TEST_F(HeartbeatSenderTest, DoSendStanza) { std::string(kChromotingBotJid)); EXPECT_EQ(stanza->Attr(buzz::QName("", "type")), "set"); + EXPECT_CALL(signal_strategy_, RemoveListener(listener)); + heartbeat_sender->OnSignallingDisconnected(); message_loop_.RunAllPending(); } @@ -125,7 +125,7 @@ TEST_F(HeartbeatSenderTest, CreateHeartbeatMessage) { // Verify that ProcessResponse parses set-interval result. TEST_F(HeartbeatSenderTest, ProcessResponse) { - scoped_ptr<XmlElement> response(new XmlElement(QName("", "iq"))); + scoped_ptr<XmlElement> response(new XmlElement(buzz::QN_IQ)); response->AddAttr(QName("", "type"), "result"); XmlElement* result = new XmlElement( diff --git a/remoting/host/register_support_host_request.cc b/remoting/host/register_support_host_request.cc index 338513d..5974675 100644 --- a/remoting/host/register_support_host_request.cc +++ b/remoting/host/register_support_host_request.cc @@ -11,7 +11,7 @@ #include "base/time.h" #include "remoting/base/constants.h" #include "remoting/host/host_config.h" -#include "remoting/jingle_glue/iq_request.h" +#include "remoting/jingle_glue/iq_sender.h" #include "remoting/jingle_glue/jingle_thread.h" #include "remoting/jingle_glue/signal_strategy.h" #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" @@ -59,12 +59,11 @@ void RegisterSupportHostRequest::OnSignallingConnected( message_loop_ = MessageLoop::current(); - request_.reset(signal_strategy->CreateIqRequest()); - request_->set_callback(base::Bind( - &RegisterSupportHostRequest::ProcessResponse, base::Unretained(this))); - - request_->SendIq(IqRequest::MakeIqStanza( - buzz::STR_SET, kChromotingBotJid, CreateRegistrationRequest(jid))); + iq_sender_.reset(new IqSender(signal_strategy)); + request_.reset(iq_sender_->SendIq( + buzz::STR_SET, kChromotingBotJid, CreateRegistrationRequest(jid), + base::Bind(&RegisterSupportHostRequest::ProcessResponse, + base::Unretained(this)))); } void RegisterSupportHostRequest::OnSignallingDisconnected() { @@ -78,6 +77,7 @@ void RegisterSupportHostRequest::OnSignallingDisconnected() { } DCHECK_EQ(message_loop_, MessageLoop::current()); request_.reset(); + iq_sender_.reset(); } // Ignore any notifications other than signalling @@ -129,7 +129,10 @@ bool RegisterSupportHostRequest::ParseResponse(const XmlElement* response, } // This method must only be called for error or result stanzas. - DCHECK_EQ(std::string(buzz::STR_RESULT), type); + if (type != buzz::STR_RESULT) { + LOG(ERROR) << "Received unexpect stanza of type \"" << type << "\""; + return false; + } const XmlElement* result_element = response->FirstNamed(QName( kChromotingXmlNamespace, kRegisterQueryResultTag)); diff --git a/remoting/host/register_support_host_request.h b/remoting/host/register_support_host_request.h index 3462bcd..e7aee28 100644 --- a/remoting/host/register_support_host_request.h +++ b/remoting/host/register_support_host_request.h @@ -27,6 +27,7 @@ class TimeDelta; namespace remoting { class IqRequest; +class IqSender; class MutableHostConfig; // RegisterSupportHostRequest sends support host registeration request @@ -77,6 +78,7 @@ class RegisterSupportHostRequest : public HostStatusObserver { MessageLoop* message_loop_; RegisterCallback callback_; + scoped_ptr<IqSender> iq_sender_; scoped_ptr<IqRequest> request_; HostKeyPair key_pair_; diff --git a/remoting/host/register_support_host_request_unittest.cc b/remoting/host/register_support_host_request_unittest.cc index 57c83b2..c5b6261 100644 --- a/remoting/host/register_support_host_request_unittest.cc +++ b/remoting/host/register_support_host_request_unittest.cc @@ -12,7 +12,7 @@ #include "remoting/host/host_key_pair.h" #include "remoting/host/in_memory_host_config.h" #include "remoting/host/test_key_pair.h" -#include "remoting/jingle_glue/iq_request.h" +#include "remoting/jingle_glue/iq_sender.h" #include "remoting/jingle_glue/mock_objects.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -35,6 +35,7 @@ const char kTestJid[] = "user@gmail.com/chromoting123"; const int64 kTestTime = 123123123; const char kSupportId[] = "AB4RF3"; const char kSupportIdLifetime[] = "300"; +const char kStanzaId[] = "123"; class MockCallback { public: @@ -62,22 +63,21 @@ TEST_F(RegisterSupportHostRequestTest, Send) { // |iq_request| is freed by RegisterSupportHostRequest. int64 start_time = static_cast<int64>(base::Time::Now().ToDoubleT()); + SignalStrategy::Listener* listener; + EXPECT_CALL(signal_strategy_, AddListener(NotNull())) + .WillOnce(SaveArg<0>(&listener)); + scoped_ptr<RegisterSupportHostRequest> request( new RegisterSupportHostRequest()); ASSERT_TRUE(request->Init( config_, base::Bind(&MockCallback::OnResponse, base::Unretained(&callback_)))); - MockIqRequest* iq_request = new MockIqRequest(); - iq_request->Init(); - EXPECT_CALL(*iq_request, set_callback(_)).Times(1); - - EXPECT_CALL(signal_strategy_, CreateIqRequest()) - .WillOnce(Return(iq_request)); - XmlElement* sent_iq = NULL; - EXPECT_CALL(*iq_request, SendIq(NotNull())) - .WillOnce(SaveArg<0>(&sent_iq)); + EXPECT_CALL(signal_strategy_, GetNextId()) + .WillOnce(Return(kStanzaId)); + EXPECT_CALL(signal_strategy_, SendStanza(NotNull())) + .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true))); request->OnSignallingConnected(&signal_strategy_, kTestJid); message_loop_.RunAllPending(); @@ -116,8 +116,9 @@ TEST_F(RegisterSupportHostRequestTest, Send) { EXPECT_CALL(callback_, OnResponse(true, kSupportId, base::TimeDelta::FromSeconds(300))); - scoped_ptr<XmlElement> response(new XmlElement(QName("", "iq"))); + scoped_ptr<XmlElement> response(new XmlElement(buzz::QN_IQ)); response->AddAttr(QName("", "type"), "result"); + response->AddAttr(QName("", "id"), kStanzaId); XmlElement* result = new XmlElement( QName(kChromotingXmlNamespace, "register-support-host-result")); @@ -133,8 +134,10 @@ TEST_F(RegisterSupportHostRequestTest, Send) { support_id_lifetime->AddText(kSupportIdLifetime); result->AddElement(support_id_lifetime); - iq_request->callback().Run(response.get()); + EXPECT_TRUE(listener->OnIncomingStanza(response.get())); message_loop_.RunAllPending(); + + EXPECT_CALL(signal_strategy_, RemoveListener(listener)); } } // namespace remoting |