diff options
author | rlarocque@chromium.org <rlarocque@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-17 19:28:46 +0000 |
---|---|---|
committer | rlarocque@chromium.org <rlarocque@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-17 19:28:46 +0000 |
commit | 001bbfdc4da9f8aec8e1a30770a1e4cef2347e0a (patch) | |
tree | 1017f4628ef475caaf86573c9c8ec2e7aa341997 /sync/notifier | |
parent | cb363e702cb70f99c707be975e57015b438eaef0 (diff) | |
download | chromium_src-001bbfdc4da9f8aec8e1a30770a1e4cef2347e0a.zip chromium_src-001bbfdc4da9f8aec8e1a30770a1e4cef2347e0a.tar.gz chromium_src-001bbfdc4da9f8aec8e1a30770a1e4cef2347e0a.tar.bz2 |
Move sync/notifier to components/invalidation
Moves all code remaining in sync/notifier to components/invalidation.
Updates gyp files, DEPS, and #includes accordingly.
In terms of program behavior, this should be one big no-op. This CL
contains no non-trivial code changes.
In terms of the build system, this is a significant change. Symbols
that were previously exported through sync_notifier and related targets
have moved into the components/invalidation related targets. Some
SYNC_EXPORT macros have been replaced with INVALIDATION_EXPORT, which is
significant since INVALIDATION_EXPORT is currently a no-op but
SYNC_EXPORT has meaning under some build configurations.
Unlike most other files in sync/notifier, invalidation_util.{cc,h} was
moved to sync/internal_api/public/base. This is so it could be
referenced from sync/internal_api/public/base/invalidation.cc. This is
a slight regression, but it should be fixed in the next CL when we move
all invalidations-related code out of sync/internal_api.
TBR=rogerta,benwells
BUG=259559
Review URL: https://codereview.chromium.org/387733004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@283840 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'sync/notifier')
27 files changed, 0 insertions, 2980 deletions
diff --git a/sync/notifier/DEPS b/sync/notifier/DEPS deleted file mode 100644 index 0856674..0000000 --- a/sync/notifier/DEPS +++ /dev/null @@ -1,7 +0,0 @@ -include_rules = [ - "+google/cacheinvalidation", - - "+sync/base", - "+sync/internal_api/public/base", - "+sync/internal_api/public/util", -] diff --git a/sync/notifier/ack_handler.cc b/sync/notifier/ack_handler.cc deleted file mode 100644 index 3b31b2b..0000000 --- a/sync/notifier/ack_handler.cc +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2013 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 "sync/notifier/ack_handler.h" - -#include "sync/internal_api/public/base/invalidation.h" - -namespace syncer { - -AckHandler::AckHandler() {} - -AckHandler::~AckHandler() {} - -} // namespace syncer diff --git a/sync/notifier/ack_handler.h b/sync/notifier/ack_handler.h deleted file mode 100644 index f1fc16f..0000000 --- a/sync/notifier/ack_handler.h +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2013 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 SYNC_NOTIFIER_ACK_HANDLER_H_ -#define SYNC_NOTIFIER_ACK_HANDLER_H_ - -#include <vector> - -#include "sync/base/sync_export.h" - -namespace invalidation { -class ObjectId; -} // namespace invalidation - -namespace syncer { - -class AckHandle; - -// An interface for classes that keep track of invalidation acknowledgements. -// -// We don't expect to support more than one "real" implementation of AckHandler, -// but this interface is very useful for testing and implementation hiding. -class SYNC_EXPORT AckHandler { - public: - AckHandler(); - virtual ~AckHandler() = 0; - - // Record the local acknowledgement of an invalidation identified by |handle|. - virtual void Acknowledge( - const invalidation::ObjectId& id, - const AckHandle& handle) = 0; - - // Record the drop of an invalidation identified by |handle|. - virtual void Drop( - const invalidation::ObjectId& id, - const AckHandle& handle) = 0; -}; - -} // namespace syncer - -#endif // SYNC_NOTIFIER_ACK_HANDLER_H_ diff --git a/sync/notifier/invalidation_handler.cc b/sync/notifier/invalidation_handler.cc deleted file mode 100644 index 8ec9440..0000000 --- a/sync/notifier/invalidation_handler.cc +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2014 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 "sync/notifier/invalidation_handler.h" - -namespace syncer { - -InvalidationHandler::InvalidationHandler() { -} - -InvalidationHandler::~InvalidationHandler() { -} - -} // namespace syncer diff --git a/sync/notifier/invalidation_handler.h b/sync/notifier/invalidation_handler.h deleted file mode 100644 index 9cd41fe..0000000 --- a/sync/notifier/invalidation_handler.h +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2012 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 SYNC_NOTIFIER_INVALIDATION_HANDLER_H_ -#define SYNC_NOTIFIER_INVALIDATION_HANDLER_H_ - -#include <string> - -#include "sync/base/sync_export.h" -#include "sync/internal_api/public/base/invalidator_state.h" - -namespace syncer { - -class ObjectIdInvalidationMap; - -class SYNC_EXPORT InvalidationHandler { - public: - InvalidationHandler(); - - // Called when the invalidator state changes. - virtual void OnInvalidatorStateChange(InvalidatorState state) = 0; - - // Called when a invalidation is received. The per-id states are in - // |id_state_map| and the source is in |source|. Note that this may be - // called regardless of the current invalidator state. - virtual void OnIncomingInvalidation( - const ObjectIdInvalidationMap& invalidation_map) = 0; - - virtual std::string GetOwnerName() const = 0; - - protected: - virtual ~InvalidationHandler(); -}; - -} // namespace syncer - -#endif // SYNC_NOTIFIER_INVALIDATION_HANDLER_H_ diff --git a/sync/notifier/invalidation_state_tracker.cc b/sync/notifier/invalidation_state_tracker.cc deleted file mode 100644 index ab3ce17..0000000 --- a/sync/notifier/invalidation_state_tracker.cc +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2014 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 "sync/notifier/invalidation_state_tracker.h" - -namespace syncer { - -InvalidationStateTracker::InvalidationStateTracker() {} - -InvalidationStateTracker::~InvalidationStateTracker() {} - -} // namespace syncer diff --git a/sync/notifier/invalidation_state_tracker.h b/sync/notifier/invalidation_state_tracker.h deleted file mode 100644 index 6f4c133..0000000 --- a/sync/notifier/invalidation_state_tracker.h +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright 2012 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. -// -// An InvalidationStateTracker is an interface that handles persisting state -// needed for invalidations. Currently, it is responsible for managing the -// following information: -// - Max version seen from the invalidation server to help dedupe invalidations. -// - Bootstrap data for the invalidation client. -// - Payloads and locally generated ack handles, to support local acking. - -#ifndef SYNC_NOTIFIER_INVALIDATION_STATE_TRACKER_H_ -#define SYNC_NOTIFIER_INVALIDATION_STATE_TRACKER_H_ - -#include <map> -#include <string> - -#include "base/basictypes.h" -#include "base/callback_forward.h" -#include "base/memory/ref_counted.h" -#include "google/cacheinvalidation/include/types.h" -#include "sync/base/sync_export.h" -#include "sync/internal_api/public/base/invalidation.h" -#include "sync/notifier/invalidation_util.h" -#include "sync/notifier/unacked_invalidation_set.h" - -namespace base { -class TaskRunner; -} // namespace base - -namespace syncer { - -class SYNC_EXPORT InvalidationStateTracker { - public: - InvalidationStateTracker(); - virtual ~InvalidationStateTracker(); - - // The per-client unique ID used to register the invalidation client with the - // server. This is used to squelch invalidation notifications that originate - // from changes made by this client. Setting the client ID clears all other - // state. - virtual void ClearAndSetNewClientId(const std::string& data) = 0; - virtual std::string GetInvalidatorClientId() const = 0; - - // Used by invalidation::InvalidationClient for persistence. |data| is an - // opaque blob that an invalidation client can use after a restart to - // bootstrap itself. |data| is binary data (not valid UTF8, embedded nulls, - // etc). - virtual void SetBootstrapData(const std::string& data) = 0; - virtual std::string GetBootstrapData() const = 0; - - // Used to store invalidations that have been acked to the server, but not yet - // handled by our clients. We store these invalidations on disk so we won't - // lose them if we need to restart. - virtual void SetSavedInvalidations(const UnackedInvalidationsMap& states) = 0; - virtual UnackedInvalidationsMap GetSavedInvalidations() const = 0; - - // Erases invalidation versions, client ID, and state stored on disk. - virtual void Clear() = 0; -}; - -} // namespace syncer - -#endif // SYNC_NOTIFIER_INVALIDATION_STATE_TRACKER_H_ diff --git a/sync/notifier/invalidation_util.cc b/sync/notifier/invalidation_util.cc deleted file mode 100644 index 27acd38..0000000 --- a/sync/notifier/invalidation_util.cc +++ /dev/null @@ -1,131 +0,0 @@ -// Copyright (c) 2012 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 "sync/notifier/invalidation_util.h" - -#include <ostream> -#include <sstream> - -#include "base/json/json_writer.h" -#include "base/memory/scoped_ptr.h" -#include "base/values.h" -#include "google/cacheinvalidation/include/types.h" -#include "google/cacheinvalidation/types.pb.h" -#include "sync/internal_api/public/base/invalidation.h" - -namespace invalidation { -void PrintTo(const invalidation::ObjectId& id, std::ostream* os) { - *os << syncer::ObjectIdToString(id); -} -} // namespace invalidation - -namespace syncer { - -bool ObjectIdLessThan::operator()(const invalidation::ObjectId& lhs, - const invalidation::ObjectId& rhs) const { - return (lhs.source() < rhs.source()) || - (lhs.source() == rhs.source() && lhs.name() < rhs.name()); -} - -bool InvalidationVersionLessThan::operator()( - const Invalidation& a, - const Invalidation& b) const { - DCHECK(a.object_id() == b.object_id()) - << "a: " << ObjectIdToString(a.object_id()) << ", " - << "b: " << ObjectIdToString(a.object_id()); - - if (a.is_unknown_version() && !b.is_unknown_version()) - return true; - - if (!a.is_unknown_version() && b.is_unknown_version()) - return false; - - if (a.is_unknown_version() && b.is_unknown_version()) - return false; - - return a.version() < b.version(); -} - -bool RealModelTypeToObjectId(ModelType model_type, - invalidation::ObjectId* object_id) { - std::string notification_type; - if (!RealModelTypeToNotificationType(model_type, ¬ification_type)) { - return false; - } - object_id->Init(ipc::invalidation::ObjectSource::CHROME_SYNC, - notification_type); - return true; -} - -bool ObjectIdToRealModelType(const invalidation::ObjectId& object_id, - ModelType* model_type) { - return NotificationTypeToRealModelType(object_id.name(), model_type); -} - -scoped_ptr<base::DictionaryValue> ObjectIdToValue( - const invalidation::ObjectId& object_id) { - scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); - value->SetInteger("source", object_id.source()); - value->SetString("name", object_id.name()); - return value.Pass(); -} - -bool ObjectIdFromValue(const base::DictionaryValue& value, - invalidation::ObjectId* out) { - *out = invalidation::ObjectId(); - std::string name; - int source = 0; - if (!value.GetInteger("source", &source) || - !value.GetString("name", &name)) { - return false; - } - *out = invalidation::ObjectId(source, name); - return true; -} - -std::string ObjectIdToString( - const invalidation::ObjectId& object_id) { - scoped_ptr<base::DictionaryValue> value(ObjectIdToValue(object_id)); - std::string str; - base::JSONWriter::Write(value.get(), &str); - return str; -} - -ObjectIdSet ModelTypeSetToObjectIdSet(ModelTypeSet model_types) { - ObjectIdSet ids; - for (ModelTypeSet::Iterator it = model_types.First(); it.Good(); it.Inc()) { - invalidation::ObjectId model_type_as_id; - if (!RealModelTypeToObjectId(it.Get(), &model_type_as_id)) { - DLOG(WARNING) << "Invalid model type " << it.Get(); - continue; - } - ids.insert(model_type_as_id); - } - return ids; -} - -ModelTypeSet ObjectIdSetToModelTypeSet(const ObjectIdSet& ids) { - ModelTypeSet model_types; - for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { - ModelType model_type; - if (!ObjectIdToRealModelType(*it, &model_type)) { - DLOG(WARNING) << "Invalid object ID " << ObjectIdToString(*it); - continue; - } - model_types.Put(model_type); - } - return model_types; -} - -std::string InvalidationToString( - const invalidation::Invalidation& invalidation) { - std::stringstream ss; - ss << "{ "; - ss << "object_id: " << ObjectIdToString(invalidation.object_id()) << ", "; - ss << "version: " << invalidation.version(); - ss << " }"; - return ss.str(); -} - -} // namespace syncer diff --git a/sync/notifier/invalidation_util.h b/sync/notifier/invalidation_util.h deleted file mode 100644 index eac4bac..0000000 --- a/sync/notifier/invalidation_util.h +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright 2012 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. -// -// Various utilities for dealing with invalidation data types. - -#ifndef SYNC_NOTIFIER_INVALIDATION_UTIL_H_ -#define SYNC_NOTIFIER_INVALIDATION_UTIL_H_ - -#include <iosfwd> -#include <map> -#include <set> -#include <string> - -#include "base/memory/scoped_ptr.h" -#include "sync/base/sync_export.h" -#include "sync/internal_api/public/base/model_type.h" - -namespace base { -class DictionaryValue; -} // namespace - -namespace invalidation { - -class Invalidation; -class ObjectId; - -// Gmock print helper -SYNC_EXPORT_PRIVATE void PrintTo(const invalidation::ObjectId& id, - std::ostream* os); - -} // namespace invalidation - -namespace syncer { - -class Invalidation; - -struct SYNC_EXPORT ObjectIdLessThan { - bool operator()(const invalidation::ObjectId& lhs, - const invalidation::ObjectId& rhs) const; -}; - -struct SYNC_EXPORT InvalidationVersionLessThan { - bool operator()(const syncer::Invalidation& a, - const syncer::Invalidation& b) const; -}; - -typedef std::set<invalidation::ObjectId, ObjectIdLessThan> ObjectIdSet; - -typedef std::map<invalidation::ObjectId, int, ObjectIdLessThan> - ObjectIdCountMap; - -SYNC_EXPORT bool RealModelTypeToObjectId(ModelType model_type, - invalidation::ObjectId* object_id); - -bool ObjectIdToRealModelType(const invalidation::ObjectId& object_id, - ModelType* model_type); - -// Caller owns the returned DictionaryValue. -scoped_ptr<base::DictionaryValue> ObjectIdToValue( - const invalidation::ObjectId& object_id); - -bool ObjectIdFromValue(const base::DictionaryValue& value, - invalidation::ObjectId* out); - -SYNC_EXPORT_PRIVATE std::string ObjectIdToString( - const invalidation::ObjectId& object_id); - -SYNC_EXPORT_PRIVATE ObjectIdSet ModelTypeSetToObjectIdSet(ModelTypeSet models); -ModelTypeSet ObjectIdSetToModelTypeSet(const ObjectIdSet& ids); - -std::string InvalidationToString( - const invalidation::Invalidation& invalidation); - -} // namespace syncer - -#endif // SYNC_NOTIFIER_INVALIDATION_UTIL_H_ diff --git a/sync/notifier/invalidator.cc b/sync/notifier/invalidator.cc deleted file mode 100644 index 5508b89..0000000 --- a/sync/notifier/invalidator.cc +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2014 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 "sync/notifier/invalidator.h" - -namespace syncer { - -Invalidator::Invalidator() { -} - -Invalidator::~Invalidator() { -} - -} // namespace syncer diff --git a/sync/notifier/invalidator.h b/sync/notifier/invalidator.h deleted file mode 100644 index 7a75fbb..0000000 --- a/sync/notifier/invalidator.h +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright 2012 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. -// -// Interface to the invalidator, which is an object that receives -// invalidations for registered object IDs. The corresponding -// InvalidationHandler is notifier when such an event occurs. - -#ifndef SYNC_NOTIFIER_INVALIDATOR_H_ -#define SYNC_NOTIFIER_INVALIDATOR_H_ - -#include <string> - -#include "base/callback.h" -#include "sync/base/sync_export.h" -#include "sync/internal_api/public/base/invalidator_state.h" -#include "sync/internal_api/public/base/model_type.h" -#include "sync/notifier/invalidation_util.h" - -namespace syncer { -class InvalidationHandler; - -class SYNC_EXPORT Invalidator { - public: - Invalidator(); - virtual ~Invalidator(); - - // Clients should follow the pattern below: - // - // When starting the client: - // - // invalidator->RegisterHandler(client_handler); - // - // When the set of IDs to register changes for the client during its lifetime - // (i.e., between calls to RegisterHandler(client_handler) and - // UnregisterHandler(client_handler): - // - // invalidator->UpdateRegisteredIds(client_handler, client_ids); - // - // When shutting down the client for profile shutdown: - // - // invalidator->UnregisterHandler(client_handler); - // - // Note that there's no call to UpdateRegisteredIds() -- this is because the - // invalidation API persists registrations across browser restarts. - // - // When permanently shutting down the client, e.g. when disabling the related - // feature: - // - // invalidator->UpdateRegisteredIds(client_handler, ObjectIdSet()); - // invalidator->UnregisterHandler(client_handler); - // - // It is an error to have registered handlers when an invalidator is - // destroyed; clients must ensure that they unregister themselves - // before then. - - // Starts sending notifications to |handler|. |handler| must not be NULL, - // and it must not already be registered. - virtual void RegisterHandler(InvalidationHandler* handler) = 0; - - // Updates the set of ObjectIds associated with |handler|. |handler| must - // not be NULL, and must already be registered. An ID must be registered for - // at most one handler. - virtual void UpdateRegisteredIds(InvalidationHandler* handler, - const ObjectIdSet& ids) = 0; - - // Stops sending notifications to |handler|. |handler| must not be NULL, and - // it must already be registered. Note that this doesn't unregister the IDs - // associated with |handler|. - virtual void UnregisterHandler(InvalidationHandler* handler) = 0; - - // Returns the current invalidator state. When called from within - // InvalidationHandler::OnInvalidatorStateChange(), this must return - // the updated state. - virtual InvalidatorState GetInvalidatorState() const = 0; - - // The observers won't be notified of any notifications until - // UpdateCredentials is called at least once. It can be called more than - // once. - virtual void UpdateCredentials( - const std::string& email, const std::string& token) = 0; - - // Requests internal detailed status to be posted back to the callback. - virtual void RequestDetailedStatus( - base::Callback<void(const base::DictionaryValue&)> callback) const = 0; -}; -} // namespace syncer - -#endif // SYNC_NOTIFIER_INVALIDATOR_H_ diff --git a/sync/notifier/mock_ack_handler.cc b/sync/notifier/mock_ack_handler.cc deleted file mode 100644 index 60300a2..0000000 --- a/sync/notifier/mock_ack_handler.cc +++ /dev/null @@ -1,123 +0,0 @@ -// Copyright 2013 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 "sync/notifier/mock_ack_handler.h" - -#include "sync/internal_api/public/base/ack_handle.h" -#include "sync/internal_api/public/base/invalidation.h" - -namespace syncer { - -namespace { - -struct AckHandleMatcher { - AckHandleMatcher(const AckHandle& handle); - bool operator()(const syncer::Invalidation& invalidation) const; - - syncer::AckHandle handle_; -}; - -AckHandleMatcher::AckHandleMatcher(const AckHandle& handle) - : handle_(handle) {} - -bool AckHandleMatcher::operator()( - const syncer::Invalidation& invalidation) const { - return handle_.Equals(invalidation.ack_handle()); -} - -} // namespace - -MockAckHandler::MockAckHandler() {} - -MockAckHandler::~MockAckHandler() {} - -void MockAckHandler::RegisterInvalidation(Invalidation* invalidation) { - unacked_invalidations_.push_back(*invalidation); - invalidation->set_ack_handler(WeakHandleThis()); -} - -void MockAckHandler::RegisterUnsentInvalidation(Invalidation* invalidation) { - unsent_invalidations_.push_back(*invalidation); -} - -bool MockAckHandler::IsUnacked(const Invalidation& invalidation) const { - AckHandleMatcher matcher(invalidation.ack_handle()); - InvalidationVector::const_iterator it = std::find_if( - unacked_invalidations_.begin(), - unacked_invalidations_.end(), - matcher); - return it != unacked_invalidations_.end(); -} - -bool MockAckHandler::IsAcknowledged(const Invalidation& invalidation) const { - AckHandleMatcher matcher(invalidation.ack_handle()); - InvalidationVector::const_iterator it = std::find_if( - acked_invalidations_.begin(), - acked_invalidations_.end(), - matcher); - return it != acked_invalidations_.end(); -} - -bool MockAckHandler::IsDropped(const Invalidation& invalidation) const { - AckHandleMatcher matcher(invalidation.ack_handle()); - InvalidationVector::const_iterator it = std::find_if( - dropped_invalidations_.begin(), - dropped_invalidations_.end(), - matcher); - return it != dropped_invalidations_.end(); -} - -bool MockAckHandler::IsUnsent(const Invalidation& invalidation) const { - AckHandleMatcher matcher(invalidation.ack_handle()); - InvalidationVector::const_iterator it1 = std::find_if( - unsent_invalidations_.begin(), - unsent_invalidations_.end(), - matcher); - return it1 != unsent_invalidations_.end(); -} - -bool MockAckHandler::AllInvalidationsAccountedFor() const { - return unacked_invalidations_.empty() && unrecovered_drop_events_.empty(); -} - -void MockAckHandler::Acknowledge( - const invalidation::ObjectId& id, - const AckHandle& handle) { - AckHandleMatcher matcher(handle); - InvalidationVector::iterator it = std::find_if( - unacked_invalidations_.begin(), - unacked_invalidations_.end(), - matcher); - if (it != unacked_invalidations_.end()) { - acked_invalidations_.push_back(*it); - unacked_invalidations_.erase(it); - } - - IdHandleMap::iterator it2 = unrecovered_drop_events_.find(id); - if (it2 != unrecovered_drop_events_.end() && it2->second.Equals(handle)) { - unrecovered_drop_events_.erase(it2); - } -} - -void MockAckHandler::Drop( - const invalidation::ObjectId& id, - const AckHandle& handle) { - AckHandleMatcher matcher(handle); - InvalidationVector::iterator it = std::find_if( - unacked_invalidations_.begin(), - unacked_invalidations_.end(), - matcher); - if (it != unacked_invalidations_.end()) { - dropped_invalidations_.push_back(*it); - unacked_invalidations_.erase(it); - } - unrecovered_drop_events_.erase(id); - unrecovered_drop_events_.insert(std::make_pair(id, handle)); -} - -WeakHandle<AckHandler> MockAckHandler::WeakHandleThis() { - return WeakHandle<AckHandler>(AsWeakPtr()); -} - -} // namespace syncer diff --git a/sync/notifier/mock_ack_handler.h b/sync/notifier/mock_ack_handler.h deleted file mode 100644 index 08f0539..0000000 --- a/sync/notifier/mock_ack_handler.h +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright 2013 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 SYNC_NOTIFIER_MOCK_ACK_HANDLER_H_ -#define SYNC_NOTIFIER_MOCK_ACK_HANDLER_H_ - -#include <map> -#include <vector> - -#include "base/compiler_specific.h" -#include "base/memory/weak_ptr.h" -#include "sync/base/sync_export.h" -#include "sync/internal_api/public/util/weak_handle.h" -#include "sync/notifier/ack_handler.h" -#include "sync/notifier/invalidation_util.h" - -namespace syncer { - -class Invalidation; - -// This AckHandler implementation colaborates with the FakeInvalidationService -// to enable unit tests to assert that invalidations are being acked properly. -class SYNC_EXPORT MockAckHandler - : public AckHandler, - public base::SupportsWeakPtr<MockAckHandler> { - public: - MockAckHandler(); - virtual ~MockAckHandler(); - - // Sets up some internal state to track this invalidation, and modifies it so - // that its Acknowledge() and Drop() methods will route back to us. - void RegisterInvalidation(Invalidation* invalidation); - - // No one was listening for this invalidation, so no one will receive it or - // ack it. We keep track of it anyway to let tests make assertions about it. - void RegisterUnsentInvalidation(Invalidation* invalidation); - - // Returns true if the specified invalidaition has been delivered, but has not - // been acknowledged yet. - bool IsUnacked(const Invalidation& invalidation) const; - - // Returns true if the specified invalidation has been delivered and - // acknowledged. - bool IsAcknowledged(const Invalidation& invalidation) const; - - // Returns true if the specified invalidation has been delivered and - // dropped. - bool IsDropped(const Invalidation& invalidation) const; - - // Returns true if the specified invalidation was never delivered. - bool IsUnsent(const Invalidation& invalidation) const; - - // Retruns true if all invalidations have been acked and all drops recovered. - bool AllInvalidationsAccountedFor() const; - - // Implementation of AckHandler. - virtual void Acknowledge( - const invalidation::ObjectId& id, - const AckHandle& handle) OVERRIDE; - virtual void Drop( - const invalidation::ObjectId& id, - const AckHandle& handle) OVERRIDE; - - private: - typedef std::vector<syncer::Invalidation> InvalidationVector; - typedef std::map<invalidation::ObjectId, - AckHandle, - ObjectIdLessThan> IdHandleMap; - - WeakHandle<AckHandler> WeakHandleThis(); - - InvalidationVector unsent_invalidations_; - InvalidationVector unacked_invalidations_; - InvalidationVector acked_invalidations_; - InvalidationVector dropped_invalidations_; - - IdHandleMap unrecovered_drop_events_; -}; - -} // namespace syncer - -#endif // SYNC_NOTIFIER_MOCK_ACK_HANDLER_H_ diff --git a/sync/notifier/object_id_invalidation_map.cc b/sync/notifier/object_id_invalidation_map.cc deleted file mode 100644 index 02b5a21..0000000 --- a/sync/notifier/object_id_invalidation_map.cc +++ /dev/null @@ -1,121 +0,0 @@ -// Copyright 2013 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 "sync/notifier/object_id_invalidation_map.h" - -#include "base/json/json_string_value_serializer.h" - -namespace syncer { - -// static -ObjectIdInvalidationMap ObjectIdInvalidationMap::InvalidateAll( - const ObjectIdSet& ids) { - ObjectIdInvalidationMap invalidate_all; - for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { - invalidate_all.Insert(Invalidation::InitUnknownVersion(*it)); - } - return invalidate_all; -} - -ObjectIdInvalidationMap::ObjectIdInvalidationMap() {} - -ObjectIdInvalidationMap::~ObjectIdInvalidationMap() {} - -ObjectIdSet ObjectIdInvalidationMap::GetObjectIds() const { - ObjectIdSet ret; - for (IdToListMap::const_iterator it = map_.begin(); it != map_.end(); ++it) { - ret.insert(it->first); - } - return ret; -} - -bool ObjectIdInvalidationMap::Empty() const { - return map_.empty(); -} - -void ObjectIdInvalidationMap::Insert(const Invalidation& invalidation) { - map_[invalidation.object_id()].Insert(invalidation); -} - -ObjectIdInvalidationMap ObjectIdInvalidationMap::GetSubsetWithObjectIds( - const ObjectIdSet& ids) const { - IdToListMap new_map; - for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { - IdToListMap::const_iterator lookup = map_.find(*it); - if (lookup != map_.end()) { - new_map[*it] = lookup->second; - } - } - return ObjectIdInvalidationMap(new_map); -} - -const SingleObjectInvalidationSet& ObjectIdInvalidationMap::ForObject( - invalidation::ObjectId id) const { - IdToListMap::const_iterator lookup = map_.find(id); - DCHECK(lookup != map_.end()); - DCHECK(!lookup->second.IsEmpty()); - return lookup->second; -} - -void ObjectIdInvalidationMap::GetAllInvalidations( - std::vector<syncer::Invalidation>* out) const { - for (IdToListMap::const_iterator it = map_.begin(); it != map_.end(); ++it) { - out->insert(out->begin(), it->second.begin(), it->second.end()); - } -} -void ObjectIdInvalidationMap::AcknowledgeAll() const { - for (IdToListMap::const_iterator it1 = map_.begin(); - it1 != map_.end(); ++it1) { - for (SingleObjectInvalidationSet::const_iterator it2 = it1->second.begin(); - it2 != it1->second.end(); ++it2) { - it2->Acknowledge(); - } - } -} - -bool ObjectIdInvalidationMap::operator==( - const ObjectIdInvalidationMap& other) const { - return map_ == other.map_; -} - -scoped_ptr<base::ListValue> ObjectIdInvalidationMap::ToValue() const { - scoped_ptr<base::ListValue> value(new base::ListValue()); - for (IdToListMap::const_iterator it1 = map_.begin(); - it1 != map_.end(); ++it1) { - for (SingleObjectInvalidationSet::const_iterator it2 = - it1->second.begin(); it2 != it1->second.end(); ++it2) { - value->Append(it2->ToValue().release()); - } - } - return value.Pass(); -} - -bool ObjectIdInvalidationMap::ResetFromValue(const base::ListValue& value) { - map_.clear(); - for (size_t i = 0; i < value.GetSize(); ++i) { - const base::DictionaryValue* dict; - if (!value.GetDictionary(i, &dict)) { - return false; - } - scoped_ptr<Invalidation> invalidation = Invalidation::InitFromValue(*dict); - if (!invalidation) { - return false; - } - Insert(*invalidation.get()); - } - return true; -} - -std::string ObjectIdInvalidationMap::ToString() const { - std::string output; - JSONStringValueSerializer serializer(&output); - serializer.set_pretty_print(true); - serializer.Serialize(*ToValue().get()); - return output; -} - -ObjectIdInvalidationMap::ObjectIdInvalidationMap(const IdToListMap& map) - : map_(map) {} - -} // namespace syncer diff --git a/sync/notifier/object_id_invalidation_map.h b/sync/notifier/object_id_invalidation_map.h deleted file mode 100644 index 3494a62..0000000 --- a/sync/notifier/object_id_invalidation_map.h +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright 2013 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 SYNC_NOTIFIER_OBJECT_ID_INVALIDATION_MAP_H_ -#define SYNC_NOTIFIER_OBJECT_ID_INVALIDATION_MAP_H_ - -#include <map> -#include <vector> - -#include "sync/base/sync_export.h" -#include "sync/internal_api/public/base/invalidation.h" -#include "sync/notifier/invalidation_util.h" -#include "sync/notifier/single_object_invalidation_set.h" - -namespace syncer { - -// A set of notifications with some helper methods to organize them by object ID -// and version number. -class SYNC_EXPORT ObjectIdInvalidationMap { - public: - // Creates an invalidation map that includes an 'unknown version' - // invalidation for each specified ID in |ids|. - static ObjectIdInvalidationMap InvalidateAll(const ObjectIdSet& ids); - - ObjectIdInvalidationMap(); - ~ObjectIdInvalidationMap(); - - // Returns set of ObjectIds for which at least one invalidation is present. - ObjectIdSet GetObjectIds() const; - - // Returns true if this map contains no invalidations. - bool Empty() const; - - // Returns true if both maps contain the same set of invalidations. - bool operator==(const ObjectIdInvalidationMap& other) const; - - // Inserts a new invalidation into this map. - void Insert(const Invalidation& invalidation); - - // Returns a new map containing the subset of invaliations from this map - // whose IDs were in the specified |ids| set. - ObjectIdInvalidationMap GetSubsetWithObjectIds(const ObjectIdSet& ids) const; - - // Returns the subset of invalidations with IDs matching |id|. - const SingleObjectInvalidationSet& ForObject( - invalidation::ObjectId id) const; - - // Returns the contents of this map in a single vector. - void GetAllInvalidations(std::vector<syncer::Invalidation>* out) const; - - // Call Acknowledge() on all contained Invalidations. - void AcknowledgeAll() const; - - // Serialize this map to a value. - scoped_ptr<base::ListValue> ToValue() const; - - // Deserialize the value into a map and use it to re-initialize this object. - bool ResetFromValue(const base::ListValue& value); - - // Prints the contentes of this map as a human-readable string. - std::string ToString() const; - - private: - typedef std::map<invalidation::ObjectId, - SingleObjectInvalidationSet, - ObjectIdLessThan> IdToListMap; - - ObjectIdInvalidationMap(const IdToListMap& map); - - IdToListMap map_; -}; - -} // namespace syncer - -#endif // SYNC_NOTIFIER_OBJECT_ID_INVALIDATION_MAP_H_ diff --git a/sync/notifier/object_id_invalidation_map_unittest.cc b/sync/notifier/object_id_invalidation_map_unittest.cc deleted file mode 100644 index 1acd920..0000000 --- a/sync/notifier/object_id_invalidation_map_unittest.cc +++ /dev/null @@ -1,104 +0,0 @@ -// Copyright 2013 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 "sync/notifier/object_id_invalidation_map.h" - -#include "google/cacheinvalidation/types.pb.h" - -#include "testing/gtest/include/gtest/gtest.h" - -namespace syncer { - -namespace { - -class ObjectIdInvalidationMapTest : public testing::Test { - public: - ObjectIdInvalidationMapTest() - : kIdOne(ipc::invalidation::ObjectSource::TEST, "one"), - kIdTwo(ipc::invalidation::ObjectSource::TEST, "two"), - kInv1(Invalidation::Init(kIdOne, 10, "ten")) { - set1.insert(kIdOne); - set2.insert(kIdTwo); - all_set.insert(kIdOne); - all_set.insert(kIdTwo); - - one_invalidation.Insert(kInv1); - invalidate_all = ObjectIdInvalidationMap::InvalidateAll(all_set); - } - - protected: - const invalidation::ObjectId kIdOne; - const invalidation::ObjectId kIdTwo; - const Invalidation kInv1; - - ObjectIdSet set1; - ObjectIdSet set2; - ObjectIdSet all_set; - ObjectIdInvalidationMap empty; - ObjectIdInvalidationMap one_invalidation; - ObjectIdInvalidationMap invalidate_all; -}; - -TEST_F(ObjectIdInvalidationMapTest, Empty) { - EXPECT_TRUE(empty.Empty()); - EXPECT_FALSE(one_invalidation.Empty()); - EXPECT_FALSE(invalidate_all.Empty()); -} - -TEST_F(ObjectIdInvalidationMapTest, Equality) { - ObjectIdInvalidationMap empty2; - EXPECT_TRUE(empty == empty2); - - ObjectIdInvalidationMap one_invalidation2; - one_invalidation2.Insert(kInv1); - EXPECT_TRUE(one_invalidation == one_invalidation2); - - EXPECT_FALSE(empty == invalidate_all); -} - -TEST_F(ObjectIdInvalidationMapTest, GetObjectIds) { - EXPECT_EQ(ObjectIdSet(), empty.GetObjectIds()); - EXPECT_EQ(set1, one_invalidation.GetObjectIds()); - EXPECT_EQ(all_set, invalidate_all.GetObjectIds()); -} - -TEST_F(ObjectIdInvalidationMapTest, GetSubsetWithObjectIds) { - EXPECT_TRUE(empty.GetSubsetWithObjectIds(set1).Empty()); - - EXPECT_TRUE(one_invalidation.GetSubsetWithObjectIds(set1) == - one_invalidation); - EXPECT_TRUE(one_invalidation.GetSubsetWithObjectIds(all_set) == - one_invalidation); - EXPECT_TRUE(one_invalidation.GetSubsetWithObjectIds(set2).Empty()); - - EXPECT_TRUE(invalidate_all.GetSubsetWithObjectIds(ObjectIdSet()).Empty()); -} - -TEST_F(ObjectIdInvalidationMapTest, SerializeEmpty) { - scoped_ptr<base::ListValue> value = empty.ToValue(); - ASSERT_TRUE(value.get()); - ObjectIdInvalidationMap deserialized; - deserialized.ResetFromValue(*value.get()); - EXPECT_TRUE(empty == deserialized); -} - -TEST_F(ObjectIdInvalidationMapTest, SerializeOneInvalidation) { - scoped_ptr<base::ListValue> value = one_invalidation.ToValue(); - ASSERT_TRUE(value.get()); - ObjectIdInvalidationMap deserialized; - deserialized.ResetFromValue(*value.get()); - EXPECT_TRUE(one_invalidation == deserialized); -} - -TEST_F(ObjectIdInvalidationMapTest, SerializeInvalidateAll) { - scoped_ptr<base::ListValue> value = invalidate_all.ToValue(); - ASSERT_TRUE(value.get()); - ObjectIdInvalidationMap deserialized; - deserialized.ResetFromValue(*value.get()); - EXPECT_TRUE(invalidate_all == deserialized); -} - -} // namespace - -} // namespace syncer diff --git a/sync/notifier/registration_manager.cc b/sync/notifier/registration_manager.cc deleted file mode 100644 index cb852e3..0000000 --- a/sync/notifier/registration_manager.cc +++ /dev/null @@ -1,305 +0,0 @@ -// Copyright (c) 2012 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 "sync/notifier/registration_manager.h" - -#include <algorithm> -#include <cstddef> -#include <iterator> -#include <string> -#include <utility> - -#include "base/rand_util.h" -#include "base/stl_util.h" -#include "google/cacheinvalidation/include/invalidation-client.h" -#include "google/cacheinvalidation/include/types.h" -#include "sync/notifier/invalidation_util.h" - -namespace syncer { - -RegistrationManager::PendingRegistrationInfo::PendingRegistrationInfo() {} - -RegistrationManager::RegistrationStatus::RegistrationStatus( - const invalidation::ObjectId& id, RegistrationManager* manager) - : id(id), - registration_manager(manager), - enabled(true), - state(invalidation::InvalidationListener::UNREGISTERED) { - DCHECK(registration_manager); -} - -RegistrationManager::RegistrationStatus::~RegistrationStatus() {} - -void RegistrationManager::RegistrationStatus::DoRegister() { - CHECK(enabled); - // We might be called explicitly, so stop the timer manually and - // reset the delay. - registration_timer.Stop(); - delay = base::TimeDelta(); - registration_manager->DoRegisterId(id); - DCHECK(!last_registration_request.is_null()); -} - -void RegistrationManager::RegistrationStatus::Disable() { - enabled = false; - state = invalidation::InvalidationListener::UNREGISTERED; - registration_timer.Stop(); - delay = base::TimeDelta(); -} - -const int RegistrationManager::kInitialRegistrationDelaySeconds = 5; -const int RegistrationManager::kRegistrationDelayExponent = 2; -const double RegistrationManager::kRegistrationDelayMaxJitter = 0.5; -const int RegistrationManager::kMinRegistrationDelaySeconds = 1; -// 1 hour. -const int RegistrationManager::kMaxRegistrationDelaySeconds = 60 * 60; - -RegistrationManager::RegistrationManager( - invalidation::InvalidationClient* invalidation_client) - : invalidation_client_(invalidation_client) { - DCHECK(invalidation_client_); -} - -RegistrationManager::~RegistrationManager() { - DCHECK(CalledOnValidThread()); - STLDeleteValues(®istration_statuses_); -} - -ObjectIdSet RegistrationManager::UpdateRegisteredIds(const ObjectIdSet& ids) { - DCHECK(CalledOnValidThread()); - - const ObjectIdSet& old_ids = GetRegisteredIds(); - const ObjectIdSet& to_register = ids; - ObjectIdSet to_unregister; - std::set_difference(old_ids.begin(), old_ids.end(), - ids.begin(), ids.end(), - std::inserter(to_unregister, to_unregister.begin()), - ObjectIdLessThan()); - - for (ObjectIdSet::const_iterator it = to_unregister.begin(); - it != to_unregister.end(); ++it) { - UnregisterId(*it); - } - - for (ObjectIdSet::const_iterator it = to_register.begin(); - it != to_register.end(); ++it) { - if (!ContainsKey(registration_statuses_, *it)) { - registration_statuses_.insert( - std::make_pair(*it, new RegistrationStatus(*it, this))); - } - if (!IsIdRegistered(*it)) { - TryRegisterId(*it, false /* is-retry */); - } - } - - return to_unregister; -} - -void RegistrationManager::MarkRegistrationLost( - const invalidation::ObjectId& id) { - DCHECK(CalledOnValidThread()); - RegistrationStatusMap::const_iterator it = registration_statuses_.find(id); - if (it == registration_statuses_.end()) { - DVLOG(1) << "Attempt to mark non-existent registration for " - << ObjectIdToString(id) << " as lost"; - return; - } - if (!it->second->enabled) { - return; - } - it->second->state = invalidation::InvalidationListener::UNREGISTERED; - bool is_retry = !it->second->last_registration_request.is_null(); - TryRegisterId(id, is_retry); -} - -void RegistrationManager::MarkAllRegistrationsLost() { - DCHECK(CalledOnValidThread()); - for (RegistrationStatusMap::const_iterator it = - registration_statuses_.begin(); - it != registration_statuses_.end(); ++it) { - if (IsIdRegistered(it->first)) { - MarkRegistrationLost(it->first); - } - } -} - -void RegistrationManager::DisableId(const invalidation::ObjectId& id) { - DCHECK(CalledOnValidThread()); - RegistrationStatusMap::const_iterator it = registration_statuses_.find(id); - if (it == registration_statuses_.end()) { - DVLOG(1) << "Attempt to disable non-existent registration for " - << ObjectIdToString(id); - return; - } - it->second->Disable(); -} - -// static -double RegistrationManager::CalculateBackoff( - double retry_interval, - double initial_retry_interval, - double min_retry_interval, - double max_retry_interval, - double backoff_exponent, - double jitter, - double max_jitter) { - // scaled_jitter lies in [-max_jitter, max_jitter]. - double scaled_jitter = jitter * max_jitter; - double new_retry_interval = - (retry_interval == 0.0) ? - (initial_retry_interval * (1.0 + scaled_jitter)) : - (retry_interval * (backoff_exponent + scaled_jitter)); - return std::max(min_retry_interval, - std::min(max_retry_interval, new_retry_interval)); -} - -ObjectIdSet RegistrationManager::GetRegisteredIdsForTest() const { - return GetRegisteredIds(); -} - -RegistrationManager::PendingRegistrationMap - RegistrationManager::GetPendingRegistrationsForTest() const { - DCHECK(CalledOnValidThread()); - PendingRegistrationMap pending_registrations; - for (RegistrationStatusMap::const_iterator it = - registration_statuses_.begin(); - it != registration_statuses_.end(); ++it) { - const invalidation::ObjectId& id = it->first; - RegistrationStatus* status = it->second; - if (status->registration_timer.IsRunning()) { - pending_registrations[id].last_registration_request = - status->last_registration_request; - pending_registrations[id].registration_attempt = - status->last_registration_attempt; - pending_registrations[id].delay = status->delay; - pending_registrations[id].actual_delay = - status->registration_timer.GetCurrentDelay(); - } - } - return pending_registrations; -} - -void RegistrationManager::FirePendingRegistrationsForTest() { - DCHECK(CalledOnValidThread()); - for (RegistrationStatusMap::const_iterator it = - registration_statuses_.begin(); - it != registration_statuses_.end(); ++it) { - if (it->second->registration_timer.IsRunning()) { - it->second->DoRegister(); - } - } -} - -double RegistrationManager::GetJitter() { - // |jitter| lies in [-1.0, 1.0), which is low-biased, but only - // barely. - // - // TODO(akalin): Fix the bias. - return 2.0 * base::RandDouble() - 1.0; -} - -void RegistrationManager::TryRegisterId(const invalidation::ObjectId& id, - bool is_retry) { - DCHECK(CalledOnValidThread()); - RegistrationStatusMap::const_iterator it = registration_statuses_.find(id); - if (it == registration_statuses_.end()) { - NOTREACHED() << "TryRegisterId called on " << ObjectIdToString(id) - << " which is not in the registration map"; - return; - } - RegistrationStatus* status = it->second; - if (!status->enabled) { - // Disabled, so do nothing. - return; - } - status->last_registration_attempt = base::Time::Now(); - if (is_retry) { - // If we're a retry, we must have tried at least once before. - DCHECK(!status->last_registration_request.is_null()); - // delay = max(0, (now - last request) + next_delay) - status->delay = - (status->last_registration_request - - status->last_registration_attempt) + - status->next_delay; - base::TimeDelta delay = - (status->delay <= base::TimeDelta()) ? - base::TimeDelta() : status->delay; - DVLOG(2) << "Registering " - << ObjectIdToString(id) << " in " - << delay.InMilliseconds() << " ms"; - status->registration_timer.Stop(); - status->registration_timer.Start(FROM_HERE, - delay, status, &RegistrationManager::RegistrationStatus::DoRegister); - double next_delay_seconds = - CalculateBackoff(static_cast<double>(status->next_delay.InSeconds()), - kInitialRegistrationDelaySeconds, - kMinRegistrationDelaySeconds, - kMaxRegistrationDelaySeconds, - kRegistrationDelayExponent, - GetJitter(), - kRegistrationDelayMaxJitter); - status->next_delay = - base::TimeDelta::FromSeconds(static_cast<int64>(next_delay_seconds)); - DVLOG(2) << "New next delay for " - << ObjectIdToString(id) << " is " - << status->next_delay.InSeconds() << " seconds"; - } else { - DVLOG(2) << "Not a retry -- registering " - << ObjectIdToString(id) << " immediately"; - status->delay = base::TimeDelta(); - status->next_delay = base::TimeDelta(); - status->DoRegister(); - } -} - -void RegistrationManager::DoRegisterId(const invalidation::ObjectId& id) { - DCHECK(CalledOnValidThread()); - invalidation_client_->Register(id); - RegistrationStatusMap::const_iterator it = registration_statuses_.find(id); - if (it == registration_statuses_.end()) { - NOTREACHED() << "DoRegisterId called on " << ObjectIdToString(id) - << " which is not in the registration map"; - return; - } - it->second->state = invalidation::InvalidationListener::REGISTERED; - it->second->last_registration_request = base::Time::Now(); -} - -void RegistrationManager::UnregisterId(const invalidation::ObjectId& id) { - DCHECK(CalledOnValidThread()); - invalidation_client_->Unregister(id); - RegistrationStatusMap::iterator it = registration_statuses_.find(id); - if (it == registration_statuses_.end()) { - NOTREACHED() << "UnregisterId called on " << ObjectIdToString(id) - << " which is not in the registration map"; - return; - } - delete it->second; - registration_statuses_.erase(it); -} - - -ObjectIdSet RegistrationManager::GetRegisteredIds() const { - DCHECK(CalledOnValidThread()); - ObjectIdSet ids; - for (RegistrationStatusMap::const_iterator it = - registration_statuses_.begin(); - it != registration_statuses_.end(); ++it) { - if (IsIdRegistered(it->first)) { - ids.insert(it->first); - } - } - return ids; -} - -bool RegistrationManager::IsIdRegistered( - const invalidation::ObjectId& id) const { - DCHECK(CalledOnValidThread()); - RegistrationStatusMap::const_iterator it = - registration_statuses_.find(id); - return it != registration_statuses_.end() && - it->second->state == invalidation::InvalidationListener::REGISTERED; -} - -} // namespace syncer diff --git a/sync/notifier/registration_manager.h b/sync/notifier/registration_manager.h deleted file mode 100644 index e190a7f..0000000 --- a/sync/notifier/registration_manager.h +++ /dev/null @@ -1,187 +0,0 @@ -// Copyright 2012 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. -// -// A class that manages the registration of types for server-issued -// notifications. - -#ifndef SYNC_NOTIFIER_REGISTRATION_MANAGER_H_ -#define SYNC_NOTIFIER_REGISTRATION_MANAGER_H_ - -#include <map> - -#include "base/basictypes.h" -#include "base/threading/non_thread_safe.h" -#include "base/time/time.h" -#include "base/timer/timer.h" -// For invalidation::InvalidationListener::RegistrationState. -#include "google/cacheinvalidation/include/invalidation-listener.h" -#include "google/cacheinvalidation/include/types.h" -#include "sync/base/sync_export.h" -#include "sync/notifier/invalidation_util.h" - -namespace syncer { - -using ::invalidation::InvalidationListener; - -// Manages the details of registering types for invalidation. -// Implements exponential backoff for repeated registration attempts -// to the invalidation client. -// -// TODO(akalin): Consolidate exponential backoff code. Other -// implementations include the syncer thread (both versions) and XMPP -// retries. The most sophisticated one is URLRequestThrottler; making -// that generic should work for everyone. -class SYNC_EXPORT_PRIVATE RegistrationManager : public base::NonThreadSafe { - public: - // Constants for exponential backoff (used by tests). - static const int kInitialRegistrationDelaySeconds; - static const int kRegistrationDelayExponent; - static const double kRegistrationDelayMaxJitter; - static const int kMinRegistrationDelaySeconds; - static const int kMaxRegistrationDelaySeconds; - - // Types used by testing functions. - struct PendingRegistrationInfo { - PendingRegistrationInfo(); - - // Last time a registration request was actually sent. - base::Time last_registration_request; - // Time the registration was attempted. - base::Time registration_attempt; - // The calculated delay of the pending registration (which may be - // negative). - base::TimeDelta delay; - // The delay of the timer, which should be max(delay, 0). - base::TimeDelta actual_delay; - }; - // Map of object IDs with pending registrations to info about the - // pending registration. - typedef std::map<invalidation::ObjectId, - PendingRegistrationInfo, - ObjectIdLessThan> - PendingRegistrationMap; - - // Does not take ownership of |invalidation_client_|. - explicit RegistrationManager( - invalidation::InvalidationClient* invalidation_client); - - virtual ~RegistrationManager(); - - // Registers all object IDs included in the given set (that are not - // already disabled) and unregisters all other object IDs. The return value is - // the set of IDs that was unregistered. - ObjectIdSet UpdateRegisteredIds(const ObjectIdSet& ids); - - // Marks the registration for the |id| lost and re-registers - // it (unless it's disabled). - void MarkRegistrationLost(const invalidation::ObjectId& id); - - // Marks registrations lost for all enabled object IDs and re-registers them. - void MarkAllRegistrationsLost(); - - // Marks the registration for the |id| permanently lost and blocks any future - // registration attempts. - void DisableId(const invalidation::ObjectId& id); - - // Calculate exponential backoff. |jitter| must be Uniform[-1.0, 1.0]. - static double CalculateBackoff(double retry_interval, - double initial_retry_interval, - double min_retry_interval, - double max_retry_interval, - double backoff_exponent, - double jitter, - double max_jitter); - - // The functions below should only be used in tests. - - // Gets all currently registered ids. - ObjectIdSet GetRegisteredIdsForTest() const; - - // Gets all pending registrations and their next min delays. - PendingRegistrationMap GetPendingRegistrationsForTest() const; - - // Run pending registrations immediately. - void FirePendingRegistrationsForTest(); - - protected: - // Overrideable for testing purposes. - virtual double GetJitter(); - - private: - struct RegistrationStatus { - RegistrationStatus(const invalidation::ObjectId& id, - RegistrationManager* manager); - ~RegistrationStatus(); - - // Calls registration_manager->DoRegister(model_type). (needed by - // |registration_timer|). Should only be called if |enabled| is - // true. - void DoRegister(); - - // Sets |enabled| to false and resets other variables. - void Disable(); - - // The object for which this is the status. - const invalidation::ObjectId id; - // The parent registration manager. - RegistrationManager* const registration_manager; - - // Whether this data type should be registered. Set to false if - // we get a non-transient registration failure. - bool enabled; - // The current registration state. - InvalidationListener::RegistrationState state; - // When we last sent a registration request. - base::Time last_registration_request; - // When we last tried to register. - base::Time last_registration_attempt; - // The calculated delay of any pending registration (which may be - // negative). - base::TimeDelta delay; - // The minimum time to wait until any next registration attempt. - // Increased after each consecutive failure. - base::TimeDelta next_delay; - // The actual timer for registration. - base::OneShotTimer<RegistrationStatus> registration_timer; - - DISALLOW_COPY_AND_ASSIGN(RegistrationStatus); - }; - typedef std::map<invalidation::ObjectId, - RegistrationStatus*, - ObjectIdLessThan> - RegistrationStatusMap; - - // Does nothing if the given id is disabled. Otherwise, if - // |is_retry| is not set, registers the given type immediately and - // resets all backoff parameters. If |is_retry| is set, registers - // the given type at some point in the future and increases the - // delay until the next retry. - void TryRegisterId(const invalidation::ObjectId& id, - bool is_retry); - - // Registers the given id, which must be valid, immediately. - // Updates |last_registration| in the appropriate - // RegistrationStatus. Should only be called by - // RegistrationStatus::DoRegister(). - void DoRegisterId(const invalidation::ObjectId& id); - - // Unregisters the given object ID. - void UnregisterId(const invalidation::ObjectId& id); - - // Gets all currently registered ids. - ObjectIdSet GetRegisteredIds() const; - - // Returns true iff the given object ID is registered. - bool IsIdRegistered(const invalidation::ObjectId& id) const; - - RegistrationStatusMap registration_statuses_; - // Weak pointer. - invalidation::InvalidationClient* invalidation_client_; - - DISALLOW_COPY_AND_ASSIGN(RegistrationManager); -}; - -} // namespace syncer - -#endif // SYNC_NOTIFIER_REGISTRATION_MANAGER_H_ diff --git a/sync/notifier/registration_manager_unittest.cc b/sync/notifier/registration_manager_unittest.cc deleted file mode 100644 index c8328fc..0000000 --- a/sync/notifier/registration_manager_unittest.cc +++ /dev/null @@ -1,433 +0,0 @@ -// Copyright (c) 2012 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 "sync/notifier/registration_manager.h" - -#include <algorithm> -#include <cmath> -#include <cstddef> -#include <deque> -#include <vector> - -#include "base/basictypes.h" -#include "base/message_loop/message_loop.h" -#include "base/stl_util.h" -#include "google/cacheinvalidation/include/invalidation-client.h" -#include "sync/notifier/invalidation_util.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace syncer { -namespace { - -// Fake registration manager that lets you override jitter. -class FakeRegistrationManager : public RegistrationManager { - public: - explicit FakeRegistrationManager( - invalidation::InvalidationClient* invalidation_client) - : RegistrationManager(invalidation_client), - jitter_(0.0) {} - - virtual ~FakeRegistrationManager() {} - - void SetJitter(double jitter) { - jitter_ = jitter; - } - - protected: - virtual double GetJitter() OVERRIDE { - return jitter_; - } - - private: - double jitter_; - - DISALLOW_COPY_AND_ASSIGN(FakeRegistrationManager); -}; - -// Fake invalidation client that just stores the currently-registered -// object IDs. -class FakeInvalidationClient : public invalidation::InvalidationClient { - public: - FakeInvalidationClient() {} - - virtual ~FakeInvalidationClient() {} - - void LoseRegistration(const invalidation::ObjectId& oid) { - EXPECT_TRUE(ContainsKey(registered_ids_, oid)); - registered_ids_.erase(oid); - } - - void LoseAllRegistrations() { - registered_ids_.clear(); - } - - // invalidation::InvalidationClient implementation. - - virtual void Start() OVERRIDE {} - virtual void Stop() OVERRIDE {} - virtual void Acknowledge(const invalidation::AckHandle& handle) OVERRIDE {} - - virtual void Register(const invalidation::ObjectId& oid) OVERRIDE { - EXPECT_FALSE(ContainsKey(registered_ids_, oid)); - registered_ids_.insert(oid); - } - - virtual void Register( - const std::vector<invalidation::ObjectId>& oids) OVERRIDE { - // Unused for now. - } - - virtual void Unregister(const invalidation::ObjectId& oid) OVERRIDE { - EXPECT_TRUE(ContainsKey(registered_ids_, oid)); - registered_ids_.erase(oid); - } - - virtual void Unregister( - const std::vector<invalidation::ObjectId>& oids) OVERRIDE { - // Unused for now. - } - - const ObjectIdSet& GetRegisteredIdsForTest() const { - return registered_ids_; - } - - private: - ObjectIdSet registered_ids_; - - DISALLOW_COPY_AND_ASSIGN(FakeInvalidationClient); -}; - -size_t kObjectIdsCount = 5; - -invalidation::ObjectId GetIdForIndex(size_t index) { - char name[2] = "a"; - name[0] += static_cast<char>(index); - return invalidation::ObjectId(1 + index, name); -} - -ObjectIdSet GetSequenceOfIdsStartingAt(size_t start, size_t count) { - ObjectIdSet ids; - for (size_t i = start; i < start + count; ++i) - ids.insert(GetIdForIndex(i)); - return ids; -} - -ObjectIdSet GetSequenceOfIds(size_t count) { - return GetSequenceOfIdsStartingAt(0, count); -} - -void ExpectPendingRegistrations( - const ObjectIdSet& expected_pending_ids, - double expected_delay_seconds, - const RegistrationManager::PendingRegistrationMap& pending_registrations) { - ObjectIdSet pending_ids; - for (RegistrationManager::PendingRegistrationMap::const_iterator it = - pending_registrations.begin(); it != pending_registrations.end(); - ++it) { - SCOPED_TRACE(ObjectIdToString(it->first)); - pending_ids.insert(it->first); - base::TimeDelta offset = - it->second.last_registration_request - - it->second.registration_attempt; - base::TimeDelta expected_delay = - base::TimeDelta::FromSeconds( - static_cast<int64>(expected_delay_seconds)) + offset; - // TODO(akalin): Add base::PrintTo() for base::Time and - // base::TimeDeltas. - EXPECT_EQ(expected_delay, it->second.delay) - << expected_delay.InMicroseconds() - << ", " << it->second.delay.InMicroseconds(); - if (it->second.delay <= base::TimeDelta()) { - EXPECT_EQ(base::TimeDelta(), it->second.actual_delay); - } else { - EXPECT_EQ(it->second.actual_delay, it->second.delay); - } - } - EXPECT_EQ(expected_pending_ids, pending_ids); -} - -class RegistrationManagerTest : public testing::Test { - protected: - RegistrationManagerTest() - : fake_registration_manager_(&fake_invalidation_client_) {} - - virtual ~RegistrationManagerTest() {} - - void LoseRegistrations(const ObjectIdSet& oids) { - for (ObjectIdSet::const_iterator it = oids.begin(); it != oids.end(); - ++it) { - fake_invalidation_client_.LoseRegistration(*it); - fake_registration_manager_.MarkRegistrationLost(*it); - } - } - - void DisableIds(const ObjectIdSet& oids) { - for (ObjectIdSet::const_iterator it = oids.begin(); it != oids.end(); - ++it) { - fake_invalidation_client_.LoseRegistration(*it); - fake_registration_manager_.DisableId(*it); - } - } - - // Used by MarkRegistrationLostBackoff* tests. - void RunBackoffTest(double jitter) { - fake_registration_manager_.SetJitter(jitter); - ObjectIdSet ids = GetSequenceOfIds(kObjectIdsCount); - fake_registration_manager_.UpdateRegisteredIds(ids); - - // Lose some ids. - ObjectIdSet lost_ids = GetSequenceOfIds(2); - LoseRegistrations(lost_ids); - ExpectPendingRegistrations( - lost_ids, 0.0, - fake_registration_manager_.GetPendingRegistrationsForTest()); - - // Trigger another failure to start delaying. - fake_registration_manager_.FirePendingRegistrationsForTest(); - LoseRegistrations(lost_ids); - - double scaled_jitter = - jitter * RegistrationManager::kRegistrationDelayMaxJitter; - - double expected_delay = - RegistrationManager::kInitialRegistrationDelaySeconds * - (1.0 + scaled_jitter); - expected_delay = std::floor(expected_delay); - ExpectPendingRegistrations( - lost_ids, expected_delay, - fake_registration_manager_.GetPendingRegistrationsForTest()); - - // Trigger another failure. - fake_registration_manager_.FirePendingRegistrationsForTest(); - LoseRegistrations(lost_ids); - expected_delay *= - RegistrationManager::kRegistrationDelayExponent + scaled_jitter; - expected_delay = std::floor(expected_delay); - ExpectPendingRegistrations( - lost_ids, expected_delay, - fake_registration_manager_.GetPendingRegistrationsForTest()); - - // Trigger enough failures to hit the ceiling. - while (expected_delay < RegistrationManager::kMaxRegistrationDelaySeconds) { - fake_registration_manager_.FirePendingRegistrationsForTest(); - LoseRegistrations(lost_ids); - expected_delay *= - RegistrationManager::kRegistrationDelayExponent + scaled_jitter; - expected_delay = std::floor(expected_delay); - } - ExpectPendingRegistrations( - lost_ids, - RegistrationManager::kMaxRegistrationDelaySeconds, - fake_registration_manager_.GetPendingRegistrationsForTest()); - } - - FakeInvalidationClient fake_invalidation_client_; - FakeRegistrationManager fake_registration_manager_; - - private: - // Needed by timers in RegistrationManager. - base::MessageLoop message_loop_; - - DISALLOW_COPY_AND_ASSIGN(RegistrationManagerTest); -}; - -// Basic test of UpdateRegisteredIds to make sure we properly register -// new IDs and unregister any IDs no longer in the set. -TEST_F(RegistrationManagerTest, UpdateRegisteredIds) { - ObjectIdSet ids = GetSequenceOfIds(kObjectIdsCount - 1); - - EXPECT_TRUE(fake_registration_manager_.GetRegisteredIdsForTest().empty()); - EXPECT_TRUE(fake_invalidation_client_.GetRegisteredIdsForTest().empty()); - - ObjectIdSet expected_unregistered_ids; - - ObjectIdSet unregistered_ids = - fake_registration_manager_.UpdateRegisteredIds(ids); - EXPECT_EQ(expected_unregistered_ids, unregistered_ids); - EXPECT_EQ(ids, fake_registration_manager_.GetRegisteredIdsForTest()); - EXPECT_EQ(ids, fake_invalidation_client_.GetRegisteredIdsForTest()); - - ids.insert(GetIdForIndex(kObjectIdsCount - 1)); - ids.erase(GetIdForIndex(kObjectIdsCount - 2)); - unregistered_ids = fake_registration_manager_.UpdateRegisteredIds(ids); - expected_unregistered_ids.insert(GetIdForIndex(kObjectIdsCount - 2)); - EXPECT_EQ(expected_unregistered_ids, unregistered_ids); - EXPECT_EQ(ids, fake_registration_manager_.GetRegisteredIdsForTest()); - EXPECT_EQ(ids, fake_invalidation_client_.GetRegisteredIdsForTest()); -} - -int GetRoundedBackoff(double retry_interval, double jitter) { - const double kInitialRetryInterval = 3.0; - const double kMinRetryInterval = 2.0; - const double kMaxRetryInterval = 20.0; - const double kBackoffExponent = 2.0; - const double kMaxJitter = 0.5; - - return static_cast<int>( - RegistrationManager::CalculateBackoff(retry_interval, - kInitialRetryInterval, - kMinRetryInterval, - kMaxRetryInterval, - kBackoffExponent, - jitter, - kMaxJitter)); -} - -TEST_F(RegistrationManagerTest, CalculateBackoff) { - // Test initial. - EXPECT_EQ(2, GetRoundedBackoff(0.0, -1.0)); - EXPECT_EQ(3, GetRoundedBackoff(0.0, 0.0)); - EXPECT_EQ(4, GetRoundedBackoff(0.0, +1.0)); - - // Test non-initial. - EXPECT_EQ(4, GetRoundedBackoff(3.0, -1.0)); - EXPECT_EQ(6, GetRoundedBackoff(3.0, 0.0)); - EXPECT_EQ(7, GetRoundedBackoff(3.0, +1.0)); - - EXPECT_EQ(7, GetRoundedBackoff(5.0, -1.0)); - EXPECT_EQ(10, GetRoundedBackoff(5.0, 0.0)); - EXPECT_EQ(12, GetRoundedBackoff(5.0, +1.0)); - - // Test ceiling. - EXPECT_EQ(19, GetRoundedBackoff(13.0, -1.0)); - EXPECT_EQ(20, GetRoundedBackoff(13.0, 0.0)); - EXPECT_EQ(20, GetRoundedBackoff(13.0, +1.0)); -} - -// Losing a registration should queue automatic re-registration. -TEST_F(RegistrationManagerTest, MarkRegistrationLost) { - ObjectIdSet ids = GetSequenceOfIds(kObjectIdsCount); - - fake_registration_manager_.UpdateRegisteredIds(ids); - EXPECT_TRUE( - fake_registration_manager_.GetPendingRegistrationsForTest().empty()); - - // Lose some ids. - ObjectIdSet lost_ids = GetSequenceOfIds(3); - ObjectIdSet non_lost_ids = GetSequenceOfIdsStartingAt(3, kObjectIdsCount - 3); - LoseRegistrations(lost_ids); - ExpectPendingRegistrations( - lost_ids, 0.0, - fake_registration_manager_.GetPendingRegistrationsForTest()); - EXPECT_EQ(non_lost_ids, fake_registration_manager_.GetRegisteredIdsForTest()); - EXPECT_EQ(non_lost_ids, fake_invalidation_client_.GetRegisteredIdsForTest()); - - // Pretend we waited long enough to re-register. - fake_registration_manager_.FirePendingRegistrationsForTest(); - EXPECT_EQ(ids, fake_registration_manager_.GetRegisteredIdsForTest()); - EXPECT_EQ(ids, fake_invalidation_client_.GetRegisteredIdsForTest()); -} - -TEST_F(RegistrationManagerTest, MarkRegistrationLostBackoffLow) { - RunBackoffTest(-1.0); -} - -TEST_F(RegistrationManagerTest, MarkRegistrationLostBackoffMid) { - RunBackoffTest(0.0); -} - -TEST_F(RegistrationManagerTest, MarkRegistrationLostBackoffHigh) { - RunBackoffTest(+1.0); -} - -// Exponential backoff on lost registrations should be reset to zero if -// UpdateRegisteredIds is called. -TEST_F(RegistrationManagerTest, MarkRegistrationLostBackoffReset) { - ObjectIdSet ids = GetSequenceOfIds(kObjectIdsCount); - - fake_registration_manager_.UpdateRegisteredIds(ids); - - // Lose some ids. - ObjectIdSet lost_ids = GetSequenceOfIds(2); - LoseRegistrations(lost_ids); - ExpectPendingRegistrations( - lost_ids, 0.0, - fake_registration_manager_.GetPendingRegistrationsForTest()); - - // Trigger another failure to start delaying. - fake_registration_manager_.FirePendingRegistrationsForTest(); - LoseRegistrations(lost_ids); - double expected_delay = - RegistrationManager::kInitialRegistrationDelaySeconds; - ExpectPendingRegistrations( - lost_ids, expected_delay, - fake_registration_manager_.GetPendingRegistrationsForTest()); - - // Set ids again. - fake_registration_manager_.UpdateRegisteredIds(ids); - ExpectPendingRegistrations( - ObjectIdSet(), - 0.0, - fake_registration_manager_.GetPendingRegistrationsForTest()); -} - -TEST_F(RegistrationManagerTest, MarkAllRegistrationsLost) { - ObjectIdSet ids = GetSequenceOfIds(kObjectIdsCount); - - fake_registration_manager_.UpdateRegisteredIds(ids); - - fake_invalidation_client_.LoseAllRegistrations(); - fake_registration_manager_.MarkAllRegistrationsLost(); - - EXPECT_TRUE(fake_registration_manager_.GetRegisteredIdsForTest().empty()); - EXPECT_TRUE(fake_invalidation_client_.GetRegisteredIdsForTest().empty()); - - ExpectPendingRegistrations( - ids, 0.0, - fake_registration_manager_.GetPendingRegistrationsForTest()); - - // Trigger another failure to start delaying. - fake_registration_manager_.FirePendingRegistrationsForTest(); - fake_invalidation_client_.LoseAllRegistrations(); - fake_registration_manager_.MarkAllRegistrationsLost(); - double expected_delay = - RegistrationManager::kInitialRegistrationDelaySeconds; - ExpectPendingRegistrations( - ids, expected_delay, - fake_registration_manager_.GetPendingRegistrationsForTest()); - - // Pretend we waited long enough to re-register. - fake_registration_manager_.FirePendingRegistrationsForTest(); - EXPECT_EQ(ids, fake_registration_manager_.GetRegisteredIdsForTest()); - EXPECT_EQ(ids, fake_invalidation_client_.GetRegisteredIdsForTest()); -} - -// IDs that are disabled should not be re-registered by UpdateRegisteredIds or -// automatic re-registration if that registration is lost. -TEST_F(RegistrationManagerTest, DisableId) { - ObjectIdSet ids = GetSequenceOfIds(kObjectIdsCount); - - fake_registration_manager_.UpdateRegisteredIds(ids); - EXPECT_TRUE( - fake_registration_manager_.GetPendingRegistrationsForTest().empty()); - - // Disable some ids. - ObjectIdSet disabled_ids = GetSequenceOfIds(3); - ObjectIdSet enabled_ids = GetSequenceOfIdsStartingAt(3, kObjectIdsCount - 3); - DisableIds(disabled_ids); - ExpectPendingRegistrations( - ObjectIdSet(), - 0.0, - fake_registration_manager_.GetPendingRegistrationsForTest()); - EXPECT_EQ(enabled_ids, fake_registration_manager_.GetRegisteredIdsForTest()); - EXPECT_EQ(enabled_ids, fake_invalidation_client_.GetRegisteredIdsForTest()); - - fake_registration_manager_.UpdateRegisteredIds(ids); - EXPECT_EQ(enabled_ids, fake_registration_manager_.GetRegisteredIdsForTest()); - - fake_registration_manager_.MarkRegistrationLost( - *disabled_ids.begin()); - ExpectPendingRegistrations( - ObjectIdSet(), - 0.0, - fake_registration_manager_.GetPendingRegistrationsForTest()); - - fake_registration_manager_.MarkAllRegistrationsLost(); - ExpectPendingRegistrations( - enabled_ids, 0.0, - fake_registration_manager_.GetPendingRegistrationsForTest()); -} - -} // namespace -} // namespace syncer diff --git a/sync/notifier/single_object_invalidation_set.cc b/sync/notifier/single_object_invalidation_set.cc deleted file mode 100644 index 93b47ab..0000000 --- a/sync/notifier/single_object_invalidation_set.cc +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright 2013 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 "sync/notifier/single_object_invalidation_set.h" - -#include "base/values.h" -#include "sync/notifier/invalidation_util.h" - -namespace syncer { - -SingleObjectInvalidationSet::SingleObjectInvalidationSet() {} - -SingleObjectInvalidationSet::~SingleObjectInvalidationSet() {} - -void SingleObjectInvalidationSet::Insert(const Invalidation& invalidation) { - invalidations_.insert(invalidation); -} - -void SingleObjectInvalidationSet::InsertAll( - const SingleObjectInvalidationSet& other) { - invalidations_.insert(other.begin(), other.end()); -} - -void SingleObjectInvalidationSet::Clear() { - invalidations_.clear(); -} - -void SingleObjectInvalidationSet::Erase(const_iterator it) { - invalidations_.erase(*it); -} - -bool SingleObjectInvalidationSet::StartsWithUnknownVersion() const { - return !invalidations_.empty() && - invalidations_.begin()->is_unknown_version(); -} - -size_t SingleObjectInvalidationSet::GetSize() const { - return invalidations_.size(); -} - -bool SingleObjectInvalidationSet::IsEmpty() const { - return invalidations_.empty(); -} - -namespace { - -struct InvalidationComparator { - bool operator()(const Invalidation& inv1, const Invalidation& inv2) { - return inv1.Equals(inv2); - } -}; - -} // namespace - -bool SingleObjectInvalidationSet::operator==( - const SingleObjectInvalidationSet& other) const { - return std::equal(invalidations_.begin(), - invalidations_.end(), - other.invalidations_.begin(), - InvalidationComparator()); -} - -SingleObjectInvalidationSet::const_iterator -SingleObjectInvalidationSet::begin() const { - return invalidations_.begin(); -} - -SingleObjectInvalidationSet::const_iterator -SingleObjectInvalidationSet::end() const { - return invalidations_.end(); -} - -SingleObjectInvalidationSet::const_reverse_iterator -SingleObjectInvalidationSet::rbegin() const { - return invalidations_.rbegin(); -} - -SingleObjectInvalidationSet::const_reverse_iterator -SingleObjectInvalidationSet::rend() const { - return invalidations_.rend(); -} - -const Invalidation& SingleObjectInvalidationSet::back() const { - return *invalidations_.rbegin(); -} - -scoped_ptr<base::ListValue> SingleObjectInvalidationSet::ToValue() const { - scoped_ptr<base::ListValue> value(new base::ListValue); - for (InvalidationsSet::const_iterator it = invalidations_.begin(); - it != invalidations_.end(); ++it) { - value->Append(it->ToValue().release()); - } - return value.Pass(); -} - -bool SingleObjectInvalidationSet::ResetFromValue( - const base::ListValue& list) { - for (size_t i = 0; i < list.GetSize(); ++i) { - const base::DictionaryValue* dict; - if (!list.GetDictionary(i, &dict)) { - DLOG(WARNING) << "Could not find invalidation at index " << i; - return false; - } - scoped_ptr<Invalidation> invalidation = Invalidation::InitFromValue(*dict); - if (!invalidation) { - DLOG(WARNING) << "Failed to parse invalidation at index " << i; - return false; - } - invalidations_.insert(*invalidation); - } - return true; -} - -} // namespace syncer diff --git a/sync/notifier/single_object_invalidation_set.h b/sync/notifier/single_object_invalidation_set.h deleted file mode 100644 index 579e55f..0000000 --- a/sync/notifier/single_object_invalidation_set.h +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright 2013 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 SYNC_NOTIFIER_SINGLE_OBJECT_INVALIDATION_SET_H_ -#define SYNC_NOTIFIER_SINGLE_OBJECT_INVALIDATION_SET_H_ - -#include <set> - -#include "base/memory/scoped_ptr.h" -#include "sync/base/sync_export.h" -#include "sync/internal_api/public/base/invalidation.h" -#include "sync/notifier/invalidation_util.h" - -namespace base { -class ListValue; -} // namespace base - -namespace syncer { - -// Holds a list of invalidations that all share the same Object ID. -// -// The list is kept sorted by version to make it easier to perform common -// operations, like checking for an unknown version invalidation or fetching the -// highest invalidation with the highest version number. -class SYNC_EXPORT SingleObjectInvalidationSet { - public: - typedef std::set<Invalidation, InvalidationVersionLessThan> InvalidationsSet; - typedef InvalidationsSet::const_iterator const_iterator; - typedef InvalidationsSet::const_reverse_iterator const_reverse_iterator; - - SingleObjectInvalidationSet(); - ~SingleObjectInvalidationSet(); - - void Insert(const Invalidation& invalidation); - void InsertAll(const SingleObjectInvalidationSet& other); - void Clear(); - void Erase(const_iterator it); - - // Returns true if this list contains an unknown version. - // - // Unknown version invalidations always end up at the start of the list, - // because they have the lowest possible value in the sort ordering. - bool StartsWithUnknownVersion() const; - size_t GetSize() const; - bool IsEmpty() const; - bool operator==(const SingleObjectInvalidationSet& other) const; - - const_iterator begin() const; - const_iterator end() const; - const_reverse_iterator rbegin() const; - const_reverse_iterator rend() const; - const Invalidation& back() const; - - scoped_ptr<base::ListValue> ToValue() const; - bool ResetFromValue(const base::ListValue& list); - - private: - InvalidationsSet invalidations_; -}; - -} // syncer - -#endif // SYNC_NOTIFIER_SINGLE_OBJECT_INVALIDATION_SET_H_ diff --git a/sync/notifier/single_object_invalidation_set_unittest.cc b/sync/notifier/single_object_invalidation_set_unittest.cc deleted file mode 100644 index 3fe074e..0000000 --- a/sync/notifier/single_object_invalidation_set_unittest.cc +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright 2013 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 "sync/notifier/single_object_invalidation_set.h" - -#include "google/cacheinvalidation/types.pb.h" -#include "sync/internal_api/public/base/invalidation_test_util.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace syncer { - -namespace { - -class SingleObjectInvalidationSetTest : public testing::Test { - public: - SingleObjectInvalidationSetTest() - : kId(ipc::invalidation::ObjectSource::TEST, "one") { - } - protected: - const invalidation::ObjectId kId; -}; - -TEST_F(SingleObjectInvalidationSetTest, InsertionAndOrdering) { - SingleObjectInvalidationSet l1; - SingleObjectInvalidationSet l2; - - Invalidation inv0 = Invalidation::InitUnknownVersion(kId); - Invalidation inv1 = Invalidation::Init(kId, 1, "one"); - Invalidation inv2 = Invalidation::Init(kId, 5, "five"); - - l1.Insert(inv0); - l1.Insert(inv1); - l1.Insert(inv2); - - l2.Insert(inv1); - l2.Insert(inv2); - l2.Insert(inv0); - - ASSERT_EQ(3U, l1.GetSize()); - ASSERT_EQ(3U, l2.GetSize()); - - SingleObjectInvalidationSet::const_iterator it1 = l1.begin(); - SingleObjectInvalidationSet::const_iterator it2 = l2.begin(); - EXPECT_THAT(inv0, Eq(*it1)); - EXPECT_THAT(inv0, Eq(*it2)); - it1++; - it2++; - EXPECT_THAT(inv1, Eq(*it1)); - EXPECT_THAT(inv1, Eq(*it2)); - it1++; - it2++; - EXPECT_THAT(inv2, Eq(*it1)); - EXPECT_THAT(inv2, Eq(*it2)); - it1++; - it2++; - EXPECT_TRUE(it1 == l1.end()); - EXPECT_TRUE(it2 == l2.end()); -} - -TEST_F(SingleObjectInvalidationSetTest, StartWithUnknownVersion) { - SingleObjectInvalidationSet list; - EXPECT_FALSE(list.StartsWithUnknownVersion()); - - list.Insert(Invalidation::Init(kId, 1, "one")); - EXPECT_FALSE(list.StartsWithUnknownVersion()); - - list.Insert(Invalidation::InitUnknownVersion(kId)); - EXPECT_TRUE(list.StartsWithUnknownVersion()); - - list.Clear(); - EXPECT_FALSE(list.StartsWithUnknownVersion()); -} - -TEST_F(SingleObjectInvalidationSetTest, SerializeEmpty) { - SingleObjectInvalidationSet list; - - scoped_ptr<base::ListValue> value = list.ToValue(); - ASSERT_TRUE(value.get()); - SingleObjectInvalidationSet deserialized; - deserialized.ResetFromValue(*value.get()); - EXPECT_TRUE(list == deserialized); -} - -TEST_F(SingleObjectInvalidationSetTest, SerializeOne) { - SingleObjectInvalidationSet list; - list.Insert(Invalidation::Init(kId, 1, "one")); - - scoped_ptr<base::ListValue> value = list.ToValue(); - ASSERT_TRUE(value.get()); - SingleObjectInvalidationSet deserialized; - deserialized.ResetFromValue(*value.get()); - EXPECT_TRUE(list == deserialized); -} - -TEST_F(SingleObjectInvalidationSetTest, SerializeMany) { - SingleObjectInvalidationSet list; - list.Insert(Invalidation::Init(kId, 1, "one")); - list.Insert(Invalidation::InitUnknownVersion(kId)); - - scoped_ptr<base::ListValue> value = list.ToValue(); - ASSERT_TRUE(value.get()); - SingleObjectInvalidationSet deserialized; - deserialized.ResetFromValue(*value.get()); - EXPECT_TRUE(list == deserialized); -} - -} // namespace - -} // namespace syncer diff --git a/sync/notifier/unacked_invalidation_set.cc b/sync/notifier/unacked_invalidation_set.cc deleted file mode 100644 index 61c4166..0000000 --- a/sync/notifier/unacked_invalidation_set.cc +++ /dev/null @@ -1,210 +0,0 @@ -// Copyright 2013 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 "sync/notifier/unacked_invalidation_set.h" - -#include "base/strings/string_number_conversions.h" -#include "sync/internal_api/public/base/ack_handle.h" -#include "sync/notifier/object_id_invalidation_map.h" - -namespace { - -const char kSourceKey[] = "source"; -const char kNameKey[] = "name"; -const char kInvalidationListKey[] = "invalidation-list"; - -} // namespace - -namespace syncer { - -const size_t UnackedInvalidationSet::kMaxBufferedInvalidations = 5; - -// static -UnackedInvalidationSet::UnackedInvalidationSet( - invalidation::ObjectId id) - : registered_(false), - object_id_(id) {} - -UnackedInvalidationSet::UnackedInvalidationSet( - const UnackedInvalidationSet& other) - : registered_(other.registered_), - object_id_(other.object_id_), - invalidations_(other.invalidations_) { -} - -UnackedInvalidationSet::~UnackedInvalidationSet() {} - -const invalidation::ObjectId& UnackedInvalidationSet::object_id() const { - return object_id_; -} - -void UnackedInvalidationSet::Add( - const Invalidation& invalidation) { - SingleObjectInvalidationSet set; - set.Insert(invalidation); - AddSet(set); - if (!registered_) - Truncate(kMaxBufferedInvalidations); -} - -void UnackedInvalidationSet::AddSet( - const SingleObjectInvalidationSet& invalidations) { - invalidations_.insert(invalidations.begin(), invalidations.end()); - if (!registered_) - Truncate(kMaxBufferedInvalidations); -} - -void UnackedInvalidationSet::ExportInvalidations( - WeakHandle<AckHandler> ack_handler, - ObjectIdInvalidationMap* out) const { - for (SingleObjectInvalidationSet::const_iterator it = invalidations_.begin(); - it != invalidations_.end(); ++it) { - // Copy the invalidation and set the copy's ack_handler. - Invalidation inv(*it); - inv.set_ack_handler(ack_handler); - out->Insert(inv); - } -} - -void UnackedInvalidationSet::Clear() { - invalidations_.clear(); -} - -void UnackedInvalidationSet::SetHandlerIsRegistered() { - registered_ = true; -} - -void UnackedInvalidationSet::SetHandlerIsUnregistered() { - registered_ = false; - Truncate(kMaxBufferedInvalidations); -} - -// Removes the matching ack handle from the list. -void UnackedInvalidationSet::Acknowledge(const AckHandle& handle) { - bool handle_found = false; - for (SingleObjectInvalidationSet::const_iterator it = invalidations_.begin(); - it != invalidations_.end(); ++it) { - if (it->ack_handle().Equals(handle)) { - invalidations_.erase(*it); - handle_found = true; - break; - } - } - DLOG_IF(WARNING, !handle_found) - << "Unrecognized to ack for object " << ObjectIdToString(object_id_); - (void)handle_found; // Silence unused variable warning in release builds. -} - -// Erase the invalidation with matching ack handle from the list. Also creates -// an 'UnknownVersion' invalidation with the same ack handle and places it at -// the beginning of the list. If an unknown version invalidation currently -// exists, it is replaced. -void UnackedInvalidationSet::Drop(const AckHandle& handle) { - SingleObjectInvalidationSet::const_iterator it; - for (it = invalidations_.begin(); it != invalidations_.end(); ++it) { - if (it->ack_handle().Equals(handle)) { - break; - } - } - if (it == invalidations_.end()) { - DLOG(WARNING) << "Unrecognized drop request for object " - << ObjectIdToString(object_id_); - return; - } - - Invalidation unknown_version = Invalidation::InitFromDroppedInvalidation(*it); - invalidations_.erase(*it); - - // If an unknown version is in the list, we remove it so we can replace it. - if (!invalidations_.empty() && invalidations_.begin()->is_unknown_version()) { - invalidations_.erase(*invalidations_.begin()); - } - - invalidations_.insert(unknown_version); -} - -scoped_ptr<base::DictionaryValue> UnackedInvalidationSet::ToValue() const { - scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); - value->SetString(kSourceKey, base::IntToString(object_id_.source())); - value->SetString(kNameKey, object_id_.name()); - - scoped_ptr<base::ListValue> list_value(new base::ListValue); - for (InvalidationsSet::const_iterator it = invalidations_.begin(); - it != invalidations_.end(); ++it) { - list_value->Append(it->ToValue().release()); - } - value->Set(kInvalidationListKey, list_value.release()); - - return value.Pass(); -} - -bool UnackedInvalidationSet::ResetFromValue( - const base::DictionaryValue& value) { - std::string source_str; - if (!value.GetString(kSourceKey, &source_str)) { - DLOG(WARNING) << "Unable to deserialize source"; - return false; - } - int source = 0; - if (!base::StringToInt(source_str, &source)) { - DLOG(WARNING) << "Invalid source: " << source_str; - return false; - } - std::string name; - if (!value.GetString(kNameKey, &name)) { - DLOG(WARNING) << "Unable to deserialize name"; - return false; - } - object_id_ = invalidation::ObjectId(source, name); - const base::ListValue* invalidation_list = NULL; - if (!value.GetList(kInvalidationListKey, &invalidation_list) - || !ResetListFromValue(*invalidation_list)) { - // Earlier versions of this class did not set this field, so we don't treat - // parsing errors here as a fatal failure. - DLOG(WARNING) << "Unable to deserialize invalidation list."; - } - return true; -} - -bool UnackedInvalidationSet::ResetListFromValue( - const base::ListValue& list) { - for (size_t i = 0; i < list.GetSize(); ++i) { - const base::DictionaryValue* dict; - if (!list.GetDictionary(i, &dict)) { - DLOG(WARNING) << "Failed to get invalidation dictionary at index " << i; - return false; - } - scoped_ptr<Invalidation> invalidation = Invalidation::InitFromValue(*dict); - if (!invalidation) { - DLOG(WARNING) << "Failed to parse invalidation at index " << i; - return false; - } - invalidations_.insert(*invalidation.get()); - } - return true; -} - -void UnackedInvalidationSet::Truncate(size_t max_size) { - DCHECK_GT(max_size, 0U); - - if (invalidations_.size() <= max_size) { - return; - } - - while (invalidations_.size() > max_size) { - invalidations_.erase(*invalidations_.begin()); - } - - // We dropped some invalidations. We remember the fact that an unknown - // amount of information has been lost by ensuring this list begins with - // an UnknownVersion invalidation. We remove the oldest remaining - // invalidation to make room for it. - invalidation::ObjectId id = invalidations_.begin()->object_id(); - invalidations_.erase(*invalidations_.begin()); - - Invalidation unknown_version = Invalidation::InitUnknownVersion(id); - invalidations_.insert(unknown_version); -} - -} // namespace syncer diff --git a/sync/notifier/unacked_invalidation_set.h b/sync/notifier/unacked_invalidation_set.h deleted file mode 100644 index ac83140..0000000 --- a/sync/notifier/unacked_invalidation_set.h +++ /dev/null @@ -1,118 +0,0 @@ -// Copyright 2013 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 SYNC_NOTIFIER_UNACKED_INVALIDATION_SET_H_ -#define SYNC_NOTIFIER_UNACKED_INVALIDATION_SET_H_ - -#include <vector> - -#include "sync/base/sync_export.h" -#include "sync/internal_api/public/base/invalidation.h" -#include "sync/internal_api/public/util/weak_handle.h" -#include "sync/notifier/invalidation_util.h" - -namespace base { -class DictionaryValue; -} // namespace base - -namespace syncer { - -namespace test_util { -class UnackedInvalidationSetEqMatcher; -} // test_util - -class SingleObjectInvalidationSet; -class ObjectIdInvalidationMap; -class AckHandle; - -// Manages the set of invalidations that are awaiting local acknowledgement for -// a particular ObjectId. This set of invalidations will be persisted across -// restarts, though this class is not directly responsible for that. -class SYNC_EXPORT UnackedInvalidationSet { - public: - static const size_t kMaxBufferedInvalidations; - - UnackedInvalidationSet(invalidation::ObjectId id); - UnackedInvalidationSet(const UnackedInvalidationSet& other); - ~UnackedInvalidationSet(); - - // Returns the ObjectID of the invalidations this class is tracking. - const invalidation::ObjectId& object_id() const; - - // Adds a new invalidation to the set awaiting acknowledgement. - void Add(const Invalidation& invalidation); - - // Adds many new invalidations to the set awaiting acknowledgement. - void AddSet(const SingleObjectInvalidationSet& invalidations); - - // Exports the set of invalidations awaiting acknowledgement as an - // ObjectIdInvalidationMap. Each of these invalidations will be associated - // with the given |ack_handler|. - // - // The contents of the UnackedInvalidationSet are not directly modified by - // this procedure, but the AckHandles stored in those exported invalidations - // are likely to end up back here in calls to Acknowledge() or Drop(). - void ExportInvalidations(WeakHandle<AckHandler> ack_handler, - ObjectIdInvalidationMap* out) const; - - // Removes all stored invalidations from this object. - void Clear(); - - // Indicates that a handler has registered to handle these invalidations. - // - // Registrations with the invalidations server persist across restarts, but - // registrations from InvalidationHandlers to the InvalidationService are not. - // In the time immediately after a restart, it's possible that the server - // will send us invalidations, and we won't have a handler to send them to. - // - // The SetIsRegistered() call indicates that this period has come to an end. - // There is now a handler that can receive these invalidations. Once this - // function has been called, the kMaxBufferedInvalidations limit will be - // ignored. It is assumed that the handler will manage its own buffer size. - void SetHandlerIsRegistered(); - - // Indicates that the handler has now unregistered itself. - // - // This causes the object to resume enforcement of the - // kMaxBufferedInvalidations limit. - void SetHandlerIsUnregistered(); - - // Given an AckHandle belonging to one of the contained invalidations, finds - // the invalidation and drops it from the list. It is considered to be - // acknowledged, so there is no need to continue maintaining its state. - void Acknowledge(const AckHandle& handle); - - // Given an AckHandle belonging to one of the contained invalidations, finds - // the invalidation, drops it from the list, and adds additional state to - // indicate that this invalidation has been lost without being acted on. - void Drop(const AckHandle& handle); - - scoped_ptr<base::DictionaryValue> ToValue() const; - bool ResetFromValue(const base::DictionaryValue& value); - - private: - // Allow this test helper to have access to our internals. - friend class test_util::UnackedInvalidationSetEqMatcher; - - typedef std::set<Invalidation, InvalidationVersionLessThan> InvalidationsSet; - - bool ResetListFromValue(const base::ListValue& value); - - // Limits the list size to the given maximum. This function will correctly - // update this class' internal data to indicate if invalidations have been - // dropped. - void Truncate(size_t max_size); - - bool registered_; - invalidation::ObjectId object_id_; - InvalidationsSet invalidations_; -}; - -typedef std::map<invalidation::ObjectId, - UnackedInvalidationSet, - ObjectIdLessThan> UnackedInvalidationsMap; - -} // namespace syncer - -#endif // SYNC_NOTIFIER_UNACKED_INVALIDATION_SET_H_ diff --git a/sync/notifier/unacked_invalidation_set_test_util.cc b/sync/notifier/unacked_invalidation_set_test_util.cc deleted file mode 100644 index 8961574c..0000000 --- a/sync/notifier/unacked_invalidation_set_test_util.cc +++ /dev/null @@ -1,181 +0,0 @@ -// Copyright 2013 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 "sync/notifier/unacked_invalidation_set_test_util.h" - -#include "base/json/json_string_value_serializer.h" -#include "sync/notifier/object_id_invalidation_map.h" -#include "testing/gmock/include/gmock/gmock-matchers.h" - -namespace syncer { - -using ::testing::MakeMatcher; -using ::testing::MatchResultListener; -using ::testing::Matcher; -using ::testing::MatcherInterface; -using ::testing::PrintToString; - -namespace test_util { - -// This class needs to be declared outside the null namespace so the -// UnackedInvalidationSet can declare it as a friend. This class needs access -// to the UnackedInvalidationSet internals to implement its comparispon -// function. -class UnackedInvalidationSetEqMatcher - : public testing::MatcherInterface<const UnackedInvalidationSet&> { - public: - explicit UnackedInvalidationSetEqMatcher( - const UnackedInvalidationSet& expected); - - virtual bool MatchAndExplain( - const UnackedInvalidationSet& actual, - MatchResultListener* listener) const OVERRIDE; - virtual void DescribeTo(::std::ostream* os) const OVERRIDE; - virtual void DescribeNegationTo(::std::ostream* os) const OVERRIDE; - - private: - const UnackedInvalidationSet expected_; - - DISALLOW_COPY_AND_ASSIGN(UnackedInvalidationSetEqMatcher); -}; - -namespace { - -struct InvalidationEq { - bool operator()(const syncer::Invalidation& a, - const syncer::Invalidation& b) const { - return a.Equals(b); - } -}; - -} // namespace - -UnackedInvalidationSetEqMatcher::UnackedInvalidationSetEqMatcher( - const UnackedInvalidationSet& expected) - : expected_(expected) {} - -bool UnackedInvalidationSetEqMatcher::MatchAndExplain( - const UnackedInvalidationSet& actual, - MatchResultListener* listener) const { - // Use our friendship with this class to compare the internals of two - // instances. - // - // Note that the registration status is intentionally not considered - // when performing this comparison. - return expected_.object_id_ == actual.object_id_ - && std::equal(expected_.invalidations_.begin(), - expected_.invalidations_.end(), - actual.invalidations_.begin(), - InvalidationEq()); -} - -void UnackedInvalidationSetEqMatcher::DescribeTo(::std::ostream* os) const { - *os << " is equal to " << PrintToString(expected_); -} - -void UnackedInvalidationSetEqMatcher::DescribeNegationTo( - ::std::ostream* os) const { - *os << " isn't equal to " << PrintToString(expected_); -} - -// We're done declaring UnackedInvalidationSetEqMatcher. Everything else can -// go into the null namespace. -namespace { - -ObjectIdInvalidationMap UnackedInvalidationsMapToObjectIdInvalidationMap( - const UnackedInvalidationsMap& state_map) { - ObjectIdInvalidationMap object_id_invalidation_map; - for (UnackedInvalidationsMap::const_iterator it = state_map.begin(); - it != state_map.end(); ++it) { - it->second.ExportInvalidations(syncer::WeakHandle<AckHandler>(), - &object_id_invalidation_map); - } - return object_id_invalidation_map; -} - -class UnackedInvalidationsMapEqMatcher - : public testing::MatcherInterface<const UnackedInvalidationsMap&> { - public: - explicit UnackedInvalidationsMapEqMatcher( - const UnackedInvalidationsMap& expected); - - virtual bool MatchAndExplain(const UnackedInvalidationsMap& actual, - MatchResultListener* listener) const; - virtual void DescribeTo(::std::ostream* os) const; - virtual void DescribeNegationTo(::std::ostream* os) const; - - private: - const UnackedInvalidationsMap expected_; - - DISALLOW_COPY_AND_ASSIGN(UnackedInvalidationsMapEqMatcher); -}; - -UnackedInvalidationsMapEqMatcher::UnackedInvalidationsMapEqMatcher( - const UnackedInvalidationsMap& expected) - : expected_(expected) { -} - -bool UnackedInvalidationsMapEqMatcher::MatchAndExplain( - const UnackedInvalidationsMap& actual, - MatchResultListener* listener) const { - ObjectIdInvalidationMap expected_inv = - UnackedInvalidationsMapToObjectIdInvalidationMap(expected_); - ObjectIdInvalidationMap actual_inv = - UnackedInvalidationsMapToObjectIdInvalidationMap(actual); - - return expected_inv == actual_inv; -} - -void UnackedInvalidationsMapEqMatcher::DescribeTo( - ::std::ostream* os) const { - *os << " is equal to " << PrintToString(expected_); -} - -void UnackedInvalidationsMapEqMatcher::DescribeNegationTo( - ::std::ostream* os) const { - *os << " isn't equal to " << PrintToString(expected_); -} - -} // namespace - -void PrintTo(const UnackedInvalidationSet& invalidations, - ::std::ostream* os) { - scoped_ptr<base::DictionaryValue> value = invalidations.ToValue(); - - std::string output; - JSONStringValueSerializer serializer(&output); - serializer.set_pretty_print(true); - serializer.Serialize(*value.get()); - - (*os) << output; -} - -void PrintTo(const UnackedInvalidationsMap& map, ::std::ostream* os) { - scoped_ptr<base::ListValue> list(new base::ListValue); - for (UnackedInvalidationsMap::const_iterator it = map.begin(); - it != map.end(); ++it) { - list->Append(it->second.ToValue().release()); - } - - std::string output; - JSONStringValueSerializer serializer(&output); - serializer.set_pretty_print(true); - serializer.Serialize(*list.get()); - - (*os) << output; -} - -Matcher<const UnackedInvalidationSet&> Eq( - const UnackedInvalidationSet& expected) { - return MakeMatcher(new UnackedInvalidationSetEqMatcher(expected)); -} - -Matcher<const UnackedInvalidationsMap&> Eq( - const UnackedInvalidationsMap& expected) { - return MakeMatcher(new UnackedInvalidationsMapEqMatcher(expected)); -} - -} // namespace test_util - -}; diff --git a/sync/notifier/unacked_invalidation_set_test_util.h b/sync/notifier/unacked_invalidation_set_test_util.h deleted file mode 100644 index e93726b..0000000 --- a/sync/notifier/unacked_invalidation_set_test_util.h +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2013 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 "sync/notifier/unacked_invalidation_set.h" - -#include "testing/gmock/include/gmock/gmock-matchers.h" - -namespace syncer { - -namespace test_util { - -void PrintTo(const UnackedInvalidationSet& invalidations, ::std::ostream* os); - -void PrintTo(const UnackedInvalidationsMap& map, ::std::ostream* os); - -::testing::Matcher<const UnackedInvalidationSet&> Eq( - const UnackedInvalidationSet& expected); - -::testing::Matcher<const UnackedInvalidationsMap&> Eq( - const UnackedInvalidationsMap& expected); - -} // namespace test_util - -} // namespace syncer diff --git a/sync/notifier/unacked_invalidation_set_unittest.cc b/sync/notifier/unacked_invalidation_set_unittest.cc deleted file mode 100644 index d6549ab..0000000 --- a/sync/notifier/unacked_invalidation_set_unittest.cc +++ /dev/null @@ -1,219 +0,0 @@ -// Copyright 2013 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 "sync/notifier/unacked_invalidation_set.h" - -#include "base/json/json_string_value_serializer.h" -#include "sync/notifier/object_id_invalidation_map.h" -#include "sync/notifier/single_object_invalidation_set.h" -#include "sync/notifier/unacked_invalidation_set_test_util.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace syncer { - -class UnackedInvalidationSetTest : public testing::Test { - public: - UnackedInvalidationSetTest() - : kObjectId_(10, "ASDF"), - unacked_invalidations_(kObjectId_) {} - - SingleObjectInvalidationSet GetStoredInvalidations() { - ObjectIdInvalidationMap map; - unacked_invalidations_.ExportInvalidations(WeakHandle<AckHandler>(), &map); - ObjectIdSet ids = map.GetObjectIds(); - if (ids.find(kObjectId_) != ids.end()) { - return map.ForObject(kObjectId_); - } else { - return SingleObjectInvalidationSet(); - } - } - - const invalidation::ObjectId kObjectId_; - UnackedInvalidationSet unacked_invalidations_; -}; - -namespace { - -// Test storage and retrieval of zero invalidations. -TEST_F(UnackedInvalidationSetTest, Empty) { - EXPECT_EQ(0U, GetStoredInvalidations().GetSize()); -} - -// Test storage and retrieval of a single invalidation. -TEST_F(UnackedInvalidationSetTest, OneInvalidation) { - Invalidation inv1 = Invalidation::Init(kObjectId_, 10, "payload"); - unacked_invalidations_.Add(inv1); - - SingleObjectInvalidationSet set = GetStoredInvalidations(); - ASSERT_EQ(1U, set.GetSize()); - EXPECT_FALSE(set.StartsWithUnknownVersion()); -} - -// Test that calling Clear() returns us to the empty state. -TEST_F(UnackedInvalidationSetTest, Clear) { - Invalidation inv1 = Invalidation::Init(kObjectId_, 10, "payload"); - unacked_invalidations_.Add(inv1); - unacked_invalidations_.Clear(); - - EXPECT_EQ(0U, GetStoredInvalidations().GetSize()); -} - -// Test that repeated unknown version invalidations are squashed together. -TEST_F(UnackedInvalidationSetTest, UnknownVersions) { - Invalidation inv1 = Invalidation::Init(kObjectId_, 10, "payload"); - Invalidation inv2 = Invalidation::InitUnknownVersion(kObjectId_); - Invalidation inv3 = Invalidation::InitUnknownVersion(kObjectId_); - unacked_invalidations_.Add(inv1); - unacked_invalidations_.Add(inv2); - unacked_invalidations_.Add(inv3); - - SingleObjectInvalidationSet set = GetStoredInvalidations(); - ASSERT_EQ(2U, set.GetSize()); - EXPECT_TRUE(set.StartsWithUnknownVersion()); -} - -// Tests that no truncation occurs while we're under the limit. -TEST_F(UnackedInvalidationSetTest, NoTruncation) { - size_t kMax = UnackedInvalidationSet::kMaxBufferedInvalidations; - - for (size_t i = 0; i < kMax; ++i) { - Invalidation inv = Invalidation::Init(kObjectId_, i, "payload"); - unacked_invalidations_.Add(inv); - } - - SingleObjectInvalidationSet set = GetStoredInvalidations(); - ASSERT_EQ(kMax, set.GetSize()); - EXPECT_FALSE(set.StartsWithUnknownVersion()); - EXPECT_EQ(0, set.begin()->version()); - EXPECT_EQ(kMax-1, static_cast<size_t>(set.rbegin()->version())); -} - -// Test that truncation happens as we reach the limit. -TEST_F(UnackedInvalidationSetTest, Truncation) { - size_t kMax = UnackedInvalidationSet::kMaxBufferedInvalidations; - - for (size_t i = 0; i < kMax + 1; ++i) { - Invalidation inv = Invalidation::Init(kObjectId_, i, "payload"); - unacked_invalidations_.Add(inv); - } - - SingleObjectInvalidationSet set = GetStoredInvalidations(); - ASSERT_EQ(kMax, set.GetSize()); - EXPECT_TRUE(set.StartsWithUnknownVersion()); - EXPECT_TRUE(set.begin()->is_unknown_version()); - EXPECT_EQ(kMax, static_cast<size_t>(set.rbegin()->version())); -} - -// Test that we don't truncate while a handler is registered. -TEST_F(UnackedInvalidationSetTest, RegistrationAndTruncation) { - unacked_invalidations_.SetHandlerIsRegistered(); - - size_t kMax = UnackedInvalidationSet::kMaxBufferedInvalidations; - - for (size_t i = 0; i < kMax + 1; ++i) { - Invalidation inv = Invalidation::Init(kObjectId_, i, "payload"); - unacked_invalidations_.Add(inv); - } - - SingleObjectInvalidationSet set = GetStoredInvalidations(); - ASSERT_EQ(kMax+1, set.GetSize()); - EXPECT_FALSE(set.StartsWithUnknownVersion()); - EXPECT_EQ(0, set.begin()->version()); - EXPECT_EQ(kMax, static_cast<size_t>(set.rbegin()->version())); - - // Unregistering should re-enable truncation. - unacked_invalidations_.SetHandlerIsUnregistered(); - SingleObjectInvalidationSet set2 = GetStoredInvalidations(); - ASSERT_EQ(kMax, set2.GetSize()); - EXPECT_TRUE(set2.StartsWithUnknownVersion()); - EXPECT_TRUE(set2.begin()->is_unknown_version()); - EXPECT_EQ(kMax, static_cast<size_t>(set2.rbegin()->version())); -} - -// Test acknowledgement. -TEST_F(UnackedInvalidationSetTest, Acknowledge) { - // inv2 is included in this test just to make sure invalidations that - // are supposed to be unaffected by this operation will be unaffected. - - // We don't expect to be receiving acks or drops unless this flag is set. - // Not that it makes much of a difference in behavior. - unacked_invalidations_.SetHandlerIsRegistered(); - - Invalidation inv1 = Invalidation::Init(kObjectId_, 10, "payload"); - Invalidation inv2 = Invalidation::InitUnknownVersion(kObjectId_); - AckHandle inv1_handle = inv1.ack_handle(); - - unacked_invalidations_.Add(inv1); - unacked_invalidations_.Add(inv2); - - unacked_invalidations_.Acknowledge(inv1_handle); - - SingleObjectInvalidationSet set = GetStoredInvalidations(); - EXPECT_EQ(1U, set.GetSize()); - EXPECT_TRUE(set.StartsWithUnknownVersion()); -} - -// Test drops. -TEST_F(UnackedInvalidationSetTest, Drop) { - // inv2 is included in this test just to make sure invalidations that - // are supposed to be unaffected by this operation will be unaffected. - - // We don't expect to be receiving acks or drops unless this flag is set. - // Not that it makes much of a difference in behavior. - unacked_invalidations_.SetHandlerIsRegistered(); - - Invalidation inv1 = Invalidation::Init(kObjectId_, 10, "payload"); - Invalidation inv2 = Invalidation::Init(kObjectId_, 15, "payload"); - AckHandle inv1_handle = inv1.ack_handle(); - - unacked_invalidations_.Add(inv1); - unacked_invalidations_.Add(inv2); - - unacked_invalidations_.Drop(inv1_handle); - - SingleObjectInvalidationSet set = GetStoredInvalidations(); - ASSERT_EQ(2U, set.GetSize()); - EXPECT_TRUE(set.StartsWithUnknownVersion()); - EXPECT_EQ(15, set.rbegin()->version()); -} - -class UnackedInvalidationSetSerializationTest - : public UnackedInvalidationSetTest { - public: - UnackedInvalidationSet SerializeDeserialize() { - scoped_ptr<base::DictionaryValue> value = unacked_invalidations_.ToValue(); - UnackedInvalidationSet deserialized(kObjectId_); - deserialized.ResetFromValue(*value.get()); - return deserialized; - } -}; - -TEST_F(UnackedInvalidationSetSerializationTest, Empty) { - UnackedInvalidationSet deserialized = SerializeDeserialize(); - EXPECT_THAT(unacked_invalidations_, test_util::Eq(deserialized)); -} - -TEST_F(UnackedInvalidationSetSerializationTest, OneInvalidation) { - Invalidation inv = Invalidation::Init(kObjectId_, 10, "payload"); - unacked_invalidations_.Add(inv); - - UnackedInvalidationSet deserialized = SerializeDeserialize(); - EXPECT_THAT(unacked_invalidations_, test_util::Eq(deserialized)); -} - -TEST_F(UnackedInvalidationSetSerializationTest, WithUnknownVersion) { - Invalidation inv1 = Invalidation::Init(kObjectId_, 10, "payload"); - Invalidation inv2 = Invalidation::InitUnknownVersion(kObjectId_); - Invalidation inv3 = Invalidation::InitUnknownVersion(kObjectId_); - unacked_invalidations_.Add(inv1); - unacked_invalidations_.Add(inv2); - unacked_invalidations_.Add(inv3); - - UnackedInvalidationSet deserialized = SerializeDeserialize(); - EXPECT_THAT(unacked_invalidations_, test_util::Eq(deserialized)); -} - -} // namespace - -} // namespace syncer |