Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / fetch / ResourceLoader.h
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/fetch/ResourceLoaderOptions.h"
33 #include "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 FINAL : public RefCounted<ResourceLoader>, protected blink::WebURLLoaderClient {
48 public:
49     static PassRefPtr<ResourceLoader> create(ResourceLoaderHost*, Resource*, const ResourceRequest&, const ResourceLoaderOptions&);
50     virtual ~ResourceLoader();
51
52     void start();
53     void changeToSynchronous();
54
55     void cancel();
56     void cancel(const ResourceError&);
57     void cancelIfNotFinishing();
58
59     Resource* cachedResource() { return m_resource; }
60     const ResourceRequest& originalRequest() const { return m_originalRequest; }
61
62     void setDefersLoading(bool);
63     bool defersLoading() const { return m_defersLoading; }
64
65     void releaseResources();
66
67     void didChangePriority(ResourceLoadPriority);
68
69     // WebURLLoaderClient
70     virtual void willSendRequest(blink::WebURLLoader*, blink::WebURLRequest&, const blink::WebURLResponse& redirectResponse) OVERRIDE;
71     virtual void didSendData(blink::WebURLLoader*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent) OVERRIDE;
72     virtual void didReceiveResponse(blink::WebURLLoader*, const blink::WebURLResponse&) OVERRIDE;
73     virtual void didReceiveData(blink::WebURLLoader*, const char*, int, int encodedDataLength) OVERRIDE;
74     virtual void didReceiveCachedMetadata(blink::WebURLLoader*, const char* data, int length) OVERRIDE;
75     virtual void didFinishLoading(blink::WebURLLoader*, double finishTime, int64 encodedDataLength) OVERRIDE;
76     virtual void didFail(blink::WebURLLoader*, const blink::WebURLError&) OVERRIDE;
77     virtual void didDownloadData(blink::WebURLLoader*, int, int) OVERRIDE;
78
79     const KURL& url() const { return m_request.url(); }
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 requestSynchronously();
91
92     void didFinishLoadingOnePart(double finishTime, int64_t encodedDataLength);
93
94     bool responseNeedsAccessControlCheck() const;
95
96     OwnPtr<blink::WebURLLoader> m_loader;
97     RefPtr<ResourceLoaderHost> m_host;
98
99     ResourceRequest m_request;
100     ResourceRequest m_originalRequest; // Before redirects.
101
102     bool m_notifiedLoadComplete;
103
104     bool m_defersLoading;
105     ResourceRequest m_deferredRequest;
106     ResourceLoaderOptions m_options;
107
108     enum ResourceLoaderState {
109         Initialized,
110         Finishing,
111         Terminated
112     };
113
114     enum ConnectionState {
115         ConnectionStateNew,
116         ConnectionStateStarted,
117         ConnectionStateReceivedResponse,
118         ConnectionStateReceivingData,
119         ConnectionStateFinishedLoading,
120         ConnectionStateCanceled,
121         ConnectionStateFailed,
122     };
123
124     class RequestCountTracker {
125     public:
126         RequestCountTracker(ResourceLoaderHost*, Resource*);
127         ~RequestCountTracker();
128     private:
129         ResourceLoaderHost* m_host;
130         Resource* m_resource;
131     };
132
133     Resource* m_resource;
134     ResourceLoaderState m_state;
135
136     // Used for sanity checking to make sure we don't experience illegal state
137     // transitions.
138     ConnectionState m_connectionState;
139
140     OwnPtr<RequestCountTracker> m_requestCountTracker;
141 };
142
143 }
144
145 #endif