blob: 93d42153fc267ccea48026eae87dc857dd03a7db [file] [log] [blame]
// 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_NON_BLOCKING_TYPE_PROCESSOR_CORE_H_
#define SYNC_ENGINE_NON_BLOCKING_TYPE_PROCESSOR_CORE_H_
#include "base/memory/weak_ptr.h"
#include "base/stl_util.h"
#include "base/threading/non_thread_safe.h"
#include "sync/base/sync_export.h"
#include "sync/engine/commit_contributor.h"
#include "sync/engine/non_blocking_sync_common.h"
#include "sync/engine/update_handler.h"
#include "sync/internal_api/public/base/model_type.h"
#include "sync/protocol/sync.pb.h"
namespace base {
class SingleThreadTaskRunner;
}
namespace syncer {
class NonBlockingTypeProcessorInterface;
class SyncThreadSyncEntity;
// A smart cache for sync types that use message passing (rather than
// transactions and the syncable::Directory) to communicate with the sync
// thread.
//
// When the non-blocking sync type wants to talk with the sync server, it will
// send a message from its thread to this object on the sync thread. This
// object ensures the appropriate sync server communication gets scheduled and
// executed. The response, if any, will be returned to the non-blocking sync
// type's thread eventually.
//
// This object also has a role to play in communications in the opposite
// direction. Sometimes the sync thread will receive changes from the sync
// server and deliver them here. This object will post this information back to
// the appropriate component on the model type's thread.
//
// This object does more than just pass along messages. It understands the sync
// protocol, and it can make decisions when it sees conflicting messages. For
// example, if the sync server sends down an update for a sync entity that is
// currently pending for commit, this object will detect this condition and
// cancel the pending commit.
class SYNC_EXPORT NonBlockingTypeProcessorCore
: public UpdateHandler,
public CommitContributor,
public base::NonThreadSafe {
public:
NonBlockingTypeProcessorCore(
ModelType type,
const DataTypeState& initial_state,
scoped_ptr<NonBlockingTypeProcessorInterface> processor_interface);
virtual ~NonBlockingTypeProcessorCore();
ModelType GetModelType() const;
// UpdateHandler implementation.
virtual void GetDownloadProgress(
sync_pb::DataTypeProgressMarker* progress_marker) const OVERRIDE;
virtual void GetDataTypeContext(sync_pb::DataTypeContext* context) const
OVERRIDE;
virtual SyncerError ProcessGetUpdatesResponse(
const sync_pb::DataTypeProgressMarker& progress_marker,
const sync_pb::DataTypeContext& mutated_context,
const SyncEntityList& applicable_updates,
sessions::StatusController* status) OVERRIDE;
virtual void ApplyUpdates(sessions::StatusController* status) OVERRIDE;
virtual void PassiveApplyUpdates(sessions::StatusController* status) OVERRIDE;
// Entry point for NonBlockingTypeProcessor to send commit requests.
void EnqueueForCommit(const CommitRequestDataList& request_list);
// CommitContributor implementation.
virtual scoped_ptr<CommitContribution> GetContribution(
size_t max_entries) OVERRIDE;
// Callback for when our contribution gets a response.
void OnCommitResponse(const CommitResponseDataList& response_list);
base::WeakPtr<NonBlockingTypeProcessorCore> AsWeakPtr();
private:
typedef std::map<std::string, SyncThreadSyncEntity*> EntityMap;
// Stores a single commit request in this object's internal state.
void StorePendingCommit(const CommitRequestData& request);
// Returns true if all data type state required for commits is available. In
// practice, this means that it returns true from the time this object first
// receives notice of a successful update fetch from the server.
bool CanCommitItems() const;
// Initializes the parts of a commit entity that are the responsibility of
// this class, and not the SyncThreadSyncEntity. Some fields, like the
// client-assigned ID, can only be set by an entity with knowledge of the
// entire data type's state.
void HelpInitializeCommitEntity(sync_pb::SyncEntity* commit_entity);
ModelType type_;
// State that applies to the entire model type.
DataTypeState data_type_state_;
// Abstraction around the NonBlockingTypeProcessor so this class
// doesn't need to know about its specific implementation or
// which thread it's on. This makes it easier to write tests.
scoped_ptr<NonBlockingTypeProcessorInterface> processor_interface_;
// A map of per-entity information known to this object.
//
// When commits are pending, their information is stored here. This
// information is dropped from memory when the commit succeeds or gets
// cancelled.
//
// This also stores some information related to received server state in
// order to implement reflection blocking and conflict detection. This
// information is kept in memory indefinitely. With a bit more coordination
// with the model thread, we could optimize this to reduce memory usage in
// the steady state.
EntityMap entities_;
STLValueDeleter<EntityMap> entities_deleter_;
base::WeakPtrFactory<NonBlockingTypeProcessorCore> weak_ptr_factory_;
};
} // namespace syncer
#endif // SYNC_ENGINE_NON_BLOCKING_TYPE_PROCESSOR_CORE_H_