summaryrefslogtreecommitdiffstats
path: root/components/autofill/content/browser/request_autocomplete_manager.cc
blob: 26dfef83f480e9ad433d9f26dabb89c71f474050 (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
// Copyright 2013 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 "components/autofill/content/browser/request_autocomplete_manager.h"

#include <stddef.h>

#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/content/common/autofill_messages.h"
#include "components/autofill/core/browser/form_structure.h"
#include "components/autofill/core/common/autofill_data_validation.h"
#include "components/autofill/core/common/form_data.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "third_party/WebKit/public/web/WebFormElement.h"
#include "url/gurl.h"

namespace autofill {

namespace {

blink::WebFormElement::AutocompleteResult ToWebkitAutocompleteResult(
    AutofillClient::RequestAutocompleteResult result) {
  switch(result) {
    case AutofillClient::AutocompleteResultSuccess:
      return blink::WebFormElement::AutocompleteResultSuccess;
    case AutofillClient::AutocompleteResultErrorDisabled:
      return blink::WebFormElement::AutocompleteResultErrorDisabled;
    case AutofillClient::AutocompleteResultErrorCancel:
      return blink::WebFormElement::AutocompleteResultErrorCancel;
    case AutofillClient::AutocompleteResultErrorInvalid:
      return blink::WebFormElement::AutocompleteResultErrorInvalid;
  }

  NOTREACHED();
  return blink::WebFormElement::AutocompleteResultErrorDisabled;
}

}  // namespace

RequestAutocompleteManager::RequestAutocompleteManager(
    ContentAutofillDriver* autofill_driver)
    : autofill_driver_(autofill_driver), weak_ptr_factory_(this) {
  DCHECK(autofill_driver_);
}

RequestAutocompleteManager::~RequestAutocompleteManager() {}

void RequestAutocompleteManager::OnRequestAutocomplete(const FormData& form) {
  if (!IsValidFormData(form))
    return;

  AutofillClient::ResultCallback callback =
      base::Bind(&RequestAutocompleteManager::ReturnAutocompleteResult,
                 weak_ptr_factory_.GetWeakPtr());
  ShowRequestAutocompleteDialog(form, callback);
}

void RequestAutocompleteManager::ReturnAutocompleteResult(
    AutofillClient::RequestAutocompleteResult result,
    const base::string16& debug_message,
    const FormStructure* form_structure) {
  content::RenderFrameHost* host = autofill_driver_->render_frame_host();
  if (!host)
    return;

  FormData form_data;
  if (form_structure) {
    form_data = form_structure->ToFormData();
    for (size_t i = 0; i < form_data.fields.size(); ++i) {
      if(!form_data.fields[i].value.empty())
        form_data.fields[i].is_autofilled = true;
    }
  }

  host->Send(new AutofillMsg_RequestAutocompleteResult(
      host->GetRoutingID(),
      ToWebkitAutocompleteResult(result),
      debug_message,
      form_data));
}

void RequestAutocompleteManager::ShowRequestAutocompleteDialog(
    const FormData& form,
    const AutofillClient::ResultCallback& callback) {
  AutofillClient* client = autofill_driver_->autofill_manager()->client();
  client->ShowRequestAutocompleteDialog(
      form, autofill_driver_->render_frame_host(), callback);
}

}  // namespace autofill