Use enum classes within FileSystem
[WebKit-https.git] / Source / WebKit / NetworkProcess / Downloads / BlobDownloadClient.cpp
1 /*
2  * Copyright (C) 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 #include "config.h"
27 #include "BlobDownloadClient.h"
28
29 #if !USE(NETWORK_SESSION)
30
31 #include "DataReference.h"
32 #include "Download.h"
33 #include "WebErrors.h"
34 #include <WebCore/MIMETypeRegistry.h>
35 #include <WebCore/ResourceError.h>
36 #include <WebCore/ResourceResponse.h>
37 #include <WebCore/SharedBuffer.h>
38 #include <wtf/CompletionHandler.h>
39
40 namespace WebKit {
41
42 using namespace WebCore;
43
44 BlobDownloadClient::BlobDownloadClient(Download& download)
45     : m_download(download)
46 {
47 }
48
49 void BlobDownloadClient::didReceiveResponseAsync(ResourceHandle*, ResourceResponse&& response)
50 {
51     m_download.didReceiveResponse(WTFMove(response));
52     String suggestedFilename = MIMETypeRegistry::appendFileExtensionIfNecessary(m_download.suggestedName().isEmpty() ? ASCIILiteral("unknown") : m_download.suggestedName(), response.mimeType());
53     m_download.decideDestinationWithSuggestedFilenameAsync(suggestedFilename);
54 }
55
56 void BlobDownloadClient::didDecideDownloadDestination(const String& destinationPath, bool allowOverwrite)
57 {
58     ASSERT(!destinationPath.isEmpty());
59
60     if (FileSystem::fileExists(destinationPath)) {
61         if (!allowOverwrite) {
62             didFail(nullptr, cancelledError(m_download.request()));
63             return;
64         }
65         FileSystem::deleteFile(destinationPath);
66     }
67
68     m_destinationPath = destinationPath;
69     m_destinationFile = FileSystem::openFile(m_destinationPath, FileSystem::FileOpenMode::Write);
70     m_download.didCreateDestination(m_destinationPath);
71
72     m_download.continueDidReceiveResponse();
73 }
74
75 void BlobDownloadClient::willSendRequestAsync(ResourceHandle*, ResourceRequest&& request, ResourceResponse&&, CompletionHandler<void(ResourceRequest&&)>&& completionHandler)
76 {
77     ASSERT_NOT_REACHED();
78     completionHandler(WTFMove(request));
79 }
80
81 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
82 void BlobDownloadClient::canAuthenticateAgainstProtectionSpaceAsync(ResourceHandle*, const ProtectionSpace&)
83 {
84     ASSERT_NOT_REACHED();
85 }
86 #endif
87
88 void BlobDownloadClient::didReceiveBuffer(ResourceHandle*, Ref<SharedBuffer>&& buffer, int)
89 {
90     FileSystem::writeToFile(m_destinationFile, buffer->data(), buffer->size());
91     m_download.didReceiveData(buffer->size());
92 }
93
94 void BlobDownloadClient::didFinishLoading(ResourceHandle*)
95 {
96     FileSystem::closeFile(m_destinationFile);
97     m_download.didFinish();
98 }
99
100 void BlobDownloadClient::didCancel()
101 {
102     FileSystem::closeFile(m_destinationFile);
103     if (!m_destinationPath.isEmpty())
104         FileSystem::deleteFile(m_destinationPath);
105
106     m_download.didCancel(IPC::DataReference());
107 }
108
109 void BlobDownloadClient::didFail(ResourceHandle*, const ResourceError& error)
110 {
111     FileSystem::closeFile(m_destinationFile);
112     if (!m_destinationPath.isEmpty())
113         FileSystem::deleteFile(m_destinationPath);
114
115     m_download.didFail(error, IPC::DataReference());
116 }
117
118 }
119
120 #endif // !USE(NETWORK_SESSION)