~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/platform/network/blackberry/NetworkManager.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2009, 2010, 2011, 2012 Research In Motion Limited. All rights reserved.
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
#include "NetworkManager.h"
 
21
 
 
22
#include "Chrome.h"
 
23
#include "CredentialStorage.h"
 
24
#include "Frame.h"
 
25
#include "FrameLoaderClientBlackBerry.h"
 
26
#include "NetworkJob.h"
 
27
#include "Page.h"
 
28
#include "ResourceHandleInternal.h"
 
29
#include "ResourceRequest.h"
 
30
#include "SecurityOrigin.h"
 
31
 
 
32
#include <BlackBerryPlatformLog.h>
 
33
#include <BuildInformation.h>
 
34
#include <network/FilterStream.h>
 
35
#include <network/NetworkRequest.h>
 
36
 
 
37
namespace WebCore {
 
38
 
 
39
SINGLETON_INITIALIZER_THREADUNSAFE(NetworkManager)
 
40
 
 
41
bool NetworkManager::startJob(int playerId, PassRefPtr<ResourceHandle> job, Frame* frame, bool defersLoading)
 
42
{
 
43
    ASSERT(job.get());
 
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);
 
47
}
 
48
 
 
49
bool NetworkManager::startJob(int playerId, PassRefPtr<ResourceHandle> job, const ResourceRequest& request, Frame* frame, bool defersLoading)
 
50
{
 
51
    Page* page = frame->page();
 
52
    if (!page)
 
53
        return false;
 
54
    BlackBerry::Platform::NetworkStreamFactory* streamFactory = page->chrome()->platformPageClient()->networkStreamFactory();
 
55
    return startJob(playerId, page->groupName(), job, request, streamFactory, frame, defersLoading ? 1 : 0);
 
56
}
 
57
 
 
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)
 
59
{
 
60
    // Make sure the ResourceHandle doesn't go out of scope while calling callbacks.
 
61
    RefPtr<ResourceHandle> guardJob(job);
 
62
 
 
63
    KURL url = request.url();
 
64
 
 
65
    // Only load the initial url once.
 
66
    bool isInitial = (url == m_initialURL);
 
67
    if (isInitial)
 
68
        m_initialURL = KURL();
 
69
 
 
70
    BlackBerry::Platform::NetworkRequest platformRequest;
 
71
    request.initializePlatformRequest(platformRequest, frame->loader() && frame->loader()->client() && static_cast<FrameLoaderClientBlackBerry*>(frame->loader()->client())->cookiesEnabled(), isInitial, redirectCount);
 
72
 
 
73
    const String& documentUrl = frame->document()->url().string();
 
74
    if (!documentUrl.isEmpty()) {
 
75
        platformRequest.setReferrer(documentUrl);
 
76
    }
 
77
 
 
78
    platformRequest.setSecurityOrigin(frame->document()->securityOrigin()->toRawString());
 
79
 
 
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();
 
86
 
 
87
        String username = credential.user();
 
88
        String password = credential.password();
 
89
 
 
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;
 
95
                break;
 
96
            case ProtectionSpaceAuthenticationSchemeHTTPDigest:
 
97
                authType = BlackBerry::Platform::NetworkRequest::AuthHTTPDigest;
 
98
                break;
 
99
            case ProtectionSpaceAuthenticationSchemeNegotiate:
 
100
                authType = BlackBerry::Platform::NetworkRequest::AuthNegotiate;
 
101
                break;
 
102
            case ProtectionSpaceAuthenticationSchemeNTLM:
 
103
                authType = BlackBerry::Platform::NetworkRequest::AuthHTTPNTLM;
 
104
                break;
 
105
            // Lots more cases to handle.
 
106
            default:
 
107
                // Defaults to AuthNone as per above.
 
108
                break;
 
109
            }
 
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;
 
114
 
 
115
        if (authType != BlackBerry::Platform::NetworkRequest::AuthNone)
 
116
            platformRequest.setCredentials(username.utf8().data(), password.utf8().data(), authType);
 
117
    }
 
118
 
 
119
    if (!request.overrideContentType().isEmpty())
 
120
        platformRequest.setOverrideContentType(request.overrideContentType().latin1().data());
 
121
 
 
122
    NetworkJob* networkJob = new NetworkJob;
 
123
    if (!networkJob)
 
124
        return false;
 
125
    if (!networkJob->initialize(playerId, pageGroupName, url, platformRequest, guardJob, streamFactory, frame, deferLoadingCount, redirectCount)) {
 
126
        delete networkJob;
 
127
        return false;
 
128
    }
 
129
 
 
130
    // Make sure we have only one NetworkJob for one ResourceHandle.
 
131
    ASSERT(!findJobForHandle(guardJob));
 
132
 
 
133
    m_jobs.append(networkJob);
 
134
 
 
135
    int result = networkJob->streamOpen();
 
136
    if (result)
 
137
        return false;
 
138
 
 
139
    return true;
 
140
}
 
141
 
 
142
bool NetworkManager::stopJob(PassRefPtr<ResourceHandle> job)
 
143
{
 
144
    if (NetworkJob* networkJob = findJobForHandle(job))
 
145
        return !networkJob->cancelJob();
 
146
    return false;
 
147
}
 
148
 
 
149
NetworkJob* NetworkManager::findJobForHandle(PassRefPtr<ResourceHandle> job)
 
150
{
 
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)
 
156
            return networkJob;
 
157
    }
 
158
    return 0;
 
159
}
 
160
 
 
161
void NetworkManager::deleteJob(NetworkJob* job)
 
162
{
 
163
    ASSERT(!job->isRunning());
 
164
    size_t position = m_jobs.find(job);
 
165
    if (position != notFound)
 
166
        m_jobs.remove(position);
 
167
    delete job;
 
168
}
 
169
 
 
170
void NetworkManager::setDefersLoading(PassRefPtr<ResourceHandle> job, bool defersLoading)
 
171
{
 
172
    if (NetworkJob* networkJob = findJobForHandle(job))
 
173
        networkJob->updateDeferLoadingCount(defersLoading ? 1 : -1);
 
174
}
 
175
 
 
176
void NetworkManager::pauseLoad(PassRefPtr<ResourceHandle> job, bool pause)
 
177
{
 
178
    if (NetworkJob* networkJob = findJobForHandle(job))
 
179
        networkJob->streamPause(pause);
 
180
}
 
181
 
 
182
BlackBerry::Platform::FilterStream* NetworkManager::streamForHandle(PassRefPtr<ResourceHandle> job)
 
183
{
 
184
    NetworkJob* networkJob = findJobForHandle(job);
 
185
    return networkJob ? networkJob->wrappedStream() : 0;
 
186
}
 
187
 
 
188
} // namespace WebCore