summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill_manager.cc
blob: 8025cbb8ea8aa82663d7ff00829ea6c3f8477c66 (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
// Copyright (c) 2006-2008 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_manager.h"

#include "base/string_util.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/tab_contents/web_contents.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"
#include "webkit/glue/autofill_form.h"

// static
void AutofillManager::RegisterUserPrefs(PrefService* prefs) {
  prefs->RegisterBooleanPref(prefs::kFormAutofillEnabled, true);
}

AutofillManager::AutofillManager(WebContents* web_contents)
    : web_contents_(web_contents),
      pending_query_handle_(0),
      node_id_(0),
      request_id_(0) {
  form_autofill_enabled_.Init(prefs::kFormAutofillEnabled,
      profile()->GetPrefs(), NULL);
}

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

void AutofillManager::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;
}

Profile* AutofillManager::profile() {
  return web_contents_->profile();
}

void AutofillManager::AutofillFormSubmitted(const AutofillForm& form) {
  StoreFormEntriesInWebDatabase(form);
}

void AutofillManager::FetchValuesForName(const std::wstring& name,
                                         const std::wstring& prefix,
                                         int limit,
                                         int64 node_id,
                                         int request_id) {
  if (!*form_autofill_enabled_)
    return;

  WebDataService* web_data_service =
      profile()->GetWebDataService(Profile::EXPLICIT_ACCESS);
  if (!web_data_service) {
    NOTREACHED();
    return;
  }

  CancelPendingQuery();

  node_id_ = node_id;
  request_id_ = request_id;

  pending_query_handle_ = web_data_service->
      GetFormValuesForElementName(name, prefix, limit, this);
}

void AutofillManager::OnWebDataServiceRequestDone(WebDataService::Handle h,
    const WDTypedResult* result) {
  DCHECK(pending_query_handle_);
  pending_query_handle_ = 0;

  if (!*form_autofill_enabled_)
    return;

  DCHECK(result);
  if (!result)
    return;

  switch (result->GetType()) {
    case AUTOFILL_VALUE_RESULT: {
      RenderViewHost* host = web_contents_->render_view_host();
      if (!host)
        return;
      const WDResult<std::vector<std::wstring> >* r =
          static_cast<const WDResult<std::vector<std::wstring> >*>(result);
      std::vector<std::wstring> suggestions = r->GetValue();
      host->AutofillSuggestionsReturned(suggestions, node_id_, request_id_, -1);
      break;
    }

    default:
      NOTREACHED();
      break;
  }
}

void AutofillManager::StoreFormEntriesInWebDatabase(
    const AutofillForm& form) {
  if (!*form_autofill_enabled_)
    return;

  if (profile()->IsOffTheRecord())
    return;

  profile()->GetWebDataService(Profile::EXPLICIT_ACCESS)->
      AddAutofillFormElements(form.elements);
}