| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 #ifndef CHROME_BROWSER_WEBDATA_AUTOCOMPLETE_SYNCABLE_SERVICE_H_ | 4 #ifndef CHROME_BROWSER_WEBDATA_AUTOCOMPLETE_SYNCABLE_SERVICE_H_ |
| 5 #define CHROME_BROWSER_WEBDATA_AUTOCOMPLETE_SYNCABLE_SERVICE_H_ | 5 #define CHROME_BROWSER_WEBDATA_AUTOCOMPLETE_SYNCABLE_SERVICE_H_ |
| 6 #pragma once | 6 #pragma once |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <utility> | 11 #include <utility> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
| 17 #include "chrome/browser/webdata/autofill_change.h" | 17 #include "chrome/browser/webdata/autofill_change.h" |
| 18 #include "chrome/browser/webdata/autofill_entry.h" | 18 #include "chrome/browser/webdata/autofill_entry.h" |
| 19 #include "chrome/browser/webdata/web_data_service.h" | 19 #include "chrome/browser/webdata/web_data_service.h" |
| 20 #include "content/public/browser/notification_observer.h" | 20 #include "content/public/browser/notification_observer.h" |
| 21 #include "content/public/browser/notification_registrar.h" | 21 #include "content/public/browser/notification_registrar.h" |
| 22 #include "sync/api/sync_change.h" | 22 #include "sync/api/sync_change.h" |
| 23 #include "sync/api/sync_data.h" | 23 #include "sync/api/sync_data.h" |
| 24 #include "sync/api/sync_error.h" | 24 #include "sync/api/sync_error.h" |
| 25 #include "sync/api/syncable_service.h" | 25 #include "sync/api/syncable_service.h" |
| 26 | 26 |
| 27 class ProfileSyncServiceAutofillTest; | 27 class ProfileSyncServiceAutofillTest; |
| 28 | 28 |
| 29 namespace csync { | 29 namespace syncer { |
| 30 class SyncErrorFactory; | 30 class SyncErrorFactory; |
| 31 } | 31 } |
| 32 | 32 |
| 33 namespace sync_pb { | 33 namespace sync_pb { |
| 34 class AutofillSpecifics; | 34 class AutofillSpecifics; |
| 35 } | 35 } |
| 36 | 36 |
| 37 // The sync implementation for autocomplete. | 37 // The sync implementation for autocomplete. |
| 38 // MergeDataAndStartSyncing() called first, it does cloud->local and | 38 // MergeDataAndStartSyncing() called first, it does cloud->local and |
| 39 // local->cloud syncs. Then for each cloud change we receive | 39 // local->cloud syncs. Then for each cloud change we receive |
| 40 // ProcessSyncChanges() and for each local change Observe() is called. | 40 // ProcessSyncChanges() and for each local change Observe() is called. |
| 41 // TODO(georgey) : remove reliance on the notifications and make it to be called | 41 // TODO(georgey) : remove reliance on the notifications and make it to be called |
| 42 // from web_data_service directly. | 42 // from web_data_service directly. |
| 43 class AutocompleteSyncableService | 43 class AutocompleteSyncableService |
| 44 : public csync::SyncableService, | 44 : public syncer::SyncableService, |
| 45 public content::NotificationObserver, | 45 public content::NotificationObserver, |
| 46 public base::NonThreadSafe { | 46 public base::NonThreadSafe { |
| 47 public: | 47 public: |
| 48 explicit AutocompleteSyncableService(WebDataService* web_data_service); | 48 explicit AutocompleteSyncableService(WebDataService* web_data_service); |
| 49 virtual ~AutocompleteSyncableService(); | 49 virtual ~AutocompleteSyncableService(); |
| 50 | 50 |
| 51 static syncable::ModelType model_type() { return syncable::AUTOFILL; } | 51 static syncable::ModelType model_type() { return syncable::AUTOFILL; } |
| 52 | 52 |
| 53 // csync::SyncableService implementation. | 53 // syncer::SyncableService implementation. |
| 54 virtual csync::SyncError MergeDataAndStartSyncing( | 54 virtual syncer::SyncError MergeDataAndStartSyncing( |
| 55 syncable::ModelType type, | 55 syncable::ModelType type, |
| 56 const csync::SyncDataList& initial_sync_data, | 56 const syncer::SyncDataList& initial_sync_data, |
| 57 scoped_ptr<csync::SyncChangeProcessor> sync_processor, | 57 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
| 58 scoped_ptr<csync::SyncErrorFactory> error_handler) OVERRIDE; | 58 scoped_ptr<syncer::SyncErrorFactory> error_handler) OVERRIDE; |
| 59 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; | 59 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; |
| 60 virtual csync::SyncDataList GetAllSyncData( | 60 virtual syncer::SyncDataList GetAllSyncData( |
| 61 syncable::ModelType type) const OVERRIDE; | 61 syncable::ModelType type) const OVERRIDE; |
| 62 virtual csync::SyncError ProcessSyncChanges( | 62 virtual syncer::SyncError ProcessSyncChanges( |
| 63 const tracked_objects::Location& from_here, | 63 const tracked_objects::Location& from_here, |
| 64 const csync::SyncChangeList& change_list) OVERRIDE; | 64 const syncer::SyncChangeList& change_list) OVERRIDE; |
| 65 | 65 |
| 66 // NotificationObserver implementation. | 66 // NotificationObserver implementation. |
| 67 virtual void Observe(int type, | 67 virtual void Observe(int type, |
| 68 const content::NotificationSource& source, | 68 const content::NotificationSource& source, |
| 69 const content::NotificationDetails& details) OVERRIDE; | 69 const content::NotificationDetails& details) OVERRIDE; |
| 70 | 70 |
| 71 protected: | 71 protected: |
| 72 // Helper to query WebDatabase for the current autocomplete state. | 72 // Helper to query WebDatabase for the current autocomplete state. |
| 73 // Made virtual for ease of mocking in the unit-test. | 73 // Made virtual for ease of mocking in the unit-test. |
| 74 virtual bool LoadAutofillData(std::vector<AutofillEntry>* entries) const; | 74 virtual bool LoadAutofillData(std::vector<AutofillEntry>* entries) const; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 87 FRIEND_TEST_ALL_PREFIXES(AutocompleteSyncableServiceTest, GetAllSyncData); | 87 FRIEND_TEST_ALL_PREFIXES(AutocompleteSyncableServiceTest, GetAllSyncData); |
| 88 FRIEND_TEST_ALL_PREFIXES(AutocompleteSyncableServiceTest, | 88 FRIEND_TEST_ALL_PREFIXES(AutocompleteSyncableServiceTest, |
| 89 ProcessSyncChanges); | 89 ProcessSyncChanges); |
| 90 FRIEND_TEST_ALL_PREFIXES(AutocompleteSyncableServiceTest, | 90 FRIEND_TEST_ALL_PREFIXES(AutocompleteSyncableServiceTest, |
| 91 ActOnChange); | 91 ActOnChange); |
| 92 | 92 |
| 93 // This is a helper map used only in Merge/Process* functions. The lifetime | 93 // This is a helper map used only in Merge/Process* functions. The lifetime |
| 94 // of the iterator is longer than the map object. The bool in the pair is used | 94 // of the iterator is longer than the map object. The bool in the pair is used |
| 95 // to indicate if the item needs to be added (true) or updated (false). | 95 // to indicate if the item needs to be added (true) or updated (false). |
| 96 typedef std::map<AutofillKey, | 96 typedef std::map<AutofillKey, |
| 97 std::pair<csync::SyncChange::SyncChangeType, | 97 std::pair<syncer::SyncChange::SyncChangeType, |
| 98 std::vector<AutofillEntry>::iterator> > | 98 std::vector<AutofillEntry>::iterator> > |
| 99 AutocompleteEntryMap; | 99 AutocompleteEntryMap; |
| 100 | 100 |
| 101 // Creates or updates an autocomplete entry based on |data|. | 101 // Creates or updates an autocomplete entry based on |data|. |
| 102 // |data| - an entry for sync. | 102 // |data| - an entry for sync. |
| 103 // |loaded_data| - entries that were loaded from local storage. | 103 // |loaded_data| - entries that were loaded from local storage. |
| 104 // |new_entries| - entries that came from the sync. | 104 // |new_entries| - entries that came from the sync. |
| 105 // |ignored_entries| - entries that came from the sync, but too old to be | 105 // |ignored_entries| - entries that came from the sync, but too old to be |
| 106 // stored and immediately discarded. | 106 // stored and immediately discarded. |
| 107 void CreateOrUpdateEntry(const csync::SyncData& data, | 107 void CreateOrUpdateEntry(const syncer::SyncData& data, |
| 108 AutocompleteEntryMap* loaded_data, | 108 AutocompleteEntryMap* loaded_data, |
| 109 std::vector<AutofillEntry>* new_entries); | 109 std::vector<AutofillEntry>* new_entries); |
| 110 | 110 |
| 111 // Writes |entry| data into supplied |autofill_specifics|. | 111 // Writes |entry| data into supplied |autofill_specifics|. |
| 112 static void WriteAutofillEntry(const AutofillEntry& entry, | 112 static void WriteAutofillEntry(const AutofillEntry& entry, |
| 113 sync_pb::EntitySpecifics* autofill_specifics); | 113 sync_pb::EntitySpecifics* autofill_specifics); |
| 114 | 114 |
| 115 // Deletes the database entry corresponding to the |autofill| specifics. | 115 // Deletes the database entry corresponding to the |autofill| specifics. |
| 116 csync::SyncError AutofillEntryDelete( | 116 syncer::SyncError AutofillEntryDelete( |
| 117 const sync_pb::AutofillSpecifics& autofill); | 117 const sync_pb::AutofillSpecifics& autofill); |
| 118 | 118 |
| 119 csync::SyncData CreateSyncData(const AutofillEntry& entry) const; | 119 syncer::SyncData CreateSyncData(const AutofillEntry& entry) const; |
| 120 | 120 |
| 121 // Syncs |changes| to the cloud. | 121 // Syncs |changes| to the cloud. |
| 122 void ActOnChanges(const AutofillChangeList& changes); | 122 void ActOnChanges(const AutofillChangeList& changes); |
| 123 | 123 |
| 124 static std::string KeyToTag(const std::string& name, | 124 static std::string KeyToTag(const std::string& name, |
| 125 const std::string& value); | 125 const std::string& value); |
| 126 | 126 |
| 127 // For unit-tests. | 127 // For unit-tests. |
| 128 AutocompleteSyncableService(); | 128 AutocompleteSyncableService(); |
| 129 void set_sync_processor(csync::SyncChangeProcessor* sync_processor) { | 129 void set_sync_processor(syncer::SyncChangeProcessor* sync_processor) { |
| 130 sync_processor_.reset(sync_processor); | 130 sync_processor_.reset(sync_processor); |
| 131 } | 131 } |
| 132 | 132 |
| 133 // Lifetime of AutocompleteSyncableService object is shorter than | 133 // Lifetime of AutocompleteSyncableService object is shorter than |
| 134 // |web_data_service_| passed to it. | 134 // |web_data_service_| passed to it. |
| 135 WebDataService* web_data_service_; | 135 WebDataService* web_data_service_; |
| 136 | 136 |
| 137 content::NotificationRegistrar notification_registrar_; | 137 content::NotificationRegistrar notification_registrar_; |
| 138 | 138 |
| 139 // We receive ownership of |sync_processor_| in MergeDataAndStartSyncing() and | 139 // We receive ownership of |sync_processor_| in MergeDataAndStartSyncing() and |
| 140 // destroy it in StopSyncing(). | 140 // destroy it in StopSyncing(). |
| 141 scoped_ptr<csync::SyncChangeProcessor> sync_processor_; | 141 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; |
| 142 | 142 |
| 143 // We receive ownership of |error_handler_| in MergeDataAndStartSyncing() and | 143 // We receive ownership of |error_handler_| in MergeDataAndStartSyncing() and |
| 144 // destroy it in StopSyncing(). | 144 // destroy it in StopSyncing(). |
| 145 scoped_ptr<csync::SyncErrorFactory> error_handler_; | 145 scoped_ptr<syncer::SyncErrorFactory> error_handler_; |
| 146 | 146 |
| 147 DISALLOW_COPY_AND_ASSIGN(AutocompleteSyncableService); | 147 DISALLOW_COPY_AND_ASSIGN(AutocompleteSyncableService); |
| 148 }; | 148 }; |
| 149 | 149 |
| 150 #endif // CHROME_BROWSER_WEBDATA_AUTOCOMPLETE_SYNCABLE_SERVICE_H_ | 150 #endif // CHROME_BROWSER_WEBDATA_AUTOCOMPLETE_SYNCABLE_SERVICE_H_ |
| OLD | NEW |