2 * Copyright 2010, The Android Open Source Project
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef WebUrlLoaderClient_h
27 #define WebUrlLoaderClient_h
29 #include "ChromiumIncludes.h"
30 #include "RefCounted.h"
31 #include "WebResponse.h"
32 #include "WebUrlLoader.h"
40 class ConditionVariable;
48 class AuthChallengeInfo;
55 class WebRequestContext;
57 // This class handles communication between the IO thread where loading happens
58 // and the webkit main thread.
60 // - Implement didFail
61 // - Implement sync requests
62 // - Implement downloadFile
63 // - Implement pauseLoad
64 class WebUrlLoaderClient : public base::RefCountedThreadSafe<WebUrlLoaderClient> {
66 WebUrlLoaderClient(WebFrame*, WebCore::ResourceHandle*, const WebCore::ResourceRequest&);
68 // Called from WebCore, will be forwarded to the IO thread
69 bool start(bool isMainResource, bool sync, WebRequestContext*);
72 void pauseLoad(bool pause);
73 void setAuth(const std::string& username, const std::string& password);
75 void proceedSslCertError();
76 void cancelSslCertError(int cert_error);
78 typedef void CallbackFunction(void*);
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);
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);
93 void reportSslCertError(int cert_error, net::X509Certificate* cert);
95 // Handle to the chrome IO thread
96 static base::Thread* ioThread();
99 friend class base::RefCountedThreadSafe<WebUrlLoaderClient>;
100 virtual ~WebUrlLoaderClient();
104 WebFrame* m_webFrame;
105 RefPtr<WebCore::ResourceHandle> m_resourceHandle;
106 bool m_isMainResource;
107 bool m_isCertMimeType;
110 volatile bool m_finished;
112 scoped_refptr<WebRequest> m_request;
113 OwnPtr<WebResponse> m_response; // NULL until didReceiveResponse is called.
115 // Check if a request is active
116 bool isActive() const;
118 // Mutex and condition variable used for synchronous requests.
119 // Note that these are static. This works because there's only one main thread.
120 static Lock* syncLock();
121 static ConditionVariable* syncCondition();
123 // Queue of callbacks to be executed by the main thread. Must only be accessed inside mutex.
124 std::deque<Task*> m_queue;
127 } // namespace android