1/*
2 * Copyright (C) 2010, 2011 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#ifndef Download_h
27#define Download_h
28
29#include "MessageSender.h"
30#include <WebCore/ResourceRequest.h>
31#include <wtf/Noncopyable.h>
32#include <wtf/PassOwnPtr.h>
33
34#if PLATFORM(MAC)
35#include <wtf/RetainPtr.h>
36
37OBJC_CLASS NSURLDownload;
38OBJC_CLASS WKDownloadAsDelegate;
39#endif
40
41#if PLATFORM(GTK) || PLATFORM(EFL)
42#include <WebCore/ResourceHandle.h>
43#include <WebCore/ResourceHandleClient.h>
44#endif
45
46#if USE(CFNETWORK)
47#include <CFNetwork/CFURLDownloadPriv.h>
48#endif
49
50namespace CoreIPC {
51    class DataReference;
52}
53
54namespace WebCore {
55    class AuthenticationChallenge;
56    class Credential;
57    class ResourceError;
58    class ResourceHandle;
59    class ResourceResponse;
60}
61
62namespace WebKit {
63
64class DownloadAuthenticationClient;
65class DownloadManager;
66class SandboxExtension;
67class WebPage;
68
69#if PLATFORM(QT)
70class QtFileDownloader;
71#endif
72
73class Download : public CoreIPC::MessageSender {
74    WTF_MAKE_NONCOPYABLE(Download);
75public:
76    static PassOwnPtr<Download> create(DownloadManager&, uint64_t downloadID, const WebCore::ResourceRequest&);
77    ~Download();
78
79    void start();
80    void startWithHandle(WebCore::ResourceHandle*, const WebCore::ResourceResponse&);
81    void cancel();
82
83    uint64_t downloadID() const { return m_downloadID; }
84
85    void didStart();
86    void didReceiveAuthenticationChallenge(const WebCore::AuthenticationChallenge&);
87    void didReceiveResponse(const WebCore::ResourceResponse&);
88    void didReceiveData(uint64_t length);
89    bool shouldDecodeSourceDataOfMIMEType(const String& mimeType);
90    String decideDestinationWithSuggestedFilename(const String& filename, bool& allowOverwrite);
91    void didCreateDestination(const String& path);
92    void didFinish();
93    void platformDidFinish();
94    void didFail(const WebCore::ResourceError&, const CoreIPC::DataReference& resumeData);
95    void didCancel(const CoreIPC::DataReference& resumeData);
96    void didDecideDestination(const String&, bool allowOverwrite);
97
98#if PLATFORM(QT)
99    void startTransfer(const String& destination);
100#endif
101
102#if USE(CFNETWORK)
103    const String& destination() const { return m_destination; }
104    DownloadAuthenticationClient* authenticationClient();
105#endif
106
107    // Authentication
108    static void receivedCredential(const WebCore::AuthenticationChallenge&, const WebCore::Credential&);
109    static void receivedRequestToContinueWithoutCredential(const WebCore::AuthenticationChallenge&);
110    static void receivedCancellation(const WebCore::AuthenticationChallenge&);
111
112    void useCredential(const WebCore::AuthenticationChallenge&, const WebCore::Credential&);
113    void continueWithoutCredential(const WebCore::AuthenticationChallenge&);
114    void cancelAuthenticationChallenge(const WebCore::AuthenticationChallenge&);
115
116private:
117    Download(DownloadManager&, uint64_t downloadID, const WebCore::ResourceRequest&);
118
119    // CoreIPC::MessageSender
120    virtual CoreIPC::Connection* messageSenderConnection() OVERRIDE;
121    virtual uint64_t messageSenderDestinationID() OVERRIDE;
122
123    void platformInvalidate();
124
125    String retrieveDestinationWithSuggestedFilename(const String& filename, bool& allowOverwrite);
126
127    DownloadManager& m_downloadManager;
128    uint64_t m_downloadID;
129    WebCore::ResourceRequest m_request;
130
131    RefPtr<SandboxExtension> m_sandboxExtension;
132
133#if PLATFORM(MAC)
134    RetainPtr<NSURLDownload> m_nsURLDownload;
135    RetainPtr<WKDownloadAsDelegate> m_delegate;
136#endif
137    bool m_allowOverwrite;
138    String m_destination;
139    String m_bundlePath;
140#if USE(CFNETWORK)
141    RetainPtr<CFURLDownloadRef> m_download;
142    RefPtr<DownloadAuthenticationClient> m_authenticationClient;
143#endif
144#if PLATFORM(QT)
145    QtFileDownloader* m_qtDownloader;
146#endif
147#if PLATFORM(GTK) || PLATFORM(EFL)
148    OwnPtr<WebCore::ResourceHandleClient> m_downloadClient;
149    RefPtr<WebCore::ResourceHandle> m_resourceHandle;
150#endif
151};
152
153} // namespace WebKit
154
155#endif // Download_h
156