| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_model_associator.h" | 5 #include "chrome/browser/sync/glue/password_model_associator.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/password_manager/password_store.h" | 13 #include "chrome/browser/password_manager/password_store.h" |
| 14 #include "chrome/browser/sync/profile_sync_service.h" | 14 #include "chrome/browser/sync/profile_sync_service.h" |
| 15 #include "content/public/common/password_form.h" | 15 #include "components/autofill/core/common/password_form.h" |
| 16 #include "net/base/escape.h" | 16 #include "net/base/escape.h" |
| 17 #include "sync/api/sync_error.h" | 17 #include "sync/api/sync_error.h" |
| 18 #include "sync/internal_api/public/read_node.h" | 18 #include "sync/internal_api/public/read_node.h" |
| 19 #include "sync/internal_api/public/read_transaction.h" | 19 #include "sync/internal_api/public/read_transaction.h" |
| 20 #include "sync/internal_api/public/write_node.h" | 20 #include "sync/internal_api/public/write_node.h" |
| 21 #include "sync/internal_api/public/write_transaction.h" | 21 #include "sync/internal_api/public/write_transaction.h" |
| 22 #include "sync/protocol/password_specifics.pb.h" | 22 #include "sync/protocol/password_specifics.pb.h" |
| 23 | 23 |
| 24 using content::BrowserThread; | 24 using content::BrowserThread; |
| 25 | 25 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 50 } | 50 } |
| 51 | 51 |
| 52 syncer::SyncError PasswordModelAssociator::AssociateModels( | 52 syncer::SyncError PasswordModelAssociator::AssociateModels( |
| 53 syncer::SyncMergeResult* local_merge_result, | 53 syncer::SyncMergeResult* local_merge_result, |
| 54 syncer::SyncMergeResult* syncer_merge_result) { | 54 syncer::SyncMergeResult* syncer_merge_result) { |
| 55 DCHECK(expected_loop_ == base::MessageLoop::current()); | 55 DCHECK(expected_loop_ == base::MessageLoop::current()); |
| 56 | 56 |
| 57 // We must not be holding a transaction when we interact with the password | 57 // We must not be holding a transaction when we interact with the password |
| 58 // store, as it can post tasks to the UI thread which can itself be blocked | 58 // store, as it can post tasks to the UI thread which can itself be blocked |
| 59 // on our transaction, resulting in deadlock. (http://crbug.com/70658) | 59 // on our transaction, resulting in deadlock. (http://crbug.com/70658) |
| 60 std::vector<content::PasswordForm*> passwords; | 60 std::vector<autofill::PasswordForm*> passwords; |
| 61 if (!password_store_->FillAutofillableLogins(&passwords) || | 61 if (!password_store_->FillAutofillableLogins(&passwords) || |
| 62 !password_store_->FillBlacklistLogins(&passwords)) { | 62 !password_store_->FillBlacklistLogins(&passwords)) { |
| 63 STLDeleteElements(&passwords); | 63 STLDeleteElements(&passwords); |
| 64 | 64 |
| 65 // Password store often fails to load passwords. Track failures with UMA. | 65 // Password store often fails to load passwords. Track failures with UMA. |
| 66 // (http://crbug.com/249000) | 66 // (http://crbug.com/249000) |
| 67 UMA_HISTOGRAM_ENUMERATION("Sync.LocalDataFailedToLoad", | 67 UMA_HISTOGRAM_ENUMERATION("Sync.LocalDataFailedToLoad", |
| 68 ModelTypeToHistogramInt(syncer::PASSWORDS), | 68 ModelTypeToHistogramInt(syncer::PASSWORDS), |
| 69 syncer::MODEL_TYPE_COUNT); | 69 syncer::MODEL_TYPE_COUNT); |
| 70 return syncer::SyncError(FROM_HERE, | 70 return syncer::SyncError(FROM_HERE, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 85 syncer::ReadNode password_root(&trans); | 85 syncer::ReadNode password_root(&trans); |
| 86 if (password_root.InitByTagLookup(kPasswordTag) != | 86 if (password_root.InitByTagLookup(kPasswordTag) != |
| 87 syncer::BaseNode::INIT_OK) { | 87 syncer::BaseNode::INIT_OK) { |
| 88 return error_handler_->CreateAndUploadError( | 88 return error_handler_->CreateAndUploadError( |
| 89 FROM_HERE, | 89 FROM_HERE, |
| 90 "Server did not create the top-level password node. We " | 90 "Server did not create the top-level password node. We " |
| 91 "might be running against an out-of-date server.", | 91 "might be running against an out-of-date server.", |
| 92 model_type()); | 92 model_type()); |
| 93 } | 93 } |
| 94 | 94 |
| 95 for (std::vector<content::PasswordForm*>::iterator ix = | 95 for (std::vector<autofill::PasswordForm*>::iterator ix = |
| 96 passwords.begin(); | 96 passwords.begin(); |
| 97 ix != passwords.end(); ++ix) { | 97 ix != passwords.end(); ++ix) { |
| 98 std::string tag = MakeTag(**ix); | 98 std::string tag = MakeTag(**ix); |
| 99 | 99 |
| 100 syncer::ReadNode node(&trans); | 100 syncer::ReadNode node(&trans); |
| 101 if (node.InitByClientTagLookup(syncer::PASSWORDS, tag) == | 101 if (node.InitByClientTagLookup(syncer::PASSWORDS, tag) == |
| 102 syncer::BaseNode::INIT_OK) { | 102 syncer::BaseNode::INIT_OK) { |
| 103 const sync_pb::PasswordSpecificsData& password = | 103 const sync_pb::PasswordSpecificsData& password = |
| 104 node.GetPasswordSpecifics(); | 104 node.GetPasswordSpecifics(); |
| 105 DCHECK_EQ(tag, MakeTag(password)); | 105 DCHECK_EQ(tag, MakeTag(password)); |
| 106 | 106 |
| 107 content::PasswordForm new_password; | 107 autofill::PasswordForm new_password; |
| 108 | 108 |
| 109 if (MergePasswords(password, **ix, &new_password)) { | 109 if (MergePasswords(password, **ix, &new_password)) { |
| 110 syncer::WriteNode write_node(&trans); | 110 syncer::WriteNode write_node(&trans); |
| 111 if (write_node.InitByClientTagLookup(syncer::PASSWORDS, tag) != | 111 if (write_node.InitByClientTagLookup(syncer::PASSWORDS, tag) != |
| 112 syncer::BaseNode::INIT_OK) { | 112 syncer::BaseNode::INIT_OK) { |
| 113 STLDeleteElements(&passwords); | 113 STLDeleteElements(&passwords); |
| 114 return error_handler_->CreateAndUploadError( | 114 return error_handler_->CreateAndUploadError( |
| 115 FROM_HERE, | 115 FROM_HERE, |
| 116 "Failed to edit password sync node.", | 116 "Failed to edit password sync node.", |
| 117 model_type()); | 117 model_type()); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 "Failed to fetch child node.", | 153 "Failed to fetch child node.", |
| 154 model_type()); | 154 model_type()); |
| 155 } | 155 } |
| 156 const sync_pb::PasswordSpecificsData& password = | 156 const sync_pb::PasswordSpecificsData& password = |
| 157 sync_child_node.GetPasswordSpecifics(); | 157 sync_child_node.GetPasswordSpecifics(); |
| 158 std::string tag = MakeTag(password); | 158 std::string tag = MakeTag(password); |
| 159 | 159 |
| 160 // The password only exists on the server. Add it to the local | 160 // The password only exists on the server. Add it to the local |
| 161 // model. | 161 // model. |
| 162 if (current_passwords.find(tag) == current_passwords.end()) { | 162 if (current_passwords.find(tag) == current_passwords.end()) { |
| 163 content::PasswordForm new_password; | 163 autofill::PasswordForm new_password; |
| 164 | 164 |
| 165 CopyPassword(password, &new_password); | 165 CopyPassword(password, &new_password); |
| 166 Associate(&tag, sync_child_node.GetId()); | 166 Associate(&tag, sync_child_node.GetId()); |
| 167 new_passwords.push_back(new_password); | 167 new_passwords.push_back(new_password); |
| 168 } | 168 } |
| 169 | 169 |
| 170 sync_child_id = sync_child_node.GetSuccessorId(); | 170 sync_child_id = sync_child_node.GetSuccessorId(); |
| 171 } | 171 } |
| 172 } | 172 } |
| 173 | 173 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 // We have to notify password store observers of the change by hand since | 317 // We have to notify password store observers of the change by hand since |
| 318 // we use internal password store interfaces to make changes synchronously. | 318 // we use internal password store interfaces to make changes synchronously. |
| 319 password_store_->PostNotifyLoginsChanged(); | 319 password_store_->PostNotifyLoginsChanged(); |
| 320 } | 320 } |
| 321 return syncer::SyncError(); | 321 return syncer::SyncError(); |
| 322 } | 322 } |
| 323 | 323 |
| 324 // static | 324 // static |
| 325 void PasswordModelAssociator::CopyPassword( | 325 void PasswordModelAssociator::CopyPassword( |
| 326 const sync_pb::PasswordSpecificsData& password, | 326 const sync_pb::PasswordSpecificsData& password, |
| 327 content::PasswordForm* new_password) { | 327 autofill::PasswordForm* new_password) { |
| 328 new_password->scheme = | 328 new_password->scheme = |
| 329 static_cast<content::PasswordForm::Scheme>(password.scheme()); | 329 static_cast<autofill::PasswordForm::Scheme>(password.scheme()); |
| 330 new_password->signon_realm = password.signon_realm(); | 330 new_password->signon_realm = password.signon_realm(); |
| 331 new_password->origin = GURL(password.origin()); | 331 new_password->origin = GURL(password.origin()); |
| 332 new_password->action = GURL(password.action()); | 332 new_password->action = GURL(password.action()); |
| 333 new_password->username_element = | 333 new_password->username_element = |
| 334 UTF8ToUTF16(password.username_element()); | 334 UTF8ToUTF16(password.username_element()); |
| 335 new_password->password_element = | 335 new_password->password_element = |
| 336 UTF8ToUTF16(password.password_element()); | 336 UTF8ToUTF16(password.password_element()); |
| 337 new_password->username_value = | 337 new_password->username_value = |
| 338 UTF8ToUTF16(password.username_value()); | 338 UTF8ToUTF16(password.username_value()); |
| 339 new_password->password_value = | 339 new_password->password_value = |
| 340 UTF8ToUTF16(password.password_value()); | 340 UTF8ToUTF16(password.password_value()); |
| 341 new_password->ssl_valid = password.ssl_valid(); | 341 new_password->ssl_valid = password.ssl_valid(); |
| 342 new_password->preferred = password.preferred(); | 342 new_password->preferred = password.preferred(); |
| 343 new_password->date_created = | 343 new_password->date_created = |
| 344 base::Time::FromInternalValue(password.date_created()); | 344 base::Time::FromInternalValue(password.date_created()); |
| 345 new_password->blacklisted_by_user = | 345 new_password->blacklisted_by_user = |
| 346 password.blacklisted(); | 346 password.blacklisted(); |
| 347 } | 347 } |
| 348 | 348 |
| 349 // static | 349 // static |
| 350 bool PasswordModelAssociator::MergePasswords( | 350 bool PasswordModelAssociator::MergePasswords( |
| 351 const sync_pb::PasswordSpecificsData& password, | 351 const sync_pb::PasswordSpecificsData& password, |
| 352 const content::PasswordForm& password_form, | 352 const autofill::PasswordForm& password_form, |
| 353 content::PasswordForm* new_password) { | 353 autofill::PasswordForm* new_password) { |
| 354 DCHECK(new_password); | 354 DCHECK(new_password); |
| 355 | 355 |
| 356 if (password.scheme() == password_form.scheme && | 356 if (password.scheme() == password_form.scheme && |
| 357 password_form.signon_realm == password.signon_realm() && | 357 password_form.signon_realm == password.signon_realm() && |
| 358 password_form.origin.spec() == password.origin() && | 358 password_form.origin.spec() == password.origin() && |
| 359 password_form.action.spec() == password.action() && | 359 password_form.action.spec() == password.action() && |
| 360 UTF16ToUTF8(password_form.username_element) == | 360 UTF16ToUTF8(password_form.username_element) == |
| 361 password.username_element() && | 361 password.username_element() && |
| 362 UTF16ToUTF8(password_form.password_element) == | 362 UTF16ToUTF8(password_form.password_element) == |
| 363 password.password_element() && | 363 password.password_element() && |
| (...skipping 14 matching lines...) Expand all Loading... |
| 378 *new_password = password_form; | 378 *new_password = password_form; |
| 379 } else { | 379 } else { |
| 380 CopyPassword(password, new_password); | 380 CopyPassword(password, new_password); |
| 381 } | 381 } |
| 382 | 382 |
| 383 return true; | 383 return true; |
| 384 } | 384 } |
| 385 | 385 |
| 386 // static | 386 // static |
| 387 void PasswordModelAssociator::WriteToSyncNode( | 387 void PasswordModelAssociator::WriteToSyncNode( |
| 388 const content::PasswordForm& password_form, | 388 const autofill::PasswordForm& password_form, |
| 389 syncer::WriteNode* node) { | 389 syncer::WriteNode* node) { |
| 390 sync_pb::PasswordSpecificsData password; | 390 sync_pb::PasswordSpecificsData password; |
| 391 password.set_scheme(password_form.scheme); | 391 password.set_scheme(password_form.scheme); |
| 392 password.set_signon_realm(password_form.signon_realm); | 392 password.set_signon_realm(password_form.signon_realm); |
| 393 password.set_origin(password_form.origin.spec()); | 393 password.set_origin(password_form.origin.spec()); |
| 394 password.set_action(password_form.action.spec()); | 394 password.set_action(password_form.action.spec()); |
| 395 password.set_username_element(UTF16ToUTF8(password_form.username_element)); | 395 password.set_username_element(UTF16ToUTF8(password_form.username_element)); |
| 396 password.set_password_element(UTF16ToUTF8(password_form.password_element)); | 396 password.set_password_element(UTF16ToUTF8(password_form.password_element)); |
| 397 password.set_username_value(UTF16ToUTF8(password_form.username_value)); | 397 password.set_username_value(UTF16ToUTF8(password_form.username_value)); |
| 398 password.set_password_value(UTF16ToUTF8(password_form.password_value)); | 398 password.set_password_value(UTF16ToUTF8(password_form.password_value)); |
| 399 password.set_ssl_valid(password_form.ssl_valid); | 399 password.set_ssl_valid(password_form.ssl_valid); |
| 400 password.set_preferred(password_form.preferred); | 400 password.set_preferred(password_form.preferred); |
| 401 password.set_date_created(password_form.date_created.ToInternalValue()); | 401 password.set_date_created(password_form.date_created.ToInternalValue()); |
| 402 password.set_blacklisted(password_form.blacklisted_by_user); | 402 password.set_blacklisted(password_form.blacklisted_by_user); |
| 403 | 403 |
| 404 node->SetPasswordSpecifics(password); | 404 node->SetPasswordSpecifics(password); |
| 405 } | 405 } |
| 406 | 406 |
| 407 // static | 407 // static |
| 408 std::string PasswordModelAssociator::MakeTag( | 408 std::string PasswordModelAssociator::MakeTag( |
| 409 const content::PasswordForm& password) { | 409 const autofill::PasswordForm& password) { |
| 410 return MakeTag(password.origin.spec(), | 410 return MakeTag(password.origin.spec(), |
| 411 UTF16ToUTF8(password.username_element), | 411 UTF16ToUTF8(password.username_element), |
| 412 UTF16ToUTF8(password.username_value), | 412 UTF16ToUTF8(password.username_value), |
| 413 UTF16ToUTF8(password.password_element), | 413 UTF16ToUTF8(password.password_element), |
| 414 password.signon_realm); | 414 password.signon_realm); |
| 415 } | 415 } |
| 416 | 416 |
| 417 // static | 417 // static |
| 418 std::string PasswordModelAssociator::MakeTag( | 418 std::string PasswordModelAssociator::MakeTag( |
| 419 const sync_pb::PasswordSpecificsData& password) { | 419 const sync_pb::PasswordSpecificsData& password) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 432 const std::string& password_element, | 432 const std::string& password_element, |
| 433 const std::string& signon_realm) { | 433 const std::string& signon_realm) { |
| 434 return net::EscapePath(origin_url) + "|" + | 434 return net::EscapePath(origin_url) + "|" + |
| 435 net::EscapePath(username_element) + "|" + | 435 net::EscapePath(username_element) + "|" + |
| 436 net::EscapePath(username_value) + "|" + | 436 net::EscapePath(username_value) + "|" + |
| 437 net::EscapePath(password_element) + "|" + | 437 net::EscapePath(password_element) + "|" + |
| 438 net::EscapePath(signon_realm); | 438 net::EscapePath(signon_realm); |
| 439 } | 439 } |
| 440 | 440 |
| 441 } // namespace browser_sync | 441 } // namespace browser_sync |
| OLD | NEW |