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_DIRECTORY_UPDATE_HANDLER_H_
6#define SYNC_ENGINE_DIRECTORY_UPDATE_HANDLER_H_
7
8#include <map>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "sync/base/sync_export.h"
14#include "sync/engine/process_updates_util.h"
15#include "sync/engine/update_handler.h"
16#include "sync/internal_api/public/base/model_type.h"
17#include "sync/internal_api/public/util/syncer_error.h"
18
19namespace sync_pb {
20class DataTypeProgressMarker;
21class GarbageCollectionDirective;
22class GetUpdatesResponse;
23}
24
25namespace syncer {
26
27namespace sessions {
28class StatusController;
29}
30
31namespace syncable {
32class Directory;
33}
34
35class DirectoryTypeDebugInfoEmitter;
36class ModelSafeWorker;
37
38// This class represents the syncable::Directory's processes for requesting and
39// processing updates from the sync server.
40//
41// Each instance of this class represents a particular type in the
42// syncable::Directory.  It can store and retreive that type's progress markers.
43// It can also process a set of received SyncEntities and store their data.
44class SYNC_EXPORT_PRIVATE DirectoryUpdateHandler : public UpdateHandler {
45 public:
46  DirectoryUpdateHandler(syncable::Directory* dir,
47                         ModelType type,
48                         scoped_refptr<ModelSafeWorker> worker,
49                         DirectoryTypeDebugInfoEmitter* debug_info_emitter);
50  virtual ~DirectoryUpdateHandler();
51
52  // UpdateHandler implementation.
53  virtual void GetDownloadProgress(
54      sync_pb::DataTypeProgressMarker* progress_marker) const OVERRIDE;
55  virtual void GetDataTypeContext(sync_pb::DataTypeContext* context) const
56      OVERRIDE;
57  virtual SyncerError ProcessGetUpdatesResponse(
58      const sync_pb::DataTypeProgressMarker& progress_marker,
59      const sync_pb::DataTypeContext& mutated_context,
60      const SyncEntityList& applicable_updates,
61      sessions::StatusController* status) OVERRIDE;
62  virtual void ApplyUpdates(sessions::StatusController* status) OVERRIDE;
63  virtual void PassiveApplyUpdates(sessions::StatusController* status) OVERRIDE;
64
65 private:
66  friend class DirectoryUpdateHandlerApplyUpdateTest;
67  friend class DirectoryUpdateHandlerProcessUpdateTest;
68
69  // Sometimes there is nothing to do, so we can return without doing anything.
70  bool IsApplyUpdatesRequired();
71
72  // Processes the given SyncEntities and stores their data in the directory.
73  // Their types must match this update handler's type.
74  void UpdateSyncEntities(
75      syncable::ModelNeutralWriteTransaction* trans,
76      const SyncEntityList& applicable_updates,
77      sessions::StatusController* status);
78
79  // Expires entries according to GC directives.
80  void ExpireEntriesIfNeeded(
81      syncable::ModelNeutralWriteTransaction* trans,
82      const sync_pb::DataTypeProgressMarker& progress_marker);
83
84  // Stores the given progress marker in the directory.
85  // Its type must match this update handler's type.
86  void UpdateProgressMarker(
87      const sync_pb::DataTypeProgressMarker& progress_marker);
88
89  bool IsValidProgressMarker(
90      const sync_pb::DataTypeProgressMarker& progress_marker) const;
91
92  // Skips all checks and goes straight to applying the updates.
93  SyncerError ApplyUpdatesImpl(sessions::StatusController* status);
94
95  syncable::Directory* dir_;
96  ModelType type_;
97  scoped_refptr<ModelSafeWorker> worker_;
98  DirectoryTypeDebugInfoEmitter* debug_info_emitter_;
99
100  scoped_ptr<sync_pb::GarbageCollectionDirective> cached_gc_directive_;
101
102  DISALLOW_COPY_AND_ASSIGN(DirectoryUpdateHandler);
103};
104
105}  // namespace syncer
106
107#endif  // SYNC_ENGINE_DIRECTORY_UPDATE_HANDLER_H_
108