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 #ifndef CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_SYNCABLE_SERVICE_H__
6 #define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_SYNCABLE_SERVICE_H__
11 #include "base/memory/scoped_ptr.h"
12 #include "components/password_manager/core/browser/password_store_change.h"
13 #include "sync/api/sync_change.h"
14 #include "sync/api/sync_data.h"
15 #include "sync/api/sync_error.h"
16 #include "sync/api/syncable_service.h"
17 #include "sync/protocol/password_specifics.pb.h"
18 #include "sync/protocol/sync.pb.h"
25 class SyncErrorFactory
;
30 class PasswordSyncableService
: public syncer::SyncableService
{
32 // TODO(lipalani) - The |PasswordStore| should outlive
33 // |PasswordSyncableService| and there should be a code
34 // guarantee to that effect. Currently this object is not instantiated.
35 // When this class is completed and instantiated the object lifetime
36 // guarantee will be implemented.
37 explicit PasswordSyncableService(
38 scoped_refptr
<PasswordStore
> password_store
);
39 virtual ~PasswordSyncableService();
41 // syncer::SyncableServiceImplementations
42 virtual syncer::SyncMergeResult
MergeDataAndStartSyncing(
43 syncer::ModelType type
,
44 const syncer::SyncDataList
& initial_sync_data
,
45 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor
,
46 scoped_ptr
<syncer::SyncErrorFactory
> error_handler
) OVERRIDE
;
47 virtual void StopSyncing(syncer::ModelType type
) OVERRIDE
;
48 virtual syncer::SyncDataList
GetAllSyncData(
49 syncer::ModelType type
) const OVERRIDE
;
50 virtual syncer::SyncError
ProcessSyncChanges(
51 const tracked_objects::Location
& from_here
,
52 const syncer::SyncChangeList
& change_list
) OVERRIDE
;
54 // Notifies sync of changes to the password database.
55 void ActOnPasswordStoreChanges(const PasswordStoreChangeList
& changes
);
58 typedef std::vector
<autofill::PasswordForm
*> PasswordForms
;
59 // Map from password sync tag to password form.
60 typedef std::map
<std::string
, autofill::PasswordForm
*> PasswordEntryMap
;
62 // Helper function to retrieve the entries from password db and fill both
63 // |password_entries| and |passwords_entry_map|. |passwords_entry_map| can be
65 bool ReadFromPasswordStore(
66 ScopedVector
<autofill::PasswordForm
>* password_entries
,
67 PasswordEntryMap
* passwords_entry_map
) const;
69 // Uses the |PasswordStore| APIs to change entries.
70 void WriteToPasswordStore(const PasswordForms
& new_entries
,
71 const PasswordForms
& updated_entries
,
72 const PasswordForms
& deleted_entries
);
74 // Notifies password store of a change that was performed by sync.
75 // Virtual so tests can override.
76 virtual void NotifyPasswordStoreOfLoginChanges(
77 const PasswordStoreChangeList
& changes
);
79 // Checks if |data|, the entry in sync db, needs to be created or updated
80 // in the passwords db. Depending on what action needs to be performed, the
81 // entry may be added to |new_sync_entries| or to |updated_sync_entries|. If
82 // the item is identical to an entry in the passwords db, no action is
83 // performed. If an item needs to be updated in the sync db, then the item is
84 // also added to |updated_db_entries| list. If |data|'s tag is identical to
85 // an entry's tag in |umatched_data_from_password_db| then that entry will be
86 // removed from |umatched_data_from_password_db|.
87 void CreateOrUpdateEntry(
88 const syncer::SyncData
& data
,
89 PasswordEntryMap
* umatched_data_from_password_db
,
90 ScopedVector
<autofill::PasswordForm
>* new_sync_entries
,
91 ScopedVector
<autofill::PasswordForm
>* updated_sync_entries
,
92 syncer::SyncChangeList
* updated_db_entries
);
94 // The factory that creates sync errors. |SyncError| has rich data
95 // suitable for debugging.
96 scoped_ptr
<syncer::SyncErrorFactory
> sync_error_factory_
;
98 // |SyncProcessor| will mirror the |PasswordStore| changes in the sync db.
99 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor_
;
101 // The password store that adds/updates/deletes password entries.
102 scoped_refptr
<PasswordStore
> password_store_
;
105 // Converts the |password| into a SyncData object.
106 syncer::SyncData
SyncDataFromPassword(const autofill::PasswordForm
& password
);
108 // Extracts the |PasswordForm| data from sync's protobuffer format.
109 void PasswordFromSpecifics(const sync_pb::PasswordSpecificsData
& password
,
110 autofill::PasswordForm
* new_password
);
112 // Returns the unique tag that will serve as the sync identifier for the
114 std::string
MakePasswordSyncTag(const sync_pb::PasswordSpecificsData
& password
);
116 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_SYNCABLE_SERVICE_H__