OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 | 4 |
5 #include "chrome/browser/webdata/autofill_profile_syncable_service.h" | 5 #include "chrome/browser/webdata/autofill_profile_syncable_service.h" |
6 | 6 |
7 #include "base/location.h" | 7 #include "base/location.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/autofill/autofill_profile.h" | 10 #include "chrome/browser/autofill/autofill_profile.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 } | 33 } |
34 | 34 |
35 } // namespace | 35 } // namespace |
36 | 36 |
37 namespace browser_sync { | 37 namespace browser_sync { |
38 | 38 |
39 const char kAutofillProfileTag[] = "google_chrome_autofill_profiles"; | 39 const char kAutofillProfileTag[] = "google_chrome_autofill_profiles"; |
40 | 40 |
41 AutofillProfileSyncableService::AutofillProfileSyncableService( | 41 AutofillProfileSyncableService::AutofillProfileSyncableService( |
42 WebDataService* web_data_service) | 42 WebDataService* web_data_service) |
43 : web_data_service_(web_data_service), | 43 : web_data_service_(web_data_service) { |
44 sync_processor_(NULL) { | |
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
46 DCHECK(web_data_service_); | 45 DCHECK(web_data_service_); |
47 notification_registrar_.Add(this, | 46 notification_registrar_.Add(this, |
48 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, | 47 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, |
49 Source<WebDataService>(web_data_service_)); | 48 Source<WebDataService>(web_data_service_)); |
50 } | 49 } |
51 | 50 |
52 AutofillProfileSyncableService::~AutofillProfileSyncableService() { | 51 AutofillProfileSyncableService::~AutofillProfileSyncableService() { |
53 DCHECK(CalledOnValidThread()); | 52 DCHECK(CalledOnValidThread()); |
54 } | 53 } |
55 | 54 |
56 AutofillProfileSyncableService::AutofillProfileSyncableService() | 55 AutofillProfileSyncableService::AutofillProfileSyncableService() |
57 : web_data_service_(NULL), | 56 : web_data_service_(NULL) { |
58 sync_processor_(NULL) { | |
59 } | 57 } |
60 | 58 |
61 SyncError AutofillProfileSyncableService::MergeDataAndStartSyncing( | 59 SyncError AutofillProfileSyncableService::MergeDataAndStartSyncing( |
62 syncable::ModelType type, | 60 syncable::ModelType type, |
63 const SyncDataList& initial_sync_data, | 61 const SyncDataList& initial_sync_data, |
64 SyncChangeProcessor* sync_processor) { | 62 SyncChangeProcessor* sync_processor) { |
65 DCHECK(CalledOnValidThread()); | 63 DCHECK(CalledOnValidThread()); |
66 DCHECK(sync_processor_ == NULL); | 64 DCHECK(!sync_processor_.get()); |
67 VLOG(1) << "Associating Autofill: MergeDataAndStartSyncing"; | 65 VLOG(1) << "Associating Autofill: MergeDataAndStartSyncing"; |
68 | 66 |
69 if (!LoadAutofillData(&profiles_.get())) { | 67 if (!LoadAutofillData(&profiles_.get())) { |
70 return SyncError( | 68 return SyncError( |
71 FROM_HERE, "Could not get the autofill data from WebDatabase.", | 69 FROM_HERE, "Could not get the autofill data from WebDatabase.", |
72 model_type()); | 70 model_type()); |
73 } | 71 } |
74 | 72 |
75 if (VLOG_IS_ON(2)) { | 73 if (VLOG_IS_ON(2)) { |
76 VLOG(2) << "[AUTOFILL MIGRATION]" | 74 VLOG(2) << "[AUTOFILL MIGRATION]" |
77 << "Printing profiles from web db"; | 75 << "Printing profiles from web db"; |
78 | 76 |
79 for (ScopedVector<AutofillProfile>::const_iterator ix = | 77 for (ScopedVector<AutofillProfile>::const_iterator ix = |
80 profiles_.begin(); ix != profiles_.end(); ++ix) { | 78 profiles_.begin(); ix != profiles_.end(); ++ix) { |
81 AutofillProfile* p = *ix; | 79 AutofillProfile* p = *ix; |
82 VLOG(2) << "[AUTOFILL MIGRATION] " | 80 VLOG(2) << "[AUTOFILL MIGRATION] " |
83 << p->GetInfo(NAME_FIRST) | 81 << p->GetInfo(NAME_FIRST) |
84 << p->GetInfo(NAME_LAST) | 82 << p->GetInfo(NAME_LAST) |
85 << p->guid(); | 83 << p->guid(); |
86 } | 84 } |
87 } | 85 } |
88 | 86 |
89 sync_processor_ = sync_processor; | 87 sync_processor_.reset(sync_processor); |
90 | 88 |
91 GUIDToProfileMap remaining_profiles; | 89 GUIDToProfileMap remaining_profiles; |
92 CreateGUIDToProfileMap(profiles_.get(), &remaining_profiles); | 90 CreateGUIDToProfileMap(profiles_.get(), &remaining_profiles); |
93 | 91 |
94 DataBundle bundle; | 92 DataBundle bundle; |
95 // Go through and check for all the profiles that sync already knows about. | 93 // Go through and check for all the profiles that sync already knows about. |
96 for (SyncDataList::const_iterator sync_iter = initial_sync_data.begin(); | 94 for (SyncDataList::const_iterator sync_iter = initial_sync_data.begin(); |
97 sync_iter != initial_sync_data.end(); | 95 sync_iter != initial_sync_data.end(); |
98 ++sync_iter) { | 96 ++sync_iter) { |
99 GUIDToProfileMap::iterator it = | 97 GUIDToProfileMap::iterator it = |
100 CreateOrUpdateProfile(*sync_iter, &remaining_profiles, &bundle); | 98 CreateOrUpdateProfile(*sync_iter, &remaining_profiles, &bundle); |
101 profiles_map_[it->first] = it->second; | 99 profiles_map_[it->first] = it->second; |
102 remaining_profiles.erase(it); | 100 remaining_profiles.erase(it); |
103 } | 101 } |
104 | 102 |
105 if (!SaveChangesToWebData(bundle)) | 103 if (!SaveChangesToWebData(bundle)) |
106 return SyncError(FROM_HERE, "Failed to update webdata.", model_type()); | 104 return SyncError(FROM_HERE, "Failed to update webdata.", model_type()); |
107 | 105 |
108 SyncChangeList new_changes; | 106 SyncChangeList new_changes; |
109 for (GUIDToProfileMap::iterator i = remaining_profiles.begin(); | 107 for (GUIDToProfileMap::iterator i = remaining_profiles.begin(); |
110 i != remaining_profiles.end(); ++i) { | 108 i != remaining_profiles.end(); ++i) { |
111 new_changes.push_back( | 109 new_changes.push_back( |
112 SyncChange(SyncChange::ACTION_ADD, CreateData(*(i->second)))); | 110 SyncChange(SyncChange::ACTION_ADD, CreateData(*(i->second)))); |
113 profiles_map_[i->first] = i->second; | 111 profiles_map_[i->first] = i->second; |
114 } | 112 } |
115 | 113 |
116 SyncError error = sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); | 114 SyncError error; |
| 115 if (!new_changes.empty()) |
| 116 error = sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); |
117 | 117 |
118 WebDataService::NotifyOfMultipleAutofillChanges(web_data_service_); | 118 WebDataService::NotifyOfMultipleAutofillChanges(web_data_service_); |
119 | 119 |
120 return error; | 120 return error; |
121 } | 121 } |
122 | 122 |
123 void AutofillProfileSyncableService::StopSyncing(syncable::ModelType type) { | 123 void AutofillProfileSyncableService::StopSyncing(syncable::ModelType type) { |
124 DCHECK(CalledOnValidThread()); | 124 DCHECK(CalledOnValidThread()); |
125 DCHECK(sync_processor_ != NULL); | |
126 DCHECK_EQ(type, syncable::AUTOFILL_PROFILE); | 125 DCHECK_EQ(type, syncable::AUTOFILL_PROFILE); |
127 | 126 |
128 sync_processor_ = NULL; | 127 sync_processor_.reset(); |
129 profiles_.reset(); | 128 profiles_.reset(); |
130 profiles_map_.clear(); | 129 profiles_map_.clear(); |
131 } | 130 } |
132 | 131 |
133 SyncDataList AutofillProfileSyncableService::GetAllSyncData( | 132 SyncDataList AutofillProfileSyncableService::GetAllSyncData( |
134 syncable::ModelType type) const { | 133 syncable::ModelType type) const { |
135 DCHECK(CalledOnValidThread()); | 134 DCHECK(CalledOnValidThread()); |
136 DCHECK(sync_processor_ != NULL); | 135 DCHECK(sync_processor_.get()); |
137 DCHECK_EQ(type, syncable::AUTOFILL_PROFILE); | 136 DCHECK_EQ(type, syncable::AUTOFILL_PROFILE); |
138 | 137 |
139 SyncDataList current_data; | 138 SyncDataList current_data; |
140 | 139 |
141 for (GUIDToProfileMap::const_iterator i = profiles_map_.begin(); | 140 for (GUIDToProfileMap::const_iterator i = profiles_map_.begin(); |
142 i != profiles_map_.end(); ++i) { | 141 i != profiles_map_.end(); ++i) { |
143 current_data.push_back(CreateData(*(i->second))); | 142 current_data.push_back(CreateData(*(i->second))); |
144 } | 143 } |
145 return current_data; | 144 return current_data; |
146 } | 145 } |
147 | 146 |
148 SyncError AutofillProfileSyncableService::ProcessSyncChanges( | 147 SyncError AutofillProfileSyncableService::ProcessSyncChanges( |
149 const tracked_objects::Location& from_here, | 148 const tracked_objects::Location& from_here, |
150 const SyncChangeList& change_list) { | 149 const SyncChangeList& change_list) { |
151 DCHECK(CalledOnValidThread()); | 150 DCHECK(CalledOnValidThread()); |
152 DCHECK(sync_processor_ != NULL); | 151 if (!sync_processor_.get()) { |
153 if (sync_processor_ == NULL) { | |
154 SyncError error(FROM_HERE, "Models not yet associated.", | 152 SyncError error(FROM_HERE, "Models not yet associated.", |
155 syncable::AUTOFILL_PROFILE); | 153 syncable::AUTOFILL_PROFILE); |
156 return error; | 154 return error; |
157 } | 155 } |
158 | 156 |
159 DataBundle bundle; | 157 DataBundle bundle; |
160 | 158 |
161 for (SyncChangeList::const_iterator i = change_list.begin(); | 159 for (SyncChangeList::const_iterator i = change_list.begin(); |
162 i != change_list.end(); ++i) { | 160 i != change_list.end(); ++i) { |
163 DCHECK(i->IsValid()); | 161 DCHECK(i->IsValid()); |
(...skipping 26 matching lines...) Expand all Loading... |
190 | 188 |
191 void AutofillProfileSyncableService::Observe(int type, | 189 void AutofillProfileSyncableService::Observe(int type, |
192 const NotificationSource& source, | 190 const NotificationSource& source, |
193 const NotificationDetails& details) { | 191 const NotificationDetails& details) { |
194 DCHECK_EQ(type, chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED); | 192 DCHECK_EQ(type, chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED); |
195 DCHECK_EQ(web_data_service_, Source<WebDataService>(source).ptr()); | 193 DCHECK_EQ(web_data_service_, Source<WebDataService>(source).ptr()); |
196 // Check if sync is on. If we receive notification prior to the sync being set | 194 // Check if sync is on. If we receive notification prior to the sync being set |
197 // up we are going to process all when MergeData..() is called. If we receive | 195 // up we are going to process all when MergeData..() is called. If we receive |
198 // notification after the sync exited, it will be sinced next time Chrome | 196 // notification after the sync exited, it will be sinced next time Chrome |
199 // starts. | 197 // starts. |
200 if (!sync_processor_) | 198 if (!sync_processor_.get()) |
201 return; | 199 return; |
202 | 200 |
203 AutofillProfileChange* change = Details<AutofillProfileChange>(details).ptr(); | 201 AutofillProfileChange* change = Details<AutofillProfileChange>(details).ptr(); |
204 ActOnChange(*change); | 202 ActOnChange(*change); |
205 } | 203 } |
206 | 204 |
207 bool AutofillProfileSyncableService::LoadAutofillData( | 205 bool AutofillProfileSyncableService::LoadAutofillData( |
208 std::vector<AutofillProfile*>* profiles) { | 206 std::vector<AutofillProfile*>* profiles) { |
209 return GetAutofillTable()->GetAutofillProfiles(profiles); | 207 return GetAutofillTable()->GetAutofillProfiles(profiles); |
210 } | 208 } |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 bundle->profiles_to_add.push_back(new_profile); | 343 bundle->profiles_to_add.push_back(new_profile); |
346 } | 344 } |
347 return it; | 345 return it; |
348 } | 346 } |
349 | 347 |
350 void AutofillProfileSyncableService::ActOnChange( | 348 void AutofillProfileSyncableService::ActOnChange( |
351 const AutofillProfileChange& change) { | 349 const AutofillProfileChange& change) { |
352 DCHECK((change.type() == AutofillProfileChange::REMOVE && | 350 DCHECK((change.type() == AutofillProfileChange::REMOVE && |
353 !change.profile()) || | 351 !change.profile()) || |
354 (change.type() != AutofillProfileChange::REMOVE && change.profile())); | 352 (change.type() != AutofillProfileChange::REMOVE && change.profile())); |
355 DCHECK(sync_processor_); | 353 DCHECK(sync_processor_.get()); |
356 SyncChangeList new_changes; | 354 SyncChangeList new_changes; |
357 DataBundle bundle; | 355 DataBundle bundle; |
358 switch (change.type()) { | 356 switch (change.type()) { |
359 case AutofillProfileChange::ADD: | 357 case AutofillProfileChange::ADD: |
360 new_changes.push_back( | 358 new_changes.push_back( |
361 SyncChange(SyncChange::ACTION_ADD, CreateData(*(change.profile())))); | 359 SyncChange(SyncChange::ACTION_ADD, CreateData(*(change.profile())))); |
362 DCHECK(profiles_map_.find(change.profile()->guid()) == | 360 DCHECK(profiles_map_.find(change.profile()->guid()) == |
363 profiles_map_.end()); | 361 profiles_map_.end()); |
364 profiles_.push_back(new AutofillProfile(*(change.profile()))); | 362 profiles_.push_back(new AutofillProfile(*(change.profile()))); |
365 profiles_map_[change.profile()->guid()] = profiles_.get().back(); | 363 profiles_map_[change.profile()->guid()] = profiles_.get().back(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 AutofillTable* AutofillProfileSyncableService::GetAutofillTable() const { | 401 AutofillTable* AutofillProfileSyncableService::GetAutofillTable() const { |
404 return web_data_service_->GetDatabase()->GetAutofillTable(); | 402 return web_data_service_->GetDatabase()->GetAutofillTable(); |
405 } | 403 } |
406 | 404 |
407 AutofillProfileSyncableService::DataBundle::DataBundle() {} | 405 AutofillProfileSyncableService::DataBundle::DataBundle() {} |
408 | 406 |
409 AutofillProfileSyncableService::DataBundle::~DataBundle() { | 407 AutofillProfileSyncableService::DataBundle::~DataBundle() { |
410 } | 408 } |
411 | 409 |
412 } // namespace browser_sync | 410 } // namespace browser_sync |
OLD | NEW |