| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/password_change_processor.h" | 5 #include "chrome/browser/sync/glue/password_change_processor.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/password_manager/password_store.h" | 9 #include "chrome/browser/password_manager/password_store.h" |
| 10 #include "chrome/browser/password_manager/password_store_change.h" | 10 #include "chrome/browser/password_manager/password_store_change.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 DCHECK(NotificationType::LOGINS_CHANGED == type); | 48 DCHECK(NotificationType::LOGINS_CHANGED == type); |
| 49 if (!observing_) | 49 if (!observing_) |
| 50 return; | 50 return; |
| 51 | 51 |
| 52 DCHECK(running()); | 52 DCHECK(running()); |
| 53 | 53 |
| 54 sync_api::WriteTransaction trans(share_handle()); | 54 sync_api::WriteTransaction trans(share_handle()); |
| 55 | 55 |
| 56 sync_api::ReadNode password_root(&trans); | 56 sync_api::ReadNode password_root(&trans); |
| 57 if (!password_root.InitByTagLookup(kPasswordTag)) { | 57 if (!password_root.InitByTagLookup(kPasswordTag)) { |
| 58 error_handler()->OnUnrecoverableError(); | 58 error_handler()->OnUnrecoverableError(FROM_HERE, |
| 59 LOG(ERROR) << "Server did not create the top-level password node. " | 59 "Server did not create the top-level password node. " |
| 60 << "We might be running against an out-of-date server."; | 60 "We might be running against an out-of-date server."); |
| 61 return; | 61 return; |
| 62 } | 62 } |
| 63 | 63 |
| 64 PasswordStoreChangeList* changes = | 64 PasswordStoreChangeList* changes = |
| 65 Details<PasswordStoreChangeList>(details).ptr(); | 65 Details<PasswordStoreChangeList>(details).ptr(); |
| 66 for (PasswordStoreChangeList::iterator change = changes->begin(); | 66 for (PasswordStoreChangeList::iterator change = changes->begin(); |
| 67 change != changes->end(); ++change) { | 67 change != changes->end(); ++change) { |
| 68 std::string tag = PasswordModelAssociator::MakeTag(change->form()); | 68 std::string tag = PasswordModelAssociator::MakeTag(change->form()); |
| 69 switch (change->type()) { | 69 switch (change->type()) { |
| 70 case PasswordStoreChange::ADD: { | 70 case PasswordStoreChange::ADD: { |
| 71 sync_api::WriteNode sync_node(&trans); | 71 sync_api::WriteNode sync_node(&trans); |
| 72 if (!sync_node.InitUniqueByCreation(syncable::PASSWORD, | 72 if (!sync_node.InitUniqueByCreation(syncable::PASSWORD, |
| 73 password_root, tag)) { | 73 password_root, tag)) { |
| 74 LOG(ERROR) << "Failed to create password sync node."; | 74 error_handler()->OnUnrecoverableError(FROM_HERE, |
| 75 error_handler()->OnUnrecoverableError(); | 75 "Failed to create password sync node."); |
| 76 return; | 76 return; |
| 77 } | 77 } |
| 78 | 78 |
| 79 PasswordModelAssociator::WriteToSyncNode(change->form(), &sync_node); | 79 PasswordModelAssociator::WriteToSyncNode(change->form(), &sync_node); |
| 80 model_associator_->Associate(&tag, sync_node.GetId()); | 80 model_associator_->Associate(&tag, sync_node.GetId()); |
| 81 break; | 81 break; |
| 82 } | 82 } |
| 83 case PasswordStoreChange::UPDATE: { | 83 case PasswordStoreChange::UPDATE: { |
| 84 sync_api::WriteNode sync_node(&trans); | 84 sync_api::WriteNode sync_node(&trans); |
| 85 int64 sync_id = model_associator_->GetSyncIdFromChromeId(tag); | 85 int64 sync_id = model_associator_->GetSyncIdFromChromeId(tag); |
| 86 if (sync_api::kInvalidId == sync_id) { | 86 if (sync_api::kInvalidId == sync_id) { |
| 87 LOG(ERROR) << "Unexpected notification for: " << tag; | 87 error_handler()->OnUnrecoverableError(FROM_HERE, |
| 88 error_handler()->OnUnrecoverableError(); | 88 "Unexpected notification for: "); |
| 89 return; | 89 return; |
| 90 } else { | 90 } else { |
| 91 if (!sync_node.InitByIdLookup(sync_id)) { | 91 if (!sync_node.InitByIdLookup(sync_id)) { |
| 92 LOG(ERROR) << "Password node lookup failed."; | 92 error_handler()->OnUnrecoverableError(FROM_HERE, |
| 93 error_handler()->OnUnrecoverableError(); | 93 "Password node lookup failed."); |
| 94 return; | 94 return; |
| 95 } | 95 } |
| 96 } | 96 } |
| 97 | 97 |
| 98 PasswordModelAssociator::WriteToSyncNode(change->form(), &sync_node); | 98 PasswordModelAssociator::WriteToSyncNode(change->form(), &sync_node); |
| 99 break; | 99 break; |
| 100 } | 100 } |
| 101 case PasswordStoreChange::REMOVE: { | 101 case PasswordStoreChange::REMOVE: { |
| 102 sync_api::WriteNode sync_node(&trans); | 102 sync_api::WriteNode sync_node(&trans); |
| 103 int64 sync_id = model_associator_->GetSyncIdFromChromeId(tag); | 103 int64 sync_id = model_associator_->GetSyncIdFromChromeId(tag); |
| 104 if (sync_api::kInvalidId == sync_id) { | 104 if (sync_api::kInvalidId == sync_id) { |
| 105 LOG(ERROR) << "Unexpected notification"; | 105 error_handler()->OnUnrecoverableError(FROM_HERE, |
| 106 error_handler()->OnUnrecoverableError(); | 106 "Unexpected notification"); |
| 107 return; | 107 return; |
| 108 } else { | 108 } else { |
| 109 if (!sync_node.InitByIdLookup(sync_id)) { | 109 if (!sync_node.InitByIdLookup(sync_id)) { |
| 110 LOG(ERROR) << "Password node lookup failed."; | 110 error_handler()->OnUnrecoverableError(FROM_HERE, |
| 111 error_handler()->OnUnrecoverableError(); | 111 "Password node lookup failed."); |
| 112 return; | 112 return; |
| 113 } | 113 } |
| 114 model_associator_->Disassociate(sync_node.GetId()); | 114 model_associator_->Disassociate(sync_node.GetId()); |
| 115 sync_node.Remove(); | 115 sync_node.Remove(); |
| 116 } | 116 } |
| 117 break; | 117 break; |
| 118 } | 118 } |
| 119 } | 119 } |
| 120 } | 120 } |
| 121 } | 121 } |
| 122 | 122 |
| 123 void PasswordChangeProcessor::ApplyChangesFromSyncModel( | 123 void PasswordChangeProcessor::ApplyChangesFromSyncModel( |
| 124 const sync_api::BaseTransaction* trans, | 124 const sync_api::BaseTransaction* trans, |
| 125 const sync_api::SyncManager::ChangeRecord* changes, | 125 const sync_api::SyncManager::ChangeRecord* changes, |
| 126 int change_count) { | 126 int change_count) { |
| 127 DCHECK(expected_loop_ == MessageLoop::current()); | 127 DCHECK(expected_loop_ == MessageLoop::current()); |
| 128 if (!running()) | 128 if (!running()) |
| 129 return; | 129 return; |
| 130 StopObserving(); | 130 StopObserving(); |
| 131 | 131 |
| 132 sync_api::ReadNode password_root(trans); | 132 sync_api::ReadNode password_root(trans); |
| 133 if (!password_root.InitByTagLookup(kPasswordTag)) { | 133 if (!password_root.InitByTagLookup(kPasswordTag)) { |
| 134 LOG(ERROR) << "Password root node lookup failed."; | 134 error_handler()->OnUnrecoverableError(FROM_HERE, |
| 135 error_handler()->OnUnrecoverableError(); | 135 "Password root node lookup failed."); |
| 136 return; | 136 return; |
| 137 } | 137 } |
| 138 | 138 |
| 139 PasswordModelAssociator::PasswordVector new_passwords; | 139 PasswordModelAssociator::PasswordVector new_passwords; |
| 140 PasswordModelAssociator::PasswordVector updated_passwords; | 140 PasswordModelAssociator::PasswordVector updated_passwords; |
| 141 PasswordModelAssociator::PasswordVector deleted_passwords; | 141 PasswordModelAssociator::PasswordVector deleted_passwords; |
| 142 | 142 |
| 143 for (int i = 0; i < change_count; ++i) { | 143 for (int i = 0; i < change_count; ++i) { |
| 144 | 144 |
| 145 sync_api::ReadNode sync_node(trans); | 145 sync_api::ReadNode sync_node(trans); |
| 146 if (!sync_node.InitByIdLookup(changes[i].id)) { | 146 if (!sync_node.InitByIdLookup(changes[i].id)) { |
| 147 LOG(ERROR) << "Password node lookup failed."; | 147 error_handler()->OnUnrecoverableError(FROM_HERE, |
| 148 error_handler()->OnUnrecoverableError(); | 148 "Password node lookup failed."); |
| 149 return; | 149 return; |
| 150 } | 150 } |
| 151 | 151 |
| 152 // Check that the changed node is a child of the passwords folder. | 152 // Check that the changed node is a child of the passwords folder. |
| 153 DCHECK(password_root.GetId() == sync_node.GetParentId()); | 153 DCHECK(password_root.GetId() == sync_node.GetParentId()); |
| 154 DCHECK(syncable::PASSWORD == sync_node.GetModelType()); | 154 DCHECK(syncable::PASSWORD == sync_node.GetModelType()); |
| 155 | 155 |
| 156 sync_pb::PasswordSpecificsData password_data; | 156 sync_pb::PasswordSpecificsData password_data; |
| 157 if (!sync_node.GetPasswordSpecifics(&password_data)) { | 157 if (!sync_node.GetPasswordSpecifics(&password_data)) { |
| 158 LOG(ERROR) << "Could not read password specifics"; | 158 error_handler()->OnUnrecoverableError(FROM_HERE, |
| 159 error_handler()->OnUnrecoverableError(); | 159 "Could not read password specifics"); |
| 160 return; | 160 return; |
| 161 } | 161 } |
| 162 webkit_glue::PasswordForm password; | 162 webkit_glue::PasswordForm password; |
| 163 PasswordModelAssociator::CopyPassword(password_data, | 163 PasswordModelAssociator::CopyPassword(password_data, |
| 164 &password); | 164 &password); |
| 165 | 165 |
| 166 if (sync_api::SyncManager::ChangeRecord::ACTION_ADD == changes[i].action) { | 166 if (sync_api::SyncManager::ChangeRecord::ACTION_ADD == changes[i].action) { |
| 167 new_passwords.push_back(password); | 167 new_passwords.push_back(password); |
| 168 } else if (sync_api::SyncManager::ChangeRecord::ACTION_DELETE == | 168 } else if (sync_api::SyncManager::ChangeRecord::ACTION_DELETE == |
| 169 changes[i].action) { | 169 changes[i].action) { |
| 170 deleted_passwords.push_back(password); | 170 deleted_passwords.push_back(password); |
| 171 } else { | 171 } else { |
| 172 DCHECK(sync_api::SyncManager::ChangeRecord::ACTION_UPDATE == | 172 DCHECK(sync_api::SyncManager::ChangeRecord::ACTION_UPDATE == |
| 173 changes[i].action); | 173 changes[i].action); |
| 174 updated_passwords.push_back(password); | 174 updated_passwords.push_back(password); |
| 175 } | 175 } |
| 176 } | 176 } |
| 177 if (!model_associator_->WriteToPasswordStore(&new_passwords, | 177 if (!model_associator_->WriteToPasswordStore(&new_passwords, |
| 178 &updated_passwords, | 178 &updated_passwords, |
| 179 &deleted_passwords)) { | 179 &deleted_passwords)) { |
| 180 LOG(ERROR) << "Error writing passwords"; | 180 error_handler()->OnUnrecoverableError(FROM_HERE, "Error writing passwords"); |
| 181 error_handler()->OnUnrecoverableError(); | |
| 182 return; | 181 return; |
| 183 } | 182 } |
| 184 | 183 |
| 185 StartObserving(); | 184 StartObserving(); |
| 186 } | 185 } |
| 187 | 186 |
| 188 void PasswordChangeProcessor::StartImpl(Profile* profile) { | 187 void PasswordChangeProcessor::StartImpl(Profile* profile) { |
| 189 DCHECK(expected_loop_ == MessageLoop::current()); | 188 DCHECK(expected_loop_ == MessageLoop::current()); |
| 190 observing_ = true; | 189 observing_ = true; |
| 191 } | 190 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 204 } | 203 } |
| 205 | 204 |
| 206 void PasswordChangeProcessor::StopObserving() { | 205 void PasswordChangeProcessor::StopObserving() { |
| 207 DCHECK(expected_loop_ == MessageLoop::current()); | 206 DCHECK(expected_loop_ == MessageLoop::current()); |
| 208 notification_registrar_.Remove(this, | 207 notification_registrar_.Remove(this, |
| 209 NotificationType::LOGINS_CHANGED, | 208 NotificationType::LOGINS_CHANGED, |
| 210 NotificationService::AllSources()); | 209 NotificationService::AllSources()); |
| 211 } | 210 } |
| 212 | 211 |
| 213 } // namespace browser_sync | 212 } // namespace browser_sync |
| OLD | NEW |