1/*
2 * Copyright (C) 2006 Apple Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "IconLoader.h"
28
29#include "CachedRawResource.h"
30#include "CachedResourceLoader.h"
31#include "CachedResourceRequest.h"
32#include "CachedResourceRequestInitiators.h"
33#include "Document.h"
34#include "Frame.h"
35#include "FrameLoader.h"
36#include "FrameLoaderClient.h"
37#include "IconController.h"
38#include "IconDatabase.h"
39#include "Logging.h"
40#include "ResourceBuffer.h"
41#include "ResourceRequest.h"
42#include <wtf/text/CString.h>
43
44namespace WebCore {
45
46IconLoader::IconLoader(Frame& frame)
47    : m_frame(frame)
48{
49}
50
51IconLoader::~IconLoader()
52{
53    stopLoading();
54}
55
56void IconLoader::startLoading()
57{
58    if (m_resource || !m_frame.document())
59        return;
60
61    CachedResourceRequest request(ResourceRequest(m_frame.loader().icon().url()), ResourceLoaderOptions(SendCallbacks, SniffContent, BufferData, DoNotAllowStoredCredentials, DoNotAskClientForAnyCredentials, DoSecurityCheck, UseDefaultOriginRestrictionsForType));
62
63    request.mutableResourceRequest().setPriority(ResourceLoadPriorityLow);
64    request.setInitiator(cachedResourceRequestInitiators().icon);
65
66    m_resource = m_frame.document()->cachedResourceLoader()->requestRawResource(request);
67    if (m_resource)
68        m_resource->addClient(this);
69    else
70        LOG_ERROR("Failed to start load for icon at url %s", m_frame.loader().icon().url().string().ascii().data());
71}
72
73void IconLoader::stopLoading()
74{
75    if (m_resource) {
76        m_resource->removeClient(this);
77        m_resource = 0;
78    }
79}
80
81void IconLoader::notifyFinished(CachedResource* resource)
82{
83    ASSERT(resource == m_resource);
84
85    // If we got a status code indicating an invalid response, then lets
86    // ignore the data and not try to decode the error page as an icon.
87    RefPtr<ResourceBuffer> data = resource->resourceBuffer();
88    int status = resource->response().httpStatusCode();
89    if (status && (status < 200 || status > 299))
90        data = 0;
91
92    static const char pdfMagicNumber[] = "%PDF";
93    static unsigned pdfMagicNumberLength = sizeof(pdfMagicNumber) - 1;
94    if (data && data->size() >= pdfMagicNumberLength && !memcmp(data->data(), pdfMagicNumber, pdfMagicNumberLength)) {
95        LOG(IconDatabase, "IconLoader::finishLoading() - Ignoring icon at %s because it appears to be a PDF", resource->url().string().ascii().data());
96        data = 0;
97    }
98
99    LOG(IconDatabase, "IconLoader::finishLoading() - Committing iconURL %s to database", resource->url().string().ascii().data());
100    m_frame.loader().icon().commitToDatabase(resource->url());
101    // Setting the icon data only after committing to the database ensures that the data is
102    // kept in memory (so it does not have to be read from the database asynchronously), since
103    // there is a page URL referencing it.
104    iconDatabase().setIconDataForIconURL(data ? data->sharedBuffer() : 0, resource->url().string());
105    m_frame.loader().client().dispatchDidReceiveIcon();
106    stopLoading();
107}
108
109}
110