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 "core/fetch/FetchRequest.h"
30#include "core/fetch/Resource.h"
31#include "wtf/CurrentTime.h"
32#include "wtf/text/TextPosition.h"
33
34namespace blink {
35
36class PreloadRequest {
37public:
38    static PassOwnPtr<PreloadRequest> create(const String& initiatorName, const TextPosition& initiatorPosition, const String& resourceURL, const KURL& baseURL, Resource::Type resourceType)
39    {
40        return adoptPtr(new PreloadRequest(initiatorName, initiatorPosition, resourceURL, baseURL, resourceType));
41    }
42
43    bool isSafeToSendToAnotherThread() const;
44
45    FetchRequest resourceRequest(Document*);
46
47    const String& charset() const { return m_charset; }
48    double discoveryTime() const { return m_discoveryTime; }
49    FetchRequest::DeferOption defer() const { return m_defer; }
50    void setDefer(FetchRequest::DeferOption defer) { m_defer = defer; }
51    void setCharset(const String& charset) { m_charset = charset.isolatedCopy(); }
52    void setCrossOriginEnabled(StoredCredentials allowCredentials)
53    {
54        m_isCORSEnabled = true;
55        m_allowCredentials = allowCredentials;
56    }
57
58    Resource::Type resourceType() const { return m_resourceType; }
59
60private:
61    PreloadRequest(const String& initiatorName, const TextPosition& initiatorPosition, const String& resourceURL, const KURL& baseURL, Resource::Type resourceType)
62        : m_initiatorName(initiatorName)
63        , m_initiatorPosition(initiatorPosition)
64        , m_resourceURL(resourceURL.isolatedCopy())
65        , m_baseURL(baseURL.copy())
66        , m_resourceType(resourceType)
67        , m_isCORSEnabled(false)
68        , m_allowCredentials(DoNotAllowStoredCredentials)
69        , m_discoveryTime(monotonicallyIncreasingTime())
70        , m_defer(FetchRequest::NoDefer)
71    {
72    }
73
74    KURL completeURL(Document*);
75
76    String m_initiatorName;
77    TextPosition m_initiatorPosition;
78    String m_resourceURL;
79    KURL m_baseURL;
80    String m_charset;
81    Resource::Type m_resourceType;
82    bool m_isCORSEnabled;
83    StoredCredentials m_allowCredentials;
84    double m_discoveryTime;
85    FetchRequest::DeferOption m_defer;
86};
87
88typedef Vector<OwnPtr<PreloadRequest> > PreloadRequestStream;
89
90class HTMLResourcePreloader FINAL : public NoBaseWillBeGarbageCollected<HTMLResourcePreloader> {
91    WTF_MAKE_NONCOPYABLE(HTMLResourcePreloader); WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
92public:
93    static PassOwnPtrWillBeRawPtr<HTMLResourcePreloader> create(Document&);
94    void trace(Visitor*);
95
96    void takeAndPreload(PreloadRequestStream&);
97    void preload(PassOwnPtr<PreloadRequest>);
98
99private:
100    explicit HTMLResourcePreloader(Document&);
101
102    RawPtrWillBeMember<Document> m_document;
103};
104
105}
106
107#endif
108