summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/network/blackberry/NetworkManager.cpp
blob: 5d036b501fc54fbde2a686f6e2e9ac2efb1255cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * Copyright (C) 2009, 2010, 2011, 2012 Research In Motion Limited. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"
#include "NetworkManager.h"

#include "Chrome.h"
#if ENABLE(BLACKBERRY_CREDENTIAL_PERSIST)
#include "CredentialBackingStore.h"
#endif
#include "CredentialStorage.h"
#include "Frame.h"
#include "FrameLoaderClientBlackBerry.h"
#include "NetworkJob.h"
#include "Page.h"
#include "ReadOnlyLatin1String.h"
#if ENABLE(BLACKBERRY_CREDENTIAL_PERSIST)
#include "ResourceHandleClient.h"
#endif
#include "ResourceHandleInternal.h"
#include "ResourceRequest.h"
#include "SecurityOrigin.h"

#include <BlackBerryPlatformLog.h>
#include <BuildInformation.h>
#include <network/FilterStream.h>
#include <network/NetworkRequest.h>

namespace WebCore {

NetworkManager* NetworkManager::instance()
{
    static NetworkManager* sInstance;
    if (!sInstance) {
        sInstance = new NetworkManager;
        ASSERT(sInstance);
    }
    return sInstance;
}

bool NetworkManager::startJob(int playerId, PassRefPtr<ResourceHandle> job, const Frame& frame, bool defersLoading)
{
    ASSERT(job.get());
    // We shouldn't call methods on PassRefPtr so make a new RefPt.
    RefPtr<ResourceHandle> refJob(job);
    return startJob(playerId, refJob, refJob->firstRequest(), frame, defersLoading);
}

bool NetworkManager::startJob(int playerId, PassRefPtr<ResourceHandle> job, const ResourceRequest& request, const Frame& frame, bool defersLoading)
{
    Page* page = frame.page();
    if (!page)
        return false;
    BlackBerry::Platform::NetworkStreamFactory* streamFactory = page->chrome()->platformPageClient()->networkStreamFactory();
    return startJob(playerId, page->groupName(), job, request, streamFactory, frame, defersLoading ? 1 : 0);
}

bool NetworkManager::startJob(int playerId, const String& pageGroupName, PassRefPtr<ResourceHandle> job, const ResourceRequest& request, BlackBerry::Platform::NetworkStreamFactory* streamFactory, const Frame& frame, int deferLoadingCount, int redirectCount)
{
    // Make sure the ResourceHandle doesn't go out of scope while calling callbacks.
    RefPtr<ResourceHandle> guardJob(job);

    KURL url = request.url();

    // Only load the initial url once.
    bool isInitial = (url == m_initialURL);
    if (isInitial)
        m_initialURL = KURL();

    BlackBerry::Platform::NetworkRequest platformRequest;
    request.initializePlatformRequest(platformRequest, frame.loader() && frame.loader()->client() && static_cast<FrameLoaderClientBlackBerry*>(frame.loader()->client())->cookiesEnabled(), isInitial, redirectCount);

    const String& documentUrl = frame.document()->url().string();
    if (!documentUrl.isEmpty()) {
        ReadOnlyLatin1String referrer(documentUrl);
        platformRequest.setReferrer(referrer.data(), referrer.length());
    }

    ReadOnlyLatin1String securityOrigin(frame.document()->securityOrigin()->toRawString());
    platformRequest.setSecurityOrigin(securityOrigin.data(), securityOrigin.length());

    // Attach any applicable auth credentials to the NetworkRequest.
    AuthenticationChallenge& challenge = guardJob->getInternal()->m_currentWebChallenge;
    if (!challenge.isNull()) {
        Credential credential = challenge.proposedCredential();
        ProtectionSpace protectionSpace = challenge.protectionSpace();
        ProtectionSpaceServerType type = protectionSpace.serverType();

        String username = credential.user();
        String password = credential.password();

        BlackBerry::Platform::NetworkRequest::AuthType authType = BlackBerry::Platform::NetworkRequest::AuthNone;
        if (type == ProtectionSpaceServerHTTP) {
            switch (protectionSpace.authenticationScheme()) {
            case ProtectionSpaceAuthenticationSchemeHTTPBasic:
                authType = BlackBerry::Platform::NetworkRequest::AuthHTTPBasic;
                break;
            case ProtectionSpaceAuthenticationSchemeHTTPDigest:
                authType = BlackBerry::Platform::NetworkRequest::AuthHTTPDigest;
                break;
            case ProtectionSpaceAuthenticationSchemeNegotiate:
                authType = BlackBerry::Platform::NetworkRequest::AuthNegotiate;
                break;
            case ProtectionSpaceAuthenticationSchemeNTLM:
                authType = BlackBerry::Platform::NetworkRequest::AuthHTTPNTLM;
                break;
            // Lots more cases to handle.
            default:
                // Defaults to AuthNone as per above.
                break;
            }
        } else if (type == ProtectionSpaceServerFTP)
            authType = BlackBerry::Platform::NetworkRequest::AuthFTP;
        else if (type == ProtectionSpaceProxyHTTP)
            authType = BlackBerry::Platform::NetworkRequest::AuthProxy;

        if (authType != BlackBerry::Platform::NetworkRequest::AuthNone)
            platformRequest.setCredentials(username.utf8().data(), password.utf8().data(), authType);
    } else if (url.protocolIsInHTTPFamily()) {
        // For URLs that match the paths of those previously challenged for HTTP Basic authentication,
        // try and reuse the credential preemptively, as allowed by RFC 2617.
        Credential credential = CredentialStorage::get(url);
#if ENABLE(BLACKBERRY_CREDENTIAL_PERSIST)
        // FIXME: needs to refactor the credentialBackingStore to get credential and protection space at one time.
        if (credential.isEmpty() && guardJob->client()->shouldUseCredentialStorage(guardJob.get())) {
            credential = credentialBackingStore().getLogin(url);
            if (!credential.isEmpty())
                CredentialStorage::set(credential, credentialBackingStore().getProtectionSpace(url), url);
        }
#endif
        if (!credential.isEmpty())
            platformRequest.setCredentials(credential.user().utf8().data(), credential.password().utf8().data(), BlackBerry::Platform::NetworkRequest::AuthHTTPBasic);
    }

    if (!request.overrideContentType().isEmpty())
        platformRequest.setOverrideContentType(request.overrideContentType().latin1().data());

    NetworkJob* networkJob = new NetworkJob;
    if (!networkJob)
        return false;
    if (!networkJob->initialize(playerId, pageGroupName, url, platformRequest, guardJob, streamFactory, frame, deferLoadingCount, redirectCount)) {
        delete networkJob;
        return false;
    }

    // Make sure we have only one NetworkJob for one ResourceHandle.
    ASSERT(!findJobForHandle(guardJob));

    m_jobs.append(networkJob);

    int result = networkJob->streamOpen();
    if (result)
        return false;

    return true;
}

bool NetworkManager::stopJob(PassRefPtr<ResourceHandle> job)
{
    if (NetworkJob* networkJob = findJobForHandle(job))
        return !networkJob->cancelJob();
    return false;
}

NetworkJob* NetworkManager::findJobForHandle(PassRefPtr<ResourceHandle> job)
{
    for (unsigned i = 0; i < m_jobs.size(); ++i) {
        NetworkJob* networkJob = m_jobs[i];
        // We have only one job for one handle (not including cancelled jobs which may hang
        // around briefly), so return the first non-cancelled job.
        if (!networkJob->isCancelled() && networkJob->handle() == job)
            return networkJob;
    }
    return 0;
}

void NetworkManager::deleteJob(NetworkJob* job)
{
    ASSERT(!job->isRunning());
    size_t position = m_jobs.find(job);
    if (position != notFound)
        m_jobs.remove(position);
    delete job;
}

void NetworkManager::setDefersLoading(PassRefPtr<ResourceHandle> job, bool defersLoading)
{
    if (NetworkJob* networkJob = findJobForHandle(job))
        networkJob->updateDeferLoadingCount(defersLoading ? 1 : -1);
}

void NetworkManager::pauseLoad(PassRefPtr<ResourceHandle> job, bool pause)
{
    if (NetworkJob* networkJob = findJobForHandle(job))
        networkJob->streamPause(pause);
}

BlackBerry::Platform::FilterStream* NetworkManager::streamForHandle(PassRefPtr<ResourceHandle> job)
{
    NetworkJob* networkJob = findJobForHandle(job);
    return networkJob ? networkJob->wrappedStream() : 0;
}

} // namespace WebCore