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
46namespace blink {
47
48class Document;
49class KURL;
50class ResourceRequest;
51class SecurityOrigin;
52class ThreadableLoaderClient;
53
54class 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 overrideTimeout(unsigned long timeout) OVERRIDE;
62
63        virtual void cancel() OVERRIDE;
64        void setDefersLoading(bool);
65
66    private:
67        enum BlockingBehavior {
68            LoadSynchronously,
69            LoadAsynchronously
70        };
71
72        DocumentThreadableLoader(Document&, ThreadableLoaderClient*, BlockingBehavior, const ResourceRequest&, const ThreadableLoaderOptions&, const ResourceLoaderOptions&);
73
74        // RawResourceClient implementation
75        virtual void dataSent(Resource*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent) OVERRIDE;
76        virtual void responseReceived(Resource*, const ResourceResponse&) OVERRIDE;
77        virtual void dataReceived(Resource*, const char* data, int dataLength) OVERRIDE;
78        virtual void redirectReceived(Resource*, ResourceRequest&, const ResourceResponse&) OVERRIDE;
79        virtual void notifyFinished(Resource*) OVERRIDE;
80        virtual void dataDownloaded(Resource*, int) OVERRIDE;
81
82        void cancelWithError(const ResourceError&);
83
84        // Notify Inspector about resource response. Use this method if response is not going to be finished normally.
85        void notifyResponseReceived(unsigned long identifier, const ResourceResponse&);
86
87        // Methods containing code to handle resource fetch results which is
88        // common to both sync and async mode.
89        void handleResponse(unsigned long identifier, const ResourceResponse&);
90        void handleReceivedData(const char* data, int dataLength);
91        void handleSuccessfulFinish(unsigned long identifier, double finishTime);
92
93        void didTimeout(Timer<DocumentThreadableLoader>*);
94        void makeCrossOriginAccessRequest(const ResourceRequest&);
95        // Loads m_actualRequest.
96        void loadActualRequest();
97        // Clears m_actualRequest and reports access control check failure to
98        // m_client.
99        void handlePreflightFailure(const String& url, const String& errorDescription);
100        // Investigates the response for the preflight request. If successful,
101        // the actual request will be made later in handleSuccessfulFinish().
102        void handlePreflightResponse(const ResourceResponse&);
103
104        void loadRequest(const ResourceRequest&, ResourceLoaderOptions);
105        bool isAllowedRedirect(const KURL&) const;
106        bool isAllowedByPolicy(const KURL&) const;
107        // Returns DoNotAllowStoredCredentials
108        // if m_forceDoNotAllowStoredCredentials is set. Otherwise, just
109        // returns allowCredentials value of m_resourceLoaderOptions.
110        StoredCredentials effectiveAllowCredentials() const;
111
112        SecurityOrigin* securityOrigin() const;
113
114        ThreadableLoaderClient* m_client;
115        Document& m_document;
116
117        const ThreadableLoaderOptions m_options;
118        // Some items may be overridden by m_forceDoNotAllowStoredCredentials
119        // and m_securityOrigin. In such a case, build a ResourceLoaderOptions
120        // with up-to-date values from them and this variable, and use it.
121        const ResourceLoaderOptions m_resourceLoaderOptions;
122
123        bool m_forceDoNotAllowStoredCredentials;
124        RefPtr<SecurityOrigin> m_securityOrigin;
125
126        bool m_sameOriginRequest;
127        bool m_simpleRequest;
128        bool m_async;
129
130        // Holds the original request and options for it during preflight
131        // request handling phase.
132        OwnPtr<ResourceRequest> m_actualRequest;
133        OwnPtr<ResourceLoaderOptions> m_actualOptions;
134
135        HTTPHeaderMap m_simpleRequestHeaders; // stores simple request headers in case of a cross-origin redirect.
136        Timer<DocumentThreadableLoader> m_timeoutTimer;
137        double m_requestStartedSeconds; // Time an asynchronous fetch request is started
138    };
139
140} // namespace blink
141
142#endif // DocumentThreadableLoader_h
143