summaryrefslogtreecommitdiffstats
path: root/jingle/glue
diff options
context:
space:
mode:
Diffstat (limited to 'jingle/glue')
-rw-r--r--jingle/glue/fake_network_manager.cc31
-rw-r--r--jingle/glue/fake_network_manager.h34
-rw-r--r--jingle/glue/thread_wrapper.cc23
-rw-r--r--jingle/glue/thread_wrapper.h4
-rw-r--r--jingle/glue/thread_wrapper_unittest.cc31
5 files changed, 105 insertions, 18 deletions
diff --git a/jingle/glue/fake_network_manager.cc b/jingle/glue/fake_network_manager.cc
new file mode 100644
index 0000000..f37208d
--- /dev/null
+++ b/jingle/glue/fake_network_manager.cc
@@ -0,0 +1,31 @@
+// 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 "jingle/glue/fake_network_manager.h"
+
+#include "base/logging.h"
+#include "net/base/ip_endpoint.h"
+#include "jingle/glue/utils.h"
+#include "third_party/libjingle/source/talk/base/socketaddress.h"
+
+namespace jingle_glue {
+
+FakeNetworkManager::FakeNetworkManager(const net::IPAddressNumber& address)
+ : address_(address) {
+}
+
+FakeNetworkManager::~FakeNetworkManager() {
+}
+
+bool FakeNetworkManager::EnumNetworks(
+ bool include_ignored, std::vector<talk_base::Network*>* networks) {
+ net::IPEndPoint endpoint(address_, 0);
+ talk_base::SocketAddress address;
+ CHECK(IPEndPointToSocketAddress(endpoint, &address));
+ networks->push_back(new talk_base::Network(
+ "fake", "Fake Network", address.ip(), 0));
+ return true;
+}
+
+} // namespace jingle_glue
diff --git a/jingle/glue/fake_network_manager.h b/jingle/glue/fake_network_manager.h
new file mode 100644
index 0000000..f099c3c
--- /dev/null
+++ b/jingle/glue/fake_network_manager.h
@@ -0,0 +1,34 @@
+// 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 JINGLE_GLUE_FAKE_NETWORK_MANAGER_H_
+#define JINGLE_GLUE_FAKE_NETWORK_MANAGER_H_
+
+#include <vector>
+
+#include "base/compiler_specific.h"
+#include "net/base/net_util.h"
+#include "third_party/libjingle/source/talk/base/network.h"
+
+namespace jingle_glue {
+
+// FakeNetworkManager always returns one interface with the IP address
+// specified in the constructor.
+class FakeNetworkManager : public talk_base::NetworkManager {
+ public:
+ FakeNetworkManager(const net::IPAddressNumber& address);
+ virtual ~FakeNetworkManager();
+
+ protected:
+ // Override from talk_base::NetworkManager.
+ virtual bool EnumNetworks(
+ bool include_ignored,
+ std::vector<talk_base::Network*>* networks) OVERRIDE;
+
+ net::IPAddressNumber address_;
+};
+
+} // namespace jingle_glue
+
+#endif // JINGLE_GLUE_FAKE_NETWORK_MANAGER_H_
diff --git a/jingle/glue/thread_wrapper.cc b/jingle/glue/thread_wrapper.cc
index f4551de..fe26094 100644
--- a/jingle/glue/thread_wrapper.cc
+++ b/jingle/glue/thread_wrapper.cc
@@ -6,6 +6,25 @@
namespace jingle_glue {
+// static
+void JingleThreadWrapper::EnsureForCurrentThread() {
+ talk_base::Thread* current_thread = talk_base::Thread::Current();
+ // If JingleThreadWrapper already exists for the current thread then
+ // it is returned by talk_base::Thread::Current().
+ // talk_base::Thread::Current() may also return non-null value for
+ // the main thread because talk_base::ThreadManager creates
+ // talk_base::Thread object for it. IsOwned() allows to distinguish
+ // talk_base::Thread object created by talk_base::ThreadManager from
+ // other talk_base::Thread objects. Because talk_base::Thread
+ // objects should never created by chromium code, we can assume that
+ // if talk_base::Thread::Current() returns non-null value and it
+ // isn't the object created by talk_base::ThreadManager then
+ // JingleThreadWrapper already exists for the current thread.
+ if (current_thread == NULL || !current_thread->IsOwned()) {
+ new JingleThreadWrapper(MessageLoop::current());
+ }
+}
+
JingleThreadWrapper::JingleThreadWrapper(MessageLoop* message_loop)
: message_loop_(message_loop) {
DCHECK_EQ(message_loop_, MessageLoop::current());
@@ -95,10 +114,8 @@ void JingleThreadWrapper::RunTask(int task_id) {
}
}
- if (have_message) {
+ if (have_message)
message.phandler->OnMessage(&message);
- delete message.pdata;
- }
}
// All methods below are marked as not reached. See comments in the
diff --git a/jingle/glue/thread_wrapper.h b/jingle/glue/thread_wrapper.h
index 755d047..5b5cf08 100644
--- a/jingle/glue/thread_wrapper.h
+++ b/jingle/glue/thread_wrapper.h
@@ -24,6 +24,10 @@ class JingleThreadWrapper
: public MessageLoop::DestructionObserver,
public talk_base::Thread {
public:
+ // Create JingleThreadWrapper for the current thread if it hasn't
+ // been created yet.
+ static void EnsureForCurrentThread();
+
JingleThreadWrapper(MessageLoop* message_loop);
// MessageLoop::DestructionObserver implementation.
diff --git a/jingle/glue/thread_wrapper_unittest.cc b/jingle/glue/thread_wrapper_unittest.cc
index 1c8943a..18f8bb4 100644
--- a/jingle/glue/thread_wrapper_unittest.cc
+++ b/jingle/glue/thread_wrapper_unittest.cc
@@ -49,7 +49,8 @@ TEST_F(ThreadWrapperTest, Create) {
EXPECT_EQ(thread(), static_cast<talk_base::Thread*>(wrapper_));
}
-MATCHER_P3(MatchMessage, handler, message_id, data, "") {
+MATCHER_P3(MatchMessageAndDeleteData, handler, message_id, data, "") {
+ delete arg->pdata;
return arg->phandler == handler &&
arg->message_id == message_id &&
arg->pdata == data;
@@ -69,13 +70,13 @@ TEST_F(ThreadWrapperTest, Post) {
InSequence in_seq;
EXPECT_CALL(handler1_, OnMessage(
- MatchMessage(&handler1_, kTestMessage1, data1_)));
+ MatchMessageAndDeleteData(&handler1_, kTestMessage1, data1_)));
EXPECT_CALL(handler1_, OnMessage(
- MatchMessage(&handler1_, kTestMessage2, data2_)));
+ MatchMessageAndDeleteData(&handler1_, kTestMessage2, data2_)));
EXPECT_CALL(handler2_, OnMessage(
- MatchMessage(&handler2_, kTestMessage1, data3_)));
+ MatchMessageAndDeleteData(&handler2_, kTestMessage1, data3_)));
EXPECT_CALL(handler2_, OnMessage(
- MatchMessage(&handler2_, kTestMessage1, data4_)));
+ MatchMessageAndDeleteData(&handler2_, kTestMessage1, data4_)));
message_loop_.RunAllPending();
}
@@ -96,13 +97,13 @@ TEST_F(ThreadWrapperTest, PostDelayed) {
InSequence in_seq;
EXPECT_CALL(handler1_, OnMessage(
- MatchMessage(&handler1_, kTestMessage1, data1_)));
+ MatchMessageAndDeleteData(&handler1_, kTestMessage1, data1_)));
EXPECT_CALL(handler1_, OnMessage(
- MatchMessage(&handler1_, kTestMessage2, data2_)));
+ MatchMessageAndDeleteData(&handler1_, kTestMessage2, data2_)));
EXPECT_CALL(handler2_, OnMessage(
- MatchMessage(&handler2_, kTestMessage1, data3_)));
+ MatchMessageAndDeleteData(&handler2_, kTestMessage1, data3_)));
EXPECT_CALL(handler2_, OnMessage(
- MatchMessage(&handler2_, kTestMessage1, data4_)));
+ MatchMessageAndDeleteData(&handler2_, kTestMessage1, data4_)));
message_loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask(),
kMaxTestDelay);
@@ -121,11 +122,11 @@ TEST_F(ThreadWrapperTest, Clear) {
talk_base::MessageData* null_data = NULL;
EXPECT_CALL(handler1_, OnMessage(
- MatchMessage(&handler1_, kTestMessage1, null_data)));
+ MatchMessageAndDeleteData(&handler1_, kTestMessage1, null_data)));
EXPECT_CALL(handler2_, OnMessage(
- MatchMessage(&handler2_, kTestMessage1, null_data)));
+ MatchMessageAndDeleteData(&handler2_, kTestMessage1, null_data)));
EXPECT_CALL(handler2_, OnMessage(
- MatchMessage(&handler2_, kTestMessage2, null_data)));
+ MatchMessageAndDeleteData(&handler2_, kTestMessage2, null_data)));
message_loop_.RunAllPending();
}
@@ -144,11 +145,11 @@ TEST_F(ThreadWrapperTest, ClearDelayed) {
talk_base::MessageData* null_data = NULL;
EXPECT_CALL(handler1_, OnMessage(
- MatchMessage(&handler1_, kTestMessage1, null_data)));
+ MatchMessageAndDeleteData(&handler1_, kTestMessage1, null_data)));
EXPECT_CALL(handler2_, OnMessage(
- MatchMessage(&handler2_, kTestMessage1, null_data)));
+ MatchMessageAndDeleteData(&handler2_, kTestMessage1, null_data)));
EXPECT_CALL(handler2_, OnMessage(
- MatchMessage(&handler2_, kTestMessage1, null_data)));
+ MatchMessageAndDeleteData(&handler2_, kTestMessage1, null_data)));
message_loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask(),
kMaxTestDelay);