1/*
2 * Copyright (C) 2009, 2010 Research In Motion Limited. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19#ifndef ResourceRequest_h
20#define ResourceRequest_h
21
22#include "ResourceRequestBase.h"
23
24namespace BlackBerry {
25namespace Platform {
26class NetworkRequest;
27}
28}
29
30namespace WebCore {
31
32class ResourceRequest : public ResourceRequestBase {
33public:
34    // The type of this ResourceRequest, based on how the resource will be used.
35    enum TargetType {
36        TargetIsMainFrame,
37        TargetIsSubframe,
38        TargetIsSubresource, // Resource is a generic subresource. (Generally a specific type should be specified)
39        TargetIsStyleSheet,
40        TargetIsScript,
41        TargetIsFontResource,
42        TargetIsImage,
43        TargetIsObject,
44        TargetIsMedia,
45        TargetIsWorker,
46        TargetIsSharedWorker,
47        TargetIsPrefetch,
48        TargetIsFavicon,
49        TargetIsXHR,
50        TargetIsTextTrack,
51        TargetIsUnspecified,
52    };
53    ResourceRequest(const String& url)
54        : ResourceRequestBase(KURL(ParsedURLString, url), UseProtocolCachePolicy)
55        , m_isXMLHTTPRequest(false)
56        , m_mustHandleInternally(false)
57        , m_forceDownload(false)
58        , m_targetType(TargetIsUnspecified)
59    {
60    }
61
62    ResourceRequest(const KURL& url)
63        : ResourceRequestBase(url, UseProtocolCachePolicy)
64        , m_isXMLHTTPRequest(false)
65        , m_mustHandleInternally(false)
66        , m_forceDownload(false)
67        , m_targetType(TargetIsUnspecified)
68    {
69    }
70
71    ResourceRequest(const KURL& url, const String& referrer, ResourceRequestCachePolicy policy = UseProtocolCachePolicy)
72        : ResourceRequestBase(url, policy)
73        , m_isXMLHTTPRequest(false)
74        , m_mustHandleInternally(false)
75        , m_forceDownload(false)
76        , m_targetType(TargetIsUnspecified)
77    {
78        setHTTPReferrer(referrer);
79    }
80
81    ResourceRequest()
82        : ResourceRequestBase(KURL(), UseProtocolCachePolicy)
83        , m_isXMLHTTPRequest(false)
84        , m_mustHandleInternally(false)
85        , m_forceDownload(false)
86        , m_targetType(TargetIsUnspecified)
87    {
88    }
89
90    void setToken(const String& token) { m_token = token; }
91    String token() const { return m_token; }
92
93    // FIXME: For RIM Bug #452. The BlackBerry application wants the anchor text for a clicked hyperlink so as to
94    // make an informed decision as to whether to allow the navigation. We should move this functionality into a
95    // UI/Policy delegate.
96    void setAnchorText(const String& anchorText) { m_anchorText = anchorText; }
97    String anchorText() const { return m_anchorText; }
98
99    void setOverrideContentType(const String& contentType) { m_overrideContentType = contentType; }
100    String overrideContentType() const { return m_overrideContentType; }
101
102    void setIsXMLHTTPRequest(bool isXMLHTTPRequest) { m_isXMLHTTPRequest = isXMLHTTPRequest; }
103    bool isXMLHTTPRequest() const { return m_isXMLHTTPRequest; }
104
105    // Marks requests which must be handled by webkit even if LinksHandledExternally is set.
106    void setMustHandleInternally(bool mustHandleInternally) { m_mustHandleInternally = mustHandleInternally; }
107    bool mustHandleInternally() const { return m_mustHandleInternally; }
108
109    void initializePlatformRequest(BlackBerry::Platform::NetworkRequest&, bool cookiesEnabled, bool isInitial = false, bool rereadCookies = false) const;
110    void setForceDownload(bool forceDownload) { m_forceDownload = forceDownload; }
111    bool forceDownload() const { return m_forceDownload; }
112    void setSuggestedSaveName(const String& name) { m_suggestedSaveName = name; }
113    String suggestedSaveName() const { return m_suggestedSaveName; }
114
115    // What this request is for.
116    TargetType targetType() const { return m_targetType; }
117    void setTargetType(TargetType type) { m_targetType = type; }
118
119    static TargetType targetTypeFromMimeType(const String& mimeType);
120
121    void clearHTTPContentLength();
122    void clearHTTPContentType();
123
124private:
125    friend class ResourceRequestBase;
126
127    String m_token;
128    String m_anchorText;
129    String m_overrideContentType;
130    String m_suggestedSaveName;
131    bool m_isXMLHTTPRequest;
132    bool m_mustHandleInternally;
133    bool m_forceDownload;
134    TargetType m_targetType;
135
136    void doUpdatePlatformRequest() { }
137    void doUpdateResourceRequest() { }
138    void doUpdatePlatformHTTPBody() { }
139    void doUpdateResourceHTTPBody() { }
140
141    PassOwnPtr<CrossThreadResourceRequestData> doPlatformCopyData(PassOwnPtr<CrossThreadResourceRequestData>) const;
142    void doPlatformAdopt(PassOwnPtr<CrossThreadResourceRequestData>);
143};
144
145struct CrossThreadResourceRequestData : public CrossThreadResourceRequestDataBase {
146    String m_token;
147    String m_anchorText;
148    String m_overrideContentType;
149    String m_suggestedSaveName;
150    bool m_isXMLHTTPRequest;
151    bool m_mustHandleInternally;
152    bool m_forceDownload;
153    ResourceRequest::TargetType m_targetType;
154};
155
156} // namespace WebCore
157
158#endif // ResourceRequest_h
159