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