2 * Copyright (C) 2005, 2006, 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #ifndef ResourceLoader_h
30 #define ResourceLoader_h
32 #include "core/CoreExport.h"
33 #include "core/fetch/ResourceLoaderOptions.h"
34 #include "platform/network/ResourceRequest.h"
35 #include "public/platform/WebURLLoader.h"
36 #include "public/platform/WebURLLoaderClient.h"
37 #include "wtf/Forward.h"
38 #include "wtf/RefCounted.h"
45 class ResourceFetcher
;
46 class ThreadedDataReceiver
;
48 class CORE_EXPORT ResourceLoader final
: public RefCountedWillBeGarbageCollectedFinalized
<ResourceLoader
>, protected WebURLLoaderClient
{
50 static PassRefPtrWillBeRawPtr
<ResourceLoader
> create(ResourceFetcher
*, Resource
*, const ResourceRequest
&, const ResourceLoaderOptions
&);
51 virtual ~ResourceLoader();
55 void changeToSynchronous();
58 void cancel(const ResourceError
&);
59 void cancelIfNotFinishing();
61 Resource
* cachedResource() { return m_resource
; }
62 const ResourceRequest
& originalRequest() const { return m_originalRequest
; }
64 void setDefersLoading(bool);
65 bool defersLoading() const { return m_defersLoading
; }
67 void attachThreadedDataReceiver(PassRefPtrWillBeRawPtr
<ThreadedDataReceiver
>);
69 void releaseResources();
71 void didChangePriority(ResourceLoadPriority
, int intraPriorityValue
);
74 void willSendRequest(WebURLLoader
*, WebURLRequest
&, const WebURLResponse
& redirectResponse
) override
;
75 void didSendData(WebURLLoader
*, unsigned long long bytesSent
, unsigned long long totalBytesToBeSent
) override
;
76 void didReceiveResponse(WebURLLoader
*, const WebURLResponse
&) override
;
77 void didReceiveResponse(WebURLLoader
*, const WebURLResponse
&, WebDataConsumerHandle
*) override
;
78 void didReceiveData(WebURLLoader
*, const char*, int, int encodedDataLength
) override
;
79 void didReceiveCachedMetadata(WebURLLoader
*, const char* data
, int length
) override
;
80 void didFinishLoading(WebURLLoader
*, double finishTime
, int64_t encodedDataLength
) override
;
81 void didFail(WebURLLoader
*, const WebURLError
&) override
;
82 void didDownloadData(WebURLLoader
*, int, int) override
;
84 const KURL
& url() const { return m_request
.url(); }
85 bool isLoadedBy(ResourceFetcher
*) const;
87 bool reachedTerminalState() const { return m_state
== Terminated
; }
88 const ResourceRequest
& request() const { return m_request
; }
90 bool loadingMultipartContent() const { return m_loadingMultipartContent
; }
93 ResourceLoader(ResourceFetcher
*, Resource
*, const ResourceLoaderOptions
&);
95 void init(const ResourceRequest
&);
96 void requestSynchronously();
98 void didFinishLoadingOnePart(double finishTime
, int64_t encodedDataLength
);
100 bool responseNeedsAccessControlCheck() const;
102 ResourceRequest
& applyOptions(ResourceRequest
&) const;
104 OwnPtr
<WebURLLoader
> m_loader
;
105 RefPtrWillBeMember
<ResourceFetcher
> m_fetcher
;
107 ResourceRequest m_request
;
108 ResourceRequest m_originalRequest
; // Before redirects.
110 bool m_notifiedLoadComplete
;
112 bool m_defersLoading
;
113 bool m_loadingMultipartContent
;
114 OwnPtr
<ResourceRequest
> m_fallbackRequestForServiceWorker
;
115 ResourceRequest m_deferredRequest
;
116 ResourceLoaderOptions m_options
;
118 enum ResourceLoaderState
{
124 enum ConnectionState
{
126 ConnectionStateStarted
,
127 ConnectionStateReceivedResponse
,
128 ConnectionStateReceivingData
,
129 ConnectionStateFinishedLoading
,
130 ConnectionStateCanceled
,
131 ConnectionStateFailed
,
134 RawPtrWillBeMember
<Resource
> m_resource
;
135 ResourceLoaderState m_state
;
137 // Used for sanity checking to make sure we don't experience illegal state
139 ConnectionState m_connectionState
;