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