1/*
2 * Copyright (C) 2010-2016 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#pragma once
27
28#include "APIObject.h"
29#include "Connection.h"
30#include "DownloadID.h"
31#include "DownloadProxyMessages.h"
32#include "SandboxExtension.h"
33#include <WebCore/ResourceRequest.h>
34#include <wtf/Forward.h>
35#include <wtf/Ref.h>
36#include <wtf/WeakPtr.h>
37
38namespace API {
39class Data;
40}
41
42namespace WebCore {
43class AuthenticationChallenge;
44class IntRect;
45class ProtectionSpace;
46class ResourceError;
47class ResourceResponse;
48}
49
50namespace WebKit {
51
52class DownloadID;
53class DownloadProxyMap;
54class WebPageProxy;
55class WebProcessPool;
56
57class DownloadProxy : public API::ObjectImpl<API::Object::Type::Download>, public IPC::MessageReceiver {
58public:
59 static Ref<DownloadProxy> create(DownloadProxyMap&, WebProcessPool&, const WebCore::ResourceRequest&);
60 ~DownloadProxy();
61
62 DownloadID downloadID() const { return m_downloadID; }
63 const WebCore::ResourceRequest& request() const { return m_request; }
64 API::Data* resumeData() const { return m_resumeData.get(); }
65
66 void cancel();
67
68 void invalidate();
69 void processDidClose();
70
71 void didReceiveDownloadProxyMessage(IPC::Connection&, IPC::Decoder&);
72 void didReceiveSyncDownloadProxyMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
73
74 WebPageProxy* originatingPage() const;
75 void setOriginatingPage(WebPageProxy*);
76
77 void setRedirectChain(Vector<URL>&& redirectChain) { m_redirectChain = WTFMove(redirectChain); }
78 const Vector<URL>& redirectChain() const { return m_redirectChain; }
79
80 void setWasUserInitiated(bool value) { m_wasUserInitiated = value; }
81 bool wasUserInitiated() const { return m_wasUserInitiated; }
82
83 String destinationFilename() const { return m_destinationFilename; }
84 void setDestinationFilename(const String& d) { m_destinationFilename = d; }
85
86 uint64_t expectedContentLength() const { return m_expectedContentLength; }
87 void setExpectedContentLength(uint64_t expectedContentLength) { m_expectedContentLength = expectedContentLength; }
88
89 uint64_t bytesLoaded() const { return m_bytesLoaded; }
90 void setBytesLoaded(uint64_t bytesLoaded) { m_bytesLoaded = bytesLoaded; }
91
92#if USE(SYSTEM_PREVIEW)
93 bool isSystemPreviewDownload() const { return request().isSystemPreview(); }
94 const WebCore::IntRect& systemPreviewDownloadRect() const { return request().systemPreviewRect(); }
95#endif
96
97#if PLATFORM(COCOA)
98 void publishProgress(const URL&);
99#endif
100
101private:
102 explicit DownloadProxy(DownloadProxyMap&, WebProcessPool&, const WebCore::ResourceRequest&);
103
104 // IPC::MessageReceiver
105 void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
106
107 // Message handlers.
108 void didStart(const WebCore::ResourceRequest&, const String& suggestedFilename);
109 void didReceiveAuthenticationChallenge(WebCore::AuthenticationChallenge&&, uint64_t challengeID);
110 void didReceiveResponse(const WebCore::ResourceResponse&);
111 void didReceiveData(uint64_t length);
112 void shouldDecodeSourceDataOfMIMEType(const String& mimeType, bool& result);
113 void didCreateDestination(const String& path);
114 void didFinish();
115 void didFail(const WebCore::ResourceError&, const IPC::DataReference& resumeData);
116 void didCancel(const IPC::DataReference& resumeData);
117 void willSendRequest(WebCore::ResourceRequest&& redirectRequest, const WebCore::ResourceResponse& redirectResponse);
118 void decideDestinationWithSuggestedFilenameAsync(DownloadID, const String& suggestedFilename);
119
120 DownloadProxyMap& m_downloadProxyMap;
121 RefPtr<WebProcessPool> m_processPool;
122 DownloadID m_downloadID;
123
124 RefPtr<API::Data> m_resumeData;
125 WebCore::ResourceRequest m_request;
126 String m_suggestedFilename;
127 String m_destinationFilename;
128 uint64_t m_expectedContentLength { 0 };
129 uint64_t m_bytesLoaded { 0 };
130
131 WeakPtr<WebPageProxy> m_originatingPage;
132 Vector<URL> m_redirectChain;
133 bool m_wasUserInitiated { true };
134};
135
136} // namespace WebKit
137