1 // Copyright 2014 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 COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_SYNCABLE_SERVICE_H__
6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_SYNCABLE_SERVICE_H__
8 #if !defined(PASSWORD_MANAGER_ENABLE_SYNC)
9 #error "Only build this file when sync is enabled in Password Manager."
15 #include "base/memory/scoped_ptr.h"
16 #include "base/threading/non_thread_safe.h"
17 #include "components/password_manager/core/browser/password_store_change.h"
18 #include "sync/api/sync_change.h"
19 #include "sync/api/sync_data.h"
20 #include "sync/api/sync_error.h"
21 #include "sync/api/syncable_service.h"
22 #include "sync/protocol/password_specifics.pb.h"
23 #include "sync/protocol/sync.pb.h"
30 class SyncErrorFactory;
33 namespace password_manager {
35 class PasswordStoreSync;
37 // The implementation of the SyncableService API for passwords.
38 class PasswordSyncableService : public syncer::SyncableService,
39 public base::NonThreadSafe {
41 // Since the constructed |PasswordSyncableService| is typically owned by the
42 // |password_store|, the constructor doesn't take ownership of the
44 explicit PasswordSyncableService(PasswordStoreSync* password_store);
45 ~PasswordSyncableService() override;
47 // syncer::SyncableService:
48 syncer::SyncMergeResult MergeDataAndStartSyncing(
49 syncer::ModelType type,
50 const syncer::SyncDataList& initial_sync_data,
51 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
52 scoped_ptr<syncer::SyncErrorFactory> error_handler) override;
53 void StopSyncing(syncer::ModelType type) override;
54 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override;
55 syncer::SyncError ProcessSyncChanges(
56 const tracked_objects::Location& from_here,
57 const syncer::SyncChangeList& change_list) override;
59 // Notifies the Sync engine of changes to the password database.
60 void ActOnPasswordStoreChanges(const PasswordStoreChangeList& changes);
62 // Provides a StartSyncFlare to the SyncableService. See
63 // chrome/browser/sync/glue/sync_start_util.h for more.
64 void InjectStartSyncFlare(
65 const syncer::SyncableService::StartSyncFlare& flare);
68 typedef std::vector<autofill::PasswordForm*> PasswordForms;
69 // Map from password sync tag to password form.
70 typedef std::map<std::string, autofill::PasswordForm*> PasswordEntryMap;
72 // The type of PasswordStoreSync::AddLoginImpl,
73 // PasswordStoreSync::UpdateLoginImpl and PasswordStoreSync::RemoveLoginImpl.
74 typedef PasswordStoreChangeList(PasswordStoreSync::*DatabaseOperation)(
75 const autofill::PasswordForm& form);
79 // Retrieves the entries from password db and fills both |password_entries|
80 // and |passwords_entry_map|. |passwords_entry_map| can be NULL.
81 bool ReadFromPasswordStore(
82 ScopedVector<autofill::PasswordForm>* password_entries,
83 PasswordEntryMap* passwords_entry_map) const;
85 // Uses the |PasswordStore| APIs to change entries.
86 void WriteToPasswordStore(const SyncEntries& entries);
88 // Examines |data|, an entry in sync db, and updates |sync_entries| or
89 // |updated_db_entries| accordingly. An element is removed from
90 // |unmatched_data_from_password_db| if its tag is identical to |data|'s.
91 void CreateOrUpdateEntry(
92 const syncer::SyncData& data,
93 PasswordEntryMap* unmatched_data_from_password_db,
94 SyncEntries* sync_entries,
95 syncer::SyncChangeList* updated_db_entries);
97 // Calls |operation| for each element in |entries| and appends the changes to
99 void WriteEntriesToDatabase(
100 DatabaseOperation operation,
101 const PasswordForms& entries,
102 PasswordStoreChangeList* all_changes);
104 // The factory that creates sync errors. |SyncError| has rich data
105 // suitable for debugging.
106 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory_;
108 // |sync_processor_| will mirror the |PasswordStore| changes in the sync db.
109 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
111 // The password store that adds/updates/deletes password entries. Not owned.
112 PasswordStoreSync* const password_store_;
114 // A signal activated by this class to start sync as soon as possible.
115 syncer::SyncableService::StartSyncFlare flare_;
117 // True if processing sync changes is in progress.
118 bool is_processing_sync_changes_;
120 DISALLOW_COPY_AND_ASSIGN(PasswordSyncableService);
123 } // namespace password_manager
125 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_SYNCABLE_SERVICE_H__