Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 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 |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 9 | * notice, this list of conditions and the following disclaimer. |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 12 | * documentation and/or other materials provided with the distribution. |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 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 |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 15 | * from this software without specific prior written permission. |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 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" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 33 | #include "core/platform/network/ResourceRequest.h" |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 34 | #include "public/platform/WebURLLoader.h" |
| 35 | #include "public/platform/WebURLLoaderClient.h" |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 36 | #include "wtf/Forward.h" |
| 37 | #include "wtf/RefCounted.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 38 | |
| 39 | namespace WebCore { |
| 40 | |
Ben Murdoch | fff8884 | 2013-07-30 15:20:09 +0100 | [diff] [blame] | 41 | class Resource; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 42 | class KURL; |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 43 | class ResourceError; |
| 44 | class ResourceResponse; |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 45 | class ResourceLoaderHost; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 46 | |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 47 | class ResourceLoader : public RefCounted<ResourceLoader>, protected WebKit::WebURLLoaderClient { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 48 | public: |
Ben Murdoch | fff8884 | 2013-07-30 15:20:09 +0100 | [diff] [blame] | 49 | static PassRefPtr<ResourceLoader> create(ResourceLoaderHost*, Resource*, const ResourceRequest&, const ResourceLoaderOptions&); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 50 | virtual ~ResourceLoader(); |
| 51 | |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 52 | static void loadResourceSynchronously(const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data); |
| 53 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 54 | void cancel(); |
| 55 | void cancel(const ResourceError&); |
| 56 | void cancelIfNotFinishing(); |
| 57 | |
Ben Murdoch | fff8884 | 2013-07-30 15:20:09 +0100 | [diff] [blame] | 58 | Resource* cachedResource() { return m_resource; } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 59 | const ResourceRequest& originalRequest() const { return m_originalRequest; } |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 60 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 61 | void setDefersLoading(bool); |
| 62 | bool defersLoading() const { return m_defersLoading; } |
| 63 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 64 | void releaseResources(); |
| 65 | |
| 66 | void didChangePriority(ResourceLoadPriority); |
| 67 | |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 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; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 77 | |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 78 | const KURL& url() const { return m_request.url(); } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 79 | bool shouldSendResourceLoadCallbacks() const { return m_options.sendLoadCallbacks == SendCallbacks; } |
| 80 | bool shouldSniffContent() const { return m_options.sniffContent == SniffContent; } |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 81 | bool isLoadedBy(ResourceLoaderHost*) const; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 82 | |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 83 | bool reachedTerminalState() const { return m_state == Terminated; } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 84 | const ResourceRequest& request() const { return m_request; } |
| 85 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 86 | private: |
Ben Murdoch | fff8884 | 2013-07-30 15:20:09 +0100 | [diff] [blame] | 87 | ResourceLoader(ResourceLoaderHost*, Resource*, const ResourceLoaderOptions&); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 88 | |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 89 | void init(const ResourceRequest&); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 90 | void start(); |
| 91 | |
| 92 | void didFinishLoadingOnePart(double finishTime); |
| 93 | |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 94 | OwnPtr<WebKit::WebURLLoader> m_loader; |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 95 | RefPtr<ResourceLoaderHost> m_host; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 96 | |
| 97 | ResourceRequest m_request; |
| 98 | ResourceRequest m_originalRequest; // Before redirects. |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 99 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 100 | bool m_notifiedLoadComplete; |
| 101 | |
| 102 | bool m_defersLoading; |
| 103 | ResourceRequest m_deferredRequest; |
| 104 | ResourceLoaderOptions m_options; |
| 105 | |
| 106 | enum ResourceLoaderState { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 107 | Initialized, |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 108 | Finishing, |
| 109 | Terminated |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 110 | }; |
| 111 | |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 112 | enum ConnectionState { |
| 113 | ConnectionStateNew, |
| 114 | ConnectionStateStarted, |
| 115 | ConnectionStateReceivedResponse, |
| 116 | ConnectionStateReceivingData, |
| 117 | ConnectionStateFinishedLoading, |
| 118 | ConnectionStateCanceled, |
| 119 | ConnectionStateFailed, |
| 120 | }; |
| 121 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 122 | class RequestCountTracker { |
| 123 | public: |
Ben Murdoch | fff8884 | 2013-07-30 15:20:09 +0100 | [diff] [blame] | 124 | RequestCountTracker(ResourceLoaderHost*, Resource*); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 125 | ~RequestCountTracker(); |
| 126 | private: |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 127 | ResourceLoaderHost* m_host; |
Ben Murdoch | fff8884 | 2013-07-30 15:20:09 +0100 | [diff] [blame] | 128 | Resource* m_resource; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 129 | }; |
| 130 | |
Ben Murdoch | fff8884 | 2013-07-30 15:20:09 +0100 | [diff] [blame] | 131 | Resource* m_resource; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 132 | ResourceLoaderState m_state; |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 133 | |
| 134 | // Used for sanity checking to make sure we don't experience illegal state |
| 135 | // transitions. |
| 136 | ConnectionState m_connectionState; |
| 137 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 138 | OwnPtr<RequestCountTracker> m_requestCountTracker; |
| 139 | }; |
| 140 | |
| 141 | } |
| 142 | |
| 143 | #endif |