| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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> |
| 8 #include <iterator> |
| 9 |
| 10 #include "base/logging.h" |
| 7 #include "chrome/browser/autofill/autofill_manager.h" | 11 #include "chrome/browser/autofill/autofill_manager.h" |
| 8 #include "chrome/browser/autofill/autofill_field.h" | 12 #include "chrome/browser/autofill/autofill_field.h" |
| 9 #include "chrome/browser/autofill/form_structure.h" | 13 #include "chrome/browser/autofill/form_structure.h" |
| 14 #include "chrome/browser/profile.h" |
| 15 #include "chrome/browser/webdata/web_data_service.h" |
| 10 | 16 |
| 11 // The minimum number of fields that must contain user data and have known types | 17 // The minimum number of fields that must contain user data and have known types |
| 12 // before autofill will attempt to import the data into a profile. | 18 // before autofill will attempt to import the data into a profile. |
| 13 static const int kMinImportSize = 5; | 19 static const int kMinImportSize = 5; |
| 14 | 20 |
| 15 // The number of digits in a phone number. | 21 // The number of digits in a phone number. |
| 16 static const int kPhoneNumberLength = 7; | 22 static const int kPhoneNumberLength = 7; |
| 17 | 23 |
| 18 // The number of digits in an area code. | 24 // The number of digits in an area code. |
| 19 static const int kPhoneCityCodeLength = 3; | 25 static const int kPhoneCityCodeLength = 3; |
| 20 | 26 |
| 21 PersonalDataManager::~PersonalDataManager() { | 27 PersonalDataManager::~PersonalDataManager() { |
| 28 CancelPendingQuery(); |
| 29 } |
| 30 |
| 31 void PersonalDataManager::OnWebDataServiceRequestDone( |
| 32 WebDataService::Handle h, |
| 33 const WDTypedResult* result) { |
| 34 // Error from the web database. |
| 35 if (!result) |
| 36 return; |
| 37 |
| 38 DCHECK(pending_query_handle_); |
| 39 DCHECK(result->GetType() == AUTOFILL_PROFILES_RESULT); |
| 40 pending_query_handle_ = 0; |
| 41 |
| 42 unique_ids_.clear(); |
| 43 profiles_.reset(); |
| 44 const WDResult<std::vector<AutoFillProfile*> >* r = |
| 45 static_cast<const WDResult<std::vector<AutoFillProfile*> >*>(result); |
| 46 std::vector<AutoFillProfile*> profiles = r->GetValue(); |
| 47 for (std::vector<AutoFillProfile*>::iterator iter = profiles.begin(); |
| 48 iter != profiles.end(); ++iter) { |
| 49 unique_ids_.insert((*iter)->unique_id()); |
| 50 profiles_.push_back(*iter); |
| 51 } |
| 52 |
| 53 is_data_loaded_ = true; |
| 54 if (observer_) |
| 55 observer_->OnPersonalDataLoaded(); |
| 56 } |
| 57 |
| 58 void PersonalDataManager::SetObserver(PersonalDataManager::Observer* observer) { |
| 59 DCHECK(observer_ == NULL); |
| 60 observer_ = observer; |
| 61 } |
| 62 |
| 63 void PersonalDataManager::RemoveObserver( |
| 64 PersonalDataManager::Observer* observer) { |
| 65 if (observer_ == observer) |
| 66 observer_ = NULL; |
| 22 } | 67 } |
| 23 | 68 |
| 24 bool PersonalDataManager::ImportFormData( | 69 bool PersonalDataManager::ImportFormData( |
| 25 const std::vector<FormStructure*>& form_structures, | 70 const std::vector<FormStructure*>& form_structures, |
| 26 AutoFillManager* autofill_manager) { | 71 AutoFillManager* autofill_manager) { |
| 27 InitializeIfNeeded(); | 72 InitializeIfNeeded(); |
| 28 | 73 |
| 29 // Parse the form and construct a profile based on the information that is | 74 // Parse the form and construct a profile based on the information that is |
| 30 // possible to import. | 75 // possible to import. |
| 31 int importable_fields = 0; | 76 int importable_fields = 0; |
| 32 int importable_credit_card_fields = 0; | 77 int importable_credit_card_fields = 0; |
| 33 imported_profile_.reset(new AutoFillProfile(string16(), | 78 imported_profile_.reset(new AutoFillProfile(string16(), |
| 34 CreateNextUniqueId())); | 79 CreateNextUniqueID())); |
| 35 imported_credit_card_.reset(new CreditCard(string16())); | 80 imported_credit_card_.reset(new CreditCard(string16())); |
| 36 | 81 |
| 37 bool billing_address_info = false; | 82 bool billing_address_info = false; |
| 38 std::vector<FormStructure*>::const_iterator iter; | 83 std::vector<FormStructure*>::const_iterator iter; |
| 39 for (iter = form_structures.begin(); iter != form_structures.end(); ++iter) { | 84 for (iter = form_structures.begin(); iter != form_structures.end(); ++iter) { |
| 40 const FormStructure* form = *iter; | 85 const FormStructure* form = *iter; |
| 41 for (size_t i = 0; i < form->field_count(); ++i) { | 86 for (size_t i = 0; i < form->field_count(); ++i) { |
| 42 const AutoFillField* field = form->field(i); | 87 const AutoFillField* field = form->field(i); |
| 43 string16 value = CollapseWhitespace(field->value(), false); | 88 string16 value = CollapseWhitespace(field->value(), false); |
| 44 | 89 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 imported_profile_.reset(); | 142 imported_profile_.reset(); |
| 98 | 143 |
| 99 if (importable_credit_card_fields == 0) | 144 if (importable_credit_card_fields == 0) |
| 100 imported_credit_card_.reset(); | 145 imported_credit_card_.reset(); |
| 101 | 146 |
| 102 // TODO(jhawkins): Alert the AutoFillManager that we have data. | 147 // TODO(jhawkins): Alert the AutoFillManager that we have data. |
| 103 | 148 |
| 104 return true; | 149 return true; |
| 105 } | 150 } |
| 106 | 151 |
| 152 void PersonalDataManager::SetProfiles(std::vector<AutoFillProfile>* profiles) { |
| 153 if (profile_->IsOffTheRecord()) |
| 154 return; |
| 155 |
| 156 WebDataService* wds = profile_->GetWebDataService(Profile::EXPLICIT_ACCESS); |
| 157 if (!wds) |
| 158 return; |
| 159 |
| 160 // Remove the unique IDs of the new set of profiles from the unique ID set. |
| 161 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); |
| 162 iter != profiles->end(); ++iter) { |
| 163 if (iter->unique_id() != 0) |
| 164 unique_ids_.erase(iter->unique_id()); |
| 165 } |
| 166 |
| 167 // Any remaining IDs are not in the new profile list and should be removed |
| 168 // from the web database. |
| 169 for (std::set<int>::iterator iter = unique_ids_.begin(); |
| 170 iter != unique_ids_.end(); ++iter) { |
| 171 wds->RemoveAutoFillProfile(*iter); |
| 172 } |
| 173 |
| 174 // Clear the unique IDs. The set of unique IDs is updated for each profile |
| 175 // added to |profile_| below. |
| 176 unique_ids_.clear(); |
| 177 |
| 178 // Update the web database with the existing profiles. We need to handle |
| 179 // these first so that |unique_ids_| is reset with the IDs of the existing |
| 180 // profiles; otherwise, new profiles added before older profiles can take |
| 181 // their unique ID. |
| 182 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); |
| 183 iter != profiles->end(); ++iter) { |
| 184 if (iter->unique_id() != 0) { |
| 185 unique_ids_.insert(iter->unique_id()); |
| 186 wds->UpdateAutoFillProfile(*iter); |
| 187 } |
| 188 } |
| 189 |
| 190 // Add the new profiles to the web database. |
| 191 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); |
| 192 iter != profiles->end(); ++iter) { |
| 193 // Profile was added by the AutoFill dialog, so we need to set the unique |
| 194 // ID. This also means we need to add this profile to the web DB. |
| 195 if (iter->unique_id() == 0) { |
| 196 iter->set_unique_id(CreateNextUniqueID()); |
| 197 wds->AddAutoFillProfile(*iter); |
| 198 } |
| 199 } |
| 200 |
| 201 profiles_.reset(); |
| 202 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); |
| 203 iter != profiles->end(); ++iter) { |
| 204 profiles_.push_back(new AutoFillProfile(*iter)); |
| 205 } |
| 206 } |
| 207 |
| 208 |
| 107 void PersonalDataManager::GetPossibleFieldTypes(const string16& text, | 209 void PersonalDataManager::GetPossibleFieldTypes(const string16& text, |
| 108 FieldTypeSet* possible_types) { | 210 FieldTypeSet* possible_types) { |
| 109 InitializeIfNeeded(); | 211 InitializeIfNeeded(); |
| 110 | 212 |
| 111 string16 clean_info = StringToLowerASCII(CollapseWhitespace(text, false)); | 213 string16 clean_info = StringToLowerASCII(CollapseWhitespace(text, false)); |
| 112 | 214 |
| 113 if (clean_info.empty()) { | 215 if (clean_info.empty()) { |
| 114 possible_types->insert(EMPTY_TYPE); | 216 possible_types->insert(EMPTY_TYPE); |
| 115 return; | 217 return; |
| 116 } | 218 } |
| 117 | 219 |
| 118 ScopedVector<FormGroup>::const_iterator iter; | 220 for (ScopedVector<AutoFillProfile>::iterator iter = profiles_.begin(); |
| 119 for (iter = profiles_.begin(); iter != profiles_.end(); ++iter) { | 221 iter != profiles_.end(); ++iter) { |
| 120 const FormGroup* profile = *iter; | 222 const FormGroup* profile = *iter; |
| 121 if (!profile) { | 223 if (!profile) { |
| 122 DLOG(ERROR) << "NULL information in profiles list"; | 224 DLOG(ERROR) << "NULL information in profiles list"; |
| 123 continue; | 225 continue; |
| 124 } | 226 } |
| 125 profile->GetPossibleFieldTypes(clean_info, possible_types); | 227 profile->GetPossibleFieldTypes(clean_info, possible_types); |
| 126 } | 228 } |
| 127 | 229 |
| 128 for (iter = credit_cards_.begin(); iter != credit_cards_.end(); ++iter) { | 230 for (ScopedVector<FormGroup>::iterator iter = credit_cards_.begin(); |
| 231 iter != credit_cards_.end(); ++iter) { |
| 129 const FormGroup* credit_card = *iter; | 232 const FormGroup* credit_card = *iter; |
| 130 if (!credit_card) { | 233 if (!credit_card) { |
| 131 DLOG(ERROR) << "NULL information in credit cards list"; | 234 DLOG(ERROR) << "NULL information in credit cards list"; |
| 132 continue; | 235 continue; |
| 133 } | 236 } |
| 134 credit_card->GetPossibleFieldTypes(clean_info, possible_types); | 237 credit_card->GetPossibleFieldTypes(clean_info, possible_types); |
| 135 } | 238 } |
| 136 | 239 |
| 137 if (possible_types->size() == 0) | 240 if (possible_types->size() == 0) |
| 138 possible_types->insert(UNKNOWN_TYPE); | 241 possible_types->insert(UNKNOWN_TYPE); |
| 139 } | 242 } |
| 140 | 243 |
| 141 bool PersonalDataManager::HasPassword() { | 244 bool PersonalDataManager::HasPassword() { |
| 142 InitializeIfNeeded(); | 245 InitializeIfNeeded(); |
| 143 return !password_hash_.empty(); | 246 return !password_hash_.empty(); |
| 144 } | 247 } |
| 145 | 248 |
| 146 PersonalDataManager::PersonalDataManager() | 249 PersonalDataManager::PersonalDataManager(Profile* profile) |
| 147 : is_initialized_(false) { | 250 : profile_(profile), |
| 251 is_initialized_(false), |
| 252 is_data_loaded_(false), |
| 253 pending_query_handle_(0), |
| 254 observer_(NULL) { |
| 255 LoadProfiles(); |
| 148 } | 256 } |
| 149 | 257 |
| 150 void PersonalDataManager::InitializeIfNeeded() { | 258 void PersonalDataManager::InitializeIfNeeded() { |
| 151 if (is_initialized_) | 259 if (is_initialized_) |
| 152 return; | 260 return; |
| 153 | 261 |
| 154 is_initialized_ = true; | 262 is_initialized_ = true; |
| 155 // TODO(jhawkins): Load data. | 263 // TODO(jhawkins): Load data. |
| 156 } | 264 } |
| 157 | 265 |
| 158 int PersonalDataManager::CreateNextUniqueId() { | 266 int PersonalDataManager::CreateNextUniqueID() { |
| 159 // Profile IDs MUST start at 1 to allow 0 as an error value when reading | 267 // Profile IDs MUST start at 1 to allow 0 as an error value when reading |
| 160 // the ID from the WebDB (see LoadData()). | 268 // the ID from the WebDB (see LoadData()). |
| 161 int id = 1; | 269 int id = 1; |
| 162 while (unique_ids_.count(id) != 0) | 270 while (unique_ids_.count(id) != 0) |
| 163 ++id; | 271 ++id; |
| 164 unique_ids_.insert(id); | 272 unique_ids_.insert(id); |
| 165 return id; | 273 return id; |
| 166 } | 274 } |
| 167 | 275 |
| 168 void PersonalDataManager::ParsePhoneNumber( | 276 void PersonalDataManager::ParsePhoneNumber( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 183 string16 city_code_str = value->substr(kPhoneCityCodeLength, | 291 string16 city_code_str = value->substr(kPhoneCityCodeLength, |
| 184 value->size() - kPhoneCityCodeLength); | 292 value->size() - kPhoneCityCodeLength); |
| 185 profile->SetInfo(AutoFillType(city_code), city_code_str); | 293 profile->SetInfo(AutoFillType(city_code), city_code_str); |
| 186 value->resize(value->size() - kPhoneCityCodeLength); | 294 value->resize(value->size() - kPhoneCityCodeLength); |
| 187 if (value->empty()) | 295 if (value->empty()) |
| 188 return; | 296 return; |
| 189 | 297 |
| 190 // Treat any remaining digits as the country code. | 298 // Treat any remaining digits as the country code. |
| 191 profile->SetInfo(AutoFillType(country_code), *value); | 299 profile->SetInfo(AutoFillType(country_code), *value); |
| 192 } | 300 } |
| 301 |
| 302 void PersonalDataManager::LoadProfiles() { |
| 303 WebDataService* web_data_service = |
| 304 profile_->GetWebDataService(Profile::EXPLICIT_ACCESS); |
| 305 if (!web_data_service) { |
| 306 NOTREACHED(); |
| 307 return; |
| 308 } |
| 309 |
| 310 CancelPendingQuery(); |
| 311 |
| 312 pending_query_handle_ = web_data_service->GetAutoFillProfiles(this); |
| 313 } |
| 314 |
| 315 void PersonalDataManager::CancelPendingQuery() { |
| 316 if (pending_query_handle_) { |
| 317 WebDataService* web_data_service = |
| 318 profile_->GetWebDataService(Profile::EXPLICIT_ACCESS); |
| 319 if (!web_data_service) { |
| 320 NOTREACHED(); |
| 321 return; |
| 322 } |
| 323 web_data_service->CancelRequest(pending_query_handle_); |
| 324 } |
| 325 pending_query_handle_ = 0; |
| 326 } |
| OLD | NEW |