1 | /* |
2 | * Copyright (C) 2012 Igalia S.L. |
3 | * |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Library General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 2 of the License, or (at your option) any later version. |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Library General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Library General Public License |
15 | * along with this library; see the file COPYING.LIB. If not, write to |
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | * Boston, MA 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "config.h" |
21 | #include "WebKitDownloadClient.h" |
22 | |
23 | #include "APIDownloadClient.h" |
24 | #include "WebKitDownloadPrivate.h" |
25 | #include "WebKitURIResponsePrivate.h" |
26 | #include "WebKitWebContextPrivate.h" |
27 | #include "WebKitWebViewPrivate.h" |
28 | #include "WebProcessPool.h" |
29 | #include <wtf/glib/GRefPtr.h> |
30 | #include <wtf/text/CString.h> |
31 | |
32 | using namespace WebCore; |
33 | using namespace WebKit; |
34 | |
35 | class DownloadClient final : public API::DownloadClient { |
36 | public: |
37 | explicit DownloadClient(WebKitWebContext* webContext) |
38 | : m_webContext(webContext) |
39 | { |
40 | } |
41 | |
42 | private: |
43 | void didStart(WebProcessPool&, DownloadProxy& downloadProxy) override |
44 | { |
45 | GRefPtr<WebKitDownload> download = webkitWebContextGetOrCreateDownload(&downloadProxy); |
46 | webkitDownloadStarted(download.get()); |
47 | webkitWebContextDownloadStarted(m_webContext, download.get()); |
48 | } |
49 | |
50 | void didReceiveAuthenticationChallenge(WebProcessPool&, DownloadProxy& downloadProxy, AuthenticationChallengeProxy& authenticationChallenge) override |
51 | { |
52 | GRefPtr<WebKitDownload> download = webkitWebContextGetOrCreateDownload(&downloadProxy); |
53 | if (webkitDownloadIsCancelled(download.get())) |
54 | return; |
55 | |
56 | // FIXME: Add API to handle authentication of downloads without a web view associted. |
57 | if (auto* webView = webkit_download_get_web_view(download.get())) |
58 | webkitWebViewHandleAuthenticationChallenge(webView, &authenticationChallenge); |
59 | } |
60 | |
61 | void didReceiveResponse(WebProcessPool&, DownloadProxy& downloadProxy, const ResourceResponse& resourceResponse) override |
62 | { |
63 | GRefPtr<WebKitDownload> download = webkitWebContextGetOrCreateDownload(&downloadProxy); |
64 | if (webkitDownloadIsCancelled(download.get())) |
65 | return; |
66 | |
67 | GRefPtr<WebKitURIResponse> response = adoptGRef(webkitURIResponseCreateForResourceResponse(resourceResponse)); |
68 | webkitDownloadSetResponse(download.get(), response.get()); |
69 | } |
70 | |
71 | void didReceiveData(WebProcessPool&, DownloadProxy& downloadProxy, uint64_t length) override |
72 | { |
73 | GRefPtr<WebKitDownload> download = webkitWebContextGetOrCreateDownload(&downloadProxy); |
74 | webkitDownloadNotifyProgress(download.get(), length); |
75 | } |
76 | |
77 | void decideDestinationWithSuggestedFilename(WebProcessPool&, DownloadProxy& downloadProxy, const String& filename, Function<void(AllowOverwrite, String)>&& completionHandler) override |
78 | { |
79 | GRefPtr<WebKitDownload> download = webkitWebContextGetOrCreateDownload(&downloadProxy); |
80 | bool allowOverwrite = false; |
81 | String destination = webkitDownloadDecideDestinationWithSuggestedFilename(download.get(), filename.utf8(), allowOverwrite); |
82 | completionHandler(allowOverwrite ? AllowOverwrite::Yes : AllowOverwrite::No, destination); |
83 | } |
84 | |
85 | void didCreateDestination(WebProcessPool&, DownloadProxy& downloadProxy, const String& path) override |
86 | { |
87 | GRefPtr<WebKitDownload> download = webkitWebContextGetOrCreateDownload(&downloadProxy); |
88 | webkitDownloadDestinationCreated(download.get(), path); |
89 | } |
90 | |
91 | void didFail(WebProcessPool&, DownloadProxy& downloadProxy, const ResourceError& error) override |
92 | { |
93 | GRefPtr<WebKitDownload> download = webkitWebContextGetOrCreateDownload(&downloadProxy); |
94 | if (webkitDownloadIsCancelled(download.get())) { |
95 | // Cancellation takes precedence over other errors. |
96 | webkitDownloadCancelled(download.get()); |
97 | } else |
98 | webkitDownloadFailed(download.get(), error); |
99 | webkitWebContextRemoveDownload(&downloadProxy); |
100 | } |
101 | |
102 | void didCancel(WebProcessPool&, DownloadProxy& downloadProxy) override |
103 | { |
104 | GRefPtr<WebKitDownload> download = webkitWebContextGetOrCreateDownload(&downloadProxy); |
105 | webkitDownloadCancelled(download.get()); |
106 | webkitWebContextRemoveDownload(&downloadProxy); |
107 | } |
108 | |
109 | void didFinish(WebProcessPool&, DownloadProxy& downloadProxy) override |
110 | { |
111 | GRefPtr<WebKitDownload> download = webkitWebContextGetOrCreateDownload(&downloadProxy); |
112 | webkitDownloadFinished(download.get()); |
113 | webkitWebContextRemoveDownload(&downloadProxy); |
114 | } |
115 | |
116 | WebKitWebContext* m_webContext; |
117 | }; |
118 | |
119 | void attachDownloadClientToContext(WebKitWebContext* webContext) |
120 | { |
121 | webkitWebContextGetProcessPool(webContext).setDownloadClient(std::make_unique<DownloadClient>(webContext)); |
122 | } |
123 | |