1/*
2 * Copyright (C) 2009 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2012 Igalia S.L.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "DNSResolveQueue.h"
29
30#include <wtf/CurrentTime.h>
31
32namespace WebCore {
33
34// When resolve queue is empty, we fire async resolution requests immediately (which is important if the prefetch is triggered by hovering).
35// But during page parsing, we should coalesce identical requests to avoid stressing out the DNS resolver.
36static const int gNamesToResolveImmediately = 4;
37
38// Coalesce prefetch requests for this long before sending them out.
39static const double gCoalesceDelayInSeconds = 1.0;
40
41// Sending many DNS requests at once can overwhelm some gateways. See <rdar://8105550> for specific CFNET issues with CFHost throttling.
42static const int gMaxSimultaneousRequests = 8;
43
44// For a page has links to many outside sites, it is likely that the system DNS resolver won't be able to cache them all anyway, and we don't want
45// to negatively affect other applications' performance by pushing their cached entries out.
46// If we end up with lots of names to prefetch, some will be dropped.
47static const int gMaxRequestsToQueue = 64;
48
49// If there were queued names that couldn't be sent simultaneously, check the state of resolvers after this delay.
50static const double gRetryResolvingInSeconds = 0.1;
51
52DNSResolveQueue::DNSResolveQueue()
53    : m_requestsInFlight(0)
54    , m_cachedProxyEnabledStatus(false)
55    , m_lastProxyEnabledStatusCheckTime(0)
56{
57}
58
59bool DNSResolveQueue::isUsingProxy()
60{
61    double time = currentTime();
62    static const double minimumProxyCheckDelay = 5;
63    if (time - m_lastProxyEnabledStatusCheckTime > minimumProxyCheckDelay) {
64        m_lastProxyEnabledStatusCheckTime = time;
65        m_cachedProxyEnabledStatus = platformProxyIsEnabledInSystemPreferences();
66    }
67    return m_cachedProxyEnabledStatus;
68}
69
70void DNSResolveQueue::add(const String& hostname)
71{
72    // If there are no names queued, and few enough are in flight, resolve immediately (the mouse may be over a link).
73    if (!m_names.size()) {
74        if (isUsingProxy())
75            return;
76        if (atomicIncrement(&m_requestsInFlight) <= gNamesToResolveImmediately) {
77            platformResolve(hostname);
78            return;
79        }
80        atomicDecrement(&m_requestsInFlight);
81    }
82
83    // It's better to not prefetch some names than to clog the queue.
84    // Dropping the newest names, because on a single page, these are likely to be below oldest ones.
85    if (m_names.size() < gMaxRequestsToQueue) {
86        m_names.add(hostname);
87        if (!isActive())
88            startOneShot(gCoalesceDelayInSeconds);
89    }
90}
91
92void DNSResolveQueue::fired()
93{
94    if (isUsingProxy()) {
95        m_names.clear();
96        return;
97    }
98
99    int requestsAllowed = gMaxSimultaneousRequests - m_requestsInFlight;
100
101    for (; !m_names.isEmpty() && requestsAllowed > 0; --requestsAllowed) {
102        atomicIncrement(&m_requestsInFlight);
103        HashSet<String>::iterator currentName = m_names.begin();
104        platformResolve(*currentName);
105        m_names.remove(currentName);
106    }
107
108    if (!m_names.isEmpty())
109        startOneShot(gRetryResolvingInSeconds);
110}
111
112} // namespace WebCore
113