Skip to content

Commit

Permalink
Add FakeSignalStrategy.
Browse files Browse the repository at this point in the history
The new class will be used in tests instead of SessionManagerPair.

BUG=None
TEST=None

Review URL: http://codereview.chromium.org/7276017

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@90867 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
sergeyu@chromium.org committed Jun 28, 2011
1 parent 0573d85 commit f7ea987
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
96 changes: 96 additions & 0 deletions remoting/jingle_glue/fake_signal_strategy.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/jingle_glue/fake_signal_strategy.h"

#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/string_number_conversions.h"
#include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
#include "third_party/libjingle/source/talk/xmpp/constants.h"

namespace remoting {

// static
void FakeSignalStrategy::Connect(FakeSignalStrategy* peer1,
FakeSignalStrategy* peer2) {
peer1->peer_ = peer2;
peer2->peer_ = peer1;
}

FakeSignalStrategy::FakeSignalStrategy(const std::string& jid)
: jid_(jid),
peer_(NULL),
listener_(NULL),
last_id_(0) {

}

FakeSignalStrategy::~FakeSignalStrategy() {
}

void FakeSignalStrategy::Init(StatusObserver* observer) {
observer->OnStateChange(StatusObserver::START);
observer->OnStateChange(StatusObserver::CONNECTING);
observer->OnJidChange(jid_);
observer->OnStateChange(StatusObserver::CONNECTED);
}

void FakeSignalStrategy::Close() {
DCHECK(CalledOnValidThread());
listener_ = NULL;
}

void FakeSignalStrategy::SetListener(Listener* listener) {
DCHECK(CalledOnValidThread());

// Don't overwrite an listener without explicitly going
// through "NULL" first.
DCHECK(listener_ == NULL || listener == NULL);
listener_ = listener;
}

void FakeSignalStrategy::SendStanza(buzz::XmlElement* stanza) {
DCHECK(CalledOnValidThread());

stanza->SetAttr(buzz::QN_FROM, jid_);

if (peer_) {
MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(&FakeSignalStrategy::OnIncomingMessage,
base::Unretained(peer_), stanza));
} else {
delete stanza;
}
}

std::string FakeSignalStrategy::GetNextId() {
++last_id_;
return base::IntToString(last_id_);
}

IqRequest* FakeSignalStrategy::CreateIqRequest() OVERRIDE {
DCHECK(CalledOnValidThread());

return new JavascriptIqRequest(this, &iq_registry_);
}

void FakeSignalStrategy::OnIncomingMessage(buzz::XmlElement* stanza) {
const std::string& to_field = stanza->Attr(buzz::QN_TO);
if (to_field != jid_) {
LOG(WARNING) << "Dropping stanza that is addressed to " << to_field
<< ". Local jid: " << jid_
<< ". Message content: " << stanza->Str();
return;
}

if (listener_)
listener_->OnIncomingStanza(stanza);
iq_registry_.OnIncomingStanza(stanza);

delete stanza;
}

} // namespace remoting
48 changes: 48 additions & 0 deletions remoting/jingle_glue/fake_signal_strategy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_JINGLE_GLUE_FAKE_SIGNAL_STRATEGY_H_
#define REMOTING_JINGLE_GLUE_FAKE_SIGNAL_STRATEGY_H_

#include <string>

#include "base/threading/non_thread_safe.h"
#include "remoting/jingle_glue/javascript_iq_request.h"
#include "remoting/jingle_glue/signal_strategy.h"

namespace remoting {

class FakeSignalStrategy : public SignalStrategy,
public base::NonThreadSafe {
public:
static void Connect(FakeSignalStrategy* peer1, FakeSignalStrategy* peer2);

FakeSignalStrategy(const std::string& jid);
virtual ~FakeSignalStrategy();

// SignalStrategy interface.
virtual void Init(StatusObserver* observer) OVERRIDE;
virtual void Close() OVERRIDE;
virtual void SetListener(Listener* listener) OVERRIDE;
virtual void SendStanza(buzz::XmlElement* stanza) OVERRIDE;
virtual std::string GetNextId() OVERRIDE;
virtual IqRequest* CreateIqRequest() OVERRIDE;

private:
// Called by the |peer_|. Takes ownership of |stanza|.
void OnIncomingMessage(buzz::XmlElement* stanza);

std::string jid_;
FakeSignalStrategy* peer_;
Listener* listener_;
JavascriptIqRegistry iq_registry_;

int last_id_;

DISALLOW_COPY_AND_ASSIGN(FakeSignalStrategy);
};

} // namespace remoting

#endif // REMOTING_JINGLE_GLUE_FAKE_SIGNAL_STRATEGY_H_
2 changes: 2 additions & 0 deletions remoting/remoting.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,8 @@
'host/self_access_verifier_unittest.cc',
'host/screen_recorder_unittest.cc',
'host/test_key_pair.h',
'jingle_glue/fake_signal_strategy.cc',
'jingle_glue/fake_signal_strategy.h',
'jingle_glue/iq_request_unittest.cc',
'jingle_glue/jingle_client_unittest.cc',
'jingle_glue/jingle_thread_unittest.cc',
Expand Down

0 comments on commit f7ea987

Please sign in to comment.