blob: f7615bb2e2e91e80dcb009fab5df5ab1265e5152 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
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 Murdoch02772c62013-07-26 10:21:05 +01009 * notice, this list of conditions and the following disclaimer.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010010 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
Ben Murdoch02772c62013-07-26 10:21:05 +010012 * documentation and/or other materials provided with the distribution.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010013 * 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 Murdoch02772c62013-07-26 10:21:05 +010015 * from this software without specific prior written permission.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010016 *
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)53e740f2013-05-09 18:38:43 +010033#include "core/platform/network/ResourceRequest.h"
Ben Murdoch83750172013-07-24 10:36:59 +010034#include "public/platform/WebURLLoader.h"
35#include "public/platform/WebURLLoaderClient.h"
Ben Murdoch591b9582013-07-10 11:41:44 +010036#include "wtf/Forward.h"
37#include "wtf/RefCounted.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010038
39namespace WebCore {
40
Ben Murdochfff88842013-07-30 15:20:09 +010041class Resource;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010042class KURL;
Ben Murdoch83750172013-07-24 10:36:59 +010043class ResourceError;
44class ResourceResponse;
Ben Murdoch7757ec22013-07-23 11:17:36 +010045class ResourceLoaderHost;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010046
Ben Murdoch83750172013-07-24 10:36:59 +010047class ResourceLoader : public RefCounted<ResourceLoader>, protected WebKit::WebURLLoaderClient {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010048public:
Ben Murdochfff88842013-07-30 15:20:09 +010049 static PassRefPtr<ResourceLoader> create(ResourceLoaderHost*, Resource*, const ResourceRequest&, const ResourceLoaderOptions&);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010050 virtual ~ResourceLoader();
51
Ben Murdoch83750172013-07-24 10:36:59 +010052 static void loadResourceSynchronously(const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
53
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010054 void cancel();
55 void cancel(const ResourceError&);
56 void cancelIfNotFinishing();
57
Ben Murdochfff88842013-07-30 15:20:09 +010058 Resource* cachedResource() { return m_resource; }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010059 const ResourceRequest& originalRequest() const { return m_originalRequest; }
Ben Murdoch02772c62013-07-26 10:21:05 +010060
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010061 void setDefersLoading(bool);
62 bool defersLoading() const { return m_defersLoading; }
63
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010064 void releaseResources();
65
66 void didChangePriority(ResourceLoadPriority);
67
Ben Murdoch83750172013-07-24 10:36:59 +010068 // 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)53e740f2013-05-09 18:38:43 +010077
Ben Murdoch83750172013-07-24 10:36:59 +010078 const KURL& url() const { return m_request.url(); }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010079 bool shouldSendResourceLoadCallbacks() const { return m_options.sendLoadCallbacks == SendCallbacks; }
80 bool shouldSniffContent() const { return m_options.sniffContent == SniffContent; }
Ben Murdoch7757ec22013-07-23 11:17:36 +010081 bool isLoadedBy(ResourceLoaderHost*) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010082
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010083 bool reachedTerminalState() const { return m_state == Terminated; }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010084 const ResourceRequest& request() const { return m_request; }
85
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010086private:
Ben Murdochfff88842013-07-30 15:20:09 +010087 ResourceLoader(ResourceLoaderHost*, Resource*, const ResourceLoaderOptions&);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010088
Ben Murdoch83750172013-07-24 10:36:59 +010089 void init(const ResourceRequest&);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010090 void start();
91
92 void didFinishLoadingOnePart(double finishTime);
93
Ben Murdoch83750172013-07-24 10:36:59 +010094 OwnPtr<WebKit::WebURLLoader> m_loader;
Ben Murdoch7757ec22013-07-23 11:17:36 +010095 RefPtr<ResourceLoaderHost> m_host;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010096
97 ResourceRequest m_request;
98 ResourceRequest m_originalRequest; // Before redirects.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010099
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100100 bool m_notifiedLoadComplete;
101
102 bool m_defersLoading;
103 ResourceRequest m_deferredRequest;
104 ResourceLoaderOptions m_options;
105
106 enum ResourceLoaderState {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100107 Initialized,
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100108 Finishing,
109 Terminated
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100110 };
111
Ben Murdoch83750172013-07-24 10:36:59 +0100112 enum ConnectionState {
113 ConnectionStateNew,
114 ConnectionStateStarted,
115 ConnectionStateReceivedResponse,
116 ConnectionStateReceivingData,
117 ConnectionStateFinishedLoading,
118 ConnectionStateCanceled,
119 ConnectionStateFailed,
120 };
121
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100122 class RequestCountTracker {
123 public:
Ben Murdochfff88842013-07-30 15:20:09 +0100124 RequestCountTracker(ResourceLoaderHost*, Resource*);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100125 ~RequestCountTracker();
126 private:
Ben Murdoch7757ec22013-07-23 11:17:36 +0100127 ResourceLoaderHost* m_host;
Ben Murdochfff88842013-07-30 15:20:09 +0100128 Resource* m_resource;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100129 };
130
Ben Murdochfff88842013-07-30 15:20:09 +0100131 Resource* m_resource;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100132 ResourceLoaderState m_state;
Ben Murdoch83750172013-07-24 10:36:59 +0100133
134 // Used for sanity checking to make sure we don't experience illegal state
135 // transitions.
136 ConnectionState m_connectionState;
137
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100138 OwnPtr<RequestCountTracker> m_requestCountTracker;
139};
140
141}
142
143#endif