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/autofill/personal_data_manager.h" | 5 #include "chrome/browser/autofill/personal_data_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
47 | 47 |
48 bool operator()(const T* form_group) { | 48 bool operator()(const T* form_group) { |
49 return form_group->guid() == guid_; | 49 return form_group->guid() == guid_; |
50 } | 50 } |
51 | 51 |
52 private: | 52 private: |
53 std::string guid_; | 53 std::string guid_; |
54 }; | 54 }; |
55 | 55 |
56 template<typename T> | 56 template<typename T> |
57 class FormGroupProfileMatchesFunctor { | |
58 public: | |
59 explicit FormGroupProfileMatchesFunctor(const AutoFillProfile& profile) | |
60 : profile_(profile) { | |
61 } | |
62 | |
63 bool operator()(const T& profile) { | |
64 return profile.Compare(profile_) == 0; | |
65 } | |
66 | |
67 private: | |
68 const AutoFillProfile& profile_; | |
69 }; | |
70 | |
71 template<typename T> | |
72 class FormGroupProfileMatchesFunctor<T *> { | |
73 public: | |
74 explicit FormGroupProfileMatchesFunctor(const AutoFillProfile& profile) | |
75 : profile_(profile) { | |
76 } | |
77 | |
78 bool operator()(const T* profile) { | |
79 return profile->Compare(profile_) == 0; | |
80 } | |
81 | |
82 private: | |
83 const AutoFillProfile& profile_; | |
84 }; | |
85 | |
86 template<typename T> | |
87 class FormGroupCreditCardMatchesFunctor { | |
88 public: | |
89 explicit FormGroupCreditCardMatchesFunctor(const CreditCard& credit_card) | |
90 : credit_card_(credit_card) { | |
91 } | |
92 | |
93 bool operator()(const T& credit_card) { | |
94 return credit_card.Compare(credit_card_) == 0; | |
95 } | |
96 | |
97 private: | |
98 const CreditCard& credit_card_; | |
99 }; | |
100 | |
101 template<typename T> | |
102 class FormGroupCreditCardMatchesFunctor<T *> { | |
103 public: | |
104 explicit FormGroupCreditCardMatchesFunctor(const CreditCard& credit_card) | |
105 : credit_card_(credit_card) { | |
106 } | |
107 | |
108 bool operator()(const T* credit_card) { | |
109 return credit_card->Compare(credit_card_) == 0; | |
110 } | |
111 | |
112 private: | |
113 const CreditCard& credit_card_; | |
114 }; | |
Ilya Sherman
2010/12/20 20:57:04
Wow, that's a lot of new functors. I think we jus
dhollowa
2010/12/20 23:16:44
Yes, better. But to do this I needed to add a sec
| |
115 | |
116 template<typename T> | |
57 class DereferenceFunctor { | 117 class DereferenceFunctor { |
58 public: | 118 public: |
59 template<typename T_Iterator> | 119 template<typename T_Iterator> |
60 const T& operator()(const T_Iterator& iterator) { | 120 const T& operator()(const T_Iterator& iterator) { |
61 return *iterator; | 121 return *iterator; |
62 } | 122 } |
63 }; | 123 }; |
64 | 124 |
65 template<typename T> | 125 template<typename T> |
66 T* address_of(T& v) { | 126 T* address_of(T& v) { |
67 return &v; | 127 return &v; |
68 } | 128 } |
69 | 129 |
70 template<typename T, typename C> | 130 template<typename T, typename C> |
71 bool FindByGUID(const C& container, const std::string& guid) { | 131 bool FindByGUID(const C& container, const std::string& guid) { |
72 return std::find_if(container.begin(), container.end(), | 132 return std::find_if(container.begin(), container.end(), |
73 FormGroupGUIDMatchesFunctor<T>(guid)) != container.end(); | 133 FormGroupGUIDMatchesFunctor<T>(guid)) != container.end(); |
74 } | 134 } |
75 | 135 |
136 template<typename T, typename C> | |
137 bool FindByContents(const C& container, const AutoFillProfile& profile) { | |
138 return std::find_if( | |
139 container.begin(), | |
140 container.end(), | |
141 FormGroupProfileMatchesFunctor<T>(profile)) != container.end(); | |
142 } | |
143 | |
144 template<typename T, typename C> | |
145 bool FindByContents(const C& container, const CreditCard& credit_card) { | |
146 return std::find_if( | |
147 container.begin(), | |
148 container.end(), | |
149 FormGroupCreditCardMatchesFunctor<T>(credit_card)) != container.end(); | |
150 } | |
Ilya Sherman
2010/12/20 20:57:04
Likewise, here we just need one function -- someth
dhollowa
2010/12/20 23:16:44
Done.
| |
151 | |
76 } // namespace | 152 } // namespace |
77 | 153 |
78 PersonalDataManager::~PersonalDataManager() { | 154 PersonalDataManager::~PersonalDataManager() { |
79 CancelPendingQuery(&pending_profiles_query_); | 155 CancelPendingQuery(&pending_profiles_query_); |
80 CancelPendingQuery(&pending_creditcards_query_); | 156 CancelPendingQuery(&pending_creditcards_query_); |
81 } | 157 } |
82 | 158 |
83 void PersonalDataManager::OnWebDataServiceRequestDone( | 159 void PersonalDataManager::OnWebDataServiceRequestDone( |
84 WebDataService::Handle h, | 160 WebDataService::Handle h, |
85 const WDTypedResult* result) { | 161 const WDTypedResult* result) { |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
306 wds->RemoveAutoFillProfileGUID((*iter)->guid()); | 382 wds->RemoveAutoFillProfileGUID((*iter)->guid()); |
307 } | 383 } |
308 | 384 |
309 // Update the web database with the existing profiles. | 385 // Update the web database with the existing profiles. |
310 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); | 386 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); |
311 iter != profiles->end(); ++iter) { | 387 iter != profiles->end(); ++iter) { |
312 if (FindByGUID<AutoFillProfile*>(web_profiles_, iter->guid())) | 388 if (FindByGUID<AutoFillProfile*>(web_profiles_, iter->guid())) |
313 wds->UpdateAutoFillProfileGUID(*iter); | 389 wds->UpdateAutoFillProfileGUID(*iter); |
314 } | 390 } |
315 | 391 |
316 // Add the new profiles to the web database. | 392 // Add the new profiles to the web database. Don't add a duplicate. |
317 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); | 393 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); |
318 iter != profiles->end(); ++iter) { | 394 iter != profiles->end(); ++iter) { |
319 if (!FindByGUID<AutoFillProfile*>(web_profiles_, iter->guid())) | 395 if (!FindByGUID<AutoFillProfile*>(web_profiles_, iter->guid()) && |
396 !FindByContents<AutoFillProfile*>(web_profiles_, *iter)) | |
Ilya Sherman
2010/12/20 20:57:04
nit: With the above changes, you should be able to
dhollowa
2010/12/20 23:16:44
Done.
| |
320 wds->AddAutoFillProfileGUID(*iter); | 397 wds->AddAutoFillProfileGUID(*iter); |
321 } | 398 } |
322 | 399 |
323 // Copy in the new profiles. | 400 // Copy in the new profiles. |
324 web_profiles_.reset(); | 401 web_profiles_.reset(); |
325 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); | 402 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); |
326 iter != profiles->end(); ++iter) { | 403 iter != profiles->end(); ++iter) { |
327 web_profiles_.push_back(new AutoFillProfile(*iter)); | 404 web_profiles_.push_back(new AutoFillProfile(*iter)); |
328 } | 405 } |
329 | 406 |
(...skipping 29 matching lines...) Expand all Loading... | |
359 wds->RemoveCreditCardGUID((*iter)->guid()); | 436 wds->RemoveCreditCardGUID((*iter)->guid()); |
360 } | 437 } |
361 | 438 |
362 // Update the web database with the existing credit cards. | 439 // Update the web database with the existing credit cards. |
363 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); | 440 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); |
364 iter != credit_cards->end(); ++iter) { | 441 iter != credit_cards->end(); ++iter) { |
365 if (FindByGUID<CreditCard*>(credit_cards_, iter->guid())) | 442 if (FindByGUID<CreditCard*>(credit_cards_, iter->guid())) |
366 wds->UpdateCreditCardGUID(*iter); | 443 wds->UpdateCreditCardGUID(*iter); |
367 } | 444 } |
368 | 445 |
369 // Add the new credit cards to the web database. | 446 // Add the new credit cards to the web database. Don't add a duplicate. |
370 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); | 447 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); |
371 iter != credit_cards->end(); ++iter) { | 448 iter != credit_cards->end(); ++iter) { |
372 if (!FindByGUID<CreditCard*>(credit_cards_, iter->guid())) | 449 if (!FindByGUID<CreditCard*>(credit_cards_, iter->guid()) && |
450 !FindByContents<CreditCard*>(credit_cards_, *iter)) | |
373 wds->AddCreditCardGUID(*iter); | 451 wds->AddCreditCardGUID(*iter); |
374 } | 452 } |
375 | 453 |
376 // Copy in the new credit cards. | 454 // Copy in the new credit cards. |
377 credit_cards_.reset(); | 455 credit_cards_.reset(); |
378 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); | 456 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); |
379 iter != credit_cards->end(); ++iter) { | 457 iter != credit_cards->end(); ++iter) { |
380 credit_cards_.push_back(new CreditCard(*iter)); | 458 credit_cards_.push_back(new CreditCard(*iter)); |
381 } | 459 } |
382 | 460 |
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
794 } | 872 } |
795 | 873 |
796 creditcards.push_back(**iter); | 874 creditcards.push_back(**iter); |
797 } | 875 } |
798 | 876 |
799 if (!merged) | 877 if (!merged) |
800 creditcards.push_back(*imported_credit_card_); | 878 creditcards.push_back(*imported_credit_card_); |
801 | 879 |
802 SetCreditCards(&creditcards); | 880 SetCreditCards(&creditcards); |
803 } | 881 } |
OLD | NEW |