1/*
2    Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
3              (C) 2012 Digia Plc. and/or its subsidiary(-ies)
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public
7    License as published by the Free Software Foundation; either
8    version 2 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public License
16    along with this library; see the file COPYING.LIB.  If not, write to
17    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18    Boston, MA 02110-1301, USA.
19*/
20
21#include "config.h"
22
23#include "DNSResolveQueue.h"
24
25#include <QHostInfo>
26#include <QObject>
27#include <QString>
28#include <wtf/text/WTFString.h>
29
30namespace WebCore {
31
32class DnsPrefetchHelper : public QObject {
33    Q_OBJECT
34public:
35    DnsPrefetchHelper() : QObject() { }
36
37public Q_SLOTS:
38    void lookup(QString hostname)
39    {
40        if (hostname.isEmpty()) {
41            DNSResolveQueue::shared().decrementRequestCount();
42            return; // this actually happens
43        }
44
45        QHostInfo::lookupHost(hostname, this, SLOT(lookedUp(QHostInfo)));
46    }
47
48    void lookedUp(const QHostInfo&)
49    {
50        // we do not cache the result, we throw it away.
51        // we currently rely on the OS to cache the results. If it does not do that
52        // then at least the ISP nameserver did it.
53        // Since Qt 4.6.3, Qt also has a small DNS cache.
54        DNSResolveQueue::shared().decrementRequestCount();
55    }
56};
57
58// This is called on mouse over a href and on page loading.
59void prefetchDNS(const String& hostname)
60{
61    if (hostname.isEmpty())
62        return;
63    DNSResolveQueue::shared().add(hostname);
64}
65
66bool DNSResolveQueue::platformProxyIsEnabledInSystemPreferences()
67{
68    // Qt expects the system or a proxy to cache the result, but other platforms disable WebCore DNS prefetching when proxies are enabled.
69    return false;
70}
71
72// This is called by the platform-independent DNSResolveQueue.
73void DNSResolveQueue::platformResolve(const String& hostname)
74{
75    static DnsPrefetchHelper dnsPrefetchHelper;
76    dnsPrefetchHelper.lookup(QString(hostname));
77}
78
79} // namespace
80
81#include "DNSQt.moc"
82