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 USE(CFNETWORK)
42#include <CFNetwork/CFURLDownloadPriv.h>
43#endif
44
45namespace CoreIPC {
46    class DataReference;
47}
48
49namespace WebCore {
50    class AuthenticationChallenge;
51    class Credential;
52    class ResourceError;
53    class ResourceHandle;
54    class ResourceResponse;
55}
56
57namespace WebKit {
58
59class SandboxExtension;
60class WebPage;
61
62class Download : public CoreIPC::MessageSender<Download> {
63    WTF_MAKE_NONCOPYABLE(Download);
64public:
65    static PassOwnPtr<Download> create(uint64_t downloadID, const WebCore::ResourceRequest&);
66    ~Download();
67
68    // Used by MessageSender.
69    CoreIPC::Connection* connection() const;
70    uint64_t destinationID() const { return downloadID(); }
71
72    void start(WebPage* initiatingWebPage);
73    void startWithHandle(WebPage* initiatingPage, WebCore::ResourceHandle*, const WebCore::ResourceRequest& initialRequest, const WebCore::ResourceResponse&);
74    void cancel();
75
76    uint64_t downloadID() const { return m_downloadID; }
77
78    void didStart();
79    void didReceiveAuthenticationChallenge(const WebCore::AuthenticationChallenge&);
80    void didReceiveResponse(const WebCore::ResourceResponse&);
81    void didReceiveData(uint64_t length);
82    bool shouldDecodeSourceDataOfMIMEType(const String& mimeType);
83    String decideDestinationWithSuggestedFilename(const String& filename, bool& allowOverwrite);
84    void didCreateDestination(const String& path);
85    void didFinish();
86    void platformDidFinish();
87    void didFail(const WebCore::ResourceError&, const CoreIPC::DataReference& resumeData);
88    void didCancel(const CoreIPC::DataReference& resumeData);
89    void didDecideDestination(const String&, bool allowOverwrite);
90
91#if USE(CFNETWORK)
92    const String& destination() const { return m_destination; }
93#endif
94
95    // Authentication
96    static void receivedCredential(const WebCore::AuthenticationChallenge&, const WebCore::Credential&);
97    static void receivedRequestToContinueWithoutCredential(const WebCore::AuthenticationChallenge&);
98    static void receivedCancellation(const WebCore::AuthenticationChallenge&);
99
100private:
101    Download(uint64_t downloadID, const WebCore::ResourceRequest&);
102
103    void platformInvalidate();
104
105    String retrieveDestinationWithSuggestedFilename(const String& filename, bool& allowOverwrite);
106
107    uint64_t m_downloadID;
108    WebCore::ResourceRequest m_request;
109
110    RefPtr<SandboxExtension> m_sandboxExtension;
111
112#if PLATFORM(MAC)
113    RetainPtr<NSURLDownload> m_nsURLDownload;
114    RetainPtr<WKDownloadAsDelegate> m_delegate;
115#endif
116    bool m_allowOverwrite;
117    String m_destination;
118    String m_bundlePath;
119#if USE(CFNETWORK)
120    RetainPtr<CFURLDownloadRef> m_download;
121#endif
122};
123
124} // namespace WebKit
125
126#endif // Download_h
127