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 40 namespace base { 41 class ConditionVariable; 42 class Lock; 43 class Thread; 44 } 45 46 namespace net { 47 class IOBuffer; 48 class AuthChallengeInfo; 49 } 50 51 namespace android { 52 53 class WebFrame; 54 class WebRequest; 55 class 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 64 class WebUrlLoaderClient : public base::RefCountedThreadSafe<WebUrlLoaderClient> { 65 public: 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 void sslClientCert(EVP_PKEY* pkey, net::X509Certificate* chain); 78 79 typedef void CallbackFunction(void*); 80 81 // This is called from the IO thread, and dispatches the callback to the main thread. 82 // (For asynchronous calls, we just delegate to WebKit's callOnMainThread.) 83 void maybeCallOnMainThread(Task* task); 84 85 // Called by WebRequest (using maybeCallOnMainThread), should be forwarded to WebCore. 86 void didReceiveResponse(PassOwnPtr<WebResponse>); 87 void didReceiveData(scoped_refptr<net::IOBuffer>, int size); 88 void didReceiveDataUrl(PassOwnPtr<std::string>); 89 void didReceiveAndroidFileData(PassOwnPtr<std::vector<char> >); 90 void didFinishLoading(); 91 void didFail(PassOwnPtr<WebResponse>); 92 void willSendRequest(PassOwnPtr<WebResponse>); 93 void authRequired(scoped_refptr<net::AuthChallengeInfo>, bool firstTime, bool suppressDialog); 94 void reportSslCertError(int cert_error, net::X509Certificate* cert); 95 void requestClientCert(net::SSLCertRequestInfo* cert); 96 97 // Handle to the chrome IO thread 98 static base::Thread* ioThread(); 99 100 private: 101 friend class base::RefCountedThreadSafe<WebUrlLoaderClient>; 102 virtual ~WebUrlLoaderClient(); 103 104 void finish(); 105 106 WebFrame* m_webFrame; 107 RefPtr<WebCore::ResourceHandle> m_resourceHandle; 108 bool m_isMainResource; 109 bool m_isMainFrame; 110 bool m_isCertMimeType; 111 bool m_cancelling; 112 bool m_sync; 113 volatile bool m_finished; 114 115 scoped_refptr<WebRequest> m_request; 116 OwnPtr<WebResponse> m_response; // NULL until didReceiveResponse is called. 117 118 // Check if a request is active 119 bool isActive() const; 120 121 // Mutex and condition variable used for synchronous requests. 122 // Note that these are static. This works because there's only one main thread. 123 static base::Lock* syncLock(); 124 static base::ConditionVariable* syncCondition(); 125 126 // Queue of callbacks to be executed by the main thread. Must only be accessed inside mutex. 127 std::deque<Task*> m_queue; 128 }; 129 130 } // namespace android 131 132 #endif 133