tizen beta release
[framework/web/webkit-efl.git] / Source / WebCore / platform / network / cf / SocketStreamHandle.h
1 /*
2  * Copyright (C) 2009 Apple Inc. All rights reserved.
3  * Copyright (C) 2009 Google Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #ifndef SocketStreamHandle_h
33 #define SocketStreamHandle_h
34
35 #include "AuthenticationClient.h"
36 #include "SocketStreamHandleBase.h"
37 #include <wtf/RetainPtr.h>
38
39 typedef struct __CFHTTPMessage* CFHTTPMessageRef;
40
41 namespace WebCore {
42
43 class AuthenticationChallenge;
44 class Credential;
45 class SocketStreamHandleClient;
46
47 class SocketStreamHandle : public ThreadSafeRefCounted<SocketStreamHandle>, public SocketStreamHandleBase, public AuthenticationClient {
48 public:
49     static PassRefPtr<SocketStreamHandle> create(const KURL& url, SocketStreamHandleClient* client) { return adoptRef(new SocketStreamHandle(url, client)); }
50
51     virtual ~SocketStreamHandle();
52
53     using ThreadSafeRefCounted<SocketStreamHandle>::ref;
54     using ThreadSafeRefCounted<SocketStreamHandle>::deref;
55
56 private:
57     virtual int platformSend(const char* data, int length);
58     virtual void platformClose();
59
60     SocketStreamHandle(const KURL&, SocketStreamHandleClient*);
61     void createStreams();
62     void scheduleStreams();
63     void chooseProxy();
64     void chooseProxyFromArray(CFArrayRef);
65     void executePACFileURL(CFURLRef);
66     void removePACRunLoopSource();
67     RetainPtr<CFRunLoopSourceRef> m_pacRunLoopSource;
68     static void pacExecutionCallback(void* client, CFArrayRef proxyList, CFErrorRef error);
69     static void pacExecutionCallbackMainThread(void*);
70     static CFStringRef copyPACExecutionDescription(void*);
71
72     bool shouldUseSSL() const { return m_url.protocolIs("wss"); }
73     unsigned short port() const;
74
75     void addCONNECTCredentials(CFHTTPMessageRef response);
76
77     static void* retainSocketStreamHandle(void*);
78     static void releaseSocketStreamHandle(void*);
79     static CFStringRef copyCFStreamDescription(void*);
80     static void readStreamCallback(CFReadStreamRef, CFStreamEventType, void*);
81     static void writeStreamCallback(CFWriteStreamRef, CFStreamEventType, void*);
82 #if PLATFORM(WIN)
83     static void readStreamCallbackMainThread(void*);
84     static void writeStreamCallbackMainThread(void*);
85 #endif
86     void readStreamCallback(CFStreamEventType);
87     void writeStreamCallback(CFStreamEventType);
88
89     void reportErrorToClient(CFErrorRef);
90
91     // No authentication for streams per se, but proxy may ask for credentials.
92     virtual void receivedCredential(const AuthenticationChallenge&, const Credential&);
93     virtual void receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&);
94     virtual void receivedCancellation(const AuthenticationChallenge&);
95
96     virtual void refAuthenticationClient() { ref(); }
97     virtual void derefAuthenticationClient() { deref(); }
98
99     enum ConnectingSubstate { New, ExecutingPACFile, WaitingForCredentials, WaitingForConnect, Connected };
100     ConnectingSubstate m_connectingSubstate;
101
102     enum ConnectionType { Unknown, Direct, SOCKSProxy, CONNECTProxy };
103     ConnectionType m_connectionType;
104     RetainPtr<CFStringRef> m_proxyHost;
105     RetainPtr<CFNumberRef> m_proxyPort;
106
107     RetainPtr<CFHTTPMessageRef> m_proxyResponseMessage;
108     bool m_sentStoredCredentials;
109     RetainPtr<CFReadStreamRef> m_readStream;
110     RetainPtr<CFWriteStreamRef> m_writeStream;
111
112     RetainPtr<CFURLRef> m_httpsURL; // ws(s): replaced with https:
113 };
114
115 }  // namespace WebCore
116
117 #endif  // SocketStreamHandle_h