summaryrefslogtreecommitdiffstats
path: root/sync/internal_api/shared_model_type_processor.cc
blob: 3db885f768ce232affb40a64525a7526c24e870a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sync/internal_api/public/shared_model_type_processor.h"

#include <utility>
#include <vector>

#include "base/bind.h"
#include "base/location.h"
#include "base/thread_task_runner_handle.h"
#include "sync/engine/commit_queue.h"
#include "sync/internal_api/public/activation_context.h"
#include "sync/internal_api/public/model_type_entity.h"
#include "sync/syncable/syncable_util.h"

namespace syncer_v2 {

namespace {

class ModelTypeProcessorProxy : public ModelTypeProcessor {
 public:
  ModelTypeProcessorProxy(
      const base::WeakPtr<ModelTypeProcessor>& processor,
      const scoped_refptr<base::SequencedTaskRunner>& processor_task_runner);
  ~ModelTypeProcessorProxy() override;

  void ConnectSync(scoped_ptr<CommitQueue> worker) override;
  void OnCommitCompleted(const sync_pb::DataTypeState& type_state,
                         const CommitResponseDataList& response_list) override;
  void OnUpdateReceived(const sync_pb::DataTypeState& type_state,
                        const UpdateResponseDataList& updates,
                        const UpdateResponseDataList& pending_updates) override;

 private:
  base::WeakPtr<ModelTypeProcessor> processor_;
  scoped_refptr<base::SequencedTaskRunner> processor_task_runner_;
};

ModelTypeProcessorProxy::ModelTypeProcessorProxy(
    const base::WeakPtr<ModelTypeProcessor>& processor,
    const scoped_refptr<base::SequencedTaskRunner>& processor_task_runner)
    : processor_(processor), processor_task_runner_(processor_task_runner) {}

ModelTypeProcessorProxy::~ModelTypeProcessorProxy() {}

void ModelTypeProcessorProxy::ConnectSync(scoped_ptr<CommitQueue> worker) {
  processor_task_runner_->PostTask(
      FROM_HERE, base::Bind(&ModelTypeProcessor::ConnectSync, processor_,
                            base::Passed(std::move(worker))));
}

void ModelTypeProcessorProxy::OnCommitCompleted(
    const sync_pb::DataTypeState& type_state,
    const CommitResponseDataList& response_list) {
  processor_task_runner_->PostTask(
      FROM_HERE, base::Bind(&ModelTypeProcessor::OnCommitCompleted, processor_,
                            type_state, response_list));
}

void ModelTypeProcessorProxy::OnUpdateReceived(
    const sync_pb::DataTypeState& type_state,
    const UpdateResponseDataList& updates,
    const UpdateResponseDataList& pending_updates) {
  processor_task_runner_->PostTask(
      FROM_HERE, base::Bind(&ModelTypeProcessor::OnUpdateReceived, processor_,
                            type_state, updates, pending_updates));
}

}  // namespace

SharedModelTypeProcessor::SharedModelTypeProcessor(syncer::ModelType type,
                                                   ModelTypeService* service)
    : type_(type),
      is_metadata_loaded_(false),
      service_(service),
      weak_ptr_factory_(this),
      weak_ptr_factory_for_sync_(this) {
  DCHECK(service);
}

SharedModelTypeProcessor::~SharedModelTypeProcessor() {}

void SharedModelTypeProcessor::OnSyncStarting(StartCallback start_callback) {
  DCHECK(CalledOnValidThread());
  DCHECK(start_callback_.is_null());
  DCHECK(!IsConnected());
  DVLOG(1) << "Sync is starting for " << ModelTypeToString(type_);

  start_callback_ = start_callback;
  ConnectIfReady();
}

void SharedModelTypeProcessor::OnMetadataLoaded(
    scoped_ptr<MetadataBatch> batch) {
  DCHECK(CalledOnValidThread());
  DCHECK(entities_.empty());
  DCHECK(!is_metadata_loaded_);
  DCHECK(!IsConnected());

  // Flip this flag here to cover all cases where we don't need to load data.
  is_pending_commit_data_loaded_ = true;

  if (batch->GetDataTypeState().initial_sync_done()) {
    EntityMetadataMap metadata_map(batch->TakeAllMetadata());
    std::vector<std::string> entities_to_commit;

    for (auto it = metadata_map.begin(); it != metadata_map.end(); it++) {
      scoped_ptr<ModelTypeEntity> entity =
          ModelTypeEntity::CreateFromMetadata(it->first, &it->second);
      if (entity->RequiresCommitData()) {
        entities_to_commit.push_back(entity->client_tag());
      }
      entities_[entity->metadata().client_tag_hash()] = std::move(entity);
    }
    data_type_state_ = batch->GetDataTypeState();
    if (!entities_to_commit.empty()) {
      is_pending_commit_data_loaded_ = false;
      service_->GetData(entities_to_commit,
                        base::Bind(&SharedModelTypeProcessor::OnDataLoaded,
                                   weak_ptr_factory_.GetWeakPtr()));
    }
  } else {
    // First time syncing; initialize metadata.
    data_type_state_.mutable_progress_marker()->set_data_type_id(
        GetSpecificsFieldNumberFromModelType(type_));
  }

  is_metadata_loaded_ = true;
  ConnectIfReady();
}

void SharedModelTypeProcessor::OnDataLoaded(syncer::SyncError error,
                                            scoped_ptr<DataBatch> data_batch) {
  while (data_batch->HasNext()) {
    TagAndData data = data_batch->Next();
    ModelTypeEntity* entity = GetEntityForTag(data.first);
    // If the entity wasn't deleted or updated with new commit.
    if (entity != nullptr && entity->RequiresCommitData()) {
      entity->CacheCommitData(data.second.get());
    }
  }
  is_pending_commit_data_loaded_ = true;
  FlushPendingCommitRequests();
}

void SharedModelTypeProcessor::ConnectIfReady() {
  DCHECK(CalledOnValidThread());
  if (!is_metadata_loaded_ || start_callback_.is_null()) {
    return;
  }

  scoped_ptr<ActivationContext> activation_context =
      make_scoped_ptr(new ActivationContext);
  activation_context->data_type_state = data_type_state_;
  activation_context->saved_pending_updates = GetPendingUpdates();
  activation_context->type_processor = make_scoped_ptr(
      new ModelTypeProcessorProxy(weak_ptr_factory_for_sync_.GetWeakPtr(),
                                  base::ThreadTaskRunnerHandle::Get()));

  start_callback_.Run(syncer::SyncError(), std::move(activation_context));
  start_callback_.Reset();
}

bool SharedModelTypeProcessor::IsAllowingChanges() const {
  return is_metadata_loaded_;
}

bool SharedModelTypeProcessor::IsConnected() const {
  DCHECK(CalledOnValidThread());
  return !!worker_;
}

void SharedModelTypeProcessor::Disable() {
  DCHECK(CalledOnValidThread());
  scoped_ptr<MetadataChangeList> change_list =
      service_->CreateMetadataChangeList();
  for (auto it = entities_.begin(); it != entities_.end(); ++it) {
    change_list->ClearMetadata(it->second->client_tag());
  }
  change_list->ClearDataTypeState();
  // Nothing to do if this fails, so just ignore the error it might return.
  service_->ApplySyncChanges(std::move(change_list), EntityChangeList());

  // Destroy this object.
  // TODO(pavely): Revisit whether there's a better way to do this deletion.
  service_->clear_change_processor();
}

void SharedModelTypeProcessor::DisconnectSync() {
  DCHECK(CalledOnValidThread());
  DCHECK(IsConnected());

  DVLOG(1) << "Disconnecting sync for " << ModelTypeToString(type_);
  weak_ptr_factory_for_sync_.InvalidateWeakPtrs();
  worker_.reset();

  for (auto it = entities_.begin(); it != entities_.end(); ++it) {
    it->second->ClearTransientSyncState();
  }
}

base::WeakPtr<SharedModelTypeProcessor>
SharedModelTypeProcessor::AsWeakPtrForUI() {
  DCHECK(CalledOnValidThread());
  return weak_ptr_factory_.GetWeakPtr();
}

void SharedModelTypeProcessor::ConnectSync(scoped_ptr<CommitQueue> worker) {
  DCHECK(CalledOnValidThread());
  DVLOG(1) << "Successfully connected " << ModelTypeToString(type_);

  worker_ = std::move(worker);

  FlushPendingCommitRequests();
}

void SharedModelTypeProcessor::Put(const std::string& client_tag,
                                   scoped_ptr<EntityData> entity_data,
                                   MetadataChangeList* metadata_change_list) {
  DCHECK(IsAllowingChanges());
  DCHECK(entity_data.get());
  DCHECK(!entity_data->is_deleted());
  DCHECK(!entity_data->non_unique_name.empty());
  DCHECK_EQ(type_, syncer::GetModelTypeFromSpecifics(entity_data->specifics));

  if (!data_type_state_.initial_sync_done()) {
    // Ignore changes before the initial sync is done.
    return;
  }

  // If the service specified an overriding hash, use that, otherwise generate
  // one from the tag.
  // TODO(skym): This behavior should be delayed, once crbug.com/561818 is fixed
  // we will only perform this logic in the create case.
  const std::string client_tag_hash(
      entity_data->client_tag_hash.empty()
          ? syncer::syncable::GenerateSyncableHash(type_, client_tag)
          : entity_data->client_tag_hash);

  base::Time now = base::Time::Now();

  // TODO(stanisc): crbug.com/561818: Search by client_tag rather than
  // client_tag_hash.
  ModelTypeEntity* entity = GetEntityForTagHash(client_tag_hash);
  if (entity == nullptr) {
    // The service is creating a new entity.
    scoped_ptr<ModelTypeEntity> scoped_entity = ModelTypeEntity::CreateNew(
        client_tag, client_tag_hash, entity_data->id, now);
    entity = scoped_entity.get();
    entities_[client_tag_hash] = std::move(scoped_entity);
  } else {
    // The service is updating an existing entity.
    DCHECK_EQ(client_tag, entity->client_tag());
  }

  // TODO(stanisc): crbug.com/561829: Avoid committing a change if there is no
  // actual change.
  entity->MakeLocalChange(std::move(entity_data), now);
  metadata_change_list->UpdateMetadata(client_tag, entity->metadata());

  FlushPendingCommitRequests();
}

void SharedModelTypeProcessor::Delete(
    const std::string& client_tag,
    MetadataChangeList* metadata_change_list) {
  DCHECK(IsAllowingChanges());

  if (!data_type_state_.initial_sync_done()) {
    // Ignore changes before the initial sync is done.
    return;
  }

  const std::string client_tag_hash(
      syncer::syncable::GenerateSyncableHash(type_, client_tag));

  // TODO(skym): crbug.com/561818: Search by client_tag rather than
  // client_tag_hash.
  ModelTypeEntity* entity = GetEntityForTagHash(client_tag_hash);
  if (entity == nullptr) {
    // That's unusual, but not necessarily a bad thing.
    // Missing is as good as deleted as far as the model is concerned.
    DLOG(WARNING) << "Attempted to delete missing item."
                  << " client tag: " << client_tag;
    return;
  }

  entity->Delete();

  metadata_change_list->UpdateMetadata(client_tag, entity->metadata());

  FlushPendingCommitRequests();
}

void SharedModelTypeProcessor::FlushPendingCommitRequests() {
  CommitRequestDataList commit_requests;

  // Don't bother sending anything if there's no one to send to.
  if (!IsConnected())
    return;

  // Don't send anything if the type is not ready to handle commits.
  if (!data_type_state_.initial_sync_done())
    return;

  // Dont send anything if the initial data load is incomplete.
  if (!is_pending_commit_data_loaded_)
    return;

  // TODO(rlarocque): Do something smarter than iterate here.
  for (auto it = entities_.begin(); it != entities_.end(); ++it) {
    ModelTypeEntity* entity = it->second.get();
    if (entity->RequiresCommitRequest()) {
      DCHECK(!entity->RequiresCommitData());
      CommitRequestData request;
      entity->InitializeCommitRequestData(&request);
      commit_requests.push_back(request);
    }
  }

  if (!commit_requests.empty())
    worker_->EnqueueForCommit(commit_requests);
}

void SharedModelTypeProcessor::OnCommitCompleted(
    const sync_pb::DataTypeState& type_state,
    const CommitResponseDataList& response_list) {
  scoped_ptr<MetadataChangeList> change_list =
      service_->CreateMetadataChangeList();

  data_type_state_ = type_state;
  change_list->UpdateDataTypeState(data_type_state_);

  for (const CommitResponseData& data : response_list) {
    ModelTypeEntity* entity = GetEntityForTagHash(data.client_tag_hash);
    if (entity == nullptr) {
      NOTREACHED() << "Received commit response for missing item."
                   << " type: " << type_
                   << " client_tag_hash: " << data.client_tag_hash;
      continue;
    }

    entity->ReceiveCommitResponse(data.id, data.sequence_number,
                                  data.response_version,
                                  data_type_state_.encryption_key_name());
    // TODO(stanisc): crbug.com/573333: Delete case.
    // This might be the right place to clear a metadata entry that has
    // been deleted locally and confirmed deleted by the server.
    change_list->UpdateMetadata(entity->client_tag(), entity->metadata());
  }

  // TODO(stanisc): crbug.com/570085: Error handling.
  service_->ApplySyncChanges(std::move(change_list), EntityChangeList());
}

void SharedModelTypeProcessor::OnUpdateReceived(
    const sync_pb::DataTypeState& data_type_state,
    const UpdateResponseDataList& updates,
    const UpdateResponseDataList& pending_updates) {
  if (!data_type_state_.initial_sync_done()) {
    OnInitialUpdateReceived(data_type_state, updates);
    return;
  }

  scoped_ptr<MetadataChangeList> metadata_changes =
      service_->CreateMetadataChangeList();
  EntityChangeList entity_changes;

  metadata_changes->UpdateDataTypeState(data_type_state);
  bool got_new_encryption_requirements =
      data_type_state_.encryption_key_name() !=
      data_type_state.encryption_key_name();
  data_type_state_ = data_type_state;

  for (const UpdateResponseData& update : updates) {
    const EntityData& data = update.entity.value();
    const std::string& client_tag_hash = data.client_tag_hash;

    // If we're being asked to apply an update to this entity, this overrides
    // the previous pending updates.
    pending_updates_map_.erase(client_tag_hash);

    ModelTypeEntity* entity = GetEntityForTagHash(client_tag_hash);
    if (entity == nullptr) {
      if (data.is_deleted()) {
        DLOG(WARNING) << "Received remote delete for a non-existing item."
                      << " client_tag_hash: " << client_tag_hash;
        continue;
      }

      // Let the service define |client_tag| based on the entity data.
      const std::string client_tag = service_->GetClientTag(data);

      scoped_ptr<ModelTypeEntity> scoped_entity = ModelTypeEntity::CreateNew(
          client_tag, client_tag_hash, data.id, data.creation_time);
      entity = scoped_entity.get();
      entities_[client_tag_hash] = std::move(scoped_entity);

      entity_changes.push_back(
          EntityChange::CreateAdd(client_tag, update.entity));

    } else {
      if (data.is_deleted()) {
        entity_changes.push_back(
            EntityChange::CreateDelete(entity->client_tag()));
      } else {
        // TODO(stanisc): crbug.com/561829: Avoid sending an update to the
        // service if there is no actual change.
        entity_changes.push_back(
            EntityChange::CreateUpdate(entity->client_tag(), update.entity));
      }
    }

    entity->ApplyUpdateFromServer(update);
    // TODO(stanisc): crbug.com/573333: Delete case.
    // This might be the right place to clear metadata entry instead of
    // updating it.
    metadata_changes->UpdateMetadata(entity->client_tag(), entity->metadata());

    // TODO(stanisc): crbug.com/521867: Do something special when conflicts are
    // detected.

    // If the received entity has out of date encryption, we schedule another
    // commit to fix it.
    if (data_type_state_.encryption_key_name() != update.encryption_key_name) {
      DVLOG(2) << ModelTypeToString(type_) << ": Requesting re-encrypt commit "
               << update.encryption_key_name << " -> "
               << data_type_state_.encryption_key_name();
      auto it2 = entities_.find(client_tag_hash);
      it2->second->UpdateDesiredEncryptionKey(
          data_type_state_.encryption_key_name());
    }
  }

  // TODO(stanisc): crbug.com/529498: stop saving pending updates.
  // Save pending updates in the appropriate data structure.
  for (const UpdateResponseData& update : pending_updates) {
    const std::string& client_tag_hash = update.entity->client_tag_hash;

    auto lookup_it = pending_updates_map_.find(client_tag_hash);
    if (lookup_it == pending_updates_map_.end()) {
      pending_updates_map_.insert(std::make_pair(
          client_tag_hash, make_scoped_ptr(new UpdateResponseData(update))));
    } else if (lookup_it->second->response_version <= update.response_version) {
      pending_updates_map_.erase(lookup_it);
      pending_updates_map_.insert(std::make_pair(
          client_tag_hash, make_scoped_ptr(new UpdateResponseData(update))));
    } else {
      // Received update is stale, do not overwrite existing.
    }
  }

  if (got_new_encryption_requirements) {
    for (auto it = entities_.begin(); it != entities_.end(); ++it) {
      it->second->UpdateDesiredEncryptionKey(
          data_type_state_.encryption_key_name());
    }
  }

  // Inform the service of the new or updated data.
  // TODO(stanisc): crbug.com/570085: Error handling.
  service_->ApplySyncChanges(std::move(metadata_changes), entity_changes);

  // We may have new reasons to commit by the time this function is done.
  FlushPendingCommitRequests();
}

void SharedModelTypeProcessor::OnInitialUpdateReceived(
    const sync_pb::DataTypeState& data_type_state,
    const UpdateResponseDataList& updates) {
  DCHECK(entities_.empty());
  // Ensure that initial sync was not already done and that the worker
  // correctly marked initial sync as done for this update.
  DCHECK(!data_type_state_.initial_sync_done());
  DCHECK(data_type_state.initial_sync_done());

  scoped_ptr<MetadataChangeList> metadata_changes =
      service_->CreateMetadataChangeList();
  EntityDataMap data_map;

  data_type_state_ = data_type_state;
  metadata_changes->UpdateDataTypeState(data_type_state_);

  for (const UpdateResponseData& update : updates) {
    const EntityData& data = update.entity.value();

    // Let the service define a client |tag| based on the entity data.
    std::string tag = service_->GetClientTag(data);

    ModelTypeEntity* entity = CreateEntity(tag, data);
    entity->ApplyUpdateFromServer(update);
    metadata_changes->UpdateMetadata(tag, entity->metadata());
    data_map[tag] = update.entity;
  }

  // Let the service handle associating and merging the data.
  // TODO(stanisc): crbug.com/570085: Error handling.
  service_->MergeSyncData(std::move(metadata_changes), data_map);

  // We may have new reasons to commit by the time this function is done.
  FlushPendingCommitRequests();
}

UpdateResponseDataList SharedModelTypeProcessor::GetPendingUpdates() {
  UpdateResponseDataList pending_updates_list;
  for (auto it = pending_updates_map_.begin(); it != pending_updates_map_.end();
       ++it) {
    pending_updates_list.push_back(*it->second);
  }
  return pending_updates_list;
}

std::string SharedModelTypeProcessor::GetHashForTag(const std::string& tag) {
  return syncer::syncable::GenerateSyncableHash(type_, tag);
}

ModelTypeEntity* SharedModelTypeProcessor::GetEntityForTag(
    const std::string& tag) {
  return GetEntityForTagHash(GetHashForTag(tag));
}

ModelTypeEntity* SharedModelTypeProcessor::GetEntityForTagHash(
    const std::string& tag_hash) {
  auto it = entities_.find(tag_hash);
  return it != entities_.end() ? it->second.get() : nullptr;
}

ModelTypeEntity* SharedModelTypeProcessor::CreateEntity(
    const std::string& tag,
    const EntityData& data) {
  DCHECK(entities_.find(data.client_tag_hash) == entities_.end());
  scoped_ptr<ModelTypeEntity> entity = ModelTypeEntity::CreateNew(
      tag, data.client_tag_hash, data.id, data.creation_time);
  ModelTypeEntity* entity_ptr = entity.get();
  entities_[data.client_tag_hash] = std::move(entity);
  return entity_ptr;
}

}  // namespace syncer_v2