2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. 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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "DownloadProxy.h"
29 #include "AuthenticationChallengeProxy.h"
30 #include "DataReference.h"
31 #include "WebContext.h"
33 #include "WebProcessMessages.h"
34 #include <wtf/text/CString.h>
35 #include <wtf/text/WTFString.h>
37 using namespace WebCore;
41 static uint64_t generateDownloadID()
43 static uint64_t uniqueDownloadID = 0;
44 return ++uniqueDownloadID;
47 PassRefPtr<DownloadProxy> DownloadProxy::create(WebContext* webContext)
49 return adoptRef(new DownloadProxy(webContext));
52 DownloadProxy::DownloadProxy(WebContext* webContext)
53 : m_webContext(webContext)
54 , m_downloadID(generateDownloadID())
58 DownloadProxy::~DownloadProxy()
60 ASSERT(!m_webContext);
63 void DownloadProxy::cancel()
68 // FIXME (Multi-WebProcess): Downloads shouldn't be handled in the web process.
69 m_webContext->sendToAllProcesses(Messages::WebProcess::CancelDownload(m_downloadID));
72 void DownloadProxy::invalidate()
78 void DownloadProxy::processDidClose()
83 m_webContext->downloadClient().processDidCrash(m_webContext, this);
86 void DownloadProxy::didStart(const ResourceRequest& request)
93 m_webContext->downloadClient().didStart(m_webContext, this);
96 void DownloadProxy::didReceiveAuthenticationChallenge(const AuthenticationChallenge& authenticationChallenge, uint64_t challengeID)
101 RefPtr<AuthenticationChallengeProxy> authenticationChallengeProxy = AuthenticationChallengeProxy::create(authenticationChallenge, challengeID, m_webContext->process());
102 m_webContext->downloadClient().didReceiveAuthenticationChallenge(m_webContext, this, authenticationChallengeProxy.get());
105 void DownloadProxy::didReceiveResponse(const ResourceResponse& response)
110 m_webContext->downloadClient().didReceiveResponse(m_webContext, this, response);
113 void DownloadProxy::didReceiveData(uint64_t length)
118 m_webContext->downloadClient().didReceiveData(m_webContext, this, length);
121 void DownloadProxy::shouldDecodeSourceDataOfMIMEType(const String& mimeType, bool& result)
126 result = m_webContext->downloadClient().shouldDecodeSourceDataOfMIMEType(m_webContext, this, mimeType);
129 void DownloadProxy::decideDestinationWithSuggestedFilename(const String& filename, String& destination, bool& allowOverwrite, SandboxExtension::Handle& sandboxExtensionHandle)
134 destination = m_webContext->downloadClient().decideDestinationWithSuggestedFilename(m_webContext, this, filename, allowOverwrite);
136 if (!destination.isNull())
137 SandboxExtension::createHandle(destination, SandboxExtension::WriteOnly, sandboxExtensionHandle);
140 void DownloadProxy::didCreateDestination(const String& path)
145 m_webContext->downloadClient().didCreateDestination(m_webContext, this, path);
148 void DownloadProxy::didFinish()
153 m_webContext->downloadClient().didFinish(m_webContext, this);
155 // This can cause the DownloadProxy object to be deleted.
156 m_webContext->downloadFinished(this);
159 static PassRefPtr<WebData> createWebData(const CoreIPC::DataReference& data)
164 return WebData::create(data.data(), data.size());
167 void DownloadProxy::didFail(const ResourceError& error, const CoreIPC::DataReference& resumeData)
172 m_resumeData = createWebData(resumeData);
174 m_webContext->downloadClient().didFail(m_webContext, this, error);
176 // This can cause the DownloadProxy object to be deleted.
177 m_webContext->downloadFinished(this);
180 void DownloadProxy::didCancel(const CoreIPC::DataReference& resumeData)
182 m_resumeData = createWebData(resumeData);
184 m_webContext->downloadClient().didCancel(m_webContext, this);
186 // This can cause the DownloadProxy object to be deleted.
187 m_webContext->downloadFinished(this);
190 } // namespace WebKit