1/*
2 * Copyright 2010, The Android Open Source Project
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 *  * Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 *  * 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 THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER 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 WebUrlLoaderClient_h
27#define WebUrlLoaderClient_h
28
29#include "ChromiumIncludes.h"
30#include "RefCounted.h"
31#include "WebResponse.h"
32#include "WebUrlLoader.h"
33
34#include <string>
35#include <deque>
36#include <string>
37#include <vector>
38
39
40namespace base {
41class ConditionVariable;
42class Lock;
43class Thread;
44}
45
46namespace net {
47class IOBuffer;
48class AuthChallengeInfo;
49}
50
51namespace android {
52
53class WebFrame;
54class WebRequest;
55class WebRequestContext;
56
57// This class handles communication between the IO thread where loading happens
58// and the webkit main thread.
59// TODO:
60// - Implement didFail
61// - Implement sync requests
62// - Implement downloadFile
63// - Implement pauseLoad
64class WebUrlLoaderClient : public base::RefCountedThreadSafe<WebUrlLoaderClient> {
65public:
66    WebUrlLoaderClient(WebFrame*, WebCore::ResourceHandle*, const WebCore::ResourceRequest&);
67
68    // Called from WebCore, will be forwarded to the IO thread
69    bool start(bool isMainResource, bool isMainFrame, bool sync, WebRequestContext*);
70    void cancel();
71    void downloadFile();
72    void pauseLoad(bool pause);
73    void setAuth(const std::string& username, const std::string& password);
74    void cancelAuth();
75    void proceedSslCertError();
76    void cancelSslCertError(int cert_error);
77
78    typedef void CallbackFunction(void*);
79
80    // This is called from the IO thread, and dispatches the callback to the main thread.
81    // (For asynchronous calls, we just delegate to WebKit's callOnMainThread.)
82    void maybeCallOnMainThread(Task* task);
83
84    // Called by WebRequest (using maybeCallOnMainThread), should be forwarded to WebCore.
85    void didReceiveResponse(PassOwnPtr<WebResponse>);
86    void didReceiveData(scoped_refptr<net::IOBuffer>, int size);
87    void didReceiveDataUrl(PassOwnPtr<std::string>);
88    void didReceiveAndroidFileData(PassOwnPtr<std::vector<char> >);
89    void didFinishLoading();
90    void didFail(PassOwnPtr<WebResponse>);
91    void willSendRequest(PassOwnPtr<WebResponse>);
92    void authRequired(scoped_refptr<net::AuthChallengeInfo>, bool firstTime, bool suppressDialog);
93    void reportSslCertError(int cert_error, net::X509Certificate* cert);
94
95    void sslClientCert(EVP_PKEY* pkey, net::X509Certificate* chain);
96    void requestClientCert(net::SSLCertRequestInfo* cert);
97
98    // Handle to the chrome IO thread
99    static base::Thread* ioThread();
100
101private:
102    friend class base::RefCountedThreadSafe<WebUrlLoaderClient>;
103    virtual ~WebUrlLoaderClient();
104
105    void finish();
106
107    WebFrame* m_webFrame;
108    RefPtr<WebCore::ResourceHandle> m_resourceHandle;
109    bool m_isMainResource;
110    bool m_isMainFrame;
111    bool m_isCertMimeType;
112    bool m_cancelling;
113    bool m_sync;
114    volatile bool m_finished;
115
116    scoped_refptr<WebRequest> m_request;
117    OwnPtr<WebResponse> m_response; // NULL until didReceiveResponse is called.
118
119    // Check if a request is active
120    bool isActive() const;
121
122    // Mutex and condition variable used for synchronous requests.
123    // Note that these are static. This works because there's only one main thread.
124    static base::Lock* syncLock();
125    static base::ConditionVariable* syncCondition();
126
127    // Queue of callbacks to be executed by the main thread. Must only be accessed inside mutex.
128    std::deque<Task*> m_queue;
129};
130
131} // namespace android
132
133#endif
134