OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync/glue/generic_change_processor.h" | 5 #include "chrome/browser/sync/glue/generic_change_processor.h" |
6 | 6 |
7 #include "chrome/browser/sync/glue/model_associator.h" | 7 #include "chrome/browser/sync/api/syncable_service.h" |
8 #include "chrome/browser/sync/syncable_service.h" | 8 #include "chrome/browser/sync/api/sync_change.h" |
| 9 #include "chrome/browser/sync/engine/syncapi.h" |
| 10 #include "chrome/browser/sync/syncable/nigori_util.h" |
9 | 11 |
10 namespace browser_sync { | 12 namespace browser_sync { |
11 | 13 |
12 GenericChangeProcessor::GenericChangeProcessor( | 14 GenericChangeProcessor::GenericChangeProcessor( |
13 SyncableService* local_service, | 15 SyncableService* local_service, |
14 UnrecoverableErrorHandler* error_handler) | 16 UnrecoverableErrorHandler* error_handler, |
| 17 sync_api::UserShare* user_share) |
15 : ChangeProcessor(error_handler), | 18 : ChangeProcessor(error_handler), |
16 local_service_(local_service) { | 19 local_service_(local_service), |
| 20 user_share_(user_share) { |
17 DCHECK(local_service_); | 21 DCHECK(local_service_); |
18 } | 22 } |
| 23 |
19 GenericChangeProcessor::~GenericChangeProcessor() { | 24 GenericChangeProcessor::~GenericChangeProcessor() { |
20 // Set to null to ensure it's not used after destruction. | 25 // Set to null to ensure it's not used after destruction. |
21 local_service_ = NULL; | 26 local_service_ = NULL; |
22 } | 27 } |
23 | 28 |
24 void GenericChangeProcessor::ApplyChangesFromSyncModel( | 29 void GenericChangeProcessor::ApplyChangesFromSyncModel( |
25 const sync_api::BaseTransaction* trans, | 30 const sync_api::BaseTransaction* trans, |
26 const sync_api::SyncManager::ChangeRecord* changes, | 31 const sync_api::SyncManager::ChangeRecord* changes, |
27 int change_count) { | 32 int change_count) { |
28 local_service_->ApplyChangesFromSync(trans, changes, change_count); | 33 DCHECK(running()); |
| 34 DCHECK(syncer_changes_.empty()); |
| 35 for (int i = 0; i < change_count; ++i) { |
| 36 SyncChange::SyncChangeType action; |
| 37 sync_pb::EntitySpecifics const* specifics = NULL; |
| 38 if (sync_api::SyncManager::ChangeRecord::ACTION_DELETE == |
| 39 changes[i].action) { |
| 40 action = SyncChange::ACTION_DELETE; |
| 41 specifics = &changes[i].specifics; |
| 42 DCHECK(specifics); |
| 43 } else if (sync_api::SyncManager::ChangeRecord::ACTION_ADD == |
| 44 changes[i].action) { |
| 45 action = SyncChange::ACTION_ADD; |
| 46 } else { // ACTION_UPDATE. |
| 47 action = SyncChange::ACTION_UPDATE; |
| 48 } |
| 49 if (!specifics) { |
| 50 // Need to load from node. |
| 51 sync_api::ReadNode read_node(trans); |
| 52 if (!read_node.InitByIdLookup(changes[i].id)) { |
| 53 error_handler()->OnUnrecoverableError(FROM_HERE, "Failed to look up " |
| 54 " data for received change with id " + changes[i].id); |
| 55 return; |
| 56 } |
| 57 specifics = &read_node.GetEntitySpecifics(); |
| 58 } |
| 59 DCHECK_NE(syncable::UNSPECIFIED, |
| 60 syncable::GetModelTypeFromSpecifics(*specifics)); |
| 61 syncer_changes_.push_back( |
| 62 SyncChange(action, SyncData::CreateRemoteData(*specifics))); |
| 63 } |
| 64 } |
| 65 |
| 66 void GenericChangeProcessor::CommitChangesFromSyncModel() { |
| 67 if (!running()) |
| 68 return; |
| 69 if (syncer_changes_.empty()) |
| 70 return; |
| 71 local_service_->ProcessSyncChanges(syncer_changes_); |
| 72 syncer_changes_.clear(); |
| 73 } |
| 74 |
| 75 bool GenericChangeProcessor::GetSyncDataForType( |
| 76 syncable::ModelType type, |
| 77 SyncDataList* current_sync_data) { |
| 78 std::string type_name = syncable::ModelTypeToString(type); |
| 79 sync_api::ReadTransaction trans(share_handle()); |
| 80 sync_api::ReadNode root(&trans); |
| 81 if (!root.InitByTagLookup(syncable::ModelTypeToRootTag(type))) { |
| 82 LOG(ERROR) << "Server did not create the top-level " + type_name + " node." |
| 83 << " We might be running against an out-of-date server."; |
| 84 return false; |
| 85 } |
| 86 |
| 87 int64 sync_child_id = root.GetFirstChildId(); |
| 88 while (sync_child_id != sync_api::kInvalidId) { |
| 89 sync_api::ReadNode sync_child_node(&trans); |
| 90 if (!sync_child_node.InitByIdLookup(sync_child_id)) { |
| 91 LOG(ERROR) << "Failed to fetch child node for type " + type_name + "."; |
| 92 return false; |
| 93 } |
| 94 current_sync_data->push_back(SyncData::CreateRemoteData( |
| 95 sync_child_node.GetEntitySpecifics())); |
| 96 sync_child_id = sync_child_node.GetSuccessorId(); |
| 97 } |
| 98 return true; |
| 99 } |
| 100 |
| 101 void GenericChangeProcessor::ProcessSyncChanges( |
| 102 const SyncChangeList& list_of_changes) { |
| 103 sync_api::WriteTransaction trans(share_handle()); |
| 104 |
| 105 for (SyncChangeList::const_iterator iter = list_of_changes.begin(); |
| 106 iter != list_of_changes.end(); |
| 107 ++iter) { |
| 108 const SyncChange& change = *iter; |
| 109 DCHECK_NE(change.sync_data().GetDataType(), syncable::UNSPECIFIED); |
| 110 std::string type_str = syncable::ModelTypeToString( |
| 111 change.sync_data().GetDataType()); |
| 112 sync_api::WriteNode sync_node(&trans); |
| 113 if (change.change_type() == SyncChange::ACTION_DELETE) { |
| 114 if (change.sync_data().GetTag() == "" || |
| 115 !sync_node.InitByClientTagLookup(change.sync_data().GetDataType(), |
| 116 change.sync_data().GetTag())) { |
| 117 NOTREACHED(); |
| 118 error_handler()->OnUnrecoverableError(FROM_HERE, |
| 119 "Failed to delete " + type_str + " node."); |
| 120 return; |
| 121 } |
| 122 sync_node.Remove(); |
| 123 } else if (change.change_type() == SyncChange::ACTION_ADD) { |
| 124 // TODO(sync): Handle other types of creation (custom parents, folders, |
| 125 // etc.). |
| 126 sync_api::ReadNode root_node(&trans); |
| 127 if (!root_node.InitByTagLookup( |
| 128 syncable::ModelTypeToRootTag(change.sync_data().GetDataType()))) { |
| 129 NOTREACHED(); |
| 130 error_handler()->OnUnrecoverableError(FROM_HERE, |
| 131 "Failed to look up root node for type " + type_str); |
| 132 return; |
| 133 } |
| 134 if (!sync_node.InitUniqueByCreation(change.sync_data().GetDataType(), |
| 135 root_node, |
| 136 change.sync_data().GetTag())) { |
| 137 error_handler()->OnUnrecoverableError(FROM_HERE, |
| 138 "Failed to create " + type_str + " node."); |
| 139 return; |
| 140 } |
| 141 sync_node.SetEntitySpecifics(change.sync_data().GetSpecifics()); |
| 142 } else if (change.change_type() == SyncChange::ACTION_UPDATE) { |
| 143 if (change.sync_data().GetTag() == "" || |
| 144 !sync_node.InitByClientTagLookup(change.sync_data().GetDataType(), |
| 145 change.sync_data().GetTag())) { |
| 146 NOTREACHED(); |
| 147 error_handler()->OnUnrecoverableError(FROM_HERE, |
| 148 "Failed to update " + type_str + " node"); |
| 149 return; |
| 150 } |
| 151 sync_node.SetEntitySpecifics(change.sync_data().GetSpecifics()); |
| 152 // TODO(sync): Support updating other parts of the sync node (title, |
| 153 // successor, parent, etc.). |
| 154 } else { |
| 155 NOTREACHED(); |
| 156 error_handler()->OnUnrecoverableError(FROM_HERE, |
| 157 "Received unset SyncChange in the change processor."); |
| 158 return; |
| 159 } |
| 160 } |
| 161 } |
| 162 |
| 163 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes( |
| 164 syncable::ModelType type, |
| 165 bool* has_nodes) { |
| 166 DCHECK(has_nodes); |
| 167 DCHECK_NE(type, syncable::UNSPECIFIED); |
| 168 std::string type_name = syncable::ModelTypeToString(type); |
| 169 std::string err_str = "Server did not create the top-level " + type_name + |
| 170 " node. We might be running against an out-of-date server."; |
| 171 *has_nodes = false; |
| 172 sync_api::ReadTransaction trans(share_handle()); |
| 173 sync_api::ReadNode type_root_node(&trans); |
| 174 if (!type_root_node.InitByTagLookup(syncable::ModelTypeToRootTag(type))) { |
| 175 LOG(ERROR) << err_str; |
| 176 return false; |
| 177 } |
| 178 |
| 179 // The sync model has user created nodes if the type's root node has any |
| 180 // children. |
| 181 *has_nodes = sync_api::kInvalidId != type_root_node.GetFirstChildId(); |
| 182 return true; |
| 183 } |
| 184 |
| 185 bool GenericChangeProcessor::CryptoReadyIfNecessary(syncable::ModelType type) { |
| 186 DCHECK_NE(type, syncable::UNSPECIFIED); |
| 187 // We only access the cryptographer while holding a transaction. |
| 188 sync_api::ReadTransaction trans(share_handle()); |
| 189 const syncable::ModelTypeSet& encrypted_types = |
| 190 syncable::GetEncryptedDataTypes(trans.GetWrappedTrans()); |
| 191 return encrypted_types.count(type) == 0 || |
| 192 trans.GetCryptographer()->is_ready(); |
29 } | 193 } |
30 | 194 |
31 void GenericChangeProcessor::StartImpl(Profile* profile) {} | 195 void GenericChangeProcessor::StartImpl(Profile* profile) {} |
32 | 196 |
33 void GenericChangeProcessor::StopImpl() {} | 197 void GenericChangeProcessor::StopImpl() {} |
34 | 198 |
| 199 sync_api::UserShare* GenericChangeProcessor::share_handle() { |
| 200 return user_share_; |
| 201 } |
| 202 |
35 } // namespace browser_sync | 203 } // namespace browser_sync |
OLD | NEW |