1/*
2 * Copyright (C) 2009, 2012 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef DocumentThreadableLoader_h
32#define DocumentThreadableLoader_h
33
34#include "CachedRawResourceClient.h"
35#include "CachedResourceHandle.h"
36#include "FrameLoaderTypes.h"
37#include "ThreadableLoader.h"
38#include <wtf/Forward.h>
39#include <wtf/OwnPtr.h>
40#include <wtf/PassRefPtr.h>
41#include <wtf/RefCounted.h>
42#include <wtf/RefPtr.h>
43#include <wtf/text/WTFString.h>
44
45namespace WebCore {
46    class CachedRawResource;
47    class Document;
48    class KURL;
49    class ResourceRequest;
50    class SecurityOrigin;
51    class ThreadableLoaderClient;
52
53    class DocumentThreadableLoader : public RefCounted<DocumentThreadableLoader>, public ThreadableLoader, private CachedRawResourceClient  {
54        WTF_MAKE_FAST_ALLOCATED;
55    public:
56        static void loadResourceSynchronously(Document*, const ResourceRequest&, ThreadableLoaderClient&, const ThreadableLoaderOptions&);
57        static PassRefPtr<DocumentThreadableLoader> create(Document*, ThreadableLoaderClient*, const ResourceRequest&, const ThreadableLoaderOptions&);
58        virtual ~DocumentThreadableLoader();
59
60        virtual void cancel();
61        virtual void setDefersLoading(bool);
62
63        using RefCounted<DocumentThreadableLoader>::ref;
64        using RefCounted<DocumentThreadableLoader>::deref;
65
66    protected:
67        virtual void refThreadableLoader() { ref(); }
68        virtual void derefThreadableLoader() { deref(); }
69
70    private:
71        enum BlockingBehavior {
72            LoadSynchronously,
73            LoadAsynchronously
74        };
75
76        DocumentThreadableLoader(Document*, ThreadableLoaderClient*, BlockingBehavior, const ResourceRequest&, const ThreadableLoaderOptions&);
77
78        void clearResource();
79
80        // CachedRawResourceClient
81        virtual void dataSent(CachedResource*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent);
82        virtual void responseReceived(CachedResource*, const ResourceResponse&);
83        virtual void dataReceived(CachedResource*, const char* data, int dataLength);
84        virtual void redirectReceived(CachedResource*, ResourceRequest&, const ResourceResponse&);
85        virtual void notifyFinished(CachedResource*);
86
87        void didReceiveResponse(unsigned long identifier, const ResourceResponse&);
88        void didReceiveData(unsigned long identifier, const char* data, int dataLength);
89        void didFinishLoading(unsigned long identifier, double finishTime);
90        void didFail(unsigned long identifier, const ResourceError&);
91        void makeCrossOriginAccessRequest(const ResourceRequest&);
92        void makeSimpleCrossOriginAccessRequest(const ResourceRequest& request);
93        void makeCrossOriginAccessRequestWithPreflight(const ResourceRequest& request);
94        void preflightSuccess();
95        void preflightFailure(unsigned long identifier, const String& url, const String& errorDescription);
96
97        void loadRequest(const ResourceRequest&, SecurityCheckPolicy);
98        bool isAllowedRedirect(const KURL&);
99
100        SecurityOrigin* securityOrigin() const;
101
102        CachedResourceHandle<CachedRawResource> m_resource;
103        ThreadableLoaderClient* m_client;
104        Document* m_document;
105        ThreadableLoaderOptions m_options;
106        bool m_sameOriginRequest;
107        bool m_simpleRequest;
108        bool m_async;
109        OwnPtr<ResourceRequest> m_actualRequest;  // non-null during Access Control preflight checks
110    };
111
112} // namespace WebCore
113
114#endif // DocumentThreadableLoader_h
115