1 /*
2 * Copyright (C) 2010 Google 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32
33 #if ENABLE(BLOB)
34
35 #include "BlobRegistryImpl.h"
36
37 #include "BlobResourceHandle.h"
38 #include "ResourceError.h"
39 #include "ResourceHandle.h"
40 #include "ResourceLoader.h"
41 #include "ResourceRequest.h"
42 #include "ResourceResponse.h"
43 #include <wtf/MainThread.h>
44 #include <wtf/StdLibExtras.h>
45
46 namespace WebCore {
47
48 #if !PLATFORM(CHROMIUM)
blobRegistry()49 BlobRegistry& blobRegistry()
50 {
51 ASSERT(isMainThread());
52 DEFINE_STATIC_LOCAL(BlobRegistryImpl, instance, ());
53 return instance;
54 }
55 #endif
56
shouldLoadResource(const ResourceRequest & request) const57 bool BlobRegistryImpl::shouldLoadResource(const ResourceRequest& request) const
58 {
59 // If the resource is not fetched using the GET method, bail out.
60 if (!equalIgnoringCase(request.httpMethod(), "GET"))
61 return false;
62
63 return true;
64 }
65
createResourceHandle(const ResourceRequest & request,ResourceHandleClient * client)66 PassRefPtr<ResourceHandle> BlobRegistryImpl::createResourceHandle(const ResourceRequest& request, ResourceHandleClient* client)
67 {
68 if (!shouldLoadResource(request))
69 return 0;
70
71 RefPtr<BlobResourceHandle> handle = BlobResourceHandle::create(m_blobs.get(request.url().string()), request, client);
72 handle->start();
73 return handle.release();
74 }
75
loadResourceSynchronously(const ResourceRequest & request,ResourceError & error,ResourceResponse & response,Vector<char> & data)76 bool BlobRegistryImpl::loadResourceSynchronously(const ResourceRequest& request, ResourceError& error, ResourceResponse& response, Vector<char>& data)
77 {
78 if (!shouldLoadResource(request))
79 return false;
80
81 BlobResourceHandle::loadResourceSynchronously(m_blobs.get(request.url().string()), request, error, response, data);
82 return true;
83 }
84
appendStorageItems(BlobStorageData * blobStorageData,const BlobDataItemList & items)85 void BlobRegistryImpl::appendStorageItems(BlobStorageData* blobStorageData, const BlobDataItemList& items)
86 {
87 for (BlobDataItemList::const_iterator iter = items.begin(); iter != items.end(); ++iter) {
88 if (iter->type == BlobDataItem::Data)
89 blobStorageData->m_data.appendData(iter->data, iter->offset, iter->length);
90 else {
91 ASSERT(iter->type == BlobDataItem::File);
92 blobStorageData->m_data.appendFile(iter->path, iter->offset, iter->length, iter->expectedModificationTime);
93 }
94 }
95 }
96
appendStorageItems(BlobStorageData * blobStorageData,const BlobDataItemList & items,long long offset,long long length)97 void BlobRegistryImpl::appendStorageItems(BlobStorageData* blobStorageData, const BlobDataItemList& items, long long offset, long long length)
98 {
99 ASSERT(length != BlobDataItem::toEndOfFile);
100
101 BlobDataItemList::const_iterator iter = items.begin();
102 if (offset) {
103 for (; iter != items.end(); ++iter) {
104 if (offset >= iter->length)
105 offset -= iter->length;
106 else
107 break;
108 }
109 }
110
111 for (; iter != items.end() && length > 0; ++iter) {
112 long long currentLength = iter->length - offset;
113 long long newLength = currentLength > length ? length : currentLength;
114 if (iter->type == BlobDataItem::Data)
115 blobStorageData->m_data.appendData(iter->data, iter->offset + offset, newLength);
116 else {
117 ASSERT(iter->type == BlobDataItem::File);
118 blobStorageData->m_data.appendFile(iter->path, iter->offset + offset, newLength, iter->expectedModificationTime);
119 }
120 length -= newLength;
121 offset = 0;
122 }
123 }
124
registerBlobURL(const KURL & url,PassOwnPtr<BlobData> blobData)125 void BlobRegistryImpl::registerBlobURL(const KURL& url, PassOwnPtr<BlobData> blobData)
126 {
127 ASSERT(isMainThread());
128
129 RefPtr<BlobStorageData> blobStorageData = BlobStorageData::create(blobData->contentType(), blobData->contentDisposition());
130
131 // The blob data is stored in the "canonical" way. That is, it only contains a list of Data and File items.
132 // 1) The Data item is denoted by the raw data and the range.
133 // 2) The File item is denoted by the file path, the range and the expected modification time.
134 // All the Blob items in the passing blob data are resolved and expanded into a set of Data and File items.
135
136 for (BlobDataItemList::const_iterator iter = blobData->items().begin(); iter != blobData->items().end(); ++iter) {
137 switch (iter->type) {
138 case BlobDataItem::Data:
139 blobStorageData->m_data.appendData(iter->data, 0, iter->data->length());
140 break;
141 case BlobDataItem::File:
142 blobStorageData->m_data.appendFile(iter->path, iter->offset, iter->length, iter->expectedModificationTime);
143 break;
144 case BlobDataItem::Blob:
145 if (m_blobs.contains(iter->url.string()))
146 appendStorageItems(blobStorageData.get(), m_blobs.get(iter->url.string())->items(), iter->offset, iter->length);
147 break;
148 }
149 }
150
151 m_blobs.set(url.string(), blobStorageData);
152 }
153
registerBlobURL(const KURL & url,const KURL & srcURL)154 void BlobRegistryImpl::registerBlobURL(const KURL& url, const KURL& srcURL)
155 {
156 ASSERT(isMainThread());
157
158 RefPtr<BlobStorageData> src = m_blobs.get(srcURL.string());
159 ASSERT(src);
160 if (!src)
161 return;
162
163 m_blobs.set(url.string(), src);
164 }
165
unregisterBlobURL(const KURL & url)166 void BlobRegistryImpl::unregisterBlobURL(const KURL& url)
167 {
168 ASSERT(isMainThread());
169 m_blobs.remove(url.string());
170 }
171
getBlobDataFromURL(const KURL & url) const172 PassRefPtr<BlobStorageData> BlobRegistryImpl::getBlobDataFromURL(const KURL& url) const
173 {
174 ASSERT(isMainThread());
175 return m_blobs.get(url.string());
176 }
177
178 } // namespace WebCore
179
180 #endif // ENABLE(BLOB)
181