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_PROFILE_CHANGE_PROCESSOR_H_ 6 #define CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_CHANGE_PROCESSOR_H_ 7 #pragma once 8 #include <string> 9 #include <vector> 10 11 #include "chrome/browser/autofill/autofill_profile.h" 12 #include "chrome/browser/autofill/personal_data_manager.h" 13 #include "chrome/browser/sync/engine/syncapi.h" 14 #include "chrome/browser/sync/glue/autofill_profile_model_associator.h" 15 #include "chrome/browser/sync/glue/change_processor.h" 16 #include "chrome/browser/sync/unrecoverable_error_handler.h" 17 #include "chrome/browser/webdata/autofill_change.h" 18 #include "chrome/browser/webdata/web_database.h" 19 #include "content/common/notification_observer.h" 20 #include "content/common/notification_registrar.h" 21 #include "content/common/notification_service.h" 22 #include "content/common/notification_type.h" 23 24 namespace browser_sync { 25 26 class AutofillProfileChangeProcessor : public ChangeProcessor, 27 public NotificationObserver { 28 public: 29 AutofillProfileChangeProcessor( 30 AutofillProfileModelAssociator *model_associator, 31 WebDatabase* web_database, 32 PersonalDataManager* personal_data_manager, 33 UnrecoverableErrorHandler* error_handler); 34 35 virtual ~AutofillProfileChangeProcessor(); 36 37 // Virtual methods from ChangeProcessor class. 38 virtual void ApplyChangesFromSyncModel( 39 const sync_api::BaseTransaction *write_trans, 40 const sync_api::SyncManager::ChangeRecord* changes, 41 int change_count); 42 43 virtual void CommitChangesFromSyncModel(); 44 45 // Virtual method implemented for the observer class. 46 virtual void Observe(NotificationType type, 47 const NotificationSource& source, 48 const NotificationDetails& details); 49 50 51 static void WriteAutofillProfile(const AutofillProfile& profile, 52 sync_api::WriteNode* node); 53 54 protected: 55 // Protected methods from ChangeProcessor. StartImpl(Profile * profile)56 virtual void StartImpl(Profile* profile) {} StopImpl()57 virtual void StopImpl() {} 58 59 struct AutofillProfileChangeRecord { 60 sync_api::SyncManager::ChangeRecord::Action action_; 61 int64 id_; 62 sync_pb::AutofillProfileSpecifics profile_specifics_; AutofillProfileChangeRecordAutofillProfileChangeRecord63 AutofillProfileChangeRecord( 64 sync_api::SyncManager::ChangeRecord::Action action, 65 int64 id, 66 const sync_pb::AutofillProfileSpecifics& profile_specifics) 67 : action_(action), 68 id_(id), 69 profile_specifics_(profile_specifics) {} 70 }; 71 72 std::vector<AutofillProfileChangeRecord> autofill_changes_; 73 74 virtual void AddAutofillProfileSyncNode(sync_api::WriteTransaction* trans, 75 sync_api::BaseNode& autofill_profile_root, 76 const AutofillProfile& profile); 77 78 void ActOnChange(AutofillProfileChange* change, 79 sync_api::WriteTransaction* trans, 80 sync_api::ReadNode& autofill_root); 81 82 private: 83 84 // This ensures that startobsrving gets called after stopobserving even 85 // if there is an early return in the function. 86 // TODO(lipalani) - generalize this and add it to other change processors. 87 class ScopedStopObserving { 88 public: 89 explicit ScopedStopObserving(AutofillProfileChangeProcessor* processor); 90 ~ScopedStopObserving(); 91 92 private: ScopedStopObserving()93 ScopedStopObserving() {} 94 AutofillProfileChangeProcessor* processor_; 95 }; 96 97 void StartObserving(); 98 void StopObserving(); 99 100 void PostOptimisticRefreshTask(); 101 102 void ApplyAutofillProfileChange( 103 sync_api::SyncManager::ChangeRecord::Action action, 104 const sync_pb::AutofillProfileSpecifics& profile, 105 int64 sync_id); 106 107 void RemoveSyncNode( 108 const std::string& guid, sync_api::WriteTransaction *trans); 109 110 AutofillProfileModelAssociator* model_associator_; 111 bool observing_; 112 113 WebDatabase* web_database_; 114 PersonalDataManager* personal_data_; 115 NotificationRegistrar notification_registrar_; 116 }; 117 118 } // namespace browser_sync 119 120 #endif // CHROME_BROWSER_SYNC_GLUE_AUTOFILL_PROFILE_CHANGE_PROCESSOR_H_ 121 122