Home | History | Annotate | Download | only in network
      1 /*
      2  * Copyright (C) 2004, 2006 Apple Computer, 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
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef ResourceHandle_h
     27 #define ResourceHandle_h
     28 
     29 #include "AuthenticationChallenge.h"
     30 #include "AuthenticationClient.h"
     31 #include "HTTPHeaderMap.h"
     32 #include "ThreadableLoader.h"
     33 #include <wtf/OwnPtr.h>
     34 
     35 #if USE(SOUP)
     36 typedef struct _SoupSession SoupSession;
     37 #endif
     38 
     39 #if PLATFORM(CF)
     40 typedef const struct __CFData * CFDataRef;
     41 #endif
     42 
     43 #if PLATFORM(WIN)
     44 typedef unsigned long DWORD;
     45 typedef unsigned long DWORD_PTR;
     46 typedef void* LPVOID;
     47 typedef LPVOID HINTERNET;
     48 typedef unsigned WPARAM;
     49 typedef long LPARAM;
     50 typedef struct HWND__* HWND;
     51 typedef _W64 long LONG_PTR;
     52 typedef LONG_PTR LRESULT;
     53 #endif
     54 
     55 
     56 #if PLATFORM(MAC)
     57 #include <wtf/RetainPtr.h>
     58 #ifdef __OBJC__
     59 @class NSData;
     60 @class NSError;
     61 @class NSURLConnection;
     62 @class WebCoreResourceHandleAsDelegate;
     63 #else
     64 class NSData;
     65 class NSError;
     66 class NSURLConnection;
     67 class WebCoreResourceHandleAsDelegate;
     68 typedef struct objc_object *id;
     69 #endif
     70 #endif
     71 
     72 #if USE(CFNETWORK)
     73 typedef struct _CFURLConnection* CFURLConnectionRef;
     74 typedef int CFHTTPCookieStorageAcceptPolicy;
     75 typedef struct OpaqueCFHTTPCookieStorage* CFHTTPCookieStorageRef;
     76 #endif
     77 
     78 namespace WebCore {
     79 
     80 class AuthenticationChallenge;
     81 class Credential;
     82 class FormData;
     83 class Frame;
     84 class KURL;
     85 class ResourceError;
     86 class ResourceHandleClient;
     87 class ResourceHandleInternal;
     88 class ResourceRequest;
     89 class ResourceResponse;
     90 class SchedulePair;
     91 class SharedBuffer;
     92 
     93 template <typename T> class Timer;
     94 
     95 class ResourceHandle : public RefCounted<ResourceHandle>
     96 #if PLATFORM(MAC) || USE(CFNETWORK) || USE(CURL)
     97     , public AuthenticationClient
     98 #endif
     99     {
    100 private:
    101     ResourceHandle(const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff, bool mightDownloadFromHandle);
    102 
    103     enum FailureType {
    104         BlockedFailure,
    105         InvalidURLFailure
    106     };
    107 
    108 public:
    109     // FIXME: should not need the Frame
    110     static PassRefPtr<ResourceHandle> create(const ResourceRequest&, ResourceHandleClient*, Frame*, bool defersLoading, bool shouldContentSniff, bool mightDownloadFromHandle = false);
    111 
    112     static void loadResourceSynchronously(const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data, Frame* frame);
    113     static bool willLoadFromCache(ResourceRequest&, Frame*);
    114 #if PLATFORM(MAC)
    115     static bool didSendBodyDataDelegateExists();
    116 #endif
    117 
    118     virtual ~ResourceHandle();
    119 
    120 #if PLATFORM(MAC) || USE(CFNETWORK)
    121     void willSendRequest(ResourceRequest&, const ResourceResponse& redirectResponse);
    122     bool shouldUseCredentialStorage();
    123 #endif
    124 #if PLATFORM(MAC) || USE(CFNETWORK) || USE(CURL)
    125     void didReceiveAuthenticationChallenge(const AuthenticationChallenge&);
    126     virtual void receivedCredential(const AuthenticationChallenge&, const Credential&);
    127     virtual void receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&);
    128     virtual void receivedCancellation(const AuthenticationChallenge&);
    129 #endif
    130 
    131 #if PLATFORM(MAC)
    132     void didCancelAuthenticationChallenge(const AuthenticationChallenge&);
    133     NSURLConnection *connection() const;
    134     WebCoreResourceHandleAsDelegate *delegate();
    135     void releaseDelegate();
    136     id releaseProxy();
    137 
    138     void schedule(SchedulePair*);
    139     void unschedule(SchedulePair*);
    140 #elif USE(CFNETWORK)
    141     CFURLConnectionRef connection() const;
    142     CFURLConnectionRef releaseConnectionForDownload();
    143     static void setHostAllowsAnyHTTPSCertificate(const String&);
    144     static void setClientCertificate(const String& host, CFDataRef);
    145 #endif
    146 
    147 #if PLATFORM(WIN) && USE(CURL)
    148     static void setHostAllowsAnyHTTPSCertificate(const String&);
    149 #endif
    150 #if PLATFORM(WIN) && USE(CURL) && PLATFORM(CF)
    151     static void setClientCertificate(const String& host, CFDataRef);
    152 #endif
    153 
    154     PassRefPtr<SharedBuffer> bufferedData();
    155     static bool supportsBufferedData();
    156 
    157     bool shouldContentSniff() const;
    158     static bool shouldContentSniffURL(const KURL&);
    159 
    160     static void forceContentSniffing();
    161 
    162 #if USE(WININET)
    163     void setHasReceivedResponse(bool = true);
    164     bool hasReceivedResponse() const;
    165     void fileLoadTimer(Timer<ResourceHandle>*);
    166     void onHandleCreated(LPARAM);
    167     void onRequestRedirected(LPARAM);
    168     void onRequestComplete(LPARAM);
    169     friend void __stdcall transferJobStatusCallback(HINTERNET, DWORD_PTR, DWORD, LPVOID, DWORD);
    170     friend LRESULT __stdcall ResourceHandleWndProc(HWND, unsigned message, WPARAM, LPARAM);
    171 #endif
    172 
    173 #if PLATFORM(QT) || USE(CURL) || USE(SOUP) || PLATFORM(ANDROID)
    174     ResourceHandleInternal* getInternal() { return d.get(); }
    175 #endif
    176 
    177 #if USE(SOUP)
    178     static SoupSession* defaultSession();
    179 #endif
    180 
    181     // Used to work around the fact that you don't get any more NSURLConnection callbacks until you return from the one you're in.
    182     static bool loadsBlocked();
    183 
    184     void clearAuthentication();
    185     void cancel();
    186 
    187     // The client may be 0, in which case no callbacks will be made.
    188     ResourceHandleClient* client() const;
    189     void setClient(ResourceHandleClient*);
    190 
    191     void setDefersLoading(bool);
    192 #if PLATFORM(ANDROID)
    193 // TODO: this needs upstreaming.
    194     void pauseLoad(bool);
    195 #endif
    196 
    197     const ResourceRequest& request() const;
    198 
    199     void fireFailure(Timer<ResourceHandle>*);
    200 
    201     using RefCounted<ResourceHandle>::ref;
    202     using RefCounted<ResourceHandle>::deref;
    203 
    204 private:
    205     void scheduleFailure(FailureType);
    206 
    207     bool start(Frame*);
    208 
    209     virtual void refAuthenticationClient() { ref(); }
    210     virtual void derefAuthenticationClient() { deref(); }
    211 
    212     friend class ResourceHandleInternal;
    213     OwnPtr<ResourceHandleInternal> d;
    214 };
    215 
    216 }
    217 
    218 #endif // ResourceHandle_h
    219