Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6812)

Unified Diff: chrome/browser/sync/notifier/non_blocking_invalidation_notifier.cc

Issue 8586014: [Sync] Replace uses of ObserverListThreadSafe with WeakHandles (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/sync/notifier/non_blocking_invalidation_notifier.cc
diff --git a/chrome/browser/sync/notifier/non_blocking_invalidation_notifier.cc b/chrome/browser/sync/notifier/non_blocking_invalidation_notifier.cc
index 40a1f91c9eb8edc6912e93d753e0b25eaa80f6a9..674c223d48824f76c21181da1c964f05a43e1217 100644
--- a/chrome/browser/sync/notifier/non_blocking_invalidation_notifier.cc
+++ b/chrome/browser/sync/notifier/non_blocking_invalidation_notifier.cc
@@ -7,10 +7,8 @@
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
-#include "base/observer_list_threadsafe.h"
#include "base/threading/thread.h"
#include "chrome/browser/sync/notifier/invalidation_notifier.h"
-#include "chrome/browser/sync/notifier/sync_notifier_observer.h"
namespace sync_notifier {
@@ -18,12 +16,11 @@ class NonBlockingInvalidationNotifier::Core
: public base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>,
public SyncNotifierObserver {
public:
- // Called on parent thread.
- Core();
-
- // Called on parent thread.
- void AddObserver(SyncNotifierObserver* observer);
- void RemoveObserver(SyncNotifierObserver* observer);
+ // Called on parent thread. |delegate_observer| should be
+ // initialized.
+ explicit Core(
+ const browser_sync::WeakHandle<SyncNotifierObserver>&
+ delegate_observer);
// Helpers called on I/O thread.
void Initialize(
@@ -50,14 +47,19 @@ class NonBlockingInvalidationNotifier::Core
// Called on parent or I/O thread.
~Core();
+ // The variables below should be used only on the I/O thread.
+ const browser_sync::WeakHandle<SyncNotifierObserver> delegate_observer_;
scoped_ptr<InvalidationNotifier> invalidation_notifier_;
scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
- scoped_refptr<ObserverListThreadSafe<SyncNotifierObserver> > observers_;
+
DISALLOW_COPY_AND_ASSIGN(Core);
};
-NonBlockingInvalidationNotifier::Core::Core()
- : observers_(new ObserverListThreadSafe<SyncNotifierObserver>()) {
+NonBlockingInvalidationNotifier::Core::Core(
+ const browser_sync::WeakHandle<SyncNotifierObserver>&
+ delegate_observer)
+ : delegate_observer_(delegate_observer) {
+ DCHECK(delegate_observer_.IsInitialized());
}
NonBlockingInvalidationNotifier::Core::~Core() {
@@ -92,16 +94,6 @@ void NonBlockingInvalidationNotifier::Core::Teardown() {
io_message_loop_proxy_ = NULL;
}
-void NonBlockingInvalidationNotifier::Core::AddObserver(
- SyncNotifierObserver* observer) {
- observers_->AddObserver(observer);
-}
-
-void NonBlockingInvalidationNotifier::Core::RemoveObserver(
- SyncNotifierObserver* observer) {
- observers_->RemoveObserver(observer);
-}
-
void NonBlockingInvalidationNotifier::Core::SetUniqueId(
const std::string& unique_id) {
DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
@@ -129,21 +121,24 @@ void NonBlockingInvalidationNotifier::Core::UpdateEnabledTypes(
void NonBlockingInvalidationNotifier::Core::OnIncomingNotification(
const syncable::ModelTypePayloadMap& type_payloads) {
DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
- observers_->Notify(&SyncNotifierObserver::OnIncomingNotification,
- type_payloads);
+ delegate_observer_.Call(FROM_HERE,
Nicolas Zea 2011/11/17 21:35:45 I wonder, would it be possible to keep the observe
akalin 2011/11/18 00:04:32 I looked into this and basically it's much more co
+ &SyncNotifierObserver::OnIncomingNotification,
+ type_payloads);
}
void NonBlockingInvalidationNotifier::Core::OnNotificationStateChange(
bool notifications_enabled) {
DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
- observers_->Notify(&SyncNotifierObserver::OnNotificationStateChange,
- notifications_enabled);
+ delegate_observer_.Call(FROM_HERE,
+ &SyncNotifierObserver::OnNotificationStateChange,
+ notifications_enabled);
}
void NonBlockingInvalidationNotifier::Core::StoreState(
const std::string& state) {
DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
- observers_->Notify(&SyncNotifierObserver::StoreState, state);
+ delegate_observer_.Call(FROM_HERE,
+ &SyncNotifierObserver::StoreState, state);
}
NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier(
@@ -152,7 +147,10 @@ NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier(
const browser_sync::WeakHandle<InvalidationVersionTracker>&
invalidation_version_tracker,
const std::string& client_info)
- : core_(new Core),
+ : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
+ core_(
+ new Core(browser_sync::MakeWeakHandle(
+ weak_ptr_factory_.GetWeakPtr()))),
parent_message_loop_proxy_(
base::MessageLoopProxy::current()),
io_message_loop_proxy_(notifier_options.request_context_getter->
@@ -183,13 +181,13 @@ NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() {
void NonBlockingInvalidationNotifier::AddObserver(
SyncNotifierObserver* observer) {
DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
- core_->AddObserver(observer);
+ observers_.AddObserver(observer);
}
void NonBlockingInvalidationNotifier::RemoveObserver(
SyncNotifierObserver* observer) {
DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
- core_->RemoveObserver(observer);
+ observers_.RemoveObserver(observer);
}
void NonBlockingInvalidationNotifier::SetUniqueId(
@@ -242,4 +240,25 @@ void NonBlockingInvalidationNotifier::SendNotification(
// need to forward on the call.
}
+void NonBlockingInvalidationNotifier::OnIncomingNotification(
+ const syncable::ModelTypePayloadMap& type_payloads) {
+ DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
+ FOR_EACH_OBSERVER(SyncNotifierObserver, observers_,
+ OnIncomingNotification(type_payloads));
+}
+
+void NonBlockingInvalidationNotifier::OnNotificationStateChange(
+ bool notifications_enabled) {
+ DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
+ FOR_EACH_OBSERVER(SyncNotifierObserver, observers_,
+ OnNotificationStateChange(notifications_enabled));
+}
+
+void NonBlockingInvalidationNotifier::StoreState(
+ const std::string& state) {
+ DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
+ FOR_EACH_OBSERVER(SyncNotifierObserver, observers_,
+ StoreState(state));
+}
+
} // namespace sync_notifier

Powered by Google App Engine
This is Rietveld 408576698