OSDN Git Service

am 31cf3287: Do not merge: Cherry-pick CL to help flash plugin performance
[android-x86/external-webkit.git] / WebKit / android / WebCoreSupport / WebUrlLoaderClient.h
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 class Lock;
40 class ConditionVariable;
41
42 namespace base {
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 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);
93     void reportSslCertError(int cert_error, net::X509Certificate* cert);
94
95     // Handle to the chrome IO thread
96     static base::Thread* ioThread();
97
98 private:
99     friend class base::RefCountedThreadSafe<WebUrlLoaderClient>;
100     virtual ~WebUrlLoaderClient();
101
102     void finish();
103
104     WebFrame* m_webFrame;
105     RefPtr<WebCore::ResourceHandle> m_resourceHandle;
106     bool m_isMainResource;
107     bool m_isCertMimeType;
108     bool m_cancelling;
109     bool m_sync;
110     volatile bool m_finished;
111
112     scoped_refptr<WebRequest> m_request;
113     OwnPtr<WebResponse> m_response; // NULL until didReceiveResponse is called.
114
115     // Check if a request is active
116     bool isActive() const;
117
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();
122
123     // Queue of callbacks to be executed by the main thread. Must only be accessed inside mutex.
124     std::deque<Task*> m_queue;
125 };
126
127 } // namespace android
128
129 #endif