1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/password_manager/password_syncable_service.h"
7 #include "base/location.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/password_manager/password_store.h"
10 #include "components/autofill/core/common/password_form.h"
11 #include "net/base/escape.h"
12 #include "sync/api/sync_error_factory.h"
14 PasswordSyncableService::PasswordSyncableService(
15 scoped_refptr
<PasswordStore
> password_store
)
16 : password_store_(password_store
) {
19 PasswordSyncableService::~PasswordSyncableService() {}
21 syncer::SyncMergeResult
22 PasswordSyncableService::MergeDataAndStartSyncing(
23 syncer::ModelType type
,
24 const syncer::SyncDataList
& initial_sync_data
,
25 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor
,
26 scoped_ptr
<syncer::SyncErrorFactory
> sync_error_factory
) {
27 syncer::SyncMergeResult
merge_result(type
);
28 sync_error_factory_
= sync_error_factory
.Pass();
29 sync_processor_
= sync_processor
.Pass();
31 merge_result
.set_error(sync_error_factory
->CreateAndUploadError(
33 "Password Syncable Service Not Implemented."));
37 void PasswordSyncableService::StopSyncing(syncer::ModelType type
) {
40 syncer::SyncDataList
PasswordSyncableService::GetAllSyncData(
41 syncer::ModelType type
) const {
42 syncer::SyncDataList sync_data
;
46 syncer::SyncError
PasswordSyncableService::ProcessSyncChanges(
47 const tracked_objects::Location
& from_here
,
48 const syncer::SyncChangeList
& change_list
) {
49 syncer::SyncError
error(FROM_HERE
,
50 syncer::SyncError::UNRECOVERABLE_ERROR
,
51 "Password Syncable Service Not Implemented.",
56 void PasswordSyncableService::WriteToPasswordStore(
57 PasswordForms
* new_entries
,
58 PasswordForms
* updated_entries
) {
59 for (std::vector
<autofill::PasswordForm
*>::const_iterator it
=
61 it
!= new_entries
->end();
63 password_store_
->AddLoginImpl(**it
);
66 for (std::vector
<autofill::PasswordForm
*>::const_iterator it
=
67 updated_entries
->begin();
68 it
!= updated_entries
->end();
70 password_store_
->UpdateLoginImpl(**it
);
73 if (!new_entries
->empty() || !updated_entries
->empty()) {
74 // We have to notify password store observers of the change by hand since
75 // we use internal password store interfaces to make changes synchronously.
76 password_store_
->PostNotifyLoginsChanged();
80 syncer::SyncData
PasswordSyncableService::CreateSyncData(
81 const autofill::PasswordForm
& password_form
) {
82 sync_pb::EntitySpecifics password_data
;
83 sync_pb::PasswordSpecificsData
* password_specifics
=
84 password_data
.mutable_password()->mutable_client_only_encrypted_data();
85 password_specifics
->set_scheme(password_form
.scheme
);
86 password_specifics
->set_signon_realm(password_form
.signon_realm
);
87 password_specifics
->set_origin(password_form
.origin
.spec());
88 password_specifics
->set_action(password_form
.action
.spec());
89 password_specifics
->set_username_element(
90 base::UTF16ToUTF8(password_form
.username_element
));
91 password_specifics
->set_password_element(
92 base::UTF16ToUTF8(password_form
.password_element
));
93 password_specifics
->set_username_value(
94 base::UTF16ToUTF8(password_form
.username_value
));
95 password_specifics
->set_password_value(
96 base::UTF16ToUTF8(password_form
.password_value
));
97 password_specifics
->set_ssl_valid(password_form
.ssl_valid
);
98 password_specifics
->set_preferred(password_form
.preferred
);
99 password_specifics
->set_date_created(
100 password_form
.date_created
.ToInternalValue());
101 password_specifics
->set_blacklisted(password_form
.blacklisted_by_user
);
103 std::string tag
= MakeTag(*password_specifics
);
104 return syncer::SyncData::CreateLocalData(tag
, tag
, password_data
);
108 std::string
PasswordSyncableService::MakeTag(
109 const std::string
& origin_url
,
110 const std::string
& username_element
,
111 const std::string
& username_value
,
112 const std::string
& password_element
,
113 const std::string
& signon_realm
) {
114 return net::EscapePath(origin_url
) + "|" +
115 net::EscapePath(username_element
) + "|" +
116 net::EscapePath(username_value
) + "|" +
117 net::EscapePath(password_element
) + "|" +
118 net::EscapePath(signon_realm
);
122 std::string
PasswordSyncableService::MakeTag(
123 const autofill::PasswordForm
& password
) {
124 return MakeTag(password
.origin
.spec(),
125 base::UTF16ToUTF8(password
.username_element
),
126 base::UTF16ToUTF8(password
.username_value
),
127 base::UTF16ToUTF8(password
.password_element
),
128 password
.signon_realm
);
132 std::string
PasswordSyncableService::MakeTag(
133 const sync_pb::PasswordSpecificsData
& password
) {
134 return MakeTag(password
.origin(),
135 password
.username_element(),
136 password
.username_value(),
137 password
.password_element(),
138 password
.signon_realm());