Unreviewed. Fix the build with ENABLE(NETWORK_CACHE) and !ENABLE(SHAREABLE_RESOURCE).
[WebKit-https.git] / Source / WebKit2 / NetworkProcess / cache / NetworkCacheEntry.cpp
1 /*
2  * Copyright (C) 2015 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 "NetworkCache.h"
28
29 #include "Logging.h"
30 #include "NetworkCacheCoders.h"
31 #include "NetworkCacheDecoder.h"
32 #include "NetworkCacheEncoder.h"
33 #include <JavaScriptCore/JSONObject.h>
34 #include <WebCore/ResourceRequest.h>
35 #include <WebCore/SharedBuffer.h>
36 #include <wtf/text/StringBuilder.h>
37
38 #if ENABLE(NETWORK_CACHE)
39
40 namespace WebKit {
41 namespace NetworkCache {
42
43 Entry::Entry(const Key& key, const WebCore::ResourceResponse& response, RefPtr<WebCore::SharedBuffer>&& buffer, const Vector<std::pair<String, String>>& varyingRequestHeaders)
44     : m_key(key)
45     , m_timeStamp(std::chrono::system_clock::now())
46     , m_response(response)
47     , m_varyingRequestHeaders(varyingRequestHeaders)
48     , m_buffer(WTF::move(buffer))
49 {
50 }
51
52 Entry::Entry(const Storage::Record& storageEntry)
53     : m_key(storageEntry.key)
54     , m_timeStamp(storageEntry.timeStamp)
55     , m_sourceStorageRecord(storageEntry)
56 {
57 }
58
59 Storage::Record Entry::encodeAsStorageRecord() const
60 {
61     Encoder encoder;
62     encoder << m_response;
63
64     bool hasVaryingRequestHeaders = !m_varyingRequestHeaders.isEmpty();
65     encoder << hasVaryingRequestHeaders;
66     if (hasVaryingRequestHeaders)
67         encoder << m_varyingRequestHeaders;
68
69     encoder.encodeChecksum();
70
71     Data header(encoder.buffer(), encoder.bufferSize());
72     Data body;
73     if (m_buffer)
74         body = { reinterpret_cast<const uint8_t*>(m_buffer->data()), m_buffer->size() };
75
76     return { m_key, m_timeStamp, header, body };
77 }
78
79 std::unique_ptr<Entry> Entry::decodeStorageRecord(const Storage::Record& storageEntry)
80 {
81     std::unique_ptr<Entry> entry(new Entry(storageEntry));
82
83     Decoder decoder(storageEntry.header.data(), storageEntry.header.size());
84     if (!decoder.decode(entry->m_response))
85         return nullptr;
86     entry->m_response.setSource(WebCore::ResourceResponse::Source::DiskCache);
87
88     bool hasVaryingRequestHeaders;
89     if (!decoder.decode(hasVaryingRequestHeaders))
90         return nullptr;
91
92     if (hasVaryingRequestHeaders) {
93         if (!decoder.decode(entry->m_varyingRequestHeaders))
94             return nullptr;
95     }
96
97     if (!decoder.verifyChecksum()) {
98         LOG(NetworkCache, "(NetworkProcess) checksum verification failure\n");
99         return nullptr;
100     }
101
102     return entry;
103 }
104
105 void Entry::initializeBufferFromStorageRecord() const
106 {
107     auto* data = m_sourceStorageRecord.body.data();
108     size_t size = m_sourceStorageRecord.body.size();
109 #if ENABLE(SHAREABLE_RESOURCE)
110     RefPtr<SharedMemory> sharedMemory = m_sourceStorageRecord.body.isMap() ? SharedMemory::create(const_cast<uint8_t*>(data), size, SharedMemory::Protection::ReadOnly) : nullptr;
111     RefPtr<ShareableResource> shareableResource = sharedMemory ? ShareableResource::create(sharedMemory.release(), 0, m_sourceStorageRecord.body.size()) : nullptr;
112
113     if (shareableResource && shareableResource->createHandle(m_shareableResourceHandle))
114         m_buffer = m_shareableResourceHandle.tryWrapInSharedBuffer();
115 #endif
116     if (!m_buffer)
117         m_buffer = WebCore::SharedBuffer::create(data, size);
118 }
119
120 WebCore::SharedBuffer* Entry::buffer() const
121 {
122     if (!m_buffer)
123         initializeBufferFromStorageRecord();
124
125     return m_buffer.get();
126 }
127
128 #if ENABLE(SHAREABLE_RESOURCE)
129 ShareableResource::Handle& Entry::shareableResourceHandle() const
130 {
131     if (!m_buffer)
132         initializeBufferFromStorageRecord();
133
134     return m_shareableResourceHandle;
135 }
136 #endif
137
138 bool Entry::needsValidation() const
139 {
140     return m_response.source() == WebCore::ResourceResponse::Source::DiskCacheAfterValidation;
141 }
142
143 void Entry::setNeedsValidation()
144 {
145     ASSERT(m_response.source() == WebCore::ResourceResponse::Source::DiskCache);
146     m_response.setSource(WebCore::ResourceResponse::Source::DiskCacheAfterValidation);
147 }
148
149 void Entry::asJSON(StringBuilder& json, const Storage::RecordInfo& info) const
150 {
151     json.appendLiteral("{\n");
152     json.appendLiteral("\"hash\": ");
153     JSC::appendQuotedJSONStringToBuilder(json, m_key.hashAsString());
154     json.appendLiteral(",\n");
155     json.appendLiteral("\"bodySize\": ");
156     json.appendNumber(info.bodySize);
157     json.appendLiteral(",\n");
158     json.appendLiteral("\"worth\": ");
159     json.appendNumber(info.worth);
160     json.appendLiteral(",\n");
161     json.appendLiteral("\"partition\": ");
162     JSC::appendQuotedJSONStringToBuilder(json, m_key.partition());
163     json.appendLiteral(",\n");
164     json.appendLiteral("\"timestamp\": ");
165     json.appendNumber(std::chrono::duration_cast<std::chrono::milliseconds>(m_timeStamp.time_since_epoch()).count());
166     json.appendLiteral(",\n");
167     json.appendLiteral("\"URL\": ");
168     JSC::appendQuotedJSONStringToBuilder(json, m_response.url().string());
169     json.appendLiteral(",\n");
170     json.appendLiteral("\"bodyHash\": ");
171     JSC::appendQuotedJSONStringToBuilder(json, info.bodyHash);
172     json.appendLiteral(",\n");
173     json.appendLiteral("\"bodyShareCount\": ");
174     json.appendNumber(info.bodyShareCount);
175     json.appendLiteral(",\n");
176     json.appendLiteral("\"headers\": {\n");
177     bool firstHeader = true;
178     for (auto& header : m_response.httpHeaderFields()) {
179         if (!firstHeader)
180             json.appendLiteral(",\n");
181         firstHeader = false;
182         json.appendLiteral("    ");
183         JSC::appendQuotedJSONStringToBuilder(json, header.key);
184         json.appendLiteral(": ");
185         JSC::appendQuotedJSONStringToBuilder(json, header.value);
186     }
187     json.appendLiteral("\n}\n");
188     json.appendLiteral("}");
189 }
190
191 }
192 }
193
194 #endif