Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / sync / engine / directory_update_handler.h
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
19 namespace sync_pb {
20 class DataTypeProgressMarker;
21 class GarbageCollectionDirective;
22 class GetUpdatesResponse;
23 }
24
25 namespace syncer {
26
27 namespace sessions {
28 class StatusController;
29 }
30
31 namespace syncable {
32 class Directory;
33 }
34
35 class DirectoryTypeDebugInfoEmitter;
36 class 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.
44 class 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   ~DirectoryUpdateHandler() override;
51
52   // UpdateHandler implementation.
53   void GetDownloadProgress(
54       sync_pb::DataTypeProgressMarker* progress_marker) const override;
55   void GetDataTypeContext(sync_pb::DataTypeContext* context) const override;
56   SyncerError ProcessGetUpdatesResponse(
57       const sync_pb::DataTypeProgressMarker& progress_marker,
58       const sync_pb::DataTypeContext& mutated_context,
59       const SyncEntityList& applicable_updates,
60       sessions::StatusController* status) override;
61   void ApplyUpdates(sessions::StatusController* status) override;
62   void PassiveApplyUpdates(sessions::StatusController* status) override;
63
64  private:
65   friend class DirectoryUpdateHandlerApplyUpdateTest;
66   friend class DirectoryUpdateHandlerProcessUpdateTest;
67
68   // Sometimes there is nothing to do, so we can return without doing anything.
69   bool IsApplyUpdatesRequired();
70
71   // Processes the given SyncEntities and stores their data in the directory.
72   // Their types must match this update handler's type.
73   void UpdateSyncEntities(
74       syncable::ModelNeutralWriteTransaction* trans,
75       const SyncEntityList& applicable_updates,
76       sessions::StatusController* status);
77
78   // Expires entries according to GC directives.
79   void ExpireEntriesIfNeeded(
80       syncable::ModelNeutralWriteTransaction* trans,
81       const sync_pb::DataTypeProgressMarker& progress_marker);
82
83   // Stores the given progress marker in the directory.
84   // Its type must match this update handler's type.
85   void UpdateProgressMarker(
86       const sync_pb::DataTypeProgressMarker& progress_marker);
87
88   bool IsValidProgressMarker(
89       const sync_pb::DataTypeProgressMarker& progress_marker) const;
90
91   // Skips all checks and goes straight to applying the updates.
92   SyncerError ApplyUpdatesImpl(sessions::StatusController* status);
93
94   syncable::Directory* dir_;
95   ModelType type_;
96   scoped_refptr<ModelSafeWorker> worker_;
97   DirectoryTypeDebugInfoEmitter* debug_info_emitter_;
98
99   scoped_ptr<sync_pb::GarbageCollectionDirective> cached_gc_directive_;
100
101   DISALLOW_COPY_AND_ASSIGN(DirectoryUpdateHandler);
102 };
103
104 }  // namespace syncer
105
106 #endif  // SYNC_ENGINE_DIRECTORY_UPDATE_HANDLER_H_