Home | History | Annotate | Download | only in glue
      1 // Copyright (c) 2011 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 CHROME_BROWSER_SYNC_GLUE_AUTOFILL_CHANGE_PROCESSOR_H_
      6 #define CHROME_BROWSER_SYNC_GLUE_AUTOFILL_CHANGE_PROCESSOR_H_
      7 #pragma once
      8 
      9 #include <vector>
     10 
     11 #include "chrome/browser/autofill/autofill_profile.h"
     12 #include "chrome/browser/autofill/credit_card.h"
     13 #include "chrome/browser/autofill/personal_data_manager.h"
     14 #include "chrome/browser/sync/engine/syncapi.h"
     15 #include "chrome/browser/sync/glue/change_processor.h"
     16 #include "chrome/browser/sync/glue/sync_backend_host.h"
     17 #include "chrome/browser/sync/protocol/autofill_specifics.pb.h"
     18 #include "chrome/browser/webdata/web_data_service.h"
     19 #include "content/common/notification_observer.h"
     20 #include "content/common/notification_registrar.h"
     21 
     22 class AutofillCreditCardChange;
     23 class AutofillEntry;
     24 class AutofillProfileChange;
     25 class PersonalDataManager;
     26 class WebDatabase;
     27 
     28 namespace browser_sync {
     29 
     30 class AutofillModelAssociator;
     31 class UnrecoverableErrorHandler;
     32 
     33 // This class is responsible for taking changes from the web data service and
     34 // applying them to the sync_api 'syncable' model, and vice versa. All
     35 // operations and use of this class are from the DB thread.
     36 class AutofillChangeProcessor : public ChangeProcessor,
     37                                 public NotificationObserver {
     38  public:
     39   AutofillChangeProcessor(AutofillModelAssociator* model_associator,
     40                           WebDatabase* web_database,
     41                           PersonalDataManager* personal_data,
     42                           UnrecoverableErrorHandler* error_handler);
     43   virtual ~AutofillChangeProcessor();
     44 
     45   // NotificationObserver implementation.
     46   // WebDataService -> sync_api model change application.
     47   virtual void Observe(NotificationType type,
     48                        const NotificationSource& source,
     49                        const NotificationDetails& details);
     50 
     51   // sync_api model -> WebDataService change application.
     52   virtual void ApplyChangesFromSyncModel(
     53       const sync_api::BaseTransaction* trans,
     54       const sync_api::SyncManager::ChangeRecord* changes,
     55       int change_count);
     56 
     57   // Commit any changes from ApplyChangesFromSyncModel buffered in
     58   // autofill_changes_.
     59   virtual void CommitChangesFromSyncModel();
     60 
     61   // Copy the properties of the given Autofill entry into the sync
     62   // node.
     63   static void WriteAutofillEntry(const AutofillEntry& entry,
     64                                  sync_api::WriteNode* node);
     65   // TODO(georgey) : add the same processing for CC info (already in protocol
     66   // buffers).
     67 
     68  protected:
     69   virtual void StartImpl(Profile* profile);
     70   virtual void StopImpl();
     71 
     72  private:
     73   void StartObserving();
     74   void StopObserving();
     75 
     76   // A function to remove the sync node for an autofill entry or profile.
     77   void RemoveSyncNode(const std::string& tag,
     78                       sync_api::WriteTransaction* trans);
     79 
     80   // These two methods are dispatched to by Observe depending on the type.
     81   void ObserveAutofillEntriesChanged(AutofillChangeList* changes,
     82       sync_api::WriteTransaction* trans,
     83       const sync_api::ReadNode& autofill_root);
     84   void ObserveAutofillProfileChanged(AutofillProfileChange* change,
     85       sync_api::WriteTransaction* trans,
     86       const sync_api::ReadNode& autofill_root);
     87 
     88   // The following methods are the implementation of ApplyChangeFromSyncModel
     89   // for the respective autofill subtypes.
     90   void ApplySyncAutofillEntryChange(
     91       sync_api::SyncManager::ChangeRecord::Action action,
     92       const sync_pb::AutofillSpecifics& autofill,
     93       std::vector<AutofillEntry>* new_entries,
     94       int64 sync_id);
     95   void ApplySyncAutofillProfileChange(
     96       sync_api::SyncManager::ChangeRecord::Action action,
     97       const sync_pb::AutofillProfileSpecifics& profile,
     98       int64 sync_id);
     99 
    100   // Delete is a special case of change application.
    101   void ApplySyncAutofillEntryDelete(
    102       const sync_pb::AutofillSpecifics& autofill);
    103   void ApplySyncAutofillProfileDelete(
    104       int64 sync_id);
    105 
    106   // Helper to post a task to the UI loop to inform the PersonalDataManager
    107   // it needs to refresh itself.
    108   void PostOptimisticRefreshTask();
    109 
    110   // Called to see if we need to upgrade to the new autofill2 profile.
    111   bool HasNotMigratedYet(const sync_api::BaseTransaction* trans);
    112 
    113   // The two models should be associated according to this ModelAssociator.
    114   AutofillModelAssociator* model_associator_;
    115 
    116   // The model we are processing changes from.  This is owned by the
    117   // WebDataService which is kept alive by our data type controller
    118   // holding a reference.
    119   WebDatabase* web_database_;
    120 
    121   // We periodically tell the PersonalDataManager to refresh as we make
    122   // changes to the autofill data in the WebDatabase.
    123   PersonalDataManager* personal_data_;
    124 
    125   NotificationRegistrar notification_registrar_;
    126 
    127   bool observing_;
    128 
    129   // Record of changes from ApplyChangesFromSyncModel. These are then processed
    130   // in CommitChangesFromSyncModel.
    131   struct AutofillChangeRecord;
    132   std::vector<AutofillChangeRecord> autofill_changes_;
    133 
    134   DISALLOW_COPY_AND_ASSIGN(AutofillChangeProcessor);
    135 };
    136 
    137 }  // namespace browser_sync
    138 
    139 #endif  // CHROME_BROWSER_SYNC_GLUE_AUTOFILL_CHANGE_PROCESSOR_H_
    140