Home | History | Annotate | Download | only in browser
      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 "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"
     24 
     25 namespace autofill {
     26 struct PasswordForm;
     27 }
     28 
     29 namespace syncer {
     30 class SyncErrorFactory;
     31 }
     32 
     33 namespace password_manager {
     34 
     35 class PasswordStoreSync;
     36 
     37 class PasswordSyncableService : public syncer::SyncableService,
     38                                 public base::NonThreadSafe {
     39  public:
     40   // |PasswordSyncableService| is owned by |PasswordStore|.
     41   explicit PasswordSyncableService(PasswordStoreSync* password_store);
     42   virtual ~PasswordSyncableService();
     43 
     44   // syncer::SyncableServiceImplementations
     45   virtual syncer::SyncMergeResult MergeDataAndStartSyncing(
     46       syncer::ModelType type,
     47       const syncer::SyncDataList& initial_sync_data,
     48       scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
     49       scoped_ptr<syncer::SyncErrorFactory> error_handler) OVERRIDE;
     50   virtual void StopSyncing(syncer::ModelType type) OVERRIDE;
     51   virtual syncer::SyncDataList GetAllSyncData(
     52       syncer::ModelType type) const OVERRIDE;
     53   virtual syncer::SyncError ProcessSyncChanges(
     54       const tracked_objects::Location& from_here,
     55       const syncer::SyncChangeList& change_list) OVERRIDE;
     56 
     57   // Notifies sync of changes to the password database.
     58   void ActOnPasswordStoreChanges(const PasswordStoreChangeList& changes);
     59 
     60   // Provides a StartSyncFlare to the SyncableService. See
     61   // sync_start_util for more.
     62   void InjectStartSyncFlare(
     63       const syncer::SyncableService::StartSyncFlare& flare);
     64 
     65  private:
     66   typedef std::vector<autofill::PasswordForm*> PasswordForms;
     67   // Map from password sync tag to password form.
     68   typedef std::map<std::string, autofill::PasswordForm*> PasswordEntryMap;
     69 
     70   // Helper function to retrieve the entries from password db and fill both
     71   // |password_entries| and |passwords_entry_map|. |passwords_entry_map| can be
     72   // NULL.
     73   bool ReadFromPasswordStore(
     74       ScopedVector<autofill::PasswordForm>* password_entries,
     75       PasswordEntryMap* passwords_entry_map) const;
     76 
     77   // Uses the |PasswordStore| APIs to change entries.
     78   void WriteToPasswordStore(const PasswordForms& new_entries,
     79                             const PasswordForms& updated_entries,
     80                             const PasswordForms& deleted_entries);
     81 
     82   // Notifies password store of a change that was performed by sync.
     83   // Virtual so tests can override.
     84   virtual void NotifyPasswordStoreOfLoginChanges(
     85       const PasswordStoreChangeList& changes);
     86 
     87   // Checks if |data|, the entry in sync db, needs to be created or updated
     88   // in the passwords db. Depending on what action needs to be performed, the
     89   // entry may be added to |new_sync_entries| or to |updated_sync_entries|. If
     90   // the item is identical to an entry in the passwords db, no action is
     91   // performed. If an item needs to be updated in the sync db, then the item is
     92   // also added to |updated_db_entries| list. If |data|'s tag is identical to
     93   // an entry's tag in |umatched_data_from_password_db| then that entry will be
     94   // removed from |umatched_data_from_password_db|.
     95   void CreateOrUpdateEntry(
     96       const syncer::SyncData& data,
     97       PasswordEntryMap* umatched_data_from_password_db,
     98       ScopedVector<autofill::PasswordForm>* new_sync_entries,
     99       ScopedVector<autofill::PasswordForm>* updated_sync_entries,
    100       syncer::SyncChangeList* updated_db_entries);
    101 
    102   // The factory that creates sync errors. |SyncError| has rich data
    103   // suitable for debugging.
    104   scoped_ptr<syncer::SyncErrorFactory> sync_error_factory_;
    105 
    106   // |SyncProcessor| will mirror the |PasswordStore| changes in the sync db.
    107   scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
    108 
    109   // The password store that adds/updates/deletes password entries.
    110   PasswordStoreSync* const password_store_;
    111 
    112   // A signal to start sync as soon as possible.
    113   syncer::SyncableService::StartSyncFlare flare_;
    114 
    115   // True if processing sync changes is in progress.
    116   bool is_processing_sync_changes_;
    117 
    118   DISALLOW_COPY_AND_ASSIGN(PasswordSyncableService);
    119 };
    120 
    121 // Converts the |password| into a SyncData object.
    122 syncer::SyncData SyncDataFromPassword(const autofill::PasswordForm& password);
    123 
    124 // Extracts the |PasswordForm| data from sync's protobuffer format.
    125 void PasswordFromSpecifics(const sync_pb::PasswordSpecificsData& password,
    126                            autofill::PasswordForm* new_password);
    127 
    128 // Returns the unique tag that will serve as the sync identifier for the
    129 // |password| entry.
    130 std::string MakePasswordSyncTag(const sync_pb::PasswordSpecificsData& password);
    131 
    132 }  // namespace password_manager
    133 
    134 #endif  // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_SYNCABLE_SERVICE_H__
    135