Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / sync / engine / sync_directory_update_handler.cc
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 #include "sync/engine/sync_directory_update_handler.h"
6
7 #include "sync/engine/conflict_resolver.h"
8 #include "sync/engine/process_updates_util.h"
9 #include "sync/engine/update_applicator.h"
10 #include "sync/sessions/status_controller.h"
11 #include "sync/syncable/directory.h"
12 #include "sync/syncable/syncable_model_neutral_write_transaction.h"
13 #include "sync/syncable/syncable_write_transaction.h"
14
15 namespace syncer {
16
17 using syncable::SYNCER;
18
19 SyncDirectoryUpdateHandler::SyncDirectoryUpdateHandler(
20     syncable::Directory* dir,
21     ModelType type,
22     scoped_refptr<ModelSafeWorker> worker)
23   : dir_(dir),
24     type_(type),
25     worker_(worker) {}
26
27 SyncDirectoryUpdateHandler::~SyncDirectoryUpdateHandler() {}
28
29 void SyncDirectoryUpdateHandler::GetDownloadProgress(
30     sync_pb::DataTypeProgressMarker* progress_marker) const {
31   dir_->GetDownloadProgress(type_, progress_marker);
32 }
33
34 void SyncDirectoryUpdateHandler::ProcessGetUpdatesResponse(
35     const sync_pb::DataTypeProgressMarker& progress_marker,
36     const SyncEntityList& applicable_updates,
37     sessions::StatusController* status) {
38   syncable::ModelNeutralWriteTransaction trans(FROM_HERE, SYNCER, dir_);
39   UpdateSyncEntities(&trans, applicable_updates, status);
40   UpdateProgressMarker(progress_marker);
41 }
42
43 void SyncDirectoryUpdateHandler::ApplyUpdates(
44     sessions::StatusController* status) {
45   if (!IsApplyUpdatesRequired()) {
46     return;
47   }
48
49   // This will invoke handlers that belong to the model and its thread, so we
50   // switch to the appropriate thread before we start this work.
51   WorkCallback c = base::Bind(
52       &SyncDirectoryUpdateHandler::ApplyUpdatesImpl,
53       // We wait until the callback is executed.  We can safely use Unretained.
54       base::Unretained(this),
55       base::Unretained(status));
56   worker_->DoWorkAndWaitUntilDone(c);
57 }
58
59 void SyncDirectoryUpdateHandler::PassiveApplyUpdates(
60     sessions::StatusController* status) {
61   if (!IsApplyUpdatesRequired()) {
62     return;
63   }
64
65   // Just do the work here instead of deferring to another thread.
66   ApplyUpdatesImpl(status);
67 }
68
69 SyncerError SyncDirectoryUpdateHandler::ApplyUpdatesImpl(
70     sessions::StatusController* status) {
71   syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir_);
72
73   std::vector<int64> handles;
74   dir_->GetUnappliedUpdateMetaHandles(
75       &trans,
76       FullModelTypeSet(type_),
77       &handles);
78
79   // First set of update application passes.
80   UpdateApplicator applicator(dir_->GetCryptographer(&trans));
81   applicator.AttemptApplications(&trans, handles);
82   status->increment_num_updates_applied_by(applicator.updates_applied());
83   status->increment_num_hierarchy_conflicts_by(
84       applicator.hierarchy_conflicts());
85   status->increment_num_encryption_conflicts_by(
86       applicator.encryption_conflicts());
87
88   if (applicator.simple_conflict_ids().size() != 0) {
89     // Resolve the simple conflicts we just detected.
90     ConflictResolver resolver;
91     resolver.ResolveConflicts(&trans,
92                               dir_->GetCryptographer(&trans),
93                               applicator.simple_conflict_ids(),
94                               status);
95
96     // Conflict resolution sometimes results in more updates to apply.
97     handles.clear();
98     dir_->GetUnappliedUpdateMetaHandles(
99         &trans,
100         FullModelTypeSet(type_),
101         &handles);
102
103     UpdateApplicator conflict_applicator(dir_->GetCryptographer(&trans));
104     conflict_applicator.AttemptApplications(&trans, handles);
105
106     // We count the number of updates from both applicator passes.
107     status->increment_num_updates_applied_by(
108         conflict_applicator.updates_applied());
109
110     // Encryption conflicts should remain unchanged by the resolution of simple
111     // conflicts.  Those can only be solved by updating our nigori key bag.
112     DCHECK_EQ(conflict_applicator.encryption_conflicts(),
113               applicator.encryption_conflicts());
114
115     // Hierarchy conflicts should also remain unchanged, for reasons that are
116     // more subtle.  Hierarchy conflicts exist when the application of a pending
117     // update from the server would make the local folder hierarchy
118     // inconsistent.  The resolution of simple conflicts could never affect the
119     // hierarchy conflicting item directly, because hierarchy conflicts are not
120     // processed by the conflict resolver.  It could, in theory, modify the
121     // local hierarchy on which hierarchy conflict detection depends.  However,
122     // the conflict resolution algorithm currently in use does not allow this.
123     DCHECK_EQ(conflict_applicator.hierarchy_conflicts(),
124               applicator.hierarchy_conflicts());
125
126     // There should be no simple conflicts remaining.  We know this because the
127     // resolver should have resolved all the conflicts we detected last time
128     // and, by the two previous assertions, that no conflicts have been
129     // downgraded from encryption or hierarchy down to simple.
130     DCHECK(conflict_applicator.simple_conflict_ids().empty());
131   }
132
133   return SYNCER_OK;
134 }
135
136 bool SyncDirectoryUpdateHandler::IsApplyUpdatesRequired() {
137   if (IsControlType(type_)) {
138     return false;  // We don't process control types here.
139   }
140
141   return dir_->TypeHasUnappliedUpdates(type_);
142 }
143
144 void SyncDirectoryUpdateHandler::UpdateSyncEntities(
145     syncable::ModelNeutralWriteTransaction* trans,
146     const SyncEntityList& applicable_updates,
147     sessions::StatusController* status) {
148   ProcessDownloadedUpdates(dir_, trans, type_, applicable_updates, status);
149 }
150
151 void SyncDirectoryUpdateHandler::UpdateProgressMarker(
152     const sync_pb::DataTypeProgressMarker& progress_marker) {
153   int field_number = progress_marker.data_type_id();
154   ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number);
155   if (!IsRealDataType(model_type) || type_ != model_type) {
156     NOTREACHED()
157         << "Update handler of type " << ModelTypeToString(type_)
158         << " asked to process progress marker with invalid type "
159         << field_number;
160   }
161   dir_->SetDownloadProgress(type_, progress_marker);
162 }
163
164 }  // namespace syncer