1 /*
2  * Copyright (C) 2009 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 "FrameLoaderTypes.h"
35 #include "SubresourceLoaderClient.h"
36 #include "ThreadableLoader.h"
37 #include <wtf/Forward.h>
38 #include <wtf/OwnPtr.h>
39 #include <wtf/PassRefPtr.h>
40 #include <wtf/RefCounted.h>
41 #include <wtf/RefPtr.h>
42 #include <wtf/text/WTFString.h>
43 
44 namespace WebCore {
45     class Document;
46     class KURL;
47     class ResourceRequest;
48     class ThreadableLoaderClient;
49 
50     class DocumentThreadableLoader : public RefCounted<DocumentThreadableLoader>, public ThreadableLoader, private SubresourceLoaderClient  {
51         WTF_MAKE_FAST_ALLOCATED;
52     public:
53         static void loadResourceSynchronously(Document*, const ResourceRequest&, ThreadableLoaderClient&, const ThreadableLoaderOptions&);
54         static PassRefPtr<DocumentThreadableLoader> create(Document*, ThreadableLoaderClient*, const ResourceRequest&, const ThreadableLoaderOptions&, const String& optionalOutgoingReferrer = String());
55         virtual ~DocumentThreadableLoader();
56 
57         virtual void cancel();
58         virtual void setDefersLoading(bool);
59 
60         using RefCounted<DocumentThreadableLoader>::ref;
61         using RefCounted<DocumentThreadableLoader>::deref;
62 
63     protected:
refThreadableLoader()64         virtual void refThreadableLoader() { ref(); }
derefThreadableLoader()65         virtual void derefThreadableLoader() { deref(); }
66 
67     private:
68         enum BlockingBehavior {
69             LoadSynchronously,
70             LoadAsynchronously
71         };
72 
73         DocumentThreadableLoader(Document*, ThreadableLoaderClient*, BlockingBehavior, const ResourceRequest&, const ThreadableLoaderOptions&, const String& optionalOutgoingReferrer);
74 
75         virtual void willSendRequest(SubresourceLoader*, ResourceRequest&, const ResourceResponse& redirectResponse);
76         virtual void didSendData(SubresourceLoader*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent);
77 
78         virtual void didReceiveResponse(SubresourceLoader*, const ResourceResponse&);
79         virtual void didReceiveData(SubresourceLoader*, const char*, int dataLength);
80         virtual void didReceiveCachedMetadata(SubresourceLoader*, const char*, int dataLength);
81         virtual void didFinishLoading(SubresourceLoader*, double);
82         virtual void didFail(SubresourceLoader*, const ResourceError&);
83 
84         virtual bool getShouldUseCredentialStorage(SubresourceLoader*, bool& shouldUseCredentialStorage);
85         virtual void didReceiveAuthenticationChallenge(SubresourceLoader*, const AuthenticationChallenge&);
86         virtual void receivedCancellation(SubresourceLoader*, const AuthenticationChallenge&);
87 
88         void didFinishLoading(unsigned long identifier, double finishTime);
89         void makeSimpleCrossOriginAccessRequest(const ResourceRequest& request);
90         void makeCrossOriginAccessRequestWithPreflight(const ResourceRequest& request);
91         void preflightSuccess();
92         void preflightFailure(const String& url, const String& errorDescription);
93 
94         void loadRequest(const ResourceRequest&, SecurityCheckPolicy);
95         bool isAllowedRedirect(const KURL&);
96 
97         RefPtr<SubresourceLoader> m_loader;
98         ThreadableLoaderClient* m_client;
99         Document* m_document;
100         ThreadableLoaderOptions m_options;
101         String m_optionalOutgoingReferrer;
102         bool m_sameOriginRequest;
103         bool m_async;
104         OwnPtr<ResourceRequest> m_actualRequest;  // non-null during Access Control preflight checks
105     };
106 
107 } // namespace WebCore
108 
109 #endif // DocumentThreadableLoader_h
110