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/loader/cache/FetchRequest.h" 30 #include "core/loader/cache/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 setCrossOriginModeAllowsCookies(bool allowsCookies) { m_crossOriginModeAllowsCookies = allowsCookies; } 57 Resource::Type resourceType() const { return m_resourceType; } 58 59 private: 60 PreloadRequest(const String& initiatorName, const TextPosition& initiatorPosition, const String& resourceURL, const KURL& baseURL, Resource::Type resourceType, const String& mediaAttribute) 61 : m_initiatorName(initiatorName) 62 , m_initiatorPosition(initiatorPosition) 63 , m_resourceURL(resourceURL.isolatedCopy()) 64 , m_baseURL(baseURL.copy()) 65 , m_resourceType(resourceType) 66 , m_mediaAttribute(mediaAttribute.isolatedCopy()) 67 , m_crossOriginModeAllowsCookies(false) 68 , m_discoveryTime(monotonicallyIncreasingTime()) 69 { 70 } 71 72 KURL completeURL(Document*); 73 74 String m_initiatorName; 75 TextPosition m_initiatorPosition; 76 String m_resourceURL; 77 KURL m_baseURL; 78 String m_charset; 79 Resource::Type m_resourceType; 80 String m_mediaAttribute; 81 bool m_crossOriginModeAllowsCookies; 82 double m_discoveryTime; 83 }; 84 85 typedef Vector<OwnPtr<PreloadRequest> > PreloadRequestStream; 86 87 class HTMLResourcePreloader { 88 WTF_MAKE_NONCOPYABLE(HTMLResourcePreloader); WTF_MAKE_FAST_ALLOCATED; 89 public: 90 explicit HTMLResourcePreloader(Document* document) 91 : m_document(document) 92 , m_weakFactory(this) 93 { 94 } 95 96 void takeAndPreload(PreloadRequestStream&); 97 void preload(PassOwnPtr<PreloadRequest>); 98 99 WeakPtr<HTMLResourcePreloader> createWeakPtr() { return m_weakFactory.createWeakPtr(); } 100 101 private: 102 Document* m_document; 103 WeakPtrFactory<HTMLResourcePreloader> m_weakFactory; 104 }; 105 106 } 107 108 #endif 109