| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync/notifier/non_blocking_invalidation_notifier.h" | 5 #include "chrome/browser/sync/notifier/non_blocking_invalidation_notifier.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 | 9 |
| 10 namespace sync_notifier { | 10 namespace sync_notifier { |
| 11 | 11 |
| 12 NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( | 12 NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( |
| 13 const notifier::NotifierOptions& notifier_options, | 13 const notifier::NotifierOptions& notifier_options, |
| 14 const std::string& client_info) | 14 const std::string& client_info) |
| 15 : parent_message_loop_(MessageLoop::current()), | 15 : construction_message_loop_(MessageLoop::current()), |
| 16 method_message_loop_(NULL), |
| 16 observers_(new ObserverListThreadSafe<SyncNotifierObserver>()), | 17 observers_(new ObserverListThreadSafe<SyncNotifierObserver>()), |
| 17 worker_thread_("InvalidationNotifier worker thread"), | 18 worker_thread_("InvalidationNotifier worker thread"), |
| 18 worker_thread_vars_(NULL) { | 19 worker_thread_vars_(NULL) { |
| 19 DCHECK_EQ(notifier::NOTIFICATION_SERVER, | 20 DCHECK_EQ(notifier::NOTIFICATION_SERVER, |
| 20 notifier_options.notification_method); | 21 notifier_options.notification_method); |
| 21 const base::Thread::Options options(MessageLoop::TYPE_IO, 0); | 22 const base::Thread::Options options(MessageLoop::TYPE_IO, 0); |
| 22 CHECK(worker_thread_.StartWithOptions(options)); | 23 CHECK(worker_thread_.StartWithOptions(options)); |
| 23 worker_message_loop()->PostTask( | 24 worker_message_loop()->PostTask( |
| 24 FROM_HERE, | 25 FROM_HERE, |
| 25 NewRunnableMethod( | 26 NewRunnableMethod( |
| 26 this, | 27 this, |
| 27 &NonBlockingInvalidationNotifier::CreateWorkerThreadVars, | 28 &NonBlockingInvalidationNotifier::CreateWorkerThreadVars, |
| 28 notifier_options, client_info)); | 29 notifier_options, client_info)); |
| 29 } | 30 } |
| 30 | 31 |
| 31 NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() { | 32 NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() { |
| 32 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); | 33 DCHECK_EQ(MessageLoop::current(), construction_message_loop_); |
| 33 worker_message_loop()->PostTask( | 34 worker_message_loop()->PostTask( |
| 34 FROM_HERE, | 35 FROM_HERE, |
| 35 NewRunnableMethod( | 36 NewRunnableMethod( |
| 36 this, | 37 this, |
| 37 &NonBlockingInvalidationNotifier::DestroyWorkerThreadVars)); | 38 &NonBlockingInvalidationNotifier::DestroyWorkerThreadVars)); |
| 38 worker_thread_.Stop(); | 39 worker_thread_.Stop(); |
| 39 CHECK(!worker_thread_vars_); | 40 CHECK(!worker_thread_vars_); |
| 40 } | 41 } |
| 41 | 42 |
| 42 void NonBlockingInvalidationNotifier::AddObserver( | 43 void NonBlockingInvalidationNotifier::AddObserver( |
| 43 SyncNotifierObserver* observer) { | 44 SyncNotifierObserver* observer) { |
| 44 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); | 45 CheckOrSetValidThread(); |
| 45 observers_->AddObserver(observer); | 46 observers_->AddObserver(observer); |
| 46 } | 47 } |
| 47 | 48 |
| 48 void NonBlockingInvalidationNotifier::RemoveObserver( | 49 void NonBlockingInvalidationNotifier::RemoveObserver( |
| 49 SyncNotifierObserver* observer) { | 50 SyncNotifierObserver* observer) { |
| 50 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); | 51 CheckOrSetValidThread(); |
| 51 observers_->RemoveObserver(observer); | 52 observers_->RemoveObserver(observer); |
| 52 } | 53 } |
| 53 | 54 |
| 54 void NonBlockingInvalidationNotifier::SetState(const std::string& state) { | 55 void NonBlockingInvalidationNotifier::SetState(const std::string& state) { |
| 55 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); | 56 CheckOrSetValidThread(); |
| 56 worker_message_loop()->PostTask( | 57 worker_message_loop()->PostTask( |
| 57 FROM_HERE, | 58 FROM_HERE, |
| 58 NewRunnableMethod( | 59 NewRunnableMethod( |
| 59 this, | 60 this, |
| 60 &NonBlockingInvalidationNotifier::SetStateOnWorkerThread, | 61 &NonBlockingInvalidationNotifier::SetStateOnWorkerThread, |
| 61 state)); | 62 state)); |
| 62 } | 63 } |
| 63 | 64 |
| 64 void NonBlockingInvalidationNotifier::UpdateCredentials( | 65 void NonBlockingInvalidationNotifier::UpdateCredentials( |
| 65 const std::string& email, const std::string& token) { | 66 const std::string& email, const std::string& token) { |
| 66 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); | 67 CheckOrSetValidThread(); |
| 67 worker_message_loop()->PostTask( | 68 worker_message_loop()->PostTask( |
| 68 FROM_HERE, | 69 FROM_HERE, |
| 69 NewRunnableMethod( | 70 NewRunnableMethod( |
| 70 this, | 71 this, |
| 71 &NonBlockingInvalidationNotifier::UpdateCredentialsOnWorkerThread, | 72 &NonBlockingInvalidationNotifier::UpdateCredentialsOnWorkerThread, |
| 72 email, token)); | 73 email, token)); |
| 73 } | 74 } |
| 74 | 75 |
| 75 void NonBlockingInvalidationNotifier::UpdateEnabledTypes( | 76 void NonBlockingInvalidationNotifier::UpdateEnabledTypes( |
| 76 const syncable::ModelTypeSet& types) { | 77 const syncable::ModelTypeSet& types) { |
| 77 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); | 78 CheckOrSetValidThread(); |
| 78 worker_message_loop()->PostTask( | 79 worker_message_loop()->PostTask( |
| 79 FROM_HERE, | 80 FROM_HERE, |
| 80 NewRunnableMethod( | 81 NewRunnableMethod( |
| 81 this, | 82 this, |
| 82 &NonBlockingInvalidationNotifier::UpdateEnabledTypesOnWorkerThread, | 83 &NonBlockingInvalidationNotifier::UpdateEnabledTypesOnWorkerThread, |
| 83 types)); | 84 types)); |
| 84 } | 85 } |
| 85 | 86 |
| 86 void NonBlockingInvalidationNotifier::SendNotification() { | 87 void NonBlockingInvalidationNotifier::SendNotification() { |
| 87 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); | 88 CheckOrSetValidThread(); |
| 88 // InvalidationClient doesn't implement SendNotification(), so no | 89 // InvalidationClient doesn't implement SendNotification(), so no |
| 89 // need to forward on the call. | 90 // need to forward on the call. |
| 90 } | 91 } |
| 91 | 92 |
| 92 MessageLoop* NonBlockingInvalidationNotifier::worker_message_loop() { | 93 MessageLoop* NonBlockingInvalidationNotifier::worker_message_loop() { |
| 93 MessageLoop* current_message_loop = MessageLoop::current(); | 94 MessageLoop* current_message_loop = MessageLoop::current(); |
| 94 DCHECK(current_message_loop); | 95 DCHECK(current_message_loop); |
| 95 MessageLoop* worker_message_loop = worker_thread_.message_loop(); | 96 MessageLoop* worker_message_loop = worker_thread_.message_loop(); |
| 96 DCHECK(worker_message_loop); | 97 DCHECK(worker_message_loop); |
| 97 DCHECK(current_message_loop == parent_message_loop_ || | 98 DCHECK(current_message_loop == construction_message_loop_ || |
| 99 current_message_loop == method_message_loop_ || |
| 98 current_message_loop == worker_message_loop); | 100 current_message_loop == worker_message_loop); |
| 99 return worker_message_loop; | 101 return worker_message_loop; |
| 100 } | 102 } |
| 101 | 103 |
| 104 void NonBlockingInvalidationNotifier::CheckOrSetValidThread() { |
| 105 if (method_message_loop_) { |
| 106 DCHECK_EQ(MessageLoop::current(), method_message_loop_); |
| 107 } else { |
| 108 method_message_loop_ = MessageLoop::current(); |
| 109 } |
| 110 } |
| 111 |
| 102 void NonBlockingInvalidationNotifier::CreateWorkerThreadVars( | 112 void NonBlockingInvalidationNotifier::CreateWorkerThreadVars( |
| 103 const notifier::NotifierOptions& notifier_options, | 113 const notifier::NotifierOptions& notifier_options, |
| 104 const std::string& client_info) { | 114 const std::string& client_info) { |
| 105 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); | 115 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
| 106 worker_thread_vars_ = | 116 worker_thread_vars_ = |
| 107 new WorkerThreadVars(notifier_options, client_info, observers_); | 117 new WorkerThreadVars(notifier_options, client_info, observers_); |
| 108 } | 118 } |
| 109 | 119 |
| 110 void NonBlockingInvalidationNotifier::DestroyWorkerThreadVars() { | 120 void NonBlockingInvalidationNotifier::DestroyWorkerThreadVars() { |
| 111 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); | 121 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 &cert_verifier_, client_info), | 178 &cert_verifier_, client_info), |
| 169 observer_router_(observers) { | 179 observer_router_(observers) { |
| 170 invalidation_notifier.AddObserver(&observer_router_); | 180 invalidation_notifier.AddObserver(&observer_router_); |
| 171 } | 181 } |
| 172 | 182 |
| 173 NonBlockingInvalidationNotifier::WorkerThreadVars::~WorkerThreadVars() { | 183 NonBlockingInvalidationNotifier::WorkerThreadVars::~WorkerThreadVars() { |
| 174 invalidation_notifier.RemoveObserver(&observer_router_); | 184 invalidation_notifier.RemoveObserver(&observer_router_); |
| 175 } | 185 } |
| 176 | 186 |
| 177 } // namespace sync_notifier | 187 } // namespace sync_notifier |
| OLD | NEW |