Home | History | Annotate | Download | only in network
      1 /*
      2  * Copyright (C) 2003, 2006 Apple Computer, Inc.  All rights reserved.
      3  * Copyright (C) 2006 Samuel Weinig <sam.weinig (at) gmail.com>
      4  * Copyright (C) 2009, 2012 Google Inc. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #ifndef ResourceRequest_h
     29 #define ResourceRequest_h
     30 
     31 #include "core/platform/network/FormData.h"
     32 #include "core/platform/network/HTTPHeaderMap.h"
     33 #include "core/platform/network/ResourceLoadPriority.h"
     34 #include "weborigin/KURL.h"
     35 #include "wtf/OwnPtr.h"
     36 
     37 namespace WebCore {
     38 
     39     enum ResourceRequestCachePolicy {
     40         UseProtocolCachePolicy, // normal load
     41         ReloadIgnoringCacheData, // reload
     42         ReturnCacheDataElseLoad, // back/forward or encoding change - allow stale data
     43         ReturnCacheDataDontLoad  // results of a post - allow stale data and only use cache
     44     };
     45 
     46     struct CrossThreadResourceRequestData;
     47 
     48     // Do not use this type directly.  Use ResourceRequest instead.
     49     class ResourceRequest {
     50         WTF_MAKE_FAST_ALLOCATED;
     51     public:
     52         // The type of this ResourceRequest, based on how the resource will be used.
     53         enum TargetType {
     54             TargetIsMainFrame,
     55             TargetIsSubframe,
     56             TargetIsSubresource, // Resource is a generic subresource. (Generally a specific type should be specified)
     57             TargetIsStyleSheet,
     58             TargetIsScript,
     59             TargetIsFont,
     60             TargetIsImage,
     61             TargetIsObject,
     62             TargetIsMedia,
     63             TargetIsWorker,
     64             TargetIsSharedWorker,
     65             TargetIsPrefetch,
     66             TargetIsFavicon,
     67             TargetIsXHR,
     68             TargetIsTextTrack,
     69             TargetIsUnspecified,
     70         };
     71 
     72         class ExtraData : public RefCounted<ExtraData> {
     73         public:
     74             virtual ~ExtraData() { }
     75         };
     76 
     77         ResourceRequest()
     78         {
     79             initialize(KURL(), UseProtocolCachePolicy);
     80         }
     81 
     82         ResourceRequest(const String& urlString)
     83         {
     84             initialize(KURL(ParsedURLString, urlString), UseProtocolCachePolicy);
     85         }
     86 
     87         ResourceRequest(const KURL& url)
     88         {
     89             initialize(url, UseProtocolCachePolicy);
     90         }
     91 
     92         ResourceRequest(const KURL& url, const String& referrer, ResourceRequestCachePolicy cachePolicy = UseProtocolCachePolicy)
     93         {
     94             initialize(url, cachePolicy);
     95             setHTTPReferrer(referrer);
     96         }
     97 
     98         static PassOwnPtr<ResourceRequest> adopt(PassOwnPtr<CrossThreadResourceRequestData>);
     99 
    100         // Gets a copy of the data suitable for passing to another thread.
    101         PassOwnPtr<CrossThreadResourceRequestData> copyData() const;
    102 
    103         bool isNull() const;
    104         bool isEmpty() const;
    105 
    106         const KURL& url() const;
    107         void setURL(const KURL& url);
    108 
    109         void removeCredentials();
    110 
    111         ResourceRequestCachePolicy cachePolicy() const;
    112         void setCachePolicy(ResourceRequestCachePolicy cachePolicy);
    113 
    114         double timeoutInterval() const; // May return 0 when using platform default.
    115         void setTimeoutInterval(double timeoutInterval);
    116 
    117         const KURL& firstPartyForCookies() const;
    118         void setFirstPartyForCookies(const KURL& firstPartyForCookies);
    119 
    120         const String& httpMethod() const;
    121         void setHTTPMethod(const String& httpMethod);
    122 
    123         const HTTPHeaderMap& httpHeaderFields() const;
    124         String httpHeaderField(const AtomicString& name) const;
    125         String httpHeaderField(const char* name) const;
    126         void setHTTPHeaderField(const AtomicString& name, const String& value);
    127         void setHTTPHeaderField(const char* name, const String& value);
    128         void addHTTPHeaderField(const AtomicString& name, const String& value);
    129         void addHTTPHeaderFields(const HTTPHeaderMap& headerFields);
    130 
    131         void clearHTTPAuthorization();
    132 
    133         String httpContentType() const { return httpHeaderField("Content-Type");  }
    134         void setHTTPContentType(const String& httpContentType) { setHTTPHeaderField("Content-Type", httpContentType); }
    135         void clearHTTPContentType();
    136 
    137         String httpReferrer() const { return httpHeaderField("Referer"); }
    138         void setHTTPReferrer(const String& httpReferrer) { setHTTPHeaderField("Referer", httpReferrer); }
    139         void clearHTTPReferrer();
    140 
    141         String httpOrigin() const { return httpHeaderField("Origin"); }
    142         void setHTTPOrigin(const String& httpOrigin) { setHTTPHeaderField("Origin", httpOrigin); }
    143         void clearHTTPOrigin();
    144 
    145         String httpUserAgent() const { return httpHeaderField("User-Agent"); }
    146         void setHTTPUserAgent(const String& httpUserAgent) { setHTTPHeaderField("User-Agent", httpUserAgent); }
    147         void clearHTTPUserAgent();
    148 
    149         String httpAccept() const { return httpHeaderField("Accept"); }
    150         void setHTTPAccept(const String& httpAccept) { setHTTPHeaderField("Accept", httpAccept); }
    151         void clearHTTPAccept();
    152 
    153         FormData* httpBody() const;
    154         void setHTTPBody(PassRefPtr<FormData> httpBody);
    155 
    156         bool allowCookies() const;
    157         void setAllowCookies(bool allowCookies);
    158 
    159         ResourceLoadPriority priority() const;
    160         void setPriority(ResourceLoadPriority);
    161 
    162         bool isConditional() const;
    163 
    164         // Whether the associated ResourceHandleClient needs to be notified of
    165         // upload progress made for that resource.
    166         bool reportUploadProgress() const { return m_reportUploadProgress; }
    167         void setReportUploadProgress(bool reportUploadProgress) { m_reportUploadProgress = reportUploadProgress; }
    168 
    169         // Whether the timing information should be collected for the request.
    170         bool reportLoadTiming() const { return m_reportLoadTiming; }
    171         void setReportLoadTiming(bool reportLoadTiming) { m_reportLoadTiming = reportLoadTiming; }
    172 
    173         // Whether actual headers being sent/received should be collected and reported for the request.
    174         bool reportRawHeaders() const { return m_reportRawHeaders; }
    175         void setReportRawHeaders(bool reportRawHeaders) { m_reportRawHeaders = reportRawHeaders; }
    176 
    177         // Allows the request to be matched up with its requestor.
    178         int requestorID() const { return m_requestorID; }
    179         void setRequestorID(int requestorID) { m_requestorID = requestorID; }
    180 
    181         // The process id of the process from which this request originated. In
    182         // the case of out-of-process plugins, this allows to link back the
    183         // request to the plugin process (as it is processed through a render
    184         // view process).
    185         int requestorProcessID() const { return m_requestorProcessID; }
    186         void setRequestorProcessID(int requestorProcessID) { m_requestorProcessID = requestorProcessID; }
    187 
    188         // Allows the request to be matched up with its app cache host.
    189         int appCacheHostID() const { return m_appCacheHostID; }
    190         void setAppCacheHostID(int id) { m_appCacheHostID = id; }
    191 
    192         // True if request was user initiated.
    193         bool hasUserGesture() const { return m_hasUserGesture; }
    194         void setHasUserGesture(bool hasUserGesture) { m_hasUserGesture = hasUserGesture; }
    195 
    196         // True if request should be downloaded to file.
    197         bool downloadToFile() const { return m_downloadToFile; }
    198         void setDownloadToFile(bool downloadToFile) { m_downloadToFile = downloadToFile; }
    199 
    200         // Extra data associated with this request.
    201         ExtraData* extraData() const { return m_extraData.get(); }
    202         void setExtraData(PassRefPtr<ExtraData> extraData) { m_extraData = extraData; }
    203 
    204         // What this request is for.
    205         TargetType targetType() const { return m_targetType; }
    206         void setTargetType(TargetType type) { m_targetType = type; }
    207 
    208         static double defaultTimeoutInterval(); // May return 0 when using platform default.
    209         static void setDefaultTimeoutInterval(double);
    210 
    211         static bool compare(const ResourceRequest&, const ResourceRequest&);
    212 
    213     private:
    214         void initialize(const KURL& url, ResourceRequestCachePolicy cachePolicy);
    215 
    216         KURL m_url;
    217         ResourceRequestCachePolicy m_cachePolicy;
    218         double m_timeoutInterval; // 0 is a magic value for platform default on platforms that have one.
    219         KURL m_firstPartyForCookies;
    220         AtomicString m_httpMethod;
    221         HTTPHeaderMap m_httpHeaderFields;
    222         RefPtr<FormData> m_httpBody;
    223         bool m_allowCookies : 1;
    224         bool m_reportUploadProgress : 1;
    225         bool m_reportLoadTiming : 1;
    226         bool m_reportRawHeaders : 1;
    227         bool m_hasUserGesture : 1;
    228         bool m_downloadToFile : 1;
    229         ResourceLoadPriority m_priority;
    230         int m_requestorID;
    231         int m_requestorProcessID;
    232         int m_appCacheHostID;
    233         RefPtr<ExtraData> m_extraData;
    234         TargetType m_targetType;
    235 
    236         static double s_defaultTimeoutInterval;
    237     };
    238 
    239     bool equalIgnoringHeaderFields(const ResourceRequest&, const ResourceRequest&);
    240 
    241     inline bool operator==(const ResourceRequest& a, const ResourceRequest& b) { return ResourceRequest::compare(a, b); }
    242     inline bool operator!=(ResourceRequest& a, const ResourceRequest& b) { return !(a == b); }
    243 
    244     struct CrossThreadResourceRequestData {
    245         WTF_MAKE_NONCOPYABLE(CrossThreadResourceRequestData); WTF_MAKE_FAST_ALLOCATED;
    246     public:
    247         CrossThreadResourceRequestData() { }
    248         KURL m_url;
    249 
    250         ResourceRequestCachePolicy m_cachePolicy;
    251         double m_timeoutInterval;
    252         KURL m_firstPartyForCookies;
    253 
    254         String m_httpMethod;
    255         OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
    256         RefPtr<FormData> m_httpBody;
    257         bool m_allowCookies;
    258         bool m_hasUserGesture;
    259         bool m_downloadToFile;
    260         ResourceLoadPriority m_priority;
    261         int m_requestorID;
    262         int m_requestorProcessID;
    263         int m_appCacheHostID;
    264         ResourceRequest::TargetType m_targetType;
    265     };
    266 
    267     unsigned initializeMaximumHTTPConnectionCountPerHost();
    268 
    269 } // namespace WebCore
    270 
    271 #endif // ResourceRequest_h
    272