[Resource Timing] Gather timing information with reliable responseEnd time
[WebKit-https.git] / Source / WebKit2 / 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
39 namespace WebKit {
40
41 using namespace WebCore;
42
43 BlobDownloadClient::BlobDownloadClient(Download& download)
44     : m_download(download)
45 {
46 }
47
48 void BlobDownloadClient::didReceiveResponseAsync(ResourceHandle*, ResourceResponse&& response)
49 {
50     m_download.didReceiveResponse(WTFMove(response));
51     String suggestedFilename = MIMETypeRegistry::appendFileExtensionIfNecessary(m_download.suggestedName().isEmpty() ? ASCIILiteral("unknown") : m_download.suggestedName(), response.mimeType());
52     m_download.decideDestinationWithSuggestedFilenameAsync(suggestedFilename);
53 }
54
55 void BlobDownloadClient::didDecideDownloadDestination(const String& destinationPath, bool allowOverwrite)
56 {
57     ASSERT(!destinationPath.isEmpty());
58
59     if (fileExists(destinationPath)) {
60         if (!allowOverwrite) {
61             didFail(nullptr, cancelledError(m_download.request()));
62             return;
63         }
64         deleteFile(destinationPath);
65     }
66
67     m_destinationPath = destinationPath;
68     m_destinationFile = openFile(m_destinationPath, OpenForWrite);
69     m_download.didCreateDestination(m_destinationPath);
70
71     m_download.continueDidReceiveResponse();
72 }
73
74 void BlobDownloadClient::didReceiveBuffer(ResourceHandle*, Ref<SharedBuffer>&& buffer, int)
75 {
76     writeToFile(m_destinationFile, buffer->data(), buffer->size());
77     m_download.didReceiveData(buffer->size());
78 }
79
80 void BlobDownloadClient::didFinishLoading(ResourceHandle*)
81 {
82     closeFile(m_destinationFile);
83     m_download.didFinish();
84 }
85
86 void BlobDownloadClient::didCancel()
87 {
88     closeFile(m_destinationFile);
89     if (!m_destinationPath.isEmpty())
90         deleteFile(m_destinationPath);
91
92     m_download.didCancel(IPC::DataReference());
93 }
94
95 void BlobDownloadClient::didFail(ResourceHandle*, const ResourceError& error)
96 {
97     closeFile(m_destinationFile);
98     if (!m_destinationPath.isEmpty())
99         deleteFile(m_destinationPath);
100
101     m_download.didFail(error, IPC::DataReference());
102 }
103
104 }
105
106 #endif // !USE(NETWORK_SESSION)