Download.cpp revision 65f03d4f644ce73618e5f4f50dd694b26f55ae12
1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26#include "Download.h"
27
28#include "Connection.h"
29#include "DataReference.h"
30#include "DownloadProxyMessages.h"
31#include "DownloadManager.h"
32#include "SandboxExtension.h"
33#include "WebCoreArgumentCoders.h"
34#include "WebProcess.h"
35
36using namespace WebCore;
37
38namespace WebKit {
39
40PassOwnPtr<Download> Download::create(uint64_t downloadID, const ResourceRequest& request)
41{
42    return adoptPtr(new Download(downloadID, request));
43}
44
45Download::Download(uint64_t downloadID, const ResourceRequest& request)
46    : m_downloadID(downloadID)
47    , m_request(request)
48{
49    ASSERT(m_downloadID);
50}
51
52Download::~Download()
53{
54    platformInvalidate();
55}
56
57CoreIPC::Connection* Download::connection() const
58{
59    return WebProcess::shared().connection();
60}
61
62void Download::didStart()
63{
64    send(Messages::DownloadProxy::DidStart(m_request));
65}
66
67void Download::didReceiveResponse(const ResourceResponse& response)
68{
69    send(Messages::DownloadProxy::DidReceiveResponse(response));
70}
71
72void Download::didReceiveData(uint64_t length)
73{
74    send(Messages::DownloadProxy::DidReceiveData(length));
75}
76
77bool Download::shouldDecodeSourceDataOfMIMEType(const String& mimeType)
78{
79    bool result;
80    if (!sendSync(Messages::DownloadProxy::ShouldDecodeSourceDataOfMIMEType(mimeType), Messages::DownloadProxy::ShouldDecodeSourceDataOfMIMEType::Reply(result)))
81        return true;
82
83    return result;
84}
85
86String Download::decideDestinationWithSuggestedFilename(const String& filename, bool& allowOverwrite)
87{
88    String destination;
89    SandboxExtension::Handle sandboxExtensionHandle;
90    if (!sendSync(Messages::DownloadProxy::DecideDestinationWithSuggestedFilename(filename), Messages::DownloadProxy::DecideDestinationWithSuggestedFilename::Reply(destination, allowOverwrite, sandboxExtensionHandle)))
91        return String();
92
93    m_sandboxExtension = SandboxExtension::create(sandboxExtensionHandle);
94    if (m_sandboxExtension)
95        m_sandboxExtension->consume();
96
97    return destination;
98}
99
100void Download::didCreateDestination(const String& path)
101{
102    send(Messages::DownloadProxy::DidCreateDestination(path));
103}
104
105void Download::didFinish()
106{
107    send(Messages::DownloadProxy::DidFinish());
108
109    if (m_sandboxExtension)
110        m_sandboxExtension->invalidate();
111    DownloadManager::shared().downloadFinished(this);
112}
113
114void Download::didFail(const ResourceError& error, const CoreIPC::DataReference& resumeData)
115{
116    send(Messages::DownloadProxy::DidFail(error, resumeData));
117
118    if (m_sandboxExtension)
119        m_sandboxExtension->invalidate();
120    DownloadManager::shared().downloadFinished(this);
121}
122
123void Download::didCancel(const CoreIPC::DataReference& resumeData)
124{
125    send(Messages::DownloadProxy::DidCancel(resumeData));
126
127    if (m_sandboxExtension)
128        m_sandboxExtension->invalidate();
129    DownloadManager::shared().downloadFinished(this);
130}
131
132} // namespace WebKit
133