1/*
2 * Copyright (C) 2013 Google 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#ifndef HTMLResourcePreloader_h
27#define HTMLResourcePreloader_h
28
29#include "CachedResource.h"
30#include "CachedResourceRequest.h"
31
32namespace WebCore {
33
34class PreloadRequest {
35public:
36    PreloadRequest(const String& initiator, const String& resourceURL, const URL& baseURL, CachedResource::Type resourceType, const String& mediaAttribute)
37        : m_initiator(initiator)
38        , m_resourceURL(resourceURL.isolatedCopy())
39        , m_baseURL(baseURL.copy())
40        , m_resourceType(resourceType)
41        , m_mediaAttribute(mediaAttribute.isolatedCopy())
42        , m_crossOriginModeAllowsCookies(false)
43    {
44    }
45
46    bool isSafeToSendToAnotherThread() const;
47
48    CachedResourceRequest resourceRequest(Document&);
49
50    const String& charset() const { return m_charset; }
51    const String& media() const { return m_mediaAttribute; }
52    void setCharset(const String& charset) { m_charset = charset.isolatedCopy(); }
53    void setCrossOriginModeAllowsCookies(bool allowsCookies) { m_crossOriginModeAllowsCookies = allowsCookies; }
54    CachedResource::Type resourceType() const { return m_resourceType; }
55
56private:
57    URL completeURL(Document&);
58
59    String m_initiator;
60    String m_resourceURL;
61    URL m_baseURL;
62    String m_charset;
63    CachedResource::Type m_resourceType;
64    String m_mediaAttribute;
65    bool m_crossOriginModeAllowsCookies;
66};
67
68typedef Vector<std::unique_ptr<PreloadRequest>> PreloadRequestStream;
69
70class HTMLResourcePreloader {
71    WTF_MAKE_NONCOPYABLE(HTMLResourcePreloader); WTF_MAKE_FAST_ALLOCATED;
72public:
73    explicit HTMLResourcePreloader(Document& document)
74        : m_document(document)
75        , m_weakFactory(this)
76    {
77    }
78
79    void preload(PreloadRequestStream);
80    void preload(std::unique_ptr<PreloadRequest>);
81
82    WeakPtr<HTMLResourcePreloader> createWeakPtr() { return m_weakFactory.createWeakPtr(); }
83
84private:
85    Document& m_document;
86    WeakPtrFactory<HTMLResourcePreloader> m_weakFactory;
87};
88
89}
90
91#endif
92