OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/sync/glue/autofill_model_associator.h" | 5 #include "chrome/browser/sync/glue/autofill_model_associator.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/task.h" | 9 #include "base/task.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/autofill/autofill_profile.h" | 13 #include "chrome/browser/autofill/autofill_profile.h" |
14 #include "chrome/browser/browser_thread.h" | 14 #include "chrome/browser/browser_thread.h" |
15 #include "chrome/browser/guid.h" | 15 #include "chrome/browser/guid.h" |
16 #include "chrome/browser/prefs/pref_service.h" | |
16 #include "chrome/browser/profile.h" | 17 #include "chrome/browser/profile.h" |
17 #include "chrome/browser/sync/engine/syncapi.h" | 18 #include "chrome/browser/sync/engine/syncapi.h" |
18 #include "chrome/browser/sync/glue/autofill_change_processor.h" | 19 #include "chrome/browser/sync/glue/autofill_change_processor.h" |
20 #include "chrome/browser/sync/glue/autofill_profile_model_associator.h" | |
21 #include "chrome/browser/sync/glue/do_optimistic_refresh_Task.h" | |
19 #include "chrome/browser/sync/profile_sync_service.h" | 22 #include "chrome/browser/sync/profile_sync_service.h" |
20 #include "chrome/browser/sync/protocol/autofill_specifics.pb.h" | 23 #include "chrome/browser/sync/protocol/autofill_specifics.pb.h" |
21 #include "chrome/browser/webdata/web_database.h" | 24 #include "chrome/browser/webdata/web_database.h" |
25 #include "chrome/common/pref_names.h" | |
22 #include "net/base/escape.h" | 26 #include "net/base/escape.h" |
23 | 27 |
24 using base::TimeTicks; | 28 using base::TimeTicks; |
25 | 29 |
26 namespace browser_sync { | 30 namespace browser_sync { |
27 | 31 |
28 const char kAutofillTag[] = "google_chrome_autofill"; | 32 const char kAutofillTag[] = "google_chrome_autofill"; |
29 const char kAutofillEntryNamespaceTag[] = "autofill_entry|"; | 33 const char kAutofillEntryNamespaceTag[] = "autofill_entry|"; |
30 | 34 |
31 struct AutofillModelAssociator::DataBundle { | 35 struct AutofillModelAssociator::DataBundle { |
32 std::set<AutofillKey> current_entries; | 36 std::set<AutofillKey> current_entries; |
33 std::vector<AutofillEntry> new_entries; | 37 std::vector<AutofillEntry> new_entries; |
34 std::set<string16> current_profiles; | 38 std::set<string16> current_profiles; |
35 std::vector<AutoFillProfile*> updated_profiles; | 39 std::vector<AutoFillProfile*> updated_profiles; |
36 std::vector<AutoFillProfile*> new_profiles; // We own these pointers. | 40 std::vector<AutoFillProfile*> new_profiles; // We own these pointers. |
37 ~DataBundle() { STLDeleteElements(&new_profiles); } | 41 ~DataBundle() { STLDeleteElements(&new_profiles); } |
38 }; | 42 }; |
39 | 43 |
40 AutofillModelAssociator::DoOptimisticRefreshTask::DoOptimisticRefreshTask( | |
41 PersonalDataManager* pdm) : pdm_(pdm) {} | |
42 | |
43 AutofillModelAssociator::DoOptimisticRefreshTask::~DoOptimisticRefreshTask() {} | |
44 | |
45 void AutofillModelAssociator::DoOptimisticRefreshTask::Run() { | |
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
47 pdm_->Refresh(); | |
48 } | |
49 | |
50 AutofillModelAssociator::AutofillModelAssociator( | 44 AutofillModelAssociator::AutofillModelAssociator( |
51 ProfileSyncService* sync_service, | 45 ProfileSyncService* sync_service, |
52 WebDatabase* web_database, | 46 WebDatabase* web_database, |
53 PersonalDataManager* personal_data) | 47 PersonalDataManager* personal_data) |
54 : sync_service_(sync_service), | 48 : sync_service_(sync_service), |
55 web_database_(web_database), | 49 web_database_(web_database), |
56 personal_data_(personal_data), | 50 personal_data_(personal_data), |
57 autofill_node_id_(sync_api::kInvalidId), | 51 autofill_node_id_(sync_api::kInvalidId), |
58 abort_association_pending_(false) { | 52 abort_association_pending_(false) { |
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 node.SetTitle(UTF8ToWide(tag)); | 108 node.SetTitle(UTF8ToWide(tag)); |
115 AutofillChangeProcessor::WriteAutofillEntry(*ix, &node); | 109 AutofillChangeProcessor::WriteAutofillEntry(*ix, &node); |
116 Associate(&tag, node.GetId()); | 110 Associate(&tag, node.GetId()); |
117 } | 111 } |
118 | 112 |
119 current_entries->insert(ix->key()); | 113 current_entries->insert(ix->key()); |
120 } | 114 } |
121 return true; | 115 return true; |
122 } | 116 } |
123 | 117 |
124 bool AutofillModelAssociator::MakeNewAutofillProfileSyncNode( | |
125 sync_api::WriteTransaction* trans, const sync_api::BaseNode& autofill_root, | |
126 const std::string& tag, const AutoFillProfile& profile, int64* sync_id) { | |
127 sync_api::WriteNode node(trans); | |
128 if (!node.InitUniqueByCreation(syncable::AUTOFILL, autofill_root, tag)) { | |
129 LOG(ERROR) << "Failed to create autofill sync node."; | |
130 return false; | |
131 } | |
132 node.SetTitle(UTF8ToWide(tag)); | |
133 AutofillChangeProcessor::WriteAutofillProfile(profile, &node); | |
134 *sync_id = node.GetId(); | |
135 return true; | |
136 } | |
137 | |
138 | |
139 bool AutofillModelAssociator::LoadAutofillData( | 118 bool AutofillModelAssociator::LoadAutofillData( |
140 std::vector<AutofillEntry>* entries, | 119 std::vector<AutofillEntry>* entries, |
141 std::vector<AutoFillProfile*>* profiles) { | 120 std::vector<AutoFillProfile*>* profiles) { |
142 if (IsAbortPending()) | 121 if (IsAbortPending()) |
143 return false; | 122 return false; |
144 if (!web_database_->GetAllAutofillEntries(entries)) | 123 if (!web_database_->GetAllAutofillEntries(entries)) |
145 return false; | 124 return false; |
146 | 125 |
147 if (IsAbortPending()) | 126 if (IsAbortPending()) |
148 return false; | 127 return false; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
250 if (!sync_child.InitByIdLookup(sync_child_id)) { | 229 if (!sync_child.InitByIdLookup(sync_child_id)) { |
251 LOG(ERROR) << "Failed to fetch child node."; | 230 LOG(ERROR) << "Failed to fetch child node."; |
252 return false; | 231 return false; |
253 } | 232 } |
254 const sync_pb::AutofillSpecifics& autofill( | 233 const sync_pb::AutofillSpecifics& autofill( |
255 sync_child.GetAutofillSpecifics()); | 234 sync_child.GetAutofillSpecifics()); |
256 | 235 |
257 if (autofill.has_value()) | 236 if (autofill.has_value()) |
258 AddNativeEntryIfNeeded(autofill, bundle, sync_child); | 237 AddNativeEntryIfNeeded(autofill, bundle, sync_child); |
259 // Ignore autofill profiles if we are not upgrading. | 238 // Ignore autofill profiles if we are not upgrading. |
260 else if (autofill.has_profile() && IsUpgrading()) { | 239 else if (autofill.has_profile() && IsUpgrading(write_trans)) { |
261 AddNativeProfileIfNeeded( | 240 AddNativeProfileIfNeeded( |
262 autofill.profile(), | 241 autofill.profile(), |
263 bundle, | 242 bundle, |
264 sync_child, | 243 sync_child, |
265 all_profiles_from_db); | 244 all_profiles_from_db); |
266 } else { | 245 } else { |
267 NOTREACHED() << "AutofillSpecifics has no autofill data!"; | 246 NOTREACHED() << "AutofillSpecifics has no autofill data!"; |
268 } | 247 } |
269 | 248 |
270 sync_child_id = sync_child.GetSuccessorId(); | 249 sync_child_id = sync_child.GetSuccessorId(); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
336 } | 315 } |
337 | 316 |
338 void AutofillModelAssociator::AddNativeProfileIfNeeded( | 317 void AutofillModelAssociator::AddNativeProfileIfNeeded( |
339 const sync_pb::AutofillProfileSpecifics& profile, | 318 const sync_pb::AutofillProfileSpecifics& profile, |
340 DataBundle* bundle, | 319 DataBundle* bundle, |
341 const sync_api::ReadNode& node, | 320 const sync_api::ReadNode& node, |
342 std::vector<AutoFillProfile*>& all_profiles_from_db) { | 321 std::vector<AutoFillProfile*>& all_profiles_from_db) { |
343 | 322 |
344 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 323 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
345 | 324 |
346 scoped_ptr<AutoFillProfile> profile_in_web_db(FindCorrespondingNodeFromWebDB( | 325 AutoFillProfile* profile_in_web_db = FindCorrespondingNodeFromWebDB( |
347 profile, all_profiles_from_db)); | 326 profile, all_profiles_from_db); |
348 | 327 |
349 if (profile_in_web_db.get() != NULL) { | 328 if (profile_in_web_db != NULL) { |
350 int64 sync_id = node.GetId(); | 329 int64 sync_id = node.GetId(); |
351 Associate(&(profile_in_web_db->guid()), sync_id); | 330 Associate(&(profile_in_web_db->guid()), sync_id); |
352 return; | 331 return; |
353 } else { // Create a new node. | 332 } else { // Create a new node. |
354 std::string guid = guid::GenerateGUID(); | 333 std::string guid = guid::GenerateGUID(); |
355 Associate(&guid, node.GetId()); | 334 Associate(&guid, node.GetId()); |
356 AutoFillProfile* p = new AutoFillProfile(guid); | 335 AutoFillProfile* p = new AutoFillProfile(guid); |
357 OverwriteProfileWithServerData(p, profile); | 336 OverwriteProfileWithServerData(p, profile); |
358 bundle->new_profiles.push_back(p); | 337 bundle->new_profiles.push_back(p); |
359 } | 338 } |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
513 diff = MergeField(p, ADDRESS_HOME_ZIP, s.address_home_zip()) || diff; | 492 diff = MergeField(p, ADDRESS_HOME_ZIP, s.address_home_zip()) || diff; |
514 diff = MergeField(p, EMAIL_ADDRESS, s.email_address()) || diff; | 493 diff = MergeField(p, EMAIL_ADDRESS, s.email_address()) || diff; |
515 diff = MergeField(p, COMPANY_NAME, s.company_name()) || diff; | 494 diff = MergeField(p, COMPANY_NAME, s.company_name()) || diff; |
516 diff = MergeField(p, PHONE_FAX_WHOLE_NUMBER, s.phone_fax_whole_number()) | 495 diff = MergeField(p, PHONE_FAX_WHOLE_NUMBER, s.phone_fax_whole_number()) |
517 || diff; | 496 || diff; |
518 diff = MergeField(p, PHONE_HOME_WHOLE_NUMBER, s.phone_home_whole_number()) | 497 diff = MergeField(p, PHONE_HOME_WHOLE_NUMBER, s.phone_home_whole_number()) |
519 || diff; | 498 || diff; |
520 return diff; | 499 return diff; |
521 } | 500 } |
522 | 501 |
523 bool AutofillModelAssociator::IsUpgrading() | 502 bool AutofillModelAssociator::IsUpgrading(sync_api::WriteTransaction* trans) { |
524 { | 503 |
lipalani
2010/12/08 21:37:46
call the fn in model associator.
| |
525 return true; | 504 sync_api::ReadNode node(trans); |
505 | |
506 return !node.InitByClientTagLookup(syncable::AUTOFILL_PROFILE, | |
507 kAutofillProfileTag); | |
526 } | 508 } |
527 | 509 |
528 } // namespace browser_sync | 510 } // namespace browser_sync |
OLD | NEW |