sync: Add interfaces for per-type sync

Puts the DirectoryCommitContributor and DirectoryUpdateHandler
classes behind interfaces.  These classes were always intended
to form the basis of an abstract interface.  Now we can start
to introduce alternate implementations of them.

This CL includes one such alternate implementation.  It adds
the MockUpdateHandler and uses it to remove some unnecessary
boilerplate from the download unit tests.

This CL also resolves an old TODO.  It renames the SyncDirectory*
classes to Directory*, since the 'Sync' part of the name is redundant.

This should have no effect on program behavior.

BUG=278484

Review URL: https://codereview.chromium.org/161253002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@251969 0039d316-1c4b-4281-b951-d872f2087c98
parent 4783dd68
......@@ -5,9 +5,9 @@
#include "sync/engine/commit.h"
#include "base/debug/trace_event.h"
#include "sync/engine/commit_contribution.h"
#include "sync/engine/commit_processor.h"
#include "sync/engine/commit_util.h"
#include "sync/engine/sync_directory_commit_contribution.h"
#include "sync/engine/syncer.h"
#include "sync/engine/syncer_proto_util.h"
#include "sync/sessions/sync_session.h"
......@@ -15,7 +15,7 @@
namespace syncer {
Commit::Commit(
const std::map<ModelType, SyncDirectoryCommitContribution*>& contributions,
const std::map<ModelType, CommitContribution*>& contributions,
const sync_pb::ClientToServerMessage& message,
ExtensionsActivity::Records extensions_activity_buffer)
: contributions_(contributions),
......@@ -71,7 +71,7 @@ Commit* Commit::Init(
commit_message);
// Finally, serialize all our contributions.
for (std::map<ModelType, SyncDirectoryCommitContribution*>::iterator it =
for (std::map<ModelType, CommitContribution*>::iterator it =
contributions.begin(); it != contributions.end(); ++it) {
it->second->AddToCommitMessage(&message);
}
......@@ -130,7 +130,7 @@ SyncerError Commit::PostAndProcessResponse(
// Let the contributors process the responses to each of their requests.
SyncerError processing_result = SYNCER_OK;
for (std::map<ModelType, SyncDirectoryCommitContribution*>::iterator it =
for (std::map<ModelType, CommitContribution*>::iterator it =
contributions_.begin(); it != contributions_.end(); ++it) {
TRACE_EVENT1("sync", "ProcessCommitResponse",
"type", ModelTypeToString(it->first));
......
......@@ -9,7 +9,7 @@
#include "base/stl_util.h"
#include "sync/base/sync_export.h"
#include "sync/engine/sync_directory_commit_contribution.h"
#include "sync/engine/commit_contribution.h"
#include "sync/internal_api/public/base/model_type.h"
#include "sync/internal_api/public/engine/model_safe_worker.h"
#include "sync/internal_api/public/util/syncer_error.h"
......@@ -37,7 +37,7 @@ class Syncer;
class SYNC_EXPORT_PRIVATE Commit {
public:
Commit(
const std::map<ModelType, SyncDirectoryCommitContribution*>&
const std::map<ModelType, CommitContribution*>&
contributions,
const sync_pb::ClientToServerMessage& message,
ExtensionsActivity::Records extensions_activity_buffer);
......@@ -64,7 +64,7 @@ class SYNC_EXPORT_PRIVATE Commit {
void CleanUp();
private:
typedef std::map<ModelType, SyncDirectoryCommitContribution*> ContributionMap;
typedef std::map<ModelType, CommitContribution*> ContributionMap;
ContributionMap contributions_;
STLValueDeleter<ContributionMap> deleter_;
......
// 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/engine/commit_contribution.h"
namespace syncer {
CommitContribution::CommitContribution() {}
CommitContribution::~CommitContribution() {}
} // namespace syncer
// 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.
#ifndef SYNC_ENGINE_COMMIT_CONTRIBUTION_H_
#define SYNC_ENGINE_COMMIT_CONTRIBUTION_H_
#include "sync/base/sync_export.h"
#include "sync/internal_api/public/util/syncer_error.h"
#include "sync/protocol/sync.pb.h"
#include "sync/sessions/status_controller.h"
namespace syncer {
namespace sessions {
class StatusController;
} // namespace sessions
// This class represents a set of items belonging to a particular data type that
// have been selected from a CommitContributor and prepared for commit.
//
// This class handles the bookkeeping related to the commit of these items.
class SYNC_EXPORT_PRIVATE CommitContribution {
public:
CommitContribution();
virtual ~CommitContribution() = 0;
// Serialize this contribution's entries to the given commit request |msg|.
//
// This function is not const. It may update some state in this contribution
// that will be used when processing the associated commit response. This
// function should not be called more than once.
virtual void AddToCommitMessage(sync_pb::ClientToServerMessage* msg) = 0;
// Updates this contribution's contents in accordance with the provided
// |response|.
//
// It is not valid to call this function unless AddToCommitMessage() was
// called earlier. This function should not be called more than once.
virtual SyncerError ProcessCommitResponse(
const sync_pb::ClientToServerResponse& response,
sessions::StatusController* status) = 0;
// Cleans up any temproary state associated with the commit. Must be called
// before destruction.
virtual void CleanUp() = 0;
// Returns the number of entries included in this contribution.
virtual size_t GetNumEntries() const = 0;
};
} // namespace syncer
#endif // SYNC_ENGINE_COMMIT_CONTRIBUTION_H_
// 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/engine/commit_contributor.h"
namespace syncer {
CommitContributor::CommitContributor() {}
CommitContributor::~CommitContributor() {}
} // namespace syncer
// 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.
#ifndef SYNC_ENGINE_COMMIT_CONTRIBUTOR_H_
#define SYNC_ENGINE_COMMIT_CONTRIBUTOR_H_
#include <cstddef>
#include "base/memory/scoped_ptr.h"
namespace syncer {
class CommitContribution;
namespace syncable {
class Directory;
}
// This class represents a source of items to commit to the sync server.
//
// When asked, it can return CommitContribution objects that contain a set of
// items to be committed from this source.
class CommitContributor {
public:
CommitContributor();
virtual ~CommitContributor() = 0;
// Gathers up to |max_entries| unsynced items from this contributor into a
// CommitContribution. Returns NULL when the contributor has nothing to
// contribute.
virtual scoped_ptr<CommitContribution> GetContribution(
size_t max_entries) = 0;
};
} // namespace
#endif // SYNC_ENGINE_COMMIT_CONTRIBUTOR_H_
......@@ -6,8 +6,8 @@
#include <map>
#include "sync/engine/sync_directory_commit_contribution.h"
#include "sync/engine/sync_directory_commit_contributor.h"
#include "sync/engine/commit_contribution.h"
#include "sync/engine/commit_contributor.h"
#include "sync/protocol/sync.pb.h"
namespace syncer {
......@@ -35,11 +35,11 @@ void CommitProcessor::GatherCommitContributions(
continue;
}
size_t spaces_remaining = max_entries - num_entries;
SyncDirectoryCommitContribution* contribution =
scoped_ptr<CommitContribution> contribution =
cm_it->second->GetContribution(spaces_remaining);
if (contribution) {
num_entries += contribution->GetNumEntries();
contributions->insert(std::make_pair(it.Get(), contribution));
contributions->insert(std::make_pair(it.Get(), contribution.release()));
}
if (num_entries >= max_entries) {
DCHECK_EQ(num_entries, max_entries)
......
......@@ -20,8 +20,8 @@ namespace syncable {
class Directory;
} // namespace syncable
class SyncDirectoryCommitContributor;
class SyncDirectoryCommitContribution;
class CommitContributor;
class CommitContribution;
// This class manages the set of per-type committer objects.
//
......@@ -31,12 +31,19 @@ class SyncDirectoryCommitContribution;
// contains a type which was not previously registered.
class SYNC_EXPORT_PRIVATE CommitProcessor {
public:
typedef std::map<ModelType, SyncDirectoryCommitContribution*> ContributionMap;
typedef std::map<ModelType, CommitContribution*> ContributionMap;
// Contructs a CommitProcessor from a map of CommitContributors.
// The CommitProcessor does not own this map.
explicit CommitProcessor(CommitContributorMap* commit_contributor_map);
~CommitProcessor();
// Gathers a set of contributions to be used to populate a commit message.
//
// For each of the |commit_types| in this CommitProcessor's CommitContributor
// map, gather any entries queued for commit into CommitContributions. The
// total number of entries in all the returned CommitContributions shall not
// exceed |max_entries|.
void GatherCommitContributions(
ModelTypeSet commit_types,
size_t max_entries,
......
// Copyright 2013 The Chromium Authors. All rights reserved.
// 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/engine/sync_directory_commit_contribution.h"
#include "sync/engine/directory_commit_contribution.h"
#include "sync/engine/commit_util.h"
#include "sync/engine/get_commit_ids.h"
......@@ -15,12 +15,12 @@ namespace syncer {
using syncable::GET_BY_HANDLE;
using syncable::SYNCER;
SyncDirectoryCommitContribution::~SyncDirectoryCommitContribution() {
DirectoryCommitContribution::~DirectoryCommitContribution() {
DCHECK(!syncing_bits_set_);
}
// static.
SyncDirectoryCommitContribution* SyncDirectoryCommitContribution::Build(
scoped_ptr<DirectoryCommitContribution> DirectoryCommitContribution::Build(
syncable::Directory* dir,
ModelType type,
size_t max_entries) {
......@@ -30,7 +30,7 @@ SyncDirectoryCommitContribution* SyncDirectoryCommitContribution::Build(
GetCommitIdsForType(&trans, type, max_entries, &metahandles);
if (metahandles.empty())
return NULL;
return scoped_ptr<DirectoryCommitContribution>();
google::protobuf::RepeatedPtrField<sync_pb::SyncEntity> entities;
for (std::vector<int64>::iterator it = metahandles.begin();
......@@ -41,10 +41,11 @@ SyncDirectoryCommitContribution* SyncDirectoryCommitContribution::Build(
entry.PutSyncing(true);
}
return new SyncDirectoryCommitContribution(metahandles, entities, dir);
return scoped_ptr<DirectoryCommitContribution>(
new DirectoryCommitContribution(metahandles, entities, dir));
}
void SyncDirectoryCommitContribution::AddToCommitMessage(
void DirectoryCommitContribution::AddToCommitMessage(
sync_pb::ClientToServerMessage* msg) {
DCHECK(syncing_bits_set_);
sync_pb::CommitMessage* commit_message = msg->mutable_commit();
......@@ -54,7 +55,7 @@ void SyncDirectoryCommitContribution::AddToCommitMessage(
RepeatedPtrFieldBackInserter(commit_message->mutable_entries()));
}
SyncerError SyncDirectoryCommitContribution::ProcessCommitResponse(
SyncerError DirectoryCommitContribution::ProcessCommitResponse(
const sync_pb::ClientToServerResponse& response,
sessions::StatusController* status) {
DCHECK(syncing_bits_set_);
......@@ -131,16 +132,16 @@ SyncerError SyncDirectoryCommitContribution::ProcessCommitResponse(
}
}
void SyncDirectoryCommitContribution::CleanUp() {
void DirectoryCommitContribution::CleanUp() {
DCHECK(syncing_bits_set_);
UnsetSyncingBits();
}
size_t SyncDirectoryCommitContribution::GetNumEntries() const {
size_t DirectoryCommitContribution::GetNumEntries() const {
return metahandles_.size();
}
SyncDirectoryCommitContribution::SyncDirectoryCommitContribution(
DirectoryCommitContribution::DirectoryCommitContribution(
const std::vector<int64>& metahandles,
const google::protobuf::RepeatedPtrField<sync_pb::SyncEntity>& entities,
syncable::Directory* dir)
......@@ -151,7 +152,7 @@ SyncDirectoryCommitContribution::SyncDirectoryCommitContribution(
syncing_bits_set_(true) {
}
void SyncDirectoryCommitContribution::UnsetSyncingBits() {
void DirectoryCommitContribution::UnsetSyncingBits() {
syncable::ModelNeutralWriteTransaction trans(FROM_HERE, SYNCER, dir_);
for (std::vector<int64>::const_iterator it = metahandles_.begin();
it != metahandles_.end(); ++it) {
......
// Copyright 2013 The Chromium Authors. All rights reserved.
// 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.
#ifndef SYNC_ENGINE_SYNC_DIRECTORY_COMMIT_CONTRIBUTION_H_
#define SYNC_ENGINE_SYNC_DIRECTORY_COMMIT_CONTRIBUTION_H_
#ifndef SYNC_ENGINE_DIRECTORY_COMMIT_CONTRIBUTION_H_
#define SYNC_ENGINE_DIRECTORY_COMMIT_CONTRIBUTION_H_
#include <vector>
#include "base/gtest_prod_util.h"
#include "base/memory/scoped_ptr.h"
#include "sync/base/sync_export.h"
#include "sync/engine/commit_contribution.h"
#include "sync/internal_api/public/base/model_type.h"
#include "sync/internal_api/public/util/syncer_error.h"
#include "sync/protocol/sync.pb.h"
......@@ -30,10 +32,11 @@ class Directory;
// This class handles the bookkeeping related to the commit of these items,
// including processing the commit response message and setting and unsetting
// the SYNCING bits.
class SYNC_EXPORT_PRIVATE SyncDirectoryCommitContribution {
class SYNC_EXPORT_PRIVATE DirectoryCommitContribution
: public CommitContribution {
public:
// This destructor will DCHECK if UnsetSyncingBits() has not been called yet.
~SyncDirectoryCommitContribution();
virtual ~DirectoryCommitContribution();
// Build a CommitContribution from the IS_UNSYNCED items in |dir| with the
// given |type|. The contribution will include at most |max_items| entries.
......@@ -41,7 +44,7 @@ class SYNC_EXPORT_PRIVATE SyncDirectoryCommitContribution {
// This function may return NULL if this type has no items ready for and
// requiring commit. This function may make model neutral changes to the
// directory.
static SyncDirectoryCommitContribution* Build(
static scoped_ptr<DirectoryCommitContribution> Build(
syncable::Directory* dir,
ModelType type,
size_t max_items);
......@@ -51,7 +54,7 @@ class SYNC_EXPORT_PRIVATE SyncDirectoryCommitContribution {
// This function is not const. It will update some state in this contribution
// that will be used when processing the associated commit response. This
// function should not be called more than once.
void AddToCommitMessage(sync_pb::ClientToServerMessage* msg);
virtual void AddToCommitMessage(sync_pb::ClientToServerMessage* msg) OVERRIDE;
// Updates this contribution's contents in accordance with the provided
// |response|.
......@@ -59,24 +62,24 @@ class SYNC_EXPORT_PRIVATE SyncDirectoryCommitContribution {
// This function may make model-neutral changes to the directory. It is not
// valid to call this function unless AddToCommitMessage() was called earlier.
// This function should not be called more than once.
SyncerError ProcessCommitResponse(
virtual SyncerError ProcessCommitResponse(
const sync_pb::ClientToServerResponse& response,
sessions::StatusController* status);
sessions::StatusController* status) OVERRIDE;
// Cleans up any temproary state associated with the commit. Must be called
// before destruction.
void CleanUp();
virtual void CleanUp() OVERRIDE;
// Returns the number of entries included in this contribution.
size_t GetNumEntries() const;
virtual size_t GetNumEntries() const OVERRIDE;
private:
class SyncDirectoryCommitContributionTest;
FRIEND_TEST_ALL_PREFIXES(SyncDirectoryCommitContributionTest, GatherByTypes);
FRIEND_TEST_ALL_PREFIXES(SyncDirectoryCommitContributionTest,
class DirectoryCommitContributionTest;
FRIEND_TEST_ALL_PREFIXES(DirectoryCommitContributionTest, GatherByTypes);
FRIEND_TEST_ALL_PREFIXES(DirectoryCommitContributionTest,
GatherAndTruncate);
SyncDirectoryCommitContribution(
DirectoryCommitContribution(
const std::vector<int64>& metahandles,
const google::protobuf::RepeatedPtrField<sync_pb::SyncEntity>& entities,
syncable::Directory* directory);
......@@ -94,9 +97,9 @@ class SYNC_EXPORT_PRIVATE SyncDirectoryCommitContribution {
// called. This flag must be unset by the time our destructor is called.
bool syncing_bits_set_;
DISALLOW_COPY_AND_ASSIGN(SyncDirectoryCommitContribution);
DISALLOW_COPY_AND_ASSIGN(DirectoryCommitContribution);
};
} // namespace syncer
#endif // SYNC_ENGINE_SYNC_DIRECTORY_COMMIT_CONTRIBUTION_H_
#endif // SYNC_ENGINE_DIRECTORY_COMMIT_CONTRIBUTION_H_
// Copyright 2013 The Chromium Authors. All rights reserved.
// 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/engine/sync_directory_commit_contribution.h"
#include "sync/engine/directory_commit_contribution.h"
#include "base/message_loop/message_loop.h"
#include "sync/sessions/status_controller.h"
......@@ -17,7 +17,7 @@
namespace syncer {
class SyncDirectoryCommitContributionTest : public ::testing::Test {
class DirectoryCommitContributionTest : public ::testing::Test {
public:
virtual void SetUp() OVERRIDE {
dir_maker_.SetUp();
......@@ -74,9 +74,9 @@ class SyncDirectoryCommitContributionTest : public ::testing::Test {
TestDirectorySetterUpper dir_maker_;
};
// Verify that the SyncDirectoryCommitContribution contains only entries of its
// Verify that the DirectoryCommitContribution contains only entries of its
// specified type.
TEST_F(SyncDirectoryCommitContributionTest, GatherByTypes) {
TEST_F(DirectoryCommitContributionTest, GatherByTypes) {
int64 pref1;
{
syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, dir());
......@@ -85,8 +85,8 @@ TEST_F(SyncDirectoryCommitContributionTest, GatherByTypes) {
CreateUnsyncedItem(&trans, EXTENSIONS, "extension1");
}
scoped_ptr<SyncDirectoryCommitContribution> cc(
SyncDirectoryCommitContribution::Build(dir(), PREFERENCES, 5));
scoped_ptr<DirectoryCommitContribution> cc(
DirectoryCommitContribution::Build(dir(), PREFERENCES, 5));
ASSERT_EQ(2U, cc->GetNumEntries());
const std::vector<int64>& metahandles = cc->metahandles_;
......@@ -98,9 +98,9 @@ TEST_F(SyncDirectoryCommitContributionTest, GatherByTypes) {
cc->CleanUp();
}
// Verify that the SyncDirectoryCommitContributionTest builder function
// Verify that the DirectoryCommitContributionTest builder function
// truncates if necessary.
TEST_F(SyncDirectoryCommitContributionTest, GatherAndTruncate) {
TEST_F(DirectoryCommitContributionTest, GatherAndTruncate) {
int64 pref1;
int64 pref2;
{
......@@ -110,8 +110,8 @@ TEST_F(SyncDirectoryCommitContributionTest, GatherAndTruncate) {
CreateUnsyncedItem(&trans, EXTENSIONS, "extension1");
}
scoped_ptr<SyncDirectoryCommitContribution> cc(
SyncDirectoryCommitContribution::Build(dir(), PREFERENCES, 1));
scoped_ptr<DirectoryCommitContribution> cc(
DirectoryCommitContribution::Build(dir(), PREFERENCES, 1));
ASSERT_EQ(1U, cc->GetNumEntries());
int64 only_metahandle = cc->metahandles_[0];
......@@ -120,11 +120,11 @@ TEST_F(SyncDirectoryCommitContributionTest, GatherAndTruncate) {
cc->CleanUp();
}
// Sanity check for building commits from SyncDirectoryCommitContributions.
// Sanity check for building commits from DirectoryCommitContributions.
// This test makes two CommitContribution objects of different types and uses
// them to initialize a commit message. Then it checks that the contents of the
// commit message match those of the directory they came from.
TEST_F(SyncDirectoryCommitContributionTest, PrepareCommit) {
TEST_F(DirectoryCommitContributionTest, PrepareCommit) {
{
syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, dir());
CreateUnsyncedItem(&trans, PREFERENCES, "pref1");
......@@ -132,10 +132,10 @@ TEST_F(SyncDirectoryCommitContributionTest, PrepareCommit) {
CreateUnsyncedItem(&trans, EXTENSIONS, "extension1");
}
scoped_ptr<SyncDirectoryCommitContribution> pref_cc(
SyncDirectoryCommitContribution::Build(dir(), PREFERENCES, 25));
scoped_ptr<SyncDirectoryCommitContribution> ext_cc(
SyncDirectoryCommitContribution::Build(dir(), EXTENSIONS, 25));
scoped_ptr<DirectoryCommitContribution> pref_cc(
DirectoryCommitContribution::Build(dir(), PREFERENCES, 25));
scoped_ptr<DirectoryCommitContribution> ext_cc(
DirectoryCommitContribution::Build(dir(), EXTENSIONS, 25));
sync_pb::ClientToServerMessage message;
pref_cc->AddToCommitMessage(&message);
......@@ -173,7 +173,7 @@ TEST_F(SyncDirectoryCommitContributionTest, PrepareCommit) {
// Creates some unsynced items, pretends to commit them, and hands back a
// specially crafted response to the syncer in order to test commit response
// processing. The response simulates a succesful commit scenario.
TEST_F(SyncDirectoryCommitContributionTest, ProcessCommitResponse) {
TEST_F(DirectoryCommitContributionTest, ProcessCommitResponse) {
int64 pref1_handle;
int64 pref2_handle;
int64 ext1_handle;
......@@ -184,10 +184,10 @@ TEST_F(SyncDirectoryCommitContributionTest, ProcessCommitResponse) {
ext1_handle = CreateUnsyncedItem(&trans, EXTENSIONS, "extension1");
}
scoped_ptr<SyncDirectoryCommitContribution> pref_cc(
SyncDirectoryCommitContribution::Build(dir(), PREFERENCES, 25));
scoped_ptr<SyncDirectoryCommitContribution> ext_cc(
SyncDirectoryCommitContribution::Build(dir(), EXTENSIONS, 25));
scoped_ptr<DirectoryCommitContribution> pref_cc(
DirectoryCommitContribution::Build(dir(), PREFERENCES, 25));
scoped_ptr<DirectoryCommitContribution> ext_cc(
DirectoryCommitContribution::Build(dir(), EXTENSIONS, 25));
sync_pb::ClientToServerMessage message;
pref_cc->AddToCommitMessage(&message);
......
// Copyright 2013 The Chromium Authors. All rights reserved.
// 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/engine/sync_directory_commit_contributor.h"
#include "sync/engine/directory_commit_contributor.h"
#include "sync/engine/sync_directory_commit_contribution.h"
#include "sync/engine/directory_commit_contribution.h"
namespace syncer {
SyncDirectoryCommitContributor::SyncDirectoryCommitContributor(
DirectoryCommitContributor::DirectoryCommitContributor(
syncable::Directory* dir,
ModelType type)
: dir_(dir),
type_(type) {}
SyncDirectoryCommitContributor::~SyncDirectoryCommitContributor() {}
DirectoryCommitContributor::~DirectoryCommitContributor() {}
SyncDirectoryCommitContribution*
SyncDirectoryCommitContributor::GetContribution(size_t max_entries) {
return SyncDirectoryCommitContribution::Build(dir_, type_, max_entries);
scoped_ptr<CommitContribution>
DirectoryCommitContributor::GetContribution(size_t max_entries) {
return DirectoryCommitContribution::Build(dir_, type_, max_entries)
.PassAs<CommitContribution>();
}
} // namespace syncer
// Copyright 2013 The Chromium Authors. All rights reserved.
// 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.
#ifndef SYNC_ENGINE_SYNC_DIRECTORY_COMMIT_CONTRIBUTOR_H_
#define SYNC_ENGINE_SYNC_DIRECTORY_COMMIT_CONTRIBUTOR_H_
#ifndef SYNC_ENGINE_DIRECTORY_COMMIT_CONTRIBUTOR_H_
#define SYNC_ENGINE_DIRECTORY_COMMIT_CONTRIBUTOR_H_
#include <map>
#include "base/memory/scoped_ptr.h"
#include "sync/engine/commit_contributor.h"
#include "sync/engine/directory_commit_contribution.h"
#include "sync/internal_api/public/base/model_type.h"
namespace syncer {
class SyncDirectoryCommitContribution;
namespace syncable {
class Directory;
}
......@@ -23,19 +24,22 @@ class Directory;
// Each instance of this class represents a particular type within the
// syncable::Directory. When asked, it will iterate through the directory, grab
// any items of its type that are ready for commit, and return them in the form
// of a SyncDirectoryCommitContribution.
class SyncDirectoryCommitContributor {
// of a DirectoryCommitContribution.
class DirectoryCommitContributor : public CommitContributor {
public:
SyncDirectoryCommitContributor(syncable::Directory* dir, ModelType type);
~SyncDirectoryCommitContributor();
DirectoryCommitContributor(syncable::Directory* dir, ModelType type);
virtual ~DirectoryCommitContributor();
SyncDirectoryCommitContribution* GetContribution(size_t max_entries);
virtual scoped_ptr<CommitContribution> GetContribution(
size_t max_entries) OVERRIDE;
private:
syncable::Directory* dir_;
ModelType type_;
DISALLOW_COPY_AND_ASSIGN(DirectoryCommitContributor);
};
} // namespace
#endif // SYNC_ENGINE_SYNC_DIRECTORY_COMMIT_CONTRIBUTOR_H_
#endif // SYNC_ENGINE_DIRECTORY_COMMIT_CONTRIBUTOR_H_
// Copyright 2013 The Chromium Authors. All rights reserved.
// 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/engine/sync_directory_update_handler.h"
#include "sync/engine/directory_update_handler.h"
#include "sync/engine/conflict_resolver.h"
#include "sync/engine/process_updates_util.h"
......@@ -16,7 +16,7 @@ namespace syncer {
using syncable::SYNCER;
SyncDirectoryUpdateHandler::SyncDirectoryUpdateHandler(
DirectoryUpdateHandler::DirectoryUpdateHandler(
syncable::Directory* dir,
ModelType type,
scoped_refptr<ModelSafeWorker> worker)
......@@ -24,14 +24,14 @@ SyncDirectoryUpdateHandler::SyncDirectoryUpdateHandler(
type_(type),
worker_(worker) {}
SyncDirectoryUpdateHandler::~SyncDirectoryUpdateHandler() {}
DirectoryUpdateHandler::~DirectoryUpdateHandler() {}
void SyncDirectoryUpdateHandler::GetDownloadProgress(
void DirectoryUpdateHandler::GetDownloadProgress(
sync_pb::DataTypeProgressMarker* progress_marker) const {
dir_->GetDownloadProgress(type_, progress_marker);
}
void SyncDirectoryUpdateHandler::ProcessGetUpdatesResponse(
void DirectoryUpdateHandler::ProcessGetUpdatesResponse(
const sync_pb::DataTypeProgressMarker& progress_marker,
const SyncEntityList& applicable_updates,
sessions::StatusController* status) {
......@@ -40,8 +40,7 @@ void SyncDirectoryUpdateHandler::ProcessGetUpdatesResponse(
UpdateProgressMarker(progress_marker);
}
void SyncDirectoryUpdateHandler::ApplyUpdates(
sessions::StatusController* status) {
void DirectoryUpdateHandler::ApplyUpdates(sessions::StatusController* status) {
if (!IsApplyUpdatesRequired()) {
return;
}
......@@ -49,14 +48,14 @@ void SyncDirectoryUpdateHandler::ApplyUpdates(
// This will invoke handlers that belong to the model and its thread, so we
// switch to the appropriate thread before we start this work.
WorkCallback c = base::Bind(
&SyncDirectoryUpdateHandler::ApplyUpdatesImpl,
&DirectoryUpdateHandler::ApplyUpdatesImpl,
// We wait until the callback is executed. We can safely use Unretained.
base::Unretained(this),
base::Unretained(status));
worker_->DoWorkAndWaitUntilDone(c);
}
void SyncDirectoryUpdateHandler::PassiveApplyUpdates(
void DirectoryUpdateHandler::PassiveApplyUpdates(
sessions::StatusController* status) {
if (!IsApplyUpdatesRequired()) {
return;
......@@ -66,7 +65,7 @@ void SyncDirectoryUpdateHandler::PassiveApplyUpdates(
ApplyUpdatesImpl(status);
}
SyncerError SyncDirectoryUpdateHandler::ApplyUpdatesImpl(
SyncerError DirectoryUpdateHandler::ApplyUpdatesImpl(
sessions::StatusController* status) {
syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir_);
......@@ -133,7 +132,7 @@ SyncerError SyncDirectoryUpdateHandler::ApplyUpdatesImpl(
return SYNCER_OK;
}
bool SyncDirectoryUpdateHandler::IsApplyUpdatesRequired() {
bool DirectoryUpdateHandler::IsApplyUpdatesRequired() {
if (IsControlType(type_)) {
return false; // We don't process control types here.
}
......@@ -141,14 +140,14 @@ bool SyncDirectoryUpdateHandler::IsApplyUpdatesRequired() {
return dir_->TypeHasUnappliedUpdates(type_);
}
void SyncDirectoryUpdateHandler::UpdateSyncEntities(
void DirectoryUpdateHandler::UpdateSyncEntities(
syncable::ModelNeutralWriteTransaction* trans,
const SyncEntityList& applicable_updates,
sessions::StatusController* status) {
ProcessDownloadedUpdates(dir_, trans, type_, applicable_updates, status);
}
void SyncDirectoryUpdateHandler::UpdateProgressMarker(
void DirectoryUpdateHandler::UpdateProgressMarker(
const sync_pb::DataTypeProgressMarker& progress_marker) {
int field_number = progress_marker.data_type_id();
ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number);
......
// Copyright 2013 The Chromium Authors. All rights reserved.
// 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.
#ifndef SYNC_ENGINE_SYNC_DIRECTORY_UPDATE_HANDLER_H_
#define SYNC_ENGINE_SYNC_DIRECTORY_UPDATE_HANDLER_H_
#ifndef SYNC_ENGINE_DIRECTORY_UPDATE_HANDLER_H_
#define SYNC_ENGINE_DIRECTORY_UPDATE_HANDLER_H_
#include <map>
......@@ -11,6 +11,7 @@
#include "base/memory/ref_counted.h"
#include "sync/base/sync_export.h"
#include "sync/engine/process_updates_util.h"
#include "sync/engine/update_handler.h"
#include "sync/internal_api/public/base/model_type.h"
#include "sync/internal_api/public/util/syncer_error.h"
......@@ -37,16 +38,16 @@ class ModelSafeWorker;
// Each instance of this class represents a particular type in the
// syncable::Directory. It can store and retreive that type's progress markers.
// It can also process a set of received SyncEntities and store their data.
class SYNC_EXPORT_PRIVATE SyncDirectoryUpdateHandler {
class SYNC_EXPORT_PRIVATE DirectoryUpdateHandler : public UpdateHandler {
public:
SyncDirectoryUpdateHandler(syncable::Directory* dir,
ModelType type,
scoped_refptr<ModelSafeWorker> worker);
~SyncDirectoryUpdateHandler();
DirectoryUpdateHandler(syncable::Directory* dir,
ModelType type,
scoped_refptr<ModelSafeWorker> worker);
virtual ~DirectoryUpdateHandler();
// Fills the given parameter with the stored progress marker for this type.
void GetDownloadProgress(
sync_pb::DataTypeProgressMarker* progress_marker) const;
virtual void GetDownloadProgress(
sync_pb::DataTypeProgressMarker* progress_marker) const OVERRIDE;
// Processes the contents of a GetUpdates response message.
//
......@@ -54,22 +55,22 @@ class SYNC_EXPORT_PRIVATE SyncDirectoryUpdateHandler {
// single GetUpdates response message. The progress marker's type must match
// this update handler's type, and the set of SyncEntities must include all
// entities of this type found in the response message.
void ProcessGetUpdatesResponse(
virtual void ProcessGetUpdatesResponse(
const sync_pb::DataTypeProgressMarker& progress_marker,
const SyncEntityList& applicable_updates,
sessions::StatusController* status);
sessions::StatusController* status) OVERRIDE;
// If there are updates to apply, apply them on the proper thread.
// Delegates to ApplyUpdatesImpl().
void ApplyUpdates(sessions::StatusController* status);
virtual void ApplyUpdates(sessions::StatusController* status) OVERRIDE;
// Apply updates on the sync thread. This is for use during initial sync
// prior to model association.
void PassiveApplyUpdates(sessions::StatusController* status);
virtual void PassiveApplyUpdates(sessions::StatusController* status) OVERRIDE;
private:
friend class SyncDirectoryUpdateHandlerApplyUpdateTest;
friend class SyncDirectoryUpdateHandlerProcessUpdateTest;
friend class DirectoryUpdateHandlerApplyUpdateTest;
friend class DirectoryUpdateHandlerProcessUpdateTest;
// Sometimes there is nothing to do, so we can return without doing anything.
bool IsApplyUpdatesRequired();
......@@ -93,9 +94,9 @@ class SYNC_EXPORT_PRIVATE SyncDirectoryUpdateHandler {
ModelType type_;
scoped_refptr<ModelSafeWorker> worker_;
DISALLOW_COPY_AND_ASSIGN(SyncDirectoryUpdateHandler);
DISALLOW_COPY_AND_ASSIGN(DirectoryUpdateHandler);
};
} // namespace syncer
#endif // SYNC_ENGINE_SYNC_DIRECTORY_UPDATE_HANDLER_H_
#endif // SYNC_ENGINE_DIRECTORY_UPDATE_HANDLER_H_
......@@ -7,15 +7,14 @@
#include "base/message_loop/message_loop.h"
#include "base/stl_util.h"
#include "sync/engine/get_updates_delegate.h"
#include "sync/engine/sync_directory_update_handler.h"
#include "sync/engine/update_handler.h"
#include "sync/internal_api/public/base/model_type_test_util.h"
#include "sync/protocol/sync.pb.h"
#include "sync/sessions/debug_info_getter.h"
#include "sync/sessions/nudge_tracker.h"
#include "sync/sessions/status_controller.h"
#include "sync/syncable/directory.h"
#include "sync/test/engine/fake_model_worker.h"
#include "sync/test/engine/test_directory_setter_upper.h"
#include "sync/test/engine/mock_update_handler.h"
#include "sync/test/sessions/mock_debug_info_getter.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -31,25 +30,15 @@ class DownloadUpdatesTest : public ::testing::Test {
update_handler_deleter_(&update_handler_map_) {}
virtual void SetUp() {
dir_maker_.SetUp();
AddUpdateHandler(AUTOFILL, GROUP_DB);
AddUpdateHandler(BOOKMARKS, GROUP_UI);
AddUpdateHandler(PREFERENCES, GROUP_UI);
}
virtual void TearDown() {
dir_maker_.TearDown();
AddUpdateHandler(AUTOFILL);
AddUpdateHandler(BOOKMARKS);
AddUpdateHandler(PREFERENCES);
}
ModelTypeSet request_types() {
return request_types_;
}
syncable::Directory* directory() {
return dir_maker_.directory();
}
scoped_ptr<GetUpdatesProcessor> BuildGetUpdatesProcessor(
const GetUpdatesDelegate& delegate) {
return scoped_ptr<GetUpdatesProcessor>(
......@@ -72,20 +61,13 @@ class DownloadUpdatesTest : public ::testing::Test {
const base::TimeTicks kTestStartTime;
private:
void AddUpdateHandler(ModelType type, ModelSafeGroup group) {
DCHECK(directory());
void AddUpdateHandler(ModelType type) {
request_types_.Put(type);
scoped_refptr<ModelSafeWorker> worker = new FakeModelWorker(group);
SyncDirectoryUpdateHandler* handler =
new SyncDirectoryUpdateHandler(directory(), type, worker);
UpdateHandler* handler = new MockUpdateHandler(type);
update_handler_map_.insert(std::make_pair(type, handler));
}
base::MessageLoop loop_; // Needed for directory init.
TestDirectorySetterUpper dir_maker_;
ModelTypeSet request_types_;
UpdateHandlerMap update_handler_map_;
STLValueDeleter<UpdateHandlerMap> update_handler_deleter_;
......
......@@ -4,8 +4,8 @@
#include "sync/engine/get_updates_delegate.h"
#include "sync/engine/directory_update_handler.h"
#include "sync/engine/get_updates_processor.h"
#include "sync/engine/sync_directory_update_handler.h"
namespace syncer {
......
......@@ -7,9 +7,12 @@
#include <map>
#include "sync/engine/get_updates_delegate.h"
#include "sync/engine/sync_directory_update_handler.h"
#include "sync/engine/update_handler.h"
#include "sync/protocol/sync.pb.h"
typedef std::vector<const sync_pb::SyncEntity*> SyncEntityList;
typedef std::map<syncer::ModelType, SyncEntityList> TypeSyncEntityMap;
namespace syncer {
typedef std::map<ModelType, size_t> TypeToIndexMap;
......
......@@ -30,10 +30,6 @@ class Directory;
} // namespace syncable
class GetUpdatesDelegate;
class SyncDirectoryUpdateHandler;
typedef std::vector<const sync_pb::SyncEntity*> SyncEntityList;
typedef std::map<ModelType, SyncEntityList> TypeSyncEntityMap;
// This class manages the set of per-type syncer objects.
//
......
// 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/engine/update_handler.h"
namespace syncer {
UpdateHandler::UpdateHandler() {}
UpdateHandler::~UpdateHandler() {}
} // namespace syncer
// 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.
#ifndef SYNC_ENGINE_UPDATE_HANDLER_H_
#define SYNC_ENGINE_UPDATE_HANDLER_H_
#include <vector>
#include "sync/base/sync_export.h"
namespace sync_pb {
class DataTypeProgressMarker;
class SyncEntity;
}
typedef std::vector<const sync_pb::SyncEntity*> SyncEntityList;
namespace syncer {
namespace sessions {
class StatusController;
}
class ModelSafeWorker;
// This class represents an entity that can request, receive, and apply updates
// from the sync server.
class SYNC_EXPORT_PRIVATE UpdateHandler {
public:
UpdateHandler();
virtual ~UpdateHandler() = 0;
// Fills the given parameter with the stored progress marker for this type.
virtual void GetDownloadProgress(
sync_pb::DataTypeProgressMarker* progress_marker) const = 0;
// Processes the contents of a GetUpdates response message.
//
// Should be invoked with the progress marker and set of SyncEntities from a
// single GetUpdates response message. The progress marker's type must match
// this update handler's type, and the set of SyncEntities must include all
// entities of this type found in the response message.
//
// In this context, "applicable_updates" means the set of updates belonging to
// this type.
virtual void ProcessGetUpdatesResponse(
const sync_pb::DataTypeProgressMarker& progress_marker,
const SyncEntityList& applicable_updates,
sessions::StatusController* status) = 0;
// Called at the end of a non-configure GetUpdates loop to apply any unapplied
// updates.
virtual void ApplyUpdates(sessions::StatusController* status) = 0;
// Called at the end of a configure GetUpdates loop to perform any required
// post-initial-download update application.
virtual void PassiveApplyUpdates(sessions::StatusController* status) = 0;
};
} // namespace syncer
#endif // SYNC_ENGINE_UPDATE_HANDLER_H_
......@@ -4,8 +4,8 @@
#include "sync/sessions/model_type_registry.h"
#include "sync/engine/sync_directory_commit_contributor.h"
#include "sync/engine/sync_directory_update_handler.h"
#include "sync/engine/directory_commit_contributor.h"
#include "sync/engine/directory_update_handler.h"
namespace syncer {
......@@ -39,10 +39,10 @@ void ModelTypeRegistry::SetEnabledDirectoryTypes(
DCHECK(worker_it != workers_map_.end());
scoped_refptr<ModelSafeWorker> worker = worker_it->second;
SyncDirectoryCommitContributor* committer =
new SyncDirectoryCommitContributor(directory_, type);
SyncDirectoryUpdateHandler* updater =
new SyncDirectoryUpdateHandler(directory_, type, worker);
DirectoryCommitContributor* committer =
new DirectoryCommitContributor(directory_, type);
DirectoryUpdateHandler* updater =
new DirectoryUpdateHandler(directory_, type, worker);
bool inserted1 =
update_handler_map_.insert(std::make_pair(type, updater)).second;
......
......@@ -20,12 +20,11 @@ namespace syncable {
class Directory;
} // namespace syncable
class SyncDirectoryUpdateHandler;
class SyncDirectoryCommitContributor;
class UpdateHandler;
class CommitContributor;
typedef std::map<ModelType, SyncDirectoryUpdateHandler*> UpdateHandlerMap;
typedef std::map<ModelType, SyncDirectoryCommitContributor*>
CommitContributorMap;
typedef std::map<ModelType, UpdateHandler*> UpdateHandlerMap;
typedef std::map<ModelType, CommitContributor*> CommitContributorMap;
// Keeps track of the sets of active update handlers and commit contributors.
class SYNC_EXPORT_PRIVATE ModelTypeRegistry {
......
......@@ -35,6 +35,10 @@
'engine/backoff_delay_provider.cc',
'engine/backoff_delay_provider.h',
'engine/commit.cc',
'engine/commit_contribution.cc',
'engine/commit_contribution.h',
'engine/commit_contributor.cc',
'engine/commit_contributor.h',
'engine/commit.h',
'engine/commit_processor.cc',
'engine/commit_processor.h',
......@@ -44,6 +48,12 @@
'engine/conflict_resolver.h',
'engine/conflict_util.cc',
'engine/conflict_util.h',
'engine/directory_commit_contribution.cc',
'engine/directory_commit_contribution.h',
'engine/directory_commit_contributor.cc',
'engine/directory_commit_contributor.h',
'engine/directory_update_handler.cc',
'engine/directory_update_handler.h',
'engine/download.cc',
'engine/download.h',
'engine/get_commit_ids.cc',
......@@ -62,18 +72,8 @@
'engine/process_updates_util.h',
'engine/sync_cycle_event.cc',
'engine/sync_cycle_event.h',
'engine/sync_directory_commit_contribution.cc',
'engine/sync_directory_commit_contribution.h',
'engine/sync_directory_commit_contributor.cc',
'engine/sync_directory_commit_contributor.h',
'engine/sync_directory_update_handler.cc',
'engine/sync_directory_update_handler.h',
'engine/sync_engine_event_listener.cc',
'engine/sync_engine_event_listener.h',
'engine/sync_scheduler.cc',
'engine/sync_scheduler.h',
'engine/sync_scheduler_impl.cc',
'engine/sync_scheduler_impl.h',
'engine/syncer.cc',
'engine/syncer.h',
'engine/syncer_proto_util.cc',
......@@ -81,12 +81,18 @@
'engine/syncer_types.h',
'engine/syncer_util.cc',
'engine/syncer_util.h',
'engine/sync_scheduler.cc',
'engine/sync_scheduler.h',
'engine/sync_scheduler_impl.cc',
'engine/sync_scheduler_impl.h',
'engine/traffic_logger.cc',
'engine/traffic_logger.h',
'engine/traffic_recorder.cc',
'engine/traffic_recorder.h',
'engine/update_applicator.cc',
'engine/update_applicator.h',
'engine/update_handler.cc',
'engine/update_handler.h',
'js/js_arg_list.cc',
'js/js_arg_list.h',
'js/js_backend.h',
......
......@@ -40,6 +40,8 @@
'test/engine/fake_sync_scheduler.h',
'test/engine/mock_connection_manager.cc',
'test/engine/mock_connection_manager.h',
'test/engine/mock_update_handler.cc',
'test/engine/mock_update_handler.h',
'test/engine/test_directory_setter_upper.cc',
'test/engine/test_directory_setter_upper.h',
'test/engine/test_id_factory.h',
......@@ -273,8 +275,8 @@
'engine/apply_control_data_updates_unittest.cc',
'engine/backoff_delay_provider_unittest.cc',
'engine/download_unittest.cc',
'engine/sync_directory_commit_contribution_unittest.cc',
'engine/sync_directory_update_handler_unittest.cc',
'engine/directory_commit_contribution_unittest.cc',
'engine/directory_update_handler_unittest.cc',
'engine/sync_scheduler_unittest.cc',
'engine/syncer_proto_util_unittest.cc',
'engine/syncer_unittest.cc',
......
// 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/test/engine/mock_update_handler.h"
#include "sync/internal_api/public/base/model_type.h"
namespace syncer {
MockUpdateHandler::MockUpdateHandler(ModelType type) {
progress_marker_.set_data_type_id(GetSpecificsFieldNumberFromModelType(type));
const std::string& token_str =
std::string("Mock token: ") + std::string(ModelTypeToString(type));
progress_marker_.set_token(token_str);
}
MockUpdateHandler::~MockUpdateHandler() {}
void MockUpdateHandler::GetDownloadProgress(
sync_pb::DataTypeProgressMarker* progress_marker) const {
progress_marker->CopyFrom(progress_marker_);
}
void MockUpdateHandler::ProcessGetUpdatesResponse(
const sync_pb::DataTypeProgressMarker& progress_marker,
const SyncEntityList& applicable_updates,
sessions::StatusController* status) {
progress_marker_.CopyFrom(progress_marker);
}
void MockUpdateHandler::ApplyUpdates(sessions::StatusController* status) {}
void MockUpdateHandler::PassiveApplyUpdates(
sessions::StatusController* status) {}
} // namespace syncer
// 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.
#ifndef SYNC_TEST_ENGINE_MOCK_UPDATE_HANDLER_H_
#define SYNC_TEST_ENGINE_MOCK_UPDATE_HANDLER_H_
#include "base/compiler_specific.h"
#include "sync/engine/update_handler.h"
#include "sync/internal_api/public/base/model_type.h"
#include "sync/protocol/sync.pb.h"
namespace syncer {
class MockUpdateHandler : public UpdateHandler {
public:
explicit MockUpdateHandler(ModelType type);
virtual ~MockUpdateHandler();
// UpdateHandler implementation.
virtual void GetDownloadProgress(
sync_pb::DataTypeProgressMarker* progress_marker) const OVERRIDE;
virtual void ProcessGetUpdatesResponse(
const sync_pb::DataTypeProgressMarker& progress_marker,
const SyncEntityList& applicable_updates,
sessions::StatusController* status) OVERRIDE;
virtual void ApplyUpdates(sessions::StatusController* status) OVERRIDE;
virtual void PassiveApplyUpdates(sessions::StatusController* status) OVERRIDE;
private:
sync_pb::DataTypeProgressMarker progress_marker_;
};
} // namespace syncer
#endif // SYNC_TEST_ENGINE_MOCK_UPDATE_HANDLER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment