Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1172)

Unified Diff: chrome/browser/password_manager/password_syncable_service.cc

Issue 27233003: [Sync] Implementation of model association for passwords using sync API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: For review. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/password_manager/password_syncable_service.cc
diff --git a/chrome/browser/password_manager/password_syncable_service.cc b/chrome/browser/password_manager/password_syncable_service.cc
index 67355e2a9668629a1af517a96612ca85160b350c..9663e279e4269a6a0d961746ab3aaeeb5a1351a9 100644
--- a/chrome/browser/password_manager/password_syncable_service.cc
+++ b/chrome/browser/password_manager/password_syncable_service.cc
@@ -5,6 +5,9 @@
#include "chrome/browser/password_manager/password_syncable_service.h"
#include "base/location.h"
+#include "base/memory/scoped_vector.h"
+#include "base/metrics/histogram.h"
+#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/password_manager/password_store.h"
#include "components/autofill/core/common/password_form.h"
@@ -13,32 +16,6 @@
namespace {
-// Converts the |PasswordSpecifics| obtained from sync to an
-// object of type |PasswordForm|.
-void ExtractPasswordFromSpecifics(
- const sync_pb::PasswordSpecificsData& password,
- autofill::PasswordForm* new_password) {
- new_password->scheme =
- static_cast<autofill::PasswordForm::Scheme>(password.scheme());
- new_password->signon_realm = password.signon_realm();
- new_password->origin = GURL(password.origin());
- new_password->action = GURL(password.action());
- new_password->username_element =
- UTF8ToUTF16(password.username_element());
- new_password->password_element =
- UTF8ToUTF16(password.password_element());
- new_password->username_value =
- UTF8ToUTF16(password.username_value());
- new_password->password_value =
- UTF8ToUTF16(password.password_value());
- new_password->ssl_valid = password.ssl_valid();
- new_password->preferred = password.preferred();
- new_password->date_created =
- base::Time::FromInternalValue(password.date_created());
- new_password->blacklisted_by_user =
- password.blacklisted();
-}
-
// Merges the sync password (obtained from the password specifics) and
// local password and stores the output in the |new_password_form| pointer.
bool MergeLocalAndSyncPasswords(
@@ -71,7 +48,9 @@ bool MergeLocalAndSyncPasswords(
password_form.date_created) {
*new_password_form = password_form;
} else {
- ExtractPasswordFromSpecifics(password_specifics, new_password_form);
+ PasswordSyncableService::ExtractPasswordFromSpecifics(
+ password_specifics,
+ new_password_form);
}
return true;
@@ -86,6 +65,50 @@ PasswordSyncableService::PasswordSyncableService(
PasswordSyncableService::~PasswordSyncableService() {}
+void PasswordSyncableService::CreateOrUpdateEntry(
+ const syncer::SyncData& data,
+ PasswordEntryMap* loaded_data,
+ ScopedVector<autofill::PasswordForm>* new_entries,
+ ScopedVector<autofill::PasswordForm>* updated_entries,
+ syncer::SyncChangeList* updated_db_entries) {
+ const sync_pb::EntitySpecifics& specifics = data.GetSpecifics();
+ const sync_pb::PasswordSpecificsData& password_specifics(
+ specifics.password().client_only_encrypted_data());
+
+ std::string tag = MakeTag(password_specifics);
+
+ // Find if the data from sync is already in password store.
+ PasswordEntryMap::iterator it = loaded_data->find(tag);
+
+ if (it == loaded_data->end()) {
+ // The sync data is not in password store, so we need to create it in
+ // password store. Add the entry to the new_entries list.
+ autofill::PasswordForm* new_password = new autofill::PasswordForm();
+ ExtractPasswordFromSpecifics(password_specifics, new_password);
+ // The new_password pointer is owned by the caller.
+ new_entries->push_back(new_password);
+ } else {
+ // The entry is in password store. If the entries are not identical the
+ // entries need to be merged.
+ scoped_ptr<autofill::PasswordForm> new_password(
+ new autofill::PasswordForm());
+ if (MergeLocalAndSyncPasswords(password_specifics,
+ **(it->second),
+ new_password.get())) {
+ updated_db_entries->push_back(
+ syncer::SyncChange(FROM_HERE,
+ syncer::SyncChange::ACTION_UPDATE,
+ CreateSyncData(*(new_password.get()))));
+
+ updated_entries->push_back(new_password.release());
+ }
+ // Remove the entry from the entry map to indicate a match has been found.
+ // Entries that remain in the map at the end of associating all sync entries
+ // will be treated as additions that needs to be propagated to sync.
+ loaded_data->erase(it);
+ }
+}
+
syncer::SyncMergeResult
PasswordSyncableService::MergeDataAndStartSyncing(
syncer::ModelType type,
@@ -96,9 +119,72 @@ PasswordSyncableService::MergeDataAndStartSyncing(
sync_error_factory_ = sync_error_factory.Pass();
sync_processor_ = sync_processor.Pass();
- merge_result.set_error(sync_error_factory->CreateAndUploadError(
- FROM_HERE,
- "Password Syncable Service Not Implemented."));
+ ScopedVector<autofill::PasswordForm> password_entries;
+ if (!password_store_->FillAutofillableLogins(&(password_entries.get())) ||
+ !password_store_->FillBlacklistLogins(&(password_entries.get()))) {
+ // Password store often fails to load passwords. Track failures with UMA.
+ // (http://crbug.com/249000)
+ UMA_HISTOGRAM_ENUMERATION("Sync.LocalDataFailedToLoad",
+ ModelTypeToHistogramInt(syncer::PASSWORDS),
+ syncer::MODEL_TYPE_COUNT);
+ merge_result.set_error(sync_error_factory->CreateAndUploadError(
+ FROM_HERE,
+ "Failed to get passwords from store."));
+ return merge_result;;
+ }
+
+ PasswordEntryMap new_db_entries;
+ for (std::vector<autofill::PasswordForm*>::iterator
+ it = password_entries.begin();
+ it != password_entries.end();
+ ++it) {
+ // We add all the db entries as |new_db_entries| initially. While
+ // association entries with matching sync entries will be removed and this
+ // list will only contain entries that are not in sync.
+ new_db_entries[MakeTag(**it)] = it;
+ }
+
+ merge_result.set_num_items_before_association(new_db_entries.size());
+
+ // List that contains the entries that are known only to sync.
+ ScopedVector<autofill::PasswordForm> new_sync_entries;
+
+ // List that contains the entries that are known to both sync and db but
+ // have to be updated in sync.
+ ScopedVector<autofill::PasswordForm> updated_sync_entries;
+
+ // Changes from password db that needs to be propagated to sync.
+ syncer::SyncChangeList db_changes;
+ for (syncer::SyncDataList::const_iterator sync_iter =
+ initial_sync_data.begin();
+ sync_iter != initial_sync_data.end(); ++sync_iter) {
+ CreateOrUpdateEntry(*sync_iter,
+ &new_db_entries,
+ &new_sync_entries,
+ &updated_sync_entries,
+ &db_changes);
+ }
+
+ WriteToPasswordStore(new_sync_entries.get(),
+ updated_sync_entries.get());
+
+ merge_result.set_num_items_after_association(
+ merge_result.num_items_before_association() + new_sync_entries.size());
+
+ merge_result.set_num_items_added(new_sync_entries.size());
+
+ merge_result.set_num_items_modified(updated_sync_entries.size());
+
+ for (PasswordEntryMap::iterator it = new_db_entries.begin();
+ it != new_db_entries.end();
+ ++it) {
+ db_changes.push_back(syncer::SyncChange(FROM_HERE,
+ syncer::SyncChange::ACTION_ADD,
+ CreateSyncData(**(it->second))));
+ }
+
+ merge_result.set_error(
+ sync_processor_->ProcessSyncChanges(FROM_HERE, db_changes));
return merge_result;
}
@@ -122,29 +208,33 @@ syncer::SyncError PasswordSyncableService::ProcessSyncChanges(
}
void PasswordSyncableService::WriteToPasswordStore(
- PasswordForms* new_entries,
- PasswordForms* updated_entries) {
+ const PasswordForms& new_entries,
+ const PasswordForms& updated_entries) {
for (std::vector<autofill::PasswordForm*>::const_iterator it =
- new_entries->begin();
- it != new_entries->end();
+ new_entries.begin();
+ it != new_entries.end();
++it) {
password_store_->AddLoginImpl(**it);
}
for (std::vector<autofill::PasswordForm*>::const_iterator it =
- updated_entries->begin();
- it != updated_entries->end();
+ updated_entries.begin();
+ it != updated_entries.end();
++it) {
password_store_->UpdateLoginImpl(**it);
}
- if (!new_entries->empty() || !updated_entries->empty()) {
+ if (!new_entries.empty() || !updated_entries.empty()) {
// We have to notify password store observers of the change by hand since
// we use internal password store interfaces to make changes synchronously.
- password_store_->PostNotifyLoginsChanged();
+ NotifyPasswordStore();
}
}
+void PasswordSyncableService::NotifyPasswordStore() {
+ password_store_->PostNotifyLoginsChanged();
+}
+
syncer::SyncData PasswordSyncableService::CreateSyncData(
const autofill::PasswordForm& password_form) {
sync_pb::EntitySpecifics password_data;
@@ -206,3 +296,28 @@ std::string PasswordSyncableService::MakeTag(
password.signon_realm());
}
+// static
+void PasswordSyncableService::ExtractPasswordFromSpecifics(
+ const sync_pb::PasswordSpecificsData& password,
+ autofill::PasswordForm* new_password) {
+ new_password->scheme =
+ static_cast<autofill::PasswordForm::Scheme>(password.scheme());
+ new_password->signon_realm = password.signon_realm();
+ new_password->origin = GURL(password.origin());
+ new_password->action = GURL(password.action());
+ new_password->username_element =
+ UTF8ToUTF16(password.username_element());
+ new_password->password_element =
+ UTF8ToUTF16(password.password_element());
+ new_password->username_value =
+ UTF8ToUTF16(password.username_value());
+ new_password->password_value =
+ UTF8ToUTF16(password.password_value());
+ new_password->ssl_valid = password.ssl_valid();
+ new_password->preferred = password.preferred();
+ new_password->date_created =
+ base::Time::FromInternalValue(password.date_created());
+ new_password->blacklisted_by_user =
+ password.blacklisted();
+}
+

Powered by Google App Engine
This is Rietveld 408576698