Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / loader / DocumentThreadableLoader.h
1 /*
2  * Copyright (C) 2009, 2012 Google Inc. All rights reserved.
3  * Copyright (C) 2013, Intel Corporation
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #ifndef DocumentThreadableLoader_h
33 #define DocumentThreadableLoader_h
34
35 #include "core/fetch/RawResource.h"
36 #include "core/fetch/ResourceOwner.h"
37 #include "core/loader/ThreadableLoader.h"
38 #include "platform/Timer.h"
39 #include "platform/network/HTTPHeaderMap.h"
40 #include "platform/network/ResourceError.h"
41 #include "wtf/Forward.h"
42 #include "wtf/OwnPtr.h"
43 #include "wtf/PassRefPtr.h"
44 #include "wtf/text/WTFString.h"
45
46 namespace WebCore {
47
48 class Document;
49 class KURL;
50 class ResourceRequest;
51 class SecurityOrigin;
52 class ThreadableLoaderClient;
53
54 class DocumentThreadableLoader FINAL : public ThreadableLoader, private ResourceOwner<RawResource>  {
55     WTF_MAKE_FAST_ALLOCATED;
56     public:
57         static void loadResourceSynchronously(Document&, const ResourceRequest&, ThreadableLoaderClient&, const ThreadableLoaderOptions&, const ResourceLoaderOptions&);
58         static PassRefPtr<DocumentThreadableLoader> create(Document&, ThreadableLoaderClient*, const ResourceRequest&, const ThreadableLoaderOptions&, const ResourceLoaderOptions&);
59         virtual ~DocumentThreadableLoader();
60
61         virtual void cancel() OVERRIDE;
62         void setDefersLoading(bool);
63
64     private:
65         enum BlockingBehavior {
66             LoadSynchronously,
67             LoadAsynchronously
68         };
69
70         DocumentThreadableLoader(Document&, ThreadableLoaderClient*, BlockingBehavior, const ResourceRequest&, const ThreadableLoaderOptions&, const ResourceLoaderOptions&);
71
72         // RawResourceClient implementation
73         virtual void dataSent(Resource*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent) OVERRIDE;
74         virtual void responseReceived(Resource*, const ResourceResponse&) OVERRIDE;
75         virtual void dataReceived(Resource*, const char* data, int dataLength) OVERRIDE;
76         virtual void redirectReceived(Resource*, ResourceRequest&, const ResourceResponse&) OVERRIDE;
77         virtual void notifyFinished(Resource*) OVERRIDE;
78         virtual void dataDownloaded(Resource*, int) OVERRIDE;
79
80         void cancelWithError(const ResourceError&);
81
82         // Methods containing code to handle resource fetch results which is
83         // common to both sync and async mode.
84         void handleResponse(unsigned long identifier, const ResourceResponse&);
85         void handleReceivedData(const char* data, int dataLength);
86         void handleSuccessfulFinish(unsigned long identifier, double finishTime);
87
88         void didTimeout(Timer<DocumentThreadableLoader>*);
89         void makeCrossOriginAccessRequest(const ResourceRequest&);
90         // Loads m_actualRequest.
91         void loadActualRequest();
92         // Clears m_actualRequest and reports access control check failure to
93         // m_client.
94         void handlePreflightFailure(const String& url, const String& errorDescription);
95         // Investigates the response for the preflight request. If successful,
96         // the actual request will be made later in handleSuccessfulFinish().
97         void handlePreflightResponse(unsigned long identifier, const ResourceResponse&);
98
99         void loadRequest(const ResourceRequest&, ResourceLoaderOptions);
100         bool isAllowedRedirect(const KURL&) const;
101         bool isAllowedByPolicy(const KURL&) const;
102         // Returns DoNotAllowStoredCredentials
103         // if m_forceDoNotAllowStoredCredentials is set. Otherwise, just
104         // returns allowCredentials value of m_resourceLoaderOptions.
105         StoredCredentials effectiveAllowCredentials() const;
106
107         SecurityOrigin* securityOrigin() const;
108
109         ThreadableLoaderClient* m_client;
110         Document& m_document;
111
112         const ThreadableLoaderOptions m_options;
113         // Some items may be overridden by m_forceDoNotAllowStoredCredentials
114         // and m_securityOrigin. In such a case, build a ResourceLoaderOptions
115         // with up-to-date values from them and this variable, and use it.
116         const ResourceLoaderOptions m_resourceLoaderOptions;
117
118         bool m_forceDoNotAllowStoredCredentials;
119         RefPtr<SecurityOrigin> m_securityOrigin;
120
121         bool m_sameOriginRequest;
122         bool m_simpleRequest;
123         bool m_async;
124
125         // Holds the original request and options for it during preflight
126         // request handling phase.
127         OwnPtr<ResourceRequest> m_actualRequest;
128         OwnPtr<ResourceLoaderOptions> m_actualOptions;
129
130         HTTPHeaderMap m_simpleRequestHeaders; // stores simple request headers in case of a cross-origin redirect.
131         Timer<DocumentThreadableLoader> m_timeoutTimer;
132     };
133
134 } // namespace WebCore
135
136 #endif // DocumentThreadableLoader_h