Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "components/sync/engine_impl/uss_migrator.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <memory> | |
| 9 #include <string> | |
| 10 #include <utility> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "components/sync/base/time.h" | |
| 15 #include "components/sync/engine_impl/model_type_worker.h" | |
| 16 #include "components/sync/protocol/sync.pb.h" | |
| 17 #include "components/sync/syncable/directory.h" | |
| 18 #include "components/sync/syncable/entry.h" | |
| 19 #include "components/sync/syncable/read_node.h" | |
| 20 #include "components/sync/syncable/read_transaction.h" | |
| 21 #include "components/sync/syncable/user_share.h" | |
| 22 | |
| 23 namespace syncer { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 bool ExtractSyncEntity(ReadTransaction* trans, | |
| 28 int64_t id, | |
| 29 sync_pb::SyncEntity* entity) { | |
| 30 ReadNode read_node(trans); | |
| 31 if (read_node.InitByIdLookup(id) != BaseNode::INIT_OK) | |
| 32 return false; | |
| 33 | |
| 34 const syncable::Entry& entry = *read_node.GetEntry(); | |
| 35 | |
| 36 // Copy the fields USS cares about from the server side of the directory so | |
| 37 // that we don't miss things that haven't been applied yet. See | |
| 38 // ModelTypeWorker::ProcessGetUpdatesResponse for which fields are used. | |
| 39 entity->set_id_string(entry.GetId().GetServerId()); | |
| 40 entity->set_version(entry.GetServerVersion()); | |
| 41 entity->set_mtime(TimeToProtoTime(entry.GetServerMtime())); | |
| 42 entity->set_ctime(TimeToProtoTime(entry.GetServerCtime())); | |
| 43 entity->set_name(entry.GetServerNonUniqueName()); | |
| 44 entity->set_deleted(entry.GetServerIsDel()); | |
| 45 entity->set_client_defined_unique_tag(entry.GetUniqueClientTag()); | |
| 46 | |
| 47 // It looks like there are fancy other ways to get e.g. passwords specifics | |
| 48 // out of Entry. Do we need to special-case them when we ship those types? | |
| 49 entity->mutable_specifics()->CopyFrom(entry.GetServerSpecifics()); | |
|
pavely
2016/10/27 23:20:19
If you were offline for a while then your local sp
maxbogue
2016/10/28 16:17:41
Wouldn't any local changes be in the model type's
pavely
2016/10/28 17:26:58
You are right. I agree.
| |
| 50 return true; | |
| 51 } | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 bool MigrateDirectoryData(ModelType type, | |
| 56 UserShare* user_share, | |
| 57 ModelTypeWorker* worker) { | |
| 58 return MigrateDirectoryDataWithBatchSize(type, user_share, worker, 64); | |
| 59 } | |
| 60 | |
| 61 bool MigrateDirectoryDataWithBatchSize(ModelType type, | |
| 62 UserShare* user_share, | |
| 63 ModelTypeWorker* worker, | |
| 64 int batch_size) { | |
| 65 DCHECK_NE(BOOKMARKS, type); | |
| 66 DCHECK_NE(PASSWORDS, type); | |
| 67 ReadTransaction trans(FROM_HERE, user_share); | |
| 68 | |
| 69 ReadNode root(&trans); | |
| 70 if (root.InitTypeRoot(type) != BaseNode::INIT_OK) { | |
| 71 LOG(ERROR) << "Missing root node for " << ModelTypeToString(type); | |
| 72 // Inform the worker so it can trigger a fallback initial GetUpdates. | |
| 73 worker->AbortMigration(); | |
| 74 return false; | |
| 75 } | |
| 76 | |
| 77 // Get the progress marker and context from the directory. | |
| 78 sync_pb::DataTypeProgressMarker progress; | |
| 79 sync_pb::DataTypeContext context; | |
| 80 user_share->directory->GetDownloadProgress(type, &progress); | |
| 81 user_share->directory->GetDataTypeContext(trans.GetWrappedTrans(), type, | |
| 82 &context); | |
| 83 | |
| 84 std::vector<int64_t> child_ids; | |
| 85 root.GetChildIds(&child_ids); | |
| 86 | |
| 87 // Process |batch_size| entities at a time to reduce memory usage. | |
| 88 size_t i = 0; | |
| 89 while (i < child_ids.size()) { | |
| 90 // Vector to own the temporary entities. | |
| 91 std::vector<std::unique_ptr<sync_pb::SyncEntity>> entities; | |
| 92 // Vector of raw pointers for passing to ProcessGetUpdatesResponse(). | |
| 93 SyncEntityList entity_ptrs; | |
| 94 | |
| 95 const size_t batch_limit = std::min(i + batch_size, child_ids.size()); | |
| 96 for (; i < batch_limit; i++) { | |
| 97 auto entity = base::MakeUnique<sync_pb::SyncEntity>(); | |
| 98 if (!ExtractSyncEntity(&trans, child_ids[i], entity.get())) { | |
| 99 LOG(ERROR) << "Failed to fetch child node for " | |
| 100 << ModelTypeToString(type); | |
| 101 // Inform the worker so it can clear any partial data and trigger a | |
| 102 // fallback initial GetUpdates. | |
| 103 worker->AbortMigration(); | |
| 104 return false; | |
| 105 } | |
| 106 entity_ptrs.push_back(entity.get()); | |
| 107 entities.push_back(std::move(entity)); | |
| 108 } | |
| 109 | |
| 110 worker->ProcessGetUpdatesResponse(progress, context, entity_ptrs, nullptr); | |
| 111 } | |
| 112 | |
| 113 worker->PassiveApplyUpdates(nullptr); | |
| 114 return true; | |
| 115 } | |
| 116 | |
| 117 } // namespace syncer | |
| OLD | NEW |