2
* Copyright (C) 2009, 2010, 2011, 2012 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
20
#include "NetworkManager.h"
23
#include "CredentialStorage.h"
25
#include "FrameLoaderClientBlackBerry.h"
26
#include "NetworkJob.h"
28
#include "ResourceHandleInternal.h"
29
#include "ResourceRequest.h"
30
#include "SecurityOrigin.h"
32
#include <BlackBerryPlatformLog.h>
33
#include <BuildInformation.h>
34
#include <network/FilterStream.h>
35
#include <network/NetworkRequest.h>
39
SINGLETON_INITIALIZER_THREADUNSAFE(NetworkManager)
41
bool NetworkManager::startJob(int playerId, PassRefPtr<ResourceHandle> job, Frame* frame, bool defersLoading)
44
// We shouldn't call methods on PassRefPtr so make a new RefPt.
45
RefPtr<ResourceHandle> refJob(job);
46
return startJob(playerId, refJob, refJob->firstRequest(), frame, defersLoading);
49
bool NetworkManager::startJob(int playerId, PassRefPtr<ResourceHandle> job, const ResourceRequest& request, Frame* frame, bool defersLoading)
51
Page* page = frame->page();
54
BlackBerry::Platform::NetworkStreamFactory* streamFactory = page->chrome()->platformPageClient()->networkStreamFactory();
55
return startJob(playerId, page->groupName(), job, request, streamFactory, frame, defersLoading ? 1 : 0);
58
bool NetworkManager::startJob(int playerId, const String& pageGroupName, PassRefPtr<ResourceHandle> job, const ResourceRequest& request, BlackBerry::Platform::NetworkStreamFactory* streamFactory, Frame* frame, int deferLoadingCount, int redirectCount)
60
// Make sure the ResourceHandle doesn't go out of scope while calling callbacks.
61
RefPtr<ResourceHandle> guardJob(job);
63
KURL url = request.url();
65
// Only load the initial url once.
66
bool isInitial = (url == m_initialURL);
68
m_initialURL = KURL();
70
BlackBerry::Platform::NetworkRequest platformRequest;
71
request.initializePlatformRequest(platformRequest, frame->loader() && frame->loader()->client() && static_cast<FrameLoaderClientBlackBerry*>(frame->loader()->client())->cookiesEnabled(), isInitial, redirectCount);
73
const String& documentUrl = frame->document()->url().string();
74
if (!documentUrl.isEmpty()) {
75
platformRequest.setReferrer(documentUrl);
78
platformRequest.setSecurityOrigin(frame->document()->securityOrigin()->toRawString());
80
// Attach any applicable auth credentials to the NetworkRequest.
81
AuthenticationChallenge& challenge = guardJob->getInternal()->m_currentWebChallenge;
82
if (!challenge.isNull()) {
83
Credential credential = challenge.proposedCredential();
84
ProtectionSpace protectionSpace = challenge.protectionSpace();
85
ProtectionSpaceServerType type = protectionSpace.serverType();
87
String username = credential.user();
88
String password = credential.password();
90
BlackBerry::Platform::NetworkRequest::AuthType authType = BlackBerry::Platform::NetworkRequest::AuthNone;
91
if (type == ProtectionSpaceServerHTTP || type == ProtectionSpaceServerHTTPS) {
92
switch (protectionSpace.authenticationScheme()) {
93
case ProtectionSpaceAuthenticationSchemeHTTPBasic:
94
authType = BlackBerry::Platform::NetworkRequest::AuthHTTPBasic;
96
case ProtectionSpaceAuthenticationSchemeHTTPDigest:
97
authType = BlackBerry::Platform::NetworkRequest::AuthHTTPDigest;
99
case ProtectionSpaceAuthenticationSchemeNegotiate:
100
authType = BlackBerry::Platform::NetworkRequest::AuthNegotiate;
102
case ProtectionSpaceAuthenticationSchemeNTLM:
103
authType = BlackBerry::Platform::NetworkRequest::AuthHTTPNTLM;
105
// Lots more cases to handle.
107
// Defaults to AuthNone as per above.
110
} else if (type == ProtectionSpaceServerFTP || type == ProtectionSpaceServerFTPS)
111
authType = BlackBerry::Platform::NetworkRequest::AuthFTP;
112
else if (type == ProtectionSpaceProxyHTTP || type == ProtectionSpaceProxyHTTPS)
113
authType = BlackBerry::Platform::NetworkRequest::AuthProxy;
115
if (authType != BlackBerry::Platform::NetworkRequest::AuthNone)
116
platformRequest.setCredentials(username.utf8().data(), password.utf8().data(), authType);
119
if (!request.overrideContentType().isEmpty())
120
platformRequest.setOverrideContentType(request.overrideContentType().latin1().data());
122
NetworkJob* networkJob = new NetworkJob;
125
if (!networkJob->initialize(playerId, pageGroupName, url, platformRequest, guardJob, streamFactory, frame, deferLoadingCount, redirectCount)) {
130
// Make sure we have only one NetworkJob for one ResourceHandle.
131
ASSERT(!findJobForHandle(guardJob));
133
m_jobs.append(networkJob);
135
int result = networkJob->streamOpen();
142
bool NetworkManager::stopJob(PassRefPtr<ResourceHandle> job)
144
if (NetworkJob* networkJob = findJobForHandle(job))
145
return !networkJob->cancelJob();
149
NetworkJob* NetworkManager::findJobForHandle(PassRefPtr<ResourceHandle> job)
151
for (unsigned i = 0; i < m_jobs.size(); ++i) {
152
NetworkJob* networkJob = m_jobs[i];
153
// We have only one job for one handle (not including cancelled jobs which may hang
154
// around briefly), so return the first non-cancelled job.
155
if (!networkJob->isCancelled() && networkJob->handle() == job)
161
void NetworkManager::deleteJob(NetworkJob* job)
163
ASSERT(!job->isRunning());
164
size_t position = m_jobs.find(job);
165
if (position != notFound)
166
m_jobs.remove(position);
170
void NetworkManager::setDefersLoading(PassRefPtr<ResourceHandle> job, bool defersLoading)
172
if (NetworkJob* networkJob = findJobForHandle(job))
173
networkJob->updateDeferLoadingCount(defersLoading ? 1 : -1);
176
void NetworkManager::pauseLoad(PassRefPtr<ResourceHandle> job, bool pause)
178
if (NetworkJob* networkJob = findJobForHandle(job))
179
networkJob->streamPause(pause);
182
BlackBerry::Platform::FilterStream* NetworkManager::streamForHandle(PassRefPtr<ResourceHandle> job)
184
NetworkJob* networkJob = findJobForHandle(job);
185
return networkJob ? networkJob->wrappedStream() : 0;
188
} // namespace WebCore