1 /* 2 * Copyright (C) 2005, 2006, 2011 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 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 14 * its contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef ResourceLoader_h 30 #define ResourceLoader_h 31 32 #include "core/loader/ResourceLoaderOptions.h" 33 #include "core/platform/network/ResourceRequest.h" 34 #include "public/platform/WebURLLoader.h" 35 #include "public/platform/WebURLLoaderClient.h" 36 #include "wtf/Forward.h" 37 #include "wtf/RefCounted.h" 38 39 namespace WebCore { 40 41 class Resource; 42 class KURL; 43 class ResourceError; 44 class ResourceResponse; 45 class ResourceLoaderHost; 46 47 class ResourceLoader : public RefCounted<ResourceLoader>, protected WebKit::WebURLLoaderClient { 48 public: 49 static PassRefPtr<ResourceLoader> create(ResourceLoaderHost*, Resource*, const ResourceRequest&, const ResourceLoaderOptions&); 50 virtual ~ResourceLoader(); 51 52 static void loadResourceSynchronously(const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data); 53 54 void cancel(); 55 void cancel(const ResourceError&); 56 void cancelIfNotFinishing(); 57 58 Resource* cachedResource() { return m_resource; } 59 const ResourceRequest& originalRequest() const { return m_originalRequest; } 60 61 void setDefersLoading(bool); 62 bool defersLoading() const { return m_defersLoading; } 63 64 void releaseResources(); 65 66 void didChangePriority(ResourceLoadPriority); 67 68 // WebURLLoaderClient 69 virtual void willSendRequest(WebKit::WebURLLoader*, WebKit::WebURLRequest&, const WebKit::WebURLResponse& redirectResponse) OVERRIDE; 70 virtual void didSendData(WebKit::WebURLLoader*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent) OVERRIDE; 71 virtual void didReceiveResponse(WebKit::WebURLLoader*, const WebKit::WebURLResponse&) OVERRIDE; 72 virtual void didReceiveData(WebKit::WebURLLoader*, const char*, int, int encodedDataLength) OVERRIDE; 73 virtual void didReceiveCachedMetadata(WebKit::WebURLLoader*, const char* data, int length) OVERRIDE; 74 virtual void didFinishLoading(WebKit::WebURLLoader*, double finishTime) OVERRIDE; 75 virtual void didFail(WebKit::WebURLLoader*, const WebKit::WebURLError&) OVERRIDE; 76 virtual void didDownloadData(WebKit::WebURLLoader*, int) OVERRIDE; 77 78 const KURL& url() const { return m_request.url(); } 79 bool shouldSendResourceLoadCallbacks() const { return m_options.sendLoadCallbacks == SendCallbacks; } 80 bool shouldSniffContent() const { return m_options.sniffContent == SniffContent; } 81 bool isLoadedBy(ResourceLoaderHost*) const; 82 83 bool reachedTerminalState() const { return m_state == Terminated; } 84 const ResourceRequest& request() const { return m_request; } 85 86 private: 87 ResourceLoader(ResourceLoaderHost*, Resource*, const ResourceLoaderOptions&); 88 89 void init(const ResourceRequest&); 90 void start(); 91 92 void didFinishLoadingOnePart(double finishTime); 93 94 OwnPtr<WebKit::WebURLLoader> m_loader; 95 RefPtr<ResourceLoaderHost> m_host; 96 97 ResourceRequest m_request; 98 ResourceRequest m_originalRequest; // Before redirects. 99 100 bool m_notifiedLoadComplete; 101 102 bool m_defersLoading; 103 ResourceRequest m_deferredRequest; 104 ResourceLoaderOptions m_options; 105 106 enum ResourceLoaderState { 107 Initialized, 108 Finishing, 109 Terminated 110 }; 111 112 enum ConnectionState { 113 ConnectionStateNew, 114 ConnectionStateStarted, 115 ConnectionStateReceivedResponse, 116 ConnectionStateReceivingData, 117 ConnectionStateFinishedLoading, 118 ConnectionStateCanceled, 119 ConnectionStateFailed, 120 }; 121 122 class RequestCountTracker { 123 public: 124 RequestCountTracker(ResourceLoaderHost*, Resource*); 125 ~RequestCountTracker(); 126 private: 127 ResourceLoaderHost* m_host; 128 Resource* m_resource; 129 }; 130 131 Resource* m_resource; 132 ResourceLoaderState m_state; 133 134 // Used for sanity checking to make sure we don't experience illegal state 135 // transitions. 136 ConnectionState m_connectionState; 137 138 OwnPtr<RequestCountTracker> m_requestCountTracker; 139 }; 140 141 } 142 143 #endif 144