summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill/personal_data_manager.cc
blob: e2b21500074ae87622948786f84348e872fc9b08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/autofill/personal_data_manager.h"

#include <algorithm>
#include <iterator>

#include "base/logging.h"
#include "chrome/browser/autofill/autofill_manager.h"
#include "chrome/browser/autofill/autofill_field.h"
#include "chrome/browser/autofill/form_structure.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/webdata/web_data_service.h"

// The minimum number of fields that must contain user data and have known types
// before autofill will attempt to import the data into a profile.
static const int kMinImportSize = 5;

// The number of digits in a phone number.
static const int kPhoneNumberLength = 7;

// The number of digits in an area code.
static const int kPhoneCityCodeLength = 3;

PersonalDataManager::~PersonalDataManager() {
  CancelPendingQuery();
}

void PersonalDataManager::OnWebDataServiceRequestDone(
    WebDataService::Handle h,
    const WDTypedResult* result) {
  // Error from the web database.
  if (!result)
    return;

  DCHECK(pending_query_handle_);
  DCHECK(result->GetType() == AUTOFILL_PROFILES_RESULT);
  pending_query_handle_ = 0;

  unique_ids_.clear();
  profiles_.reset();
  const WDResult<std::vector<AutoFillProfile*> >* r =
      static_cast<const WDResult<std::vector<AutoFillProfile*> >*>(result);
  std::vector<AutoFillProfile*> profiles = r->GetValue();
  for (std::vector<AutoFillProfile*>::iterator iter = profiles.begin();
       iter != profiles.end(); ++iter) {
    unique_ids_.insert((*iter)->unique_id());
    profiles_.push_back(*iter);
  }

  is_data_loaded_ = true;
  if (observer_)
    observer_->OnPersonalDataLoaded();
}

void PersonalDataManager::SetObserver(PersonalDataManager::Observer* observer) {
  DCHECK(observer_ == NULL);
  observer_ = observer;
}

void PersonalDataManager::RemoveObserver(
    PersonalDataManager::Observer* observer) {
  if (observer_ == observer)
    observer_ = NULL;
}

bool PersonalDataManager::ImportFormData(
    const std::vector<FormStructure*>& form_structures,
    AutoFillManager* autofill_manager) {
  InitializeIfNeeded();

  // Parse the form and construct a profile based on the information that is
  // possible to import.
  int importable_fields = 0;
  int importable_credit_card_fields = 0;
  imported_profile_.reset(new AutoFillProfile(string16(),
                                              CreateNextUniqueID()));
  imported_credit_card_.reset(new CreditCard(string16()));

  bool billing_address_info = false;
  std::vector<FormStructure*>::const_iterator iter;
  for (iter = form_structures.begin(); iter != form_structures.end(); ++iter) {
    const FormStructure* form = *iter;
    for (size_t i = 0; i < form->field_count(); ++i) {
      const AutoFillField* field = form->field(i);
      string16 value = CollapseWhitespace(field->value(), false);

      // If we don't know the type of the field, or the user hasn't entered any
      // information into the field, then skip it.
      if (!field->IsFieldFillable() || value.empty())
        continue;

      AutoFillType field_type(field->type());
      FieldTypeGroup group(field_type.group());

      if (group == AutoFillType::CREDIT_CARD) {
        // If the user has a password set, we have no way of setting credit
        // card numbers.
        if (!HasPassword()) {
          imported_credit_card_->SetInfo(AutoFillType(field_type.field_type()),
                                         value);
          ++importable_credit_card_fields;
        }
      } else {
        // In the case of a phone number, if the whole phone number was entered
        // into a single field, then parse it and set the sub components.
        if (field_type.subgroup() == AutoFillType::PHONE_WHOLE_NUMBER) {
          if (group == AutoFillType::PHONE_HOME) {
            ParsePhoneNumber(imported_profile_.get(), &value, PHONE_HOME_NUMBER,
                             PHONE_HOME_CITY_CODE, PHONE_HOME_COUNTRY_CODE);
          } else if (group == AutoFillType::PHONE_FAX) {
            ParsePhoneNumber(imported_profile_.get(), &value, PHONE_FAX_NUMBER,
                             PHONE_FAX_CITY_CODE, PHONE_FAX_COUNTRY_CODE);
          }
          continue;
        }

        imported_profile_->SetInfo(AutoFillType(field_type.field_type()),
                                   value);

        // If we found any billing address information, then set the profile to
        // use a separate billing address.
        if (group == AutoFillType::ADDRESS_BILLING)
          billing_address_info = true;

        ++importable_fields;
      }
    }
  }

  // If the user did not enter enough information on the page then don't bother
  // importing the data.
  if (importable_fields + importable_credit_card_fields < kMinImportSize)
    return false;

  if (!billing_address_info)
    imported_profile_->set_use_billing_address(false);

  if (importable_fields == 0)
    imported_profile_.reset();

  if (importable_credit_card_fields == 0)
    imported_credit_card_.reset();

  // TODO(jhawkins): Alert the AutoFillManager that we have data.

  return true;
}

void PersonalDataManager::SetProfiles(std::vector<AutoFillProfile>* profiles) {
  if (profile_->IsOffTheRecord())
    return;

  WebDataService* wds = profile_->GetWebDataService(Profile::EXPLICIT_ACCESS);
  if (!wds)
    return;

  // Remove the unique IDs of the new set of profiles from the unique ID set.
  for (std::vector<AutoFillProfile>::iterator iter = profiles->begin();
       iter != profiles->end(); ++iter) {
    if (iter->unique_id() != 0)
      unique_ids_.erase(iter->unique_id());
  }

  // Any remaining IDs are not in the new profile list and should be removed
  // from the web database.
  for (std::set<int>::iterator iter = unique_ids_.begin();
       iter != unique_ids_.end(); ++iter) {
    wds->RemoveAutoFillProfile(*iter);
  }

  // Clear the unique IDs.  The set of unique IDs is updated for each profile
  // added to |profile_| below.
  unique_ids_.clear();

  // Update the web database with the existing profiles.  We need to handle
  // these first so that |unique_ids_| is reset with the IDs of the existing
  // profiles; otherwise, new profiles added before older profiles can take
  // their unique ID.
  for (std::vector<AutoFillProfile>::iterator iter = profiles->begin();
       iter != profiles->end(); ++iter) {
    if (iter->unique_id() != 0) {
      unique_ids_.insert(iter->unique_id());
      wds->UpdateAutoFillProfile(*iter);
    }
  }

  // Add the new profiles to the web database.
  for (std::vector<AutoFillProfile>::iterator iter = profiles->begin();
       iter != profiles->end(); ++iter) {
    // Profile was added by the AutoFill dialog, so we need to set the unique
    // ID.  This also means we need to add this profile to the web DB.
    if (iter->unique_id() == 0) {
      iter->set_unique_id(CreateNextUniqueID());
      wds->AddAutoFillProfile(*iter);
    }
  }

  profiles_.reset();
  for (std::vector<AutoFillProfile>::iterator iter = profiles->begin();
       iter != profiles->end(); ++iter) {
    profiles_.push_back(new AutoFillProfile(*iter));
  }
}


void PersonalDataManager::GetPossibleFieldTypes(const string16& text,
                                                FieldTypeSet* possible_types) {
  InitializeIfNeeded();

  string16 clean_info = StringToLowerASCII(CollapseWhitespace(text, false));

  if (clean_info.empty()) {
    possible_types->insert(EMPTY_TYPE);
    return;
  }

  for (ScopedVector<AutoFillProfile>::iterator iter = profiles_.begin();
       iter != profiles_.end(); ++iter) {
    const FormGroup* profile = *iter;
    if (!profile) {
      DLOG(ERROR) << "NULL information in profiles list";
      continue;
    }
    profile->GetPossibleFieldTypes(clean_info, possible_types);
  }

  for (ScopedVector<FormGroup>::iterator iter = credit_cards_.begin();
       iter != credit_cards_.end(); ++iter) {
    const FormGroup* credit_card = *iter;
    if (!credit_card) {
      DLOG(ERROR) << "NULL information in credit cards list";
      continue;
    }
    credit_card->GetPossibleFieldTypes(clean_info, possible_types);
  }

  if (possible_types->size() == 0)
    possible_types->insert(UNKNOWN_TYPE);
}

bool PersonalDataManager::HasPassword() {
  InitializeIfNeeded();
  return !password_hash_.empty();
}

PersonalDataManager::PersonalDataManager(Profile* profile)
    : profile_(profile),
      is_initialized_(false),
      is_data_loaded_(false),
      pending_query_handle_(0),
      observer_(NULL) {
  LoadProfiles();
}

void PersonalDataManager::InitializeIfNeeded() {
  if (is_initialized_)
    return;

  is_initialized_ = true;
  // TODO(jhawkins): Load data.
}

int PersonalDataManager::CreateNextUniqueID() {
  // Profile IDs MUST start at 1 to allow 0 as an error value when reading
  // the ID from the WebDB (see LoadData()).
  int id = 1;
  while (unique_ids_.count(id) != 0)
    ++id;
  unique_ids_.insert(id);
  return id;
}

void PersonalDataManager::ParsePhoneNumber(
    AutoFillProfile* profile,
    string16* value,
    AutoFillFieldType number,
    AutoFillFieldType city_code,
    AutoFillFieldType country_code) const {
  // Treat the last 7 digits as the number.
  string16 number_str = value->substr(kPhoneNumberLength,
                                      value->size() - kPhoneNumberLength);
  profile->SetInfo(AutoFillType(number), number_str);
  value->resize(value->size() - kPhoneNumberLength);
  if (value->empty())
    return;

  // Treat the next three digits as the city code.
  string16 city_code_str = value->substr(kPhoneCityCodeLength,
                                         value->size() - kPhoneCityCodeLength);
  profile->SetInfo(AutoFillType(city_code), city_code_str);
  value->resize(value->size() - kPhoneCityCodeLength);
  if (value->empty())
    return;

  // Treat any remaining digits as the country code.
  profile->SetInfo(AutoFillType(country_code), *value);
}

void PersonalDataManager::LoadProfiles() {
  WebDataService* web_data_service =
      profile_->GetWebDataService(Profile::EXPLICIT_ACCESS);
  if (!web_data_service) {
    NOTREACHED();
    return;
  }

  CancelPendingQuery();

  pending_query_handle_ = web_data_service->GetAutoFillProfiles(this);
}

void PersonalDataManager::CancelPendingQuery() {
  if (pending_query_handle_) {
    WebDataService* web_data_service =
        profile_->GetWebDataService(Profile::EXPLICIT_ACCESS);
    if (!web_data_service) {
      NOTREACHED();
      return;
    }
    web_data_service->CancelRequest(pending_query_handle_);
  }
  pending_query_handle_ = 0;
}