Chromium Code Reviews| 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 "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/sync/api/syncable_service.h" | 9 #include "chrome/browser/sync/api/syncable_service.h" |
| 10 #include "chrome/browser/sync/api/sync_change.h" | 10 #include "chrome/browser/sync/api/sync_change.h" |
| 11 #include "chrome/browser/sync/api/sync_error.h" | 11 #include "chrome/browser/sync/api/sync_error.h" |
| 12 #include "chrome/browser/sync/internal_api/base_node.h" | 12 #include "chrome/browser/sync/internal_api/base_node.h" |
| 13 #include "chrome/browser/sync/internal_api/change_record.h" | 13 #include "chrome/browser/sync/internal_api/change_record.h" |
| 14 #include "chrome/browser/sync/internal_api/read_node.h" | 14 #include "chrome/browser/sync/internal_api/read_node.h" |
| 15 #include "chrome/browser/sync/internal_api/read_transaction.h" | 15 #include "chrome/browser/sync/internal_api/read_transaction.h" |
| 16 #include "chrome/browser/sync/internal_api/write_node.h" | 16 #include "chrome/browser/sync/internal_api/write_node.h" |
| 17 #include "chrome/browser/sync/internal_api/write_transaction.h" | 17 #include "chrome/browser/sync/internal_api/write_transaction.h" |
| 18 #include "chrome/browser/sync/unrecoverable_error_handler.h" | 18 #include "chrome/browser/sync/unrecoverable_error_handler.h" |
| 19 #include "content/browser/browser_thread.h" | |
| 19 | 20 |
| 20 namespace browser_sync { | 21 namespace browser_sync { |
| 21 | 22 |
| 22 GenericChangeProcessor::GenericChangeProcessor( | 23 GenericChangeProcessor::GenericChangeProcessor( |
| 24 UnrecoverableErrorHandler* error_handler, | |
| 23 SyncableService* local_service, | 25 SyncableService* local_service, |
| 24 UnrecoverableErrorHandler* error_handler, | |
| 25 sync_api::UserShare* user_share) | 26 sync_api::UserShare* user_share) |
| 26 : ChangeProcessor(error_handler), | 27 : ChangeProcessor(error_handler), |
| 27 local_service_(local_service), | 28 share_handle_(user_share) { |
| 28 user_share_(user_share) { | 29 DCHECK(CalledOnValidThread()); |
| 29 DCHECK(local_service_); | 30 if (local_service) |
|
akalin
2011/10/11 22:41:08
hmm, prefer passing weak ptr directly to construct
Nicolas Zea
2011/10/12 04:24:19
Done.
| |
| 31 local_service_ = local_service->AsWeakPtr(); | |
| 30 } | 32 } |
| 31 | 33 |
| 32 GenericChangeProcessor::~GenericChangeProcessor() { | 34 GenericChangeProcessor::~GenericChangeProcessor() { |
| 33 // Set to null to ensure it's not used after destruction. | 35 // We can either be deleted when the DTC is destroyed (on UI thread), |
| 34 local_service_ = NULL; | 36 // or when the SyncableService stop's syncing (datatype thread). |
| 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | |
|
akalin
2011/10/11 22:41:08
prefer removing this, and making the owner ensure
Nicolas Zea
2011/10/12 04:24:19
Done.
| |
| 38 CalledOnValidThread()); | |
| 39 DetachFromThread(); | |
| 35 } | 40 } |
| 36 | 41 |
| 37 void GenericChangeProcessor::ApplyChangesFromSyncModel( | 42 void GenericChangeProcessor::ApplyChangesFromSyncModel( |
| 38 const sync_api::BaseTransaction* trans, | 43 const sync_api::BaseTransaction* trans, |
| 39 const sync_api::ImmutableChangeRecordList& changes) { | 44 const sync_api::ImmutableChangeRecordList& changes) { |
| 45 DCHECK(CalledOnValidThread()); | |
| 40 DCHECK(running()); | 46 DCHECK(running()); |
| 41 DCHECK(syncer_changes_.empty()); | 47 DCHECK(syncer_changes_.empty()); |
| 42 for (sync_api::ChangeRecordList::const_iterator it = | 48 for (sync_api::ChangeRecordList::const_iterator it = |
| 43 changes.Get().begin(); it != changes.Get().end(); ++it) { | 49 changes.Get().begin(); it != changes.Get().end(); ++it) { |
| 44 if (it->action == sync_api::ChangeRecord::ACTION_DELETE) { | 50 if (it->action == sync_api::ChangeRecord::ACTION_DELETE) { |
| 45 syncer_changes_.push_back( | 51 syncer_changes_.push_back( |
| 46 SyncChange(SyncChange::ACTION_DELETE, | 52 SyncChange(SyncChange::ACTION_DELETE, |
| 47 SyncData::CreateRemoteData(it->id, it->specifics))); | 53 SyncData::CreateRemoteData(it->id, it->specifics))); |
| 48 } else { | 54 } else { |
| 49 SyncChange::SyncChangeType action = | 55 SyncChange::SyncChangeType action = |
| 50 (it->action == sync_api::ChangeRecord::ACTION_ADD) ? | 56 (it->action == sync_api::ChangeRecord::ACTION_ADD) ? |
| 51 SyncChange::ACTION_ADD : SyncChange::ACTION_UPDATE; | 57 SyncChange::ACTION_ADD : SyncChange::ACTION_UPDATE; |
| 52 // Need to load specifics from node. | 58 // Need to load specifics from node. |
| 53 sync_api::ReadNode read_node(trans); | 59 sync_api::ReadNode read_node(trans); |
| 54 if (!read_node.InitByIdLookup(it->id)) { | 60 if (!read_node.InitByIdLookup(it->id)) { |
| 55 error_handler()->OnUnrecoverableError(FROM_HERE, "Failed to look up " | 61 error_handler()->OnUnrecoverableError(FROM_HERE, "Failed to look up " |
| 56 " data for received change with id " + it->id); | 62 " data for received change with id " + it->id); |
| 57 return; | 63 return; |
| 58 } | 64 } |
| 59 syncer_changes_.push_back( | 65 syncer_changes_.push_back( |
| 60 SyncChange(action, | 66 SyncChange(action, |
| 61 SyncData::CreateRemoteData( | 67 SyncData::CreateRemoteData( |
| 62 it->id, read_node.GetEntitySpecifics()))); | 68 it->id, read_node.GetEntitySpecifics()))); |
| 63 } | 69 } |
| 64 } | 70 } |
| 65 } | 71 } |
| 66 | 72 |
| 67 void GenericChangeProcessor::CommitChangesFromSyncModel() { | 73 void GenericChangeProcessor::CommitChangesFromSyncModel() { |
| 74 DCHECK(CalledOnValidThread()); | |
| 68 if (!running()) | 75 if (!running()) |
| 69 return; | 76 return; |
| 70 if (syncer_changes_.empty()) | 77 if (syncer_changes_.empty()) |
| 71 return; | 78 return; |
| 79 if (!local_service_) { | |
| 80 syncable::ModelType type = syncer_changes_[0].sync_data().GetDataType(); | |
| 81 SyncError error(FROM_HERE, "Local service destroyed.", type); | |
| 82 error_handler()->OnUnrecoverableError(error.location(), error.message()); | |
| 83 } | |
| 72 SyncError error = local_service_->ProcessSyncChanges(FROM_HERE, | 84 SyncError error = local_service_->ProcessSyncChanges(FROM_HERE, |
| 73 syncer_changes_); | 85 syncer_changes_); |
| 74 syncer_changes_.clear(); | 86 syncer_changes_.clear(); |
| 75 if (error.IsSet()) { | 87 if (error.IsSet()) { |
| 76 error_handler()->OnUnrecoverableError(error.location(), error.message()); | 88 error_handler()->OnUnrecoverableError(error.location(), error.message()); |
| 77 } | 89 } |
| 78 } | 90 } |
| 79 | 91 |
| 80 SyncError GenericChangeProcessor::GetSyncDataForType( | 92 SyncError GenericChangeProcessor::GetSyncDataForType( |
| 81 syncable::ModelType type, | 93 syncable::ModelType type, |
| 82 SyncDataList* current_sync_data) { | 94 SyncDataList* current_sync_data) { |
| 95 DCHECK(CalledOnValidThread()); | |
| 83 std::string type_name = syncable::ModelTypeToString(type); | 96 std::string type_name = syncable::ModelTypeToString(type); |
| 84 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); | 97 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); |
| 85 sync_api::ReadNode root(&trans); | 98 sync_api::ReadNode root(&trans); |
| 86 if (!root.InitByTagLookup(syncable::ModelTypeToRootTag(type))) { | 99 if (!root.InitByTagLookup(syncable::ModelTypeToRootTag(type))) { |
| 87 SyncError error(FROM_HERE, | 100 SyncError error(FROM_HERE, |
| 88 "Server did not create the top-level " + type_name + | 101 "Server did not create the top-level " + type_name + |
| 89 " node. We might be running against an out-of-date server.", | 102 " node. We might be running against an out-of-date server.", |
| 90 type); | 103 type); |
| 91 return error; | 104 return error; |
| 92 } | 105 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 } | 143 } |
| 131 node->Remove(); | 144 node->Remove(); |
| 132 return true; | 145 return true; |
| 133 } | 146 } |
| 134 | 147 |
| 135 } // namespace | 148 } // namespace |
| 136 | 149 |
| 137 SyncError GenericChangeProcessor::ProcessSyncChanges( | 150 SyncError GenericChangeProcessor::ProcessSyncChanges( |
| 138 const tracked_objects::Location& from_here, | 151 const tracked_objects::Location& from_here, |
| 139 const SyncChangeList& list_of_changes) { | 152 const SyncChangeList& list_of_changes) { |
| 153 DCHECK(CalledOnValidThread()); | |
| 140 sync_api::WriteTransaction trans(from_here, share_handle()); | 154 sync_api::WriteTransaction trans(from_here, share_handle()); |
| 141 | 155 |
| 142 for (SyncChangeList::const_iterator iter = list_of_changes.begin(); | 156 for (SyncChangeList::const_iterator iter = list_of_changes.begin(); |
| 143 iter != list_of_changes.end(); | 157 iter != list_of_changes.end(); |
| 144 ++iter) { | 158 ++iter) { |
| 145 const SyncChange& change = *iter; | 159 const SyncChange& change = *iter; |
| 146 DCHECK_NE(change.sync_data().GetDataType(), syncable::UNSPECIFIED); | 160 DCHECK_NE(change.sync_data().GetDataType(), syncable::UNSPECIFIED); |
| 147 syncable::ModelType type = change.sync_data().GetDataType(); | 161 syncable::ModelType type = change.sync_data().GetDataType(); |
| 148 std::string type_str = syncable::ModelTypeToString(type); | 162 std::string type_str = syncable::ModelTypeToString(type); |
| 149 sync_api::WriteNode sync_node(&trans); | 163 sync_api::WriteNode sync_node(&trans); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 error.message()); | 223 error.message()); |
| 210 return error; | 224 return error; |
| 211 } | 225 } |
| 212 } | 226 } |
| 213 return SyncError(); | 227 return SyncError(); |
| 214 } | 228 } |
| 215 | 229 |
| 216 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes( | 230 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes( |
| 217 syncable::ModelType type, | 231 syncable::ModelType type, |
| 218 bool* has_nodes) { | 232 bool* has_nodes) { |
| 233 DCHECK(CalledOnValidThread()); | |
| 219 DCHECK(has_nodes); | 234 DCHECK(has_nodes); |
| 220 DCHECK_NE(type, syncable::UNSPECIFIED); | 235 DCHECK_NE(type, syncable::UNSPECIFIED); |
| 221 std::string type_name = syncable::ModelTypeToString(type); | 236 std::string type_name = syncable::ModelTypeToString(type); |
| 222 std::string err_str = "Server did not create the top-level " + type_name + | 237 std::string err_str = "Server did not create the top-level " + type_name + |
| 223 " node. We might be running against an out-of-date server."; | 238 " node. We might be running against an out-of-date server."; |
| 224 *has_nodes = false; | 239 *has_nodes = false; |
| 225 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); | 240 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); |
| 226 sync_api::ReadNode type_root_node(&trans); | 241 sync_api::ReadNode type_root_node(&trans); |
| 227 if (!type_root_node.InitByTagLookup(syncable::ModelTypeToRootTag(type))) { | 242 if (!type_root_node.InitByTagLookup(syncable::ModelTypeToRootTag(type))) { |
| 228 LOG(ERROR) << err_str; | 243 LOG(ERROR) << err_str; |
| 229 return false; | 244 return false; |
| 230 } | 245 } |
| 231 | 246 |
| 232 // The sync model has user created nodes if the type's root node has any | 247 // The sync model has user created nodes if the type's root node has any |
| 233 // children. | 248 // children. |
| 234 *has_nodes = sync_api::kInvalidId != type_root_node.GetFirstChildId(); | 249 *has_nodes = sync_api::kInvalidId != type_root_node.GetFirstChildId(); |
| 235 return true; | 250 return true; |
| 236 } | 251 } |
| 237 | 252 |
| 238 bool GenericChangeProcessor::CryptoReadyIfNecessary(syncable::ModelType type) { | 253 bool GenericChangeProcessor::CryptoReadyIfNecessary(syncable::ModelType type) { |
| 254 DCHECK(CalledOnValidThread()); | |
| 239 DCHECK_NE(type, syncable::UNSPECIFIED); | 255 DCHECK_NE(type, syncable::UNSPECIFIED); |
| 240 // We only access the cryptographer while holding a transaction. | 256 // We only access the cryptographer while holding a transaction. |
| 241 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); | 257 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); |
| 242 const syncable::ModelTypeSet& encrypted_types = | 258 const syncable::ModelTypeSet& encrypted_types = |
| 243 GetEncryptedTypes(&trans); | 259 GetEncryptedTypes(&trans); |
| 244 return encrypted_types.count(type) == 0 || | 260 return encrypted_types.count(type) == 0 || |
| 245 trans.GetCryptographer()->is_ready(); | 261 trans.GetCryptographer()->is_ready(); |
| 246 } | 262 } |
| 247 | 263 |
| 248 void GenericChangeProcessor::StartImpl(Profile* profile) {} | 264 void GenericChangeProcessor::StartImpl(Profile* profile) { |
| 265 DCHECK(CalledOnValidThread()); | |
| 266 } | |
| 249 | 267 |
| 250 void GenericChangeProcessor::StopImpl() {} | 268 void GenericChangeProcessor::StopImpl() { |
| 269 } | |
|
akalin
2011/10/11 22:41:08
stopimpl is called on the UI thread or something w
Nicolas Zea
2011/10/12 04:24:19
Done.
| |
| 251 | 270 |
| 252 sync_api::UserShare* GenericChangeProcessor::share_handle() { | 271 sync_api::UserShare* GenericChangeProcessor::share_handle() const { |
| 253 return user_share_; | 272 DCHECK(CalledOnValidThread()); |
| 273 return share_handle_; | |
| 254 } | 274 } |
| 255 | 275 |
| 256 } // namespace browser_sync | 276 } // namespace browser_sync |
| OLD | NEW |