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