Home | History | Annotate | Download | only in engine
      1 // Copyright 2013 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_SYNC_DIRECTORY_UPDATE_HANDLER_H_
      6 #define SYNC_ENGINE_SYNC_DIRECTORY_UPDATE_HANDLER_H_
      7 
      8 #include <map>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "sync/base/sync_export.h"
     13 #include "sync/engine/process_updates_util.h"
     14 #include "sync/internal_api/public/base/model_type.h"
     15 #include "sync/internal_api/public/util/syncer_error.h"
     16 
     17 namespace sync_pb {
     18 class DataTypeProgressMarker;
     19 class GetUpdatesResponse;
     20 }
     21 
     22 namespace syncer {
     23 
     24 namespace sessions {
     25 class StatusController;
     26 }
     27 
     28 namespace syncable {
     29 class Directory;
     30 }
     31 
     32 class ModelSafeWorker;
     33 
     34 // This class represents the syncable::Directory's processes for requesting and
     35 // processing updates from the sync server.
     36 //
     37 // Each instance of this class represents a particular type in the
     38 // syncable::Directory.  It can store and retreive that type's progress markers.
     39 // It can also process a set of received SyncEntities and store their data.
     40 class SYNC_EXPORT_PRIVATE SyncDirectoryUpdateHandler {
     41  public:
     42   SyncDirectoryUpdateHandler(syncable::Directory* dir,
     43                              ModelType type,
     44                              scoped_refptr<ModelSafeWorker> worker);
     45   ~SyncDirectoryUpdateHandler();
     46 
     47   // Fills the given parameter with the stored progress marker for this type.
     48   void GetDownloadProgress(
     49       sync_pb::DataTypeProgressMarker* progress_marker) const;
     50 
     51   // Processes the contents of a GetUpdates response message.
     52   //
     53   // Should be invoked with the progress marker and set of SyncEntities from a
     54   // single GetUpdates response message.  The progress marker's type must match
     55   // this update handler's type, and the set of SyncEntities must include all
     56   // entities of this type found in the response message.
     57   void ProcessGetUpdatesResponse(
     58       const sync_pb::DataTypeProgressMarker& progress_marker,
     59       const SyncEntityList& applicable_updates,
     60       sessions::StatusController* status);
     61 
     62   // If there are updates to apply, apply them on the proper thread.
     63   // Delegates to ApplyUpdatesImpl().
     64   void ApplyUpdates(sessions::StatusController* status);
     65 
     66  private:
     67   friend class SyncDirectoryUpdateHandlerApplyUpdateTest;
     68   friend class SyncDirectoryUpdateHandlerProcessUpdateTest;
     69 
     70   // Processes the given SyncEntities and stores their data in the directory.
     71   // Their types must match this update handler's type.
     72   void UpdateSyncEntities(
     73       syncable::ModelNeutralWriteTransaction* trans,
     74       const SyncEntityList& applicable_updates,
     75       sessions::StatusController* status);
     76 
     77   // Stores the given progress marker in the directory.
     78   // Its type must match this update handler's type.
     79   void UpdateProgressMarker(
     80       const sync_pb::DataTypeProgressMarker& progress_marker);
     81 
     82   // Skips all checks and goes straight to applying the updates.
     83   SyncerError ApplyUpdatesImpl(sessions::StatusController* status);
     84 
     85   syncable::Directory* dir_;
     86   ModelType type_;
     87   scoped_refptr<ModelSafeWorker> worker_;
     88 
     89   DISALLOW_COPY_AND_ASSIGN(SyncDirectoryUpdateHandler);
     90 };
     91 
     92 // TODO(rlarocque): Find a better place to define this.
     93 typedef std::map<ModelType, SyncDirectoryUpdateHandler*> UpdateHandlerMap;
     94 
     95 }  // namespace syncer
     96 
     97 #endif  // SYNC_ENGINE_SYNC_DIRECTORY_UPDATE_HANDLER_H_
     98