summaryrefslogtreecommitdiffstats
path: root/src/core/autofill_client_qt.cpp
blob: 27834498001f4f9ff2fb6c1004028f386cb49c89 (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "autofill_client_qt.h"

#include "autofill_popup_controller.h"
#include "autofill_popup_controller_p.h"
#include "render_widget_host_view_qt.h"
#include "type_conversion.h"
#include "web_contents_adapter_client.h"
#include "web_contents_view_qt.h"

#include "chrome/browser/profiles/profile.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/core/browser/foundations/browser_autofill_manager.h"
#include "components/autofill/core/common/autofill_prefs.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"

namespace QtWebEngineCore {

void AutofillClientQt::CreateForWebContents(content::WebContents *contents)
{
    DCHECK(contents);
    if (!FromWebContents(contents))
        contents->SetUserData(UserDataKey(), base::WrapUnique(new AutofillClientQt(contents)));
}

AutofillClientQt::AutofillClientQt(content::WebContents *webContents)
    : autofill::ContentAutofillClient(webContents)
    , content::WebContentsObserver(webContents)
    , m_popupController(new AutofillPopupController(new AutofillPopupControllerPrivate))
{
}

AutofillClientQt::~AutofillClientQt() { }

autofill::AutocompleteHistoryManager *AutofillClientQt::GetAutocompleteHistoryManager()
{
    return nullptr;
}

std::unique_ptr<autofill::AutofillManager> AutofillClientQt::CreateManager(base::PassKey<autofill::ContentAutofillDriver>, autofill::ContentAutofillDriver &driver)
{
    return base::WrapUnique(new autofill::BrowserAutofillManager(&driver));
}

PrefService *AutofillClientQt::GetPrefs()
{
    return const_cast<PrefService *>(std::as_const(*this).GetPrefs());
}

const PrefService *AutofillClientQt::GetPrefs() const
{
    Profile *profile = Profile::FromBrowserContext(web_contents()->GetBrowserContext());
    return profile->GetPrefs();
}

autofill::AutofillClient::SuggestionUiSessionId AutofillClientQt::ShowAutofillSuggestions(
        const autofill::AutofillClient::PopupOpenArgs &open_args,
        base::WeakPtr<autofill::AutofillSuggestionDelegate> delegate)
{
    m_popupController->d->delegate = delegate;
    m_popupController->d->suggestions = open_args.suggestions;
    m_popupController->updateModel();

    bool autoSelectFirstSuggestion =
        open_args.trigger_source == autofill::AutofillSuggestionTriggerSource::kTextFieldDidReceiveKeyDown;

    adapterClient()->showAutofillPopup(m_popupController.data(),
                                       QRect(toQt(gfx::ToEnclosingRect(open_args.element_bounds))),
                                       autoSelectFirstSuggestion);
    return {};
}

void AutofillClientQt::UpdateAutofillDataListValues(
        base::span<const autofill::SelectOption> datalist)
{
    if (datalist.empty())
        HideAutofillSuggestions(autofill::SuggestionHidingReason::kNoSuggestions);
}


base::span<const autofill::Suggestion> AutofillClientQt::GetAutofillSuggestions() const
{
    // Called by password_manager component only.
    NOTIMPLEMENTED();
    return {};
}

void AutofillClientQt::HideAutofillSuggestions(autofill::SuggestionHidingReason)
{
    adapterClient()->hideAutofillPopup();
}

bool AutofillClientQt::IsAutofillEnabled() const
{
    // Returns false, this is not required to be enabled for <datalist>.
    return IsAutofillProfileEnabled() || IsAutofillPaymentMethodsEnabled();
}

bool AutofillClientQt::IsAutofillProfileEnabled() const
{
    return autofill::prefs::IsAutofillProfileEnabled(GetPrefs());
}

bool AutofillClientQt::IsAutofillPaymentMethodsEnabled() const
{
    return autofill::prefs::IsAutofillPaymentMethodsEnabled(GetPrefs());
}

bool AutofillClientQt::IsAutocompleteEnabled() const
{
    return autofill::prefs::IsAutocompleteEnabled(GetPrefs());
}

bool AutofillClientQt::IsPasswordManagerEnabled() const
{
    return false;
}

const std::string& AutofillClientQt::GetAppLocale() const
{
    static const std::string empty;
    return empty;
}

bool AutofillClientQt::IsOffTheRecord() const
{
    return web_contents()->GetBrowserContext()->IsOffTheRecord();
}

scoped_refptr<network::SharedURLLoaderFactory> AutofillClientQt::GetURLLoaderFactory()
{
    return nullptr;
}

WebContentsAdapterClient *AutofillClientQt::adapterClient()
{
    return WebContentsViewQt::from(
                   static_cast<content::WebContentsImpl *>(web_contents())->GetView())
            ->client();
}

base::WeakPtr<autofill::AutofillClient> AutofillClientQt::GetWeakPtr()
{
    return weak_ptr_factory_.GetWeakPtr();
}

} // namespace QtWebEngineCore