Index: jingle/notifier/listener/talk_mediator_impl.cc |
diff --git a/jingle/notifier/listener/talk_mediator_impl.cc b/jingle/notifier/listener/talk_mediator_impl.cc |
index fa0fc4fc16316a9eb7547c67af333281cd05d291..5bd70dcbc9b08eb5bbc7c4b5e2e758cb54f896f6 100644 |
--- a/jingle/notifier/listener/talk_mediator_impl.cc |
+++ b/jingle/notifier/listener/talk_mediator_impl.cc |
@@ -5,6 +5,7 @@ |
#include "jingle/notifier/listener/talk_mediator_impl.h" |
#include "base/logging.h" |
+#include "base/message_loop.h" |
#include "jingle/notifier/base/notifier_options_util.h" |
namespace notifier { |
@@ -14,21 +15,20 @@ TalkMediatorImpl::TalkMediatorImpl( |
const NotifierOptions& notifier_options) |
: delegate_(NULL), |
mediator_thread_(mediator_thread), |
- notifier_options_(notifier_options) { |
- DCHECK(non_thread_safe_.CalledOnValidThread()); |
+ notifier_options_(notifier_options), |
+ construction_message_loop_(MessageLoop::current()), |
+ method_message_loop_(NULL) { |
mediator_thread_->Start(); |
state_.started = 1; |
} |
TalkMediatorImpl::~TalkMediatorImpl() { |
- DCHECK(non_thread_safe_.CalledOnValidThread()); |
- if (state_.started) { |
- Logout(); |
- } |
+ DCHECK_EQ(MessageLoop::current(), construction_message_loop_); |
+ DCHECK(!state_.started); |
} |
bool TalkMediatorImpl::Login() { |
- DCHECK(non_thread_safe_.CalledOnValidThread()); |
+ CheckOrSetValidThread(); |
// Connect to the mediator thread and start processing messages. |
mediator_thread_->AddObserver(this); |
if (state_.initialized && !state_.logging_in && !state_.logged_in) { |
@@ -40,7 +40,7 @@ bool TalkMediatorImpl::Login() { |
} |
bool TalkMediatorImpl::Logout() { |
- DCHECK(non_thread_safe_.CalledOnValidThread()); |
+ CheckOrSetValidThread(); |
if (state_.started) { |
state_.started = 0; |
state_.logging_in = 0; |
@@ -56,7 +56,7 @@ bool TalkMediatorImpl::Logout() { |
} |
bool TalkMediatorImpl::SendNotification(const Notification& data) { |
- DCHECK(non_thread_safe_.CalledOnValidThread()); |
+ CheckOrSetValidThread(); |
if (state_.logged_in && state_.subscribed) { |
mediator_thread_->SendNotification(data); |
return true; |
@@ -65,15 +65,14 @@ bool TalkMediatorImpl::SendNotification(const Notification& data) { |
} |
void TalkMediatorImpl::SetDelegate(TalkMediator::Delegate* delegate) { |
- DCHECK(non_thread_safe_.CalledOnValidThread()); |
+ DCHECK_EQ(MessageLoop::current(), construction_message_loop_); |
delegate_ = delegate; |
} |
void TalkMediatorImpl::SetAuthToken(const std::string& email, |
const std::string& token, |
const std::string& token_service) { |
- DCHECK(non_thread_safe_.CalledOnValidThread()); |
- |
+ CheckOrSetValidThread(); |
xmpp_settings_ = |
MakeXmppClientSettings(notifier_options_, email, token, token_service); |
@@ -87,7 +86,7 @@ void TalkMediatorImpl::SetAuthToken(const std::string& email, |
} |
void TalkMediatorImpl::AddSubscription(const Subscription& subscription) { |
- DCHECK(non_thread_safe_.CalledOnValidThread()); |
+ CheckOrSetValidThread(); |
subscriptions_.push_back(subscription); |
if (state_.logged_in) { |
VLOG(1) << "Resubscribing for updates, a new service got added"; |
@@ -97,7 +96,7 @@ void TalkMediatorImpl::AddSubscription(const Subscription& subscription) { |
void TalkMediatorImpl::OnConnectionStateChange(bool logged_in) { |
- DCHECK(non_thread_safe_.CalledOnValidThread()); |
+ CheckOrSetValidThread(); |
// If we just lost connection, then the MediatorThread implementation will |
// try to log in again. We need to set state_.logging_in to true in that case. |
state_.logging_in = !logged_in; |
@@ -116,7 +115,7 @@ void TalkMediatorImpl::OnConnectionStateChange(bool logged_in) { |
} |
void TalkMediatorImpl::OnSubscriptionStateChange(bool subscribed) { |
- DCHECK(non_thread_safe_.CalledOnValidThread()); |
+ CheckOrSetValidThread(); |
state_.subscribed = subscribed; |
VLOG(1) << "P2P: " << (subscribed ? "subscribed" : "unsubscribed"); |
if (delegate_) |
@@ -125,18 +124,26 @@ void TalkMediatorImpl::OnSubscriptionStateChange(bool subscribed) { |
void TalkMediatorImpl::OnIncomingNotification( |
const Notification& notification) { |
- DCHECK(non_thread_safe_.CalledOnValidThread()); |
+ CheckOrSetValidThread(); |
VLOG(1) << "P2P: Updates are available on the server."; |
if (delegate_) |
delegate_->OnIncomingNotification(notification); |
} |
void TalkMediatorImpl::OnOutgoingNotification() { |
- DCHECK(non_thread_safe_.CalledOnValidThread()); |
+ CheckOrSetValidThread(); |
VLOG(1) << "P2P: Peers were notified that updates are available on the " |
"server."; |
if (delegate_) |
delegate_->OnOutgoingNotification(); |
} |
+void TalkMediatorImpl::CheckOrSetValidThread() { |
+ if (method_message_loop_) { |
+ DCHECK_EQ(MessageLoop::current(), method_message_loop_); |
+ } else { |
+ method_message_loop_ = MessageLoop::current(); |
+ } |
+} |
+ |
} // namespace notifier |