Home | History | Annotate | Download | only in engine
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef SYNC_ENGINE_NON_BLOCKING_TYPE_PROCESSOR_CORE_H_
      6 #define SYNC_ENGINE_NON_BLOCKING_TYPE_PROCESSOR_CORE_H_
      7 
      8 #include "base/memory/weak_ptr.h"
      9 #include "base/stl_util.h"
     10 #include "base/threading/non_thread_safe.h"
     11 #include "sync/base/sync_export.h"
     12 #include "sync/engine/commit_contributor.h"
     13 #include "sync/engine/non_blocking_sync_common.h"
     14 #include "sync/engine/update_handler.h"
     15 #include "sync/internal_api/public/base/model_type.h"
     16 #include "sync/protocol/sync.pb.h"
     17 
     18 namespace base {
     19 class SingleThreadTaskRunner;
     20 }
     21 
     22 namespace syncer {
     23 
     24 class NonBlockingTypeProcessorInterface;
     25 class SyncThreadSyncEntity;
     26 
     27 // A smart cache for sync types that use message passing (rather than
     28 // transactions and the syncable::Directory) to communicate with the sync
     29 // thread.
     30 //
     31 // When the non-blocking sync type wants to talk with the sync server, it will
     32 // send a message from its thread to this object on the sync thread.  This
     33 // object ensures the appropriate sync server communication gets scheduled and
     34 // executed.  The response, if any, will be returned to the non-blocking sync
     35 // type's thread eventually.
     36 //
     37 // This object also has a role to play in communications in the opposite
     38 // direction.  Sometimes the sync thread will receive changes from the sync
     39 // server and deliver them here.  This object will post this information back to
     40 // the appropriate component on the model type's thread.
     41 //
     42 // This object does more than just pass along messages.  It understands the sync
     43 // protocol, and it can make decisions when it sees conflicting messages.  For
     44 // example, if the sync server sends down an update for a sync entity that is
     45 // currently pending for commit, this object will detect this condition and
     46 // cancel the pending commit.
     47 class SYNC_EXPORT NonBlockingTypeProcessorCore
     48     : public UpdateHandler,
     49       public CommitContributor,
     50       public base::NonThreadSafe {
     51  public:
     52   NonBlockingTypeProcessorCore(
     53       ModelType type,
     54       const DataTypeState& initial_state,
     55       scoped_ptr<NonBlockingTypeProcessorInterface> processor_interface);
     56   virtual ~NonBlockingTypeProcessorCore();
     57 
     58   ModelType GetModelType() const;
     59 
     60   // UpdateHandler implementation.
     61   virtual void GetDownloadProgress(
     62       sync_pb::DataTypeProgressMarker* progress_marker) const OVERRIDE;
     63   virtual void GetDataTypeContext(sync_pb::DataTypeContext* context) const
     64       OVERRIDE;
     65   virtual SyncerError ProcessGetUpdatesResponse(
     66       const sync_pb::DataTypeProgressMarker& progress_marker,
     67       const sync_pb::DataTypeContext& mutated_context,
     68       const SyncEntityList& applicable_updates,
     69       sessions::StatusController* status) OVERRIDE;
     70   virtual void ApplyUpdates(sessions::StatusController* status) OVERRIDE;
     71   virtual void PassiveApplyUpdates(sessions::StatusController* status) OVERRIDE;
     72 
     73   // Entry point for NonBlockingTypeProcessor to send commit requests.
     74   void EnqueueForCommit(const CommitRequestDataList& request_list);
     75 
     76   // CommitContributor implementation.
     77   virtual scoped_ptr<CommitContribution> GetContribution(
     78       size_t max_entries) OVERRIDE;
     79 
     80   // Callback for when our contribution gets a response.
     81   void OnCommitResponse(const CommitResponseDataList& response_list);
     82 
     83   base::WeakPtr<NonBlockingTypeProcessorCore> AsWeakPtr();
     84 
     85  private:
     86   typedef std::map<std::string, SyncThreadSyncEntity*> EntityMap;
     87 
     88   // Stores a single commit request in this object's internal state.
     89   void StorePendingCommit(const CommitRequestData& request);
     90 
     91   // Returns true if all data type state required for commits is available.  In
     92   // practice, this means that it returns true from the time this object first
     93   // receives notice of a successful update fetch from the server.
     94   bool CanCommitItems() const;
     95 
     96   // Initializes the parts of a commit entity that are the responsibility of
     97   // this class, and not the SyncThreadSyncEntity.  Some fields, like the
     98   // client-assigned ID, can only be set by an entity with knowledge of the
     99   // entire data type's state.
    100   void HelpInitializeCommitEntity(sync_pb::SyncEntity* commit_entity);
    101 
    102   ModelType type_;
    103 
    104   // State that applies to the entire model type.
    105   DataTypeState data_type_state_;
    106 
    107   // Abstraction around the NonBlockingTypeProcessor so this class
    108   // doesn't need to know about its specific implementation or
    109   // which thread it's on.  This makes it easier to write tests.
    110   scoped_ptr<NonBlockingTypeProcessorInterface> processor_interface_;
    111 
    112   // A map of per-entity information known to this object.
    113   //
    114   // When commits are pending, their information is stored here.  This
    115   // information is dropped from memory when the commit succeeds or gets
    116   // cancelled.
    117   //
    118   // This also stores some information related to received server state in
    119   // order to implement reflection blocking and conflict detection.  This
    120   // information is kept in memory indefinitely.  With a bit more coordination
    121   // with the model thread, we could optimize this to reduce memory usage in
    122   // the steady state.
    123   EntityMap entities_;
    124   STLValueDeleter<EntityMap> entities_deleter_;
    125 
    126   base::WeakPtrFactory<NonBlockingTypeProcessorCore> weak_ptr_factory_;
    127 };
    128 
    129 }  // namespace syncer
    130 
    131 #endif  // SYNC_ENGINE_NON_BLOCKING_TYPE_PROCESSOR_CORE_H_
    132