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