password_generation_manager.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "components/password_manager/core/browser/password_generation_manager.h"
6
7#include "components/autofill/core/browser/autofill_field.h"
8#include "components/autofill/core/browser/field_types.h"
9#include "components/autofill/core/browser/form_structure.h"
10#include "components/autofill/core/common/form_data.h"
11#include "components/password_manager/core/browser/password_manager.h"
12#include "components/password_manager/core/browser/password_manager_client.h"
13#include "components/password_manager/core/browser/password_manager_driver.h"
14
15PasswordGenerationManager::PasswordGenerationManager(
16    PasswordManagerClient* client)
17    : client_(client),
18      driver_(client->GetDriver()) {}
19
20PasswordGenerationManager::~PasswordGenerationManager() {}
21
22void PasswordGenerationManager::DetectAccountCreationForms(
23    const std::vector<autofill::FormStructure*>& forms) {
24  std::vector<autofill::FormData> account_creation_forms;
25  for (std::vector<autofill::FormStructure*>::const_iterator form_it =
26           forms.begin(); form_it != forms.end(); ++form_it) {
27    autofill::FormStructure* form = *form_it;
28    for (std::vector<autofill::AutofillField*>::const_iterator field_it =
29             form->begin(); field_it != form->end(); ++field_it) {
30      autofill::AutofillField* field = *field_it;
31      if (field->server_type() == autofill::ACCOUNT_CREATION_PASSWORD) {
32        account_creation_forms.push_back(form->ToFormData());
33        break;
34      }
35    }
36  }
37  if (!account_creation_forms.empty() && IsGenerationEnabled())
38    driver_->AccountCreationFormsFound(account_creation_forms);
39}
40
41// In order for password generation to be enabled, we need to make sure:
42// (1) Password sync is enabled, and
43// (2) Password saving is enabled.
44bool PasswordGenerationManager::IsGenerationEnabled() const {
45  if (!driver_->GetPasswordManager()->IsSavingEnabled()) {
46    DVLOG(2) << "Generation disabled because password saving is disabled";
47    return false;
48  }
49
50  if (!client_->IsPasswordSyncEnabled()) {
51    DVLOG(2) << "Generation disabled because passwords are not being synced";
52    return false;
53  }
54
55  return true;
56}
57