2 * Copyright (C) 2009, 2010, 2011 Research In Motion Limited. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "DeferredData.h"
23 #include "PlatformString.h"
24 #include "ProtectionSpace.h"
25 #include "ResourceHandle.h"
26 #include "ResourceResponse.h"
29 #include <network/FilterStream.h>
30 #include <wtf/OwnPtr.h>
31 #include <wtf/RefPtr.h>
33 namespace BlackBerry {
36 class NetworkStreamFactory;
44 class ResourceRequest;
46 class NetworkJob : public BlackBerry::Platform::FilterStream {
49 bool initialize(int playerId,
50 const String& pageGroupName,
52 const BlackBerry::Platform::NetworkRequest&,
53 PassRefPtr<ResourceHandle>,
54 BlackBerry::Platform::NetworkStreamFactory*,
56 int deferLoadingCount,
58 PassRefPtr<ResourceHandle> handle() const { return m_handle; }
60 bool isRunning() const { return m_isRunning; }
62 bool isCancelled() const { return m_cancelled; }
63 void loadDataURL() { m_loadDataTimer.startOneShot(0); }
66 bool isDeferringLoading() const { return m_deferLoadingCount > 0; }
67 void updateDeferLoadingCount(int delta);
68 virtual void notifyStatusReceived(int status, const char* message);
69 void handleNotifyStatusReceived(int status, const String& message);
70 virtual void notifyWMLOverride();
71 void handleNotifyWMLOverride() { m_response.setIsWML(true); }
72 virtual void notifyHeadersReceived(BlackBerry::Platform::NetworkRequest::HeaderList& headers);
73 virtual void notifyMultipartHeaderReceived(const char* key, const char* value);
74 // Exists only to resolve ambiguity between char* and String parameters
75 void notifyStringHeaderReceived(const String& key, const String& value);
76 void handleNotifyHeaderReceived(const String& key, const String& value);
77 void handleNotifyMultipartHeaderReceived(const String& key, const String& value);
78 void handleSetCookieHeader(const String& value);
79 void notifyDataReceivedPlain(const char* buf, size_t len);
80 void handleNotifyDataReceived(const char* buf, size_t len);
81 virtual void notifyDataSent(unsigned long long bytesSent, unsigned long long totalBytesToBeSent);
82 void handleNotifyDataSent(unsigned long long bytesSent, unsigned long long totalBytesToBeSent);
83 virtual void notifyClose(int status);
84 void handleNotifyClose(int status);
87 bool isClientAvailable() const { return !m_cancelled && m_handle && m_handle->client(); }
89 virtual void notifyDataReceived(BlackBerry::Platform::NetworkBuffer* buffer)
91 notifyDataReceivedPlain(BlackBerry::Platform::networkBufferData(buffer), BlackBerry::Platform::networkBufferDataLength(buffer));
94 virtual void setWasDiskCached(bool value)
96 m_response.setWasCached(value);
99 bool shouldSendClientData() const;
101 bool shouldNotifyClientFinished();
103 bool shouldDeferLoading() const
105 return isDeferringLoading() || m_deferredData.hasDeferredData();
108 bool retryAsFTPDirectory();
110 bool startNewJobWithRequest(ResourceRequest& newRequest, bool increasRedirectCount = false);
112 bool handleRedirect();
114 // This can cause m_cancelled to be set to true, if it passes up an error to m_handle->client() which causes the job to be cancelled
115 void sendResponseIfNeeded();
116 void sendMultipartResponseIfNeeded();
118 void fireLoadDataTimer(Timer<NetworkJob>*)
123 void fireLoadAboutTimer(Timer<NetworkJob>*)
128 void fireDeleteJobTimer(Timer<NetworkJob>*);
134 // The server needs authentication credentials. Search in the
135 // CredentialStorage or prompt the user via dialog.
136 bool handleAuthHeader(const ProtectionSpaceServerType, const String& header);
138 bool handleFTPHeader(const String& header);
140 bool sendRequestWithCredentials(ProtectionSpaceServerType, ProtectionSpaceAuthenticationScheme, const String& realm);
142 void storeCredentials();
144 void purgeCredentials();
146 bool isError(int statusCode)
148 return statusCode < 0 || (!m_isXHR && (400 <= statusCode && statusCode < 600));
153 String m_pageGroupName;
154 RefPtr<ResourceHandle> m_handle;
155 ResourceResponse m_response;
156 Timer<NetworkJob> m_loadDataTimer;
157 Timer<NetworkJob> m_loadAboutTimer;
158 OwnPtr<ResourceResponse> m_multipartResponse;
159 Timer<NetworkJob> m_deleteJobTimer;
160 String m_contentType;
161 String m_sniffedMimeType;
162 String m_contentDisposition;
163 BlackBerry::Platform::NetworkStreamFactory* m_streamFactory;
173 bool m_statusReceived;
176 bool m_callingClient;
177 bool m_isXHR; // FIXME - After 7.0, remove this. Only the Qt port reports HTTP error statuses as didFails, so we probably shouldn't.
178 bool m_needsRetryAsFTPDirectory;
179 bool m_isOverrideContentType;
181 // If an HTTP status code is received, m_extendedStatusCode and m_response.httpStatusCode will both be set to it.
182 // If a platform error code is received, m_extendedStatusCode will be set to it and m_response.httpStatusCode will be set to 404.
183 int m_extendedStatusCode;
187 DeferredData m_deferredData;
188 int m_deferLoadingCount;
189 const Frame* m_frame;
192 } // namespace WebCore
194 #endif // NetworkJob_h