summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/notifier
diff options
context:
space:
mode:
authorKristian Monsen <kristianm@google.com>2011-06-09 11:47:42 +0100
committerKristian Monsen <kristianm@google.com>2011-06-29 14:33:03 +0100
commitdc0f95d653279beabeb9817299e2902918ba123e (patch)
tree32eb121cd532053a5b9cb0c390331349af8d6baa /chrome/browser/sync/notifier
parentba160cd4054d13d0cb0b1b46e61c3bed67095811 (diff)
downloadexternal_chromium-dc0f95d653279beabeb9817299e2902918ba123e.zip
external_chromium-dc0f95d653279beabeb9817299e2902918ba123e.tar.gz
external_chromium-dc0f95d653279beabeb9817299e2902918ba123e.tar.bz2
Merge Chromium at r11.0.696.0: Initial merge by git
Change-Id: I273dde2843af0839dfc08b419bb443fbd449532d
Diffstat (limited to 'chrome/browser/sync/notifier')
-rw-r--r--chrome/browser/sync/notifier/chrome_invalidation_client.cc11
-rw-r--r--chrome/browser/sync/notifier/chrome_invalidation_client.h2
-rw-r--r--chrome/browser/sync/notifier/registration_manager.cc59
-rw-r--r--chrome/browser/sync/notifier/registration_manager.h18
-rw-r--r--chrome/browser/sync/notifier/registration_manager_unittest.cc92
-rw-r--r--chrome/browser/sync/notifier/server_notifier_thread.cc35
-rw-r--r--chrome/browser/sync/notifier/server_notifier_thread.h10
-rw-r--r--chrome/browser/sync/notifier/server_notifier_thread_unittest.cc14
8 files changed, 190 insertions, 51 deletions
diff --git a/chrome/browser/sync/notifier/chrome_invalidation_client.cc b/chrome/browser/sync/notifier/chrome_invalidation_client.cc
index f80a346..0c38936 100644
--- a/chrome/browser/sync/notifier/chrome_invalidation_client.cc
+++ b/chrome/browser/sync/notifier/chrome_invalidation_client.cc
@@ -75,7 +75,6 @@ void ChromeInvalidationClient::Start(
ChangeBaseTask(base_task);
registration_manager_.reset(
new RegistrationManager(invalidation_client_.get()));
- RegisterTypes();
}
void ChromeInvalidationClient::ChangeBaseTask(
@@ -103,15 +102,11 @@ void ChromeInvalidationClient::Stop() {
listener_ = NULL;
}
-void ChromeInvalidationClient::RegisterTypes() {
+void ChromeInvalidationClient::RegisterTypes(
+ const syncable::ModelTypeSet& types) {
DCHECK(non_thread_safe_.CalledOnValidThread());
- // TODO(akalin): Make this configurable instead of listening to
- // notifications for all possible types.
- for (int i = syncable::FIRST_REAL_MODEL_TYPE;
- i < syncable::MODEL_TYPE_COUNT; ++i) {
- registration_manager_->RegisterType(syncable::ModelTypeFromInt(i));
- }
+ registration_manager_->SetRegisteredTypes(types);
}
void ChromeInvalidationClient::Invalidate(
diff --git a/chrome/browser/sync/notifier/chrome_invalidation_client.h b/chrome/browser/sync/notifier/chrome_invalidation_client.h
index 873b877..904b91d 100644
--- a/chrome/browser/sync/notifier/chrome_invalidation_client.h
+++ b/chrome/browser/sync/notifier/chrome_invalidation_client.h
@@ -69,7 +69,7 @@ class ChromeInvalidationClient
// Register the sync types that we're interested in getting
// notifications for. Must only be called between calls to Start()
// and Stop().
- void RegisterTypes();
+ void RegisterTypes(const syncable::ModelTypeSet& types);
// invalidation::InvalidationListener implementation.
//
diff --git a/chrome/browser/sync/notifier/registration_manager.cc b/chrome/browser/sync/notifier/registration_manager.cc
index c03c07c..69181a4 100644
--- a/chrome/browser/sync/notifier/registration_manager.cc
+++ b/chrome/browser/sync/notifier/registration_manager.cc
@@ -21,20 +21,18 @@ RegistrationManager::~RegistrationManager() {
DCHECK(non_thread_safe_.CalledOnValidThread());
}
-void RegistrationManager::RegisterType(syncable::ModelType model_type) {
+void RegistrationManager::SetRegisteredTypes(
+ const syncable::ModelTypeSet& types) {
DCHECK(non_thread_safe_.CalledOnValidThread());
- invalidation::ObjectId object_id;
- if (!RealModelTypeToObjectId(model_type, &object_id)) {
- LOG(DFATAL) << "Invalid model type: " << model_type;
- return;
- }
- RegistrationStatusMap::iterator it =
- registration_status_.insert(
- std::make_pair(
- model_type,
- invalidation::RegistrationState_UNREGISTERED)).first;
- if (it->second == invalidation::RegistrationState_UNREGISTERED) {
- RegisterObject(object_id, it);
+
+ for (int i = syncable::FIRST_REAL_MODEL_TYPE; i < syncable::MODEL_TYPE_COUNT;
+ ++i) {
+ syncable::ModelType type = syncable::ModelTypeFromInt(i);
+ if (types.count(type) > 0) {
+ RegisterType(type);
+ } else {
+ UnregisterType(type);
+ }
}
}
@@ -82,6 +80,40 @@ void RegistrationManager::MarkAllRegistrationsLost() {
}
}
+void RegistrationManager::RegisterType(syncable::ModelType model_type) {
+ DCHECK(non_thread_safe_.CalledOnValidThread());
+ invalidation::ObjectId object_id;
+ if (!RealModelTypeToObjectId(model_type, &object_id)) {
+ LOG(DFATAL) << "Invalid model type: " << model_type;
+ return;
+ }
+ RegistrationStatusMap::iterator it =
+ registration_status_.insert(
+ std::make_pair(
+ model_type,
+ invalidation::RegistrationState_UNREGISTERED)).first;
+ if (it->second == invalidation::RegistrationState_UNREGISTERED) {
+ RegisterObject(object_id, it);
+ }
+}
+
+void RegistrationManager::UnregisterType(syncable::ModelType model_type) {
+ DCHECK(non_thread_safe_.CalledOnValidThread());
+ invalidation::ObjectId object_id;
+ if (!RealModelTypeToObjectId(model_type, &object_id)) {
+ LOG(DFATAL) << "Invalid model type: " << model_type;
+ return;
+ }
+ RegistrationStatusMap::iterator it = registration_status_.find(model_type);
+
+ if (it != registration_status_.end()) {
+ if (it->second == invalidation::RegistrationState_REGISTERED) {
+ invalidation_client_->Unregister(object_id);
+ }
+ registration_status_.erase(it);
+ }
+}
+
void RegistrationManager::RegisterObject(
const invalidation::ObjectId& object_id,
RegistrationStatusMap::iterator it) {
@@ -89,5 +121,4 @@ void RegistrationManager::RegisterObject(
invalidation_client_->Register(object_id);
it->second = invalidation::RegistrationState_REGISTERED;
}
-
} // namespace sync_notifier
diff --git a/chrome/browser/sync/notifier/registration_manager.h b/chrome/browser/sync/notifier/registration_manager.h
index 2694373..d7edb04 100644
--- a/chrome/browser/sync/notifier/registration_manager.h
+++ b/chrome/browser/sync/notifier/registration_manager.h
@@ -12,6 +12,7 @@
#include <map>
#include "base/basictypes.h"
+#include "base/gtest_prod_util.h"
#include "base/threading/non_thread_safe.h"
#include "chrome/browser/sync/syncable/model_type.h"
#include "google/cacheinvalidation/invalidation-client.h"
@@ -19,6 +20,13 @@
namespace sync_notifier {
class RegistrationManager {
+
+ friend class RegistrationManagerTest;
+
+ FRIEND_TEST_ALL_PREFIXES(RegistrationManagerTest, RegisterType);
+ FRIEND_TEST_ALL_PREFIXES(RegistrationManagerTest, UnregisterType);
+ FRIEND_TEST_ALL_PREFIXES(RegistrationManagerTest, MarkRegistrationLost);
+ FRIEND_TEST_ALL_PREFIXES(RegistrationManagerTest, MarkAllRegistrationsLost);
public:
// Does not take ownership of |invalidation_client_|.
explicit RegistrationManager(
@@ -26,16 +34,13 @@ class RegistrationManager {
~RegistrationManager();
- // Registers the given |model_type|, which must be valid.
- void RegisterType(syncable::ModelType model_type);
+ void SetRegisteredTypes(const syncable::ModelTypeSet& types);
// Returns true iff |model_type| is currently registered.
//
// Currently only used by unit tests.
bool IsRegistered(syncable::ModelType model_type) const;
- // TODO(akalin): We will eventually need an UnregisterType().
-
// Marks the registration for the |model_type| lost and re-registers
// it.
void MarkRegistrationLost(syncable::ModelType model_type);
@@ -47,6 +52,11 @@ class RegistrationManager {
typedef std::map<syncable::ModelType, invalidation::RegistrationState>
RegistrationStatusMap;
+ // Registers the given |model_type|, which must be valid.
+ void RegisterType(syncable::ModelType model_type);
+
+ void UnregisterType(syncable::ModelType model_type);
+
// Calls invalidation_client_->Register() on |object_id|. sets
// it->second to UNREGISTERED -> PENDING.
void RegisterObject(const invalidation::ObjectId& object_id,
diff --git a/chrome/browser/sync/notifier/registration_manager_unittest.cc b/chrome/browser/sync/notifier/registration_manager_unittest.cc
index da93ac8..ac5b236 100644
--- a/chrome/browser/sync/notifier/registration_manager_unittest.cc
+++ b/chrome/browser/sync/notifier/registration_manager_unittest.cc
@@ -4,6 +4,7 @@
#include "chrome/browser/sync/notifier/registration_manager.h"
+#include <algorithm>
#include <cstddef>
#include <deque>
#include <vector>
@@ -15,7 +16,13 @@
#include "testing/gtest/include/gtest/gtest.h"
namespace sync_notifier {
-namespace {
+
+syncable::ModelType ObjectIdToModelType(
+ const invalidation::ObjectId& object_id) {
+ syncable::ModelType model_type = syncable::UNSPECIFIED;
+ EXPECT_TRUE(ObjectIdToRealModelType(object_id, &model_type));
+ return model_type;
+}
// Fake invalidation client that just stores the args of calls to
// Register().
@@ -28,11 +35,22 @@ class FakeInvalidationClient : public invalidation::InvalidationClient {
virtual void Start(const std::string& state) {}
virtual void Register(const invalidation::ObjectId& oid) {
- registered_oids.push_back(oid);
+ registered_types.push_back(ObjectIdToModelType(oid));
}
virtual void Unregister(const invalidation::ObjectId& oid) {
- ADD_FAILURE();
+ syncable::ModelType type_to_unregister = ObjectIdToModelType(oid);
+ std::vector<syncable::ModelType>::iterator it = std::find(
+ registered_types.begin(),
+ registered_types.end(),
+ type_to_unregister);
+
+ if(it == registered_types.end()) {
+ // We should not be unregistering a thing that is not yet registered.
+ ADD_FAILURE();
+ } else {
+ registered_types.erase(it);
+ }
}
virtual invalidation::NetworkEndpoint* network_endpoint() {
@@ -40,7 +58,7 @@ class FakeInvalidationClient : public invalidation::InvalidationClient {
return NULL;
}
- std::deque<invalidation::ObjectId> registered_oids;
+ std::vector<syncable::ModelType> registered_types;
private:
DISALLOW_COPY_AND_ASSIGN(FakeInvalidationClient);
@@ -60,13 +78,6 @@ class RegistrationManagerTest : public testing::Test {
DISALLOW_COPY_AND_ASSIGN(RegistrationManagerTest);
};
-syncable::ModelType ObjectIdToModelType(
- const invalidation::ObjectId& object_id) {
- syncable::ModelType model_type = syncable::UNSPECIFIED;
- EXPECT_TRUE(ObjectIdToRealModelType(object_id, &model_type));
- return model_type;
-}
-
TEST_F(RegistrationManagerTest, RegisterType) {
const syncable::ModelType kModelTypes[] = {
syncable::BOOKMARKS,
@@ -85,7 +96,7 @@ TEST_F(RegistrationManagerTest, RegisterType) {
}
ASSERT_EQ(kModelTypeCount,
- fake_invalidation_client_.registered_oids.size());
+ fake_invalidation_client_.registered_types.size());
// Everything should be registered.
for (size_t i = 0; i < kModelTypeCount; ++i) {
@@ -95,8 +106,52 @@ TEST_F(RegistrationManagerTest, RegisterType) {
// Check object IDs.
for (size_t i = 0; i < kModelTypeCount; ++i) {
EXPECT_EQ(kModelTypes[i],
- ObjectIdToModelType(
- fake_invalidation_client_.registered_oids[i]));
+ fake_invalidation_client_.registered_types[i]);
+ }
+}
+
+TEST_F(RegistrationManagerTest, UnregisterType) {
+ const syncable::ModelType kModelTypes[] = {
+ syncable::BOOKMARKS,
+ syncable::PREFERENCES,
+ syncable::THEMES,
+ syncable::AUTOFILL,
+ syncable::EXTENSIONS,
+ };
+ const size_t kModelTypeCount = arraysize(kModelTypes);
+
+ // Register types.
+ for (size_t i = 0; i < kModelTypeCount; ++i) {
+ // Register twice; it shouldn't matter.
+ registration_manager_.RegisterType(kModelTypes[i]);
+ }
+
+ ASSERT_EQ(kModelTypeCount,
+ fake_invalidation_client_.registered_types.size());
+
+ // Everything should be registered.
+ for (size_t i = 0; i < kModelTypeCount; ++i) {
+ EXPECT_TRUE(registration_manager_.IsRegistered(kModelTypes[i]));
+ }
+
+ // Check object IDs.
+ for (size_t i = 0; i < kModelTypeCount; ++i) {
+ EXPECT_EQ(kModelTypes[i],
+ fake_invalidation_client_.registered_types[i]);
+ }
+
+ // Now unregister the extension.
+ registration_manager_.UnregisterType(syncable::EXTENSIONS);
+
+ // Check the count and the types currently registered to ensure extensions
+ // is unregistered.
+ ASSERT_EQ(kModelTypeCount - 1,
+ fake_invalidation_client_.registered_types.size());
+
+ // Check object IDs.
+ for (size_t i = 0; i < kModelTypeCount - 1; ++i) {
+ EXPECT_EQ(kModelTypes[i],
+ fake_invalidation_client_.registered_types[i]);
}
}
@@ -116,7 +171,7 @@ TEST_F(RegistrationManagerTest, MarkRegistrationLost) {
}
ASSERT_EQ(kModelTypeCount,
- fake_invalidation_client_.registered_oids.size());
+ fake_invalidation_client_.registered_types.size());
// All should be registered.
for (size_t i = 0; i < kModelTypeCount; ++i) {
@@ -129,7 +184,7 @@ TEST_F(RegistrationManagerTest, MarkRegistrationLost) {
}
ASSERT_EQ(2 * kModelTypeCount - 1,
- fake_invalidation_client_.registered_oids.size());
+ fake_invalidation_client_.registered_types.size());
// All should still be registered.
for (size_t i = 0; i < kModelTypeCount; ++i) {
@@ -153,7 +208,7 @@ TEST_F(RegistrationManagerTest, MarkAllRegistrationsLost) {
}
ASSERT_EQ(kModelTypeCount,
- fake_invalidation_client_.registered_oids.size());
+ fake_invalidation_client_.registered_types.size());
// All should be registered.
for (size_t i = 0; i < kModelTypeCount; ++i) {
@@ -168,7 +223,7 @@ TEST_F(RegistrationManagerTest, MarkAllRegistrationsLost) {
registration_manager_.MarkAllRegistrationsLost();
ASSERT_EQ(3 * kModelTypeCount - 1,
- fake_invalidation_client_.registered_oids.size());
+ fake_invalidation_client_.registered_types.size());
// All should still be registered.
for (size_t i = 0; i < kModelTypeCount; ++i) {
@@ -176,5 +231,4 @@ TEST_F(RegistrationManagerTest, MarkAllRegistrationsLost) {
}
}
-} // namespace
} // namespace notifier
diff --git a/chrome/browser/sync/notifier/server_notifier_thread.cc b/chrome/browser/sync/notifier/server_notifier_thread.cc
index e8ca751..9dd62bf 100644
--- a/chrome/browser/sync/notifier/server_notifier_thread.cc
+++ b/chrome/browser/sync/notifier/server_notifier_thread.cc
@@ -45,7 +45,28 @@ void ServerNotifierThread::SubscribeForUpdates(
worker_message_loop()->PostTask(
FROM_HERE,
NewRunnableMethod(
- this, &ServerNotifierThread::RegisterTypesAndSignalSubscribed));
+ this, &ServerNotifierThread::RegisterTypes));
+
+ worker_message_loop()->PostTask(
+ FROM_HERE,
+ NewRunnableMethod(
+ this, &ServerNotifierThread::SignalSubscribed));
+}
+
+void ServerNotifierThread::UpdateEnabledTypes(
+ const syncable::ModelTypeSet& types) {
+ DCHECK_EQ(MessageLoop::current(), parent_message_loop_);
+ worker_message_loop()->PostTask(
+ FROM_HERE,
+ NewRunnableMethod(
+ this,
+ &ServerNotifierThread::SetRegisteredTypes,
+ types));
+
+ worker_message_loop()->PostTask(
+ FROM_HERE,
+ NewRunnableMethod(
+ this, &ServerNotifierThread::RegisterTypes));
}
void ServerNotifierThread::Logout() {
@@ -124,16 +145,20 @@ void ServerNotifierThread::DoListenForUpdates() {
const std::string& client_info = webkit_glue::GetUserAgent(GURL());
chrome_invalidation_client_->Start(
kClientId, client_info, state_, this, this, base_task_);
+ RegisterTypes();
state_.clear();
}
}
-void ServerNotifierThread::RegisterTypesAndSignalSubscribed() {
+void ServerNotifierThread::RegisterTypes() {
DCHECK_EQ(MessageLoop::current(), worker_message_loop());
if (!chrome_invalidation_client_.get()) {
return;
}
- chrome_invalidation_client_->RegisterTypes();
+ chrome_invalidation_client_->RegisterTypes(registered_types_);
+}
+
+void ServerNotifierThread::SignalSubscribed() {
observers_->Notify(&Observer::OnSubscriptionStateChange, true);
}
@@ -142,4 +167,8 @@ void ServerNotifierThread::StopInvalidationListener() {
chrome_invalidation_client_.reset();
}
+void ServerNotifierThread::SetRegisteredTypes(syncable::ModelTypeSet types) {
+ registered_types_ = types;
+}
+
} // namespace sync_notifier
diff --git a/chrome/browser/sync/notifier/server_notifier_thread.h b/chrome/browser/sync/notifier/server_notifier_thread.h
index b0b9819..4800735 100644
--- a/chrome/browser/sync/notifier/server_notifier_thread.h
+++ b/chrome/browser/sync/notifier/server_notifier_thread.h
@@ -72,12 +72,16 @@ class ServerNotifierThread
// StateWriter implementation.
virtual void WriteState(const std::string& state);
+ virtual void UpdateEnabledTypes(const syncable::ModelTypeSet& types);
+
private:
// Posted to the worker thread by ListenForUpdates().
void DoListenForUpdates();
// Posted to the worker thread by SubscribeForUpdates().
- void RegisterTypesAndSignalSubscribed();
+ void RegisterTypes();
+
+ void SignalSubscribed();
// Posted to the worker thread by Logout().
void StopInvalidationListener();
@@ -89,6 +93,10 @@ class ServerNotifierThread
// |state_writers_|.
StateWriter* state_writer_;
scoped_ptr<ChromeInvalidationClient> chrome_invalidation_client_;
+
+ syncable::ModelTypeSet registered_types_;
+
+ void SetRegisteredTypes(syncable::ModelTypeSet types);
};
} // namespace sync_notifier
diff --git a/chrome/browser/sync/notifier/server_notifier_thread_unittest.cc b/chrome/browser/sync/notifier/server_notifier_thread_unittest.cc
index 416ffe5..3873c42 100644
--- a/chrome/browser/sync/notifier/server_notifier_thread_unittest.cc
+++ b/chrome/browser/sync/notifier/server_notifier_thread_unittest.cc
@@ -20,7 +20,7 @@ namespace sync_notifier {
class FakeServerNotifierThread : public ServerNotifierThread {
public:
- FakeServerNotifierThread()
+ FakeServerNotifierThread()
: ServerNotifierThread(notifier::NotifierOptions(), "fake state",
ALLOW_THIS_IN_INITIALIZER_LIST(this)) {}
@@ -110,6 +110,18 @@ class ServerNotifierThreadTest : public testing::Test {
MessageLoop message_loop_;
};
+syncable::ModelTypeSet GetTypeSetWithAllTypes() {
+ syncable::ModelTypeSet all_types;
+
+ for (int i = syncable::FIRST_REAL_MODEL_TYPE;
+ i < syncable::MODEL_TYPE_COUNT; ++i) {
+ syncable::ModelType model_type = syncable::ModelTypeFromInt(i);
+ all_types.insert(model_type);
+ }
+
+ return all_types;
+}
+
TEST_F(ServerNotifierThreadTest, Basic) {
FakeServerNotifierThread server_notifier_thread;