| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 #if ENABLE(BLOB) | 33 #if ENABLE(BLOB) |
| 34 | 34 |
| 35 #include "BlobRegistryImpl.h" | 35 #include "BlobRegistryImpl.h" |
| 36 | 36 |
| 37 #include "BlobResourceHandle.h" | 37 #include "BlobResourceHandle.h" |
| 38 #include "ResourceError.h" | 38 #include "ResourceError.h" |
| 39 #include "ResourceHandle.h" | 39 #include "ResourceHandle.h" |
| 40 #include "ResourceLoader.h" | 40 #include "ResourceLoader.h" |
| 41 #include "ResourceRequest.h" | 41 #include "ResourceRequest.h" |
| 42 #include "ResourceResponse.h" | 42 #include "ResourceResponse.h" |
| 43 #include "SecurityOrigin.h" |
| 43 #include <wtf/MainThread.h> | 44 #include <wtf/MainThread.h> |
| 44 #include <wtf/StdLibExtras.h> | 45 #include <wtf/StdLibExtras.h> |
| 45 | 46 |
| 47 |
| 48 |
| 46 namespace WebCore { | 49 namespace WebCore { |
| 47 | 50 |
| 48 #if !PLATFORM(CHROMIUM) | 51 #if !PLATFORM(CHROMIUM) |
| 49 BlobRegistry& blobRegistry() | 52 BlobRegistry& blobRegistry() |
| 50 { | 53 { |
| 51 ASSERT(isMainThread()); | 54 AtomicallyInitializedStatic(BlobRegistryImpl&, intance = *new BlobRegistryIm
pl); |
| 52 DEFINE_STATIC_LOCAL(BlobRegistryImpl, instance, ()); | 55 return intance; |
| 53 return instance; | |
| 54 } | 56 } |
| 55 | 57 |
| 56 static PassRefPtr<ResourceHandle> createResourceHandle(const ResourceRequest& re
quest, ResourceHandleClient* client) | 58 static PassRefPtr<ResourceHandle> createResourceHandle(const ResourceRequest& re
quest, ResourceHandleClient* client) |
| 57 { | 59 { |
| 60 ASSERT(isMainThread()); |
| 58 return static_cast<BlobRegistryImpl&>(blobRegistry()).createResourceHandle(r
equest, client); | 61 return static_cast<BlobRegistryImpl&>(blobRegistry()).createResourceHandle(r
equest, client); |
| 59 } | 62 } |
| 60 | 63 |
| 61 static void registerBlobResourceHandleConstructor() | 64 static void registerBlobResourceHandleConstructor() |
| 62 { | 65 { |
| 66 ASSERT(isMainThread()); |
| 63 static bool didRegister = false; | 67 static bool didRegister = false; |
| 64 if (!didRegister) { | 68 if (!didRegister) { |
| 65 ResourceHandle::registerBuiltinConstructor("blob", createResourceHandle)
; | 69 ResourceHandle::registerBuiltinConstructor("blob", createResourceHandle)
; |
| 66 didRegister = true; | 70 didRegister = true; |
| 67 } | 71 } |
| 68 } | 72 } |
| 69 | 73 |
| 70 #else | 74 #else |
| 71 | 75 // FIXME: This entire file should not be compliled at all for the chromium port,
remove it from gyp(i) files |
| 76 // and remove these conditionals compliation flags. |
| 72 static void registerBlobResourceHandleConstructor() | 77 static void registerBlobResourceHandleConstructor() |
| 73 { | 78 { |
| 74 } | 79 } |
| 75 #endif | 80 #endif |
| 76 | 81 |
| 77 bool BlobRegistryImpl::shouldLoadResource(const ResourceRequest& request) const | 82 bool BlobRegistryImpl::shouldLoadResource(const ResourceRequest& request) const |
| 78 { | 83 { |
| 84 ASSERT(isMainThread()); |
| 79 // If the resource is not fetched using the GET method, bail out. | 85 // If the resource is not fetched using the GET method, bail out. |
| 80 if (!equalIgnoringCase(request.httpMethod(), "GET")) | 86 if (!equalIgnoringCase(request.httpMethod(), "GET")) |
| 81 return false; | 87 return false; |
| 82 | 88 |
| 83 return true; | 89 return true; |
| 84 } | 90 } |
| 85 | 91 |
| 86 PassRefPtr<ResourceHandle> BlobRegistryImpl::createResourceHandle(const Resource
Request& request, ResourceHandleClient* client) | 92 PassRefPtr<ResourceHandle> BlobRegistryImpl::createResourceHandle(const Resource
Request& request, ResourceHandleClient* client) |
| 87 { | 93 { |
| 88 if (!shouldLoadResource(request)) | 94 if (!shouldLoadResource(request)) |
| 89 return 0; | 95 return 0; |
| 90 | 96 |
| 91 RefPtr<BlobResourceHandle> handle = BlobResourceHandle::create(m_blobs.get(r
equest.url().string()), request, client); | 97 RefPtr<BlobResourceHandle> handle = BlobResourceHandle::create(getBlobDataFr
omURL(request.url()), request, client); |
| 92 handle->start(); | 98 handle->start(); |
| 93 return handle.release(); | 99 return handle.release(); |
| 94 } | 100 } |
| 95 | 101 |
| 96 bool BlobRegistryImpl::loadResourceSynchronously(const ResourceRequest& request,
ResourceError& error, ResourceResponse& response, Vector<char>& data) | 102 bool BlobRegistryImpl::loadResourceSynchronously(const ResourceRequest& request,
ResourceError& error, ResourceResponse& response, Vector<char>& data) |
| 97 { | 103 { |
| 98 if (!shouldLoadResource(request)) | 104 if (!shouldLoadResource(request)) |
| 99 return false; | 105 return false; |
| 100 | 106 |
| 101 BlobResourceHandle::loadResourceSynchronously(m_blobs.get(request.url().stri
ng()), request, error, response, data); | 107 BlobResourceHandle::loadResourceSynchronously(getBlobDataFromURL(request.url
()), request, error, response, data); |
| 102 return true; | 108 return true; |
| 103 } | 109 } |
| 104 | 110 |
| 105 void BlobRegistryImpl::appendStorageItems(BlobStorageData* blobStorageData, cons
t BlobDataItemList& items) | |
| 106 { | |
| 107 for (BlobDataItemList::const_iterator iter = items.begin(); iter != items.en
d(); ++iter) { | |
| 108 if (iter->type == BlobDataItem::Data) | |
| 109 blobStorageData->m_data.appendData(iter->data, iter->offset, iter->l
ength); | |
| 110 #if ENABLE(FILE_SYSTEM) | |
| 111 else if (iter->type == BlobDataItem::URL) | |
| 112 blobStorageData->m_data.appendURL(iter->url, iter->offset, iter->len
gth, iter->expectedModificationTime); | |
| 113 #endif | |
| 114 else { | |
| 115 ASSERT(iter->type == BlobDataItem::File); | |
| 116 blobStorageData->m_data.appendFile(iter->path, iter->offset, iter->l
ength, iter->expectedModificationTime); | |
| 117 } | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 void BlobRegistryImpl::appendStorageItems(BlobStorageData* blobStorageData, cons
t BlobDataItemList& items, long long offset, long long length) | 111 void BlobRegistryImpl::appendStorageItems(BlobStorageData* blobStorageData, cons
t BlobDataItemList& items, long long offset, long long length) |
| 122 { | 112 { |
| 123 ASSERT(length != BlobDataItem::toEndOfFile); | 113 ASSERT(length != BlobDataItem::toEndOfFile); |
| 124 | 114 |
| 125 BlobDataItemList::const_iterator iter = items.begin(); | 115 BlobDataItemList::const_iterator iter = items.begin(); |
| 126 if (offset) { | 116 if (offset) { |
| 127 for (; iter != items.end(); ++iter) { | 117 for (; iter != items.end(); ++iter) { |
| 128 if (offset >= iter->length) | 118 if (offset >= iter->length) |
| 129 offset -= iter->length; | 119 offset -= iter->length; |
| 130 else | 120 else |
| 131 break; | 121 break; |
| 132 } | 122 } |
| 133 } | 123 } |
| 134 | 124 |
| 135 for (; iter != items.end() && length > 0; ++iter) { | 125 for (; iter != items.end() && length > 0; ++iter) { |
| 136 long long currentLength = iter->length - offset; | 126 long long currentLength = iter->length - offset; |
| 137 long long newLength = currentLength > length ? length : currentLength; | 127 long long newLength = currentLength > length ? length : currentLength; |
| 138 if (iter->type == BlobDataItem::Data) | 128 if (iter->type == BlobDataItem::Data) |
| 139 blobStorageData->m_data.appendData(iter->data, iter->offset + offset
, newLength); | 129 blobStorageData->m_data.appendData(iter->data, iter->offset + offset
, newLength); |
| 140 #if ENABLE(FILE_SYSTEM) | 130 #if ENABLE(FILE_SYSTEM) |
| 141 else if (iter->type == BlobDataItem::URL) | 131 else if (iter->type == BlobDataItem::URL) |
| 142 blobStorageData->m_data.appendURL(iter->url, iter->offset + offset,
newLength, iter->expectedModificationTime); | 132 blobStorageData->m_data.appendURL(iter->url.copy(), iter->offset + o
ffset, newLength, iter->expectedModificationTime); |
| 143 #endif | 133 #endif |
| 144 else { | 134 else { |
| 145 ASSERT(iter->type == BlobDataItem::File); | 135 ASSERT(iter->type == BlobDataItem::File); |
| 146 blobStorageData->m_data.appendFile(iter->path, iter->offset + offset
, newLength, iter->expectedModificationTime); | 136 blobStorageData->m_data.appendFile(iter->path.isolatedCopy(), iter->
offset + offset, newLength, iter->expectedModificationTime); |
| 147 } | 137 } |
| 148 length -= newLength; | 138 length -= newLength; |
| 149 offset = 0; | 139 offset = 0; |
| 150 } | 140 } |
| 151 } | 141 } |
| 152 | 142 |
| 153 void BlobRegistryImpl::registerBlobURL(const KURL& url, PassOwnPtr<BlobData> blo
bData) | 143 void BlobRegistryImpl::registerBlobData(const String& uuid, PassOwnPtr<BlobData>
blobData) |
| 154 { | 144 { |
| 155 ASSERT(isMainThread()); | |
| 156 registerBlobResourceHandleConstructor(); | |
| 157 | |
| 158 RefPtr<BlobStorageData> blobStorageData = BlobStorageData::create(blobData->
contentType(), blobData->contentDisposition()); | |
| 159 | |
| 160 // The blob data is stored in the "canonical" way. That is, it only contains
a list of Data and File items. | 145 // The blob data is stored in the "canonical" way. That is, it only contains
a list of Data and File items. |
| 161 // 1) The Data item is denoted by the raw data and the range. | 146 // 1) The Data item is denoted by the raw data and the range. |
| 162 // 2) The File item is denoted by the file path, the range and the expected
modification time. | 147 // 2) The File item is denoted by the file path, the range and the expected
modification time. |
| 163 // 3) The URL item is denoted by the URL, the range and the expected modific
ation time. | 148 // 3) The URL item is denoted by the URL, the range and the expected modific
ation time. |
| 164 // All the Blob items in the passing blob data are resolved and expanded int
o a set of Data and File items. | 149 // All the Blob items in the passing blob data are resolved and expanded int
o a set of Data and File items. |
| 165 | 150 |
| 151 RefPtr<BlobStorageData> blobStorageData = BlobStorageData::create(blobData->
contentType(), blobData->contentDisposition()); |
| 166 for (BlobDataItemList::const_iterator iter = blobData->items().begin(); iter
!= blobData->items().end(); ++iter) { | 152 for (BlobDataItemList::const_iterator iter = blobData->items().begin(); iter
!= blobData->items().end(); ++iter) { |
| 167 switch (iter->type) { | 153 switch (iter->type) { |
| 168 case BlobDataItem::Data: | 154 case BlobDataItem::Data: |
| 169 blobStorageData->m_data.appendData(iter->data, 0, iter->data->length
()); | 155 blobStorageData->m_data.appendData(iter->data, 0, iter->data->length
()); |
| 170 break; | 156 break; |
| 171 case BlobDataItem::File: | 157 case BlobDataItem::File: |
| 172 blobStorageData->m_data.appendFile(iter->path, iter->offset, iter->l
ength, iter->expectedModificationTime); | 158 blobStorageData->m_data.appendFile(iter->path.isolatedCopy(), iter->
offset, iter->length, iter->expectedModificationTime); |
| 173 break; | 159 break; |
| 174 #if ENABLE(FILE_SYSTEM) | 160 #if ENABLE(FILE_SYSTEM) |
| 175 case BlobDataItem::URL: | 161 case BlobDataItem::URL: |
| 176 blobStorageData->m_data.appendURL(iter->url, iter->offset, iter->len
gth, iter->expectedModificationTime); | 162 blobStorageData->m_data.appendURL(iter->url.copy(), iter->offset, it
er->length, iter->expectedModificationTime); |
| 177 break; | 163 break; |
| 178 #endif | 164 #endif |
| 179 case BlobDataItem::Blob: | 165 case BlobDataItem::Blob: { |
| 180 if (m_blobs.contains(iter->url.string())) | 166 RefPtr<BlobStorageData> other = getBlobDataFromUUID(iter->blobDa
taHandle->uuid()); |
| 181 appendStorageItems(blobStorageData.get(), m_blobs.get(iter->url.
string())->items(), iter->offset, iter->length); | 167 if (other) |
| 168 appendStorageItems(blobStorageData.get(), other->items(), it
er->offset, iter->length); |
| 169 } |
| 182 break; | 170 break; |
| 183 } | 171 } |
| 184 } | 172 } |
| 185 | 173 |
| 186 m_blobs.set(url.string(), blobStorageData); | 174 |
| 175 MutexLocker locker(m_mutex); |
| 176 ASSERT(m_blobs.find(uuid) == m_blobs.end()); |
| 177 m_blobs.set(uuid.isolatedCopy(), std::make_pair(1, blobStorageData)); |
| 187 } | 178 } |
| 188 | 179 |
| 189 void BlobRegistryImpl::registerBlobURL(const KURL& url, const KURL& srcURL) | 180 void BlobRegistryImpl::addBlobDataRef(const String& uuid) |
| 190 { | 181 { |
| 191 ASSERT(isMainThread()); | 182 MutexLocker locker(m_mutex); |
| 192 registerBlobResourceHandleConstructor(); | 183 BlobMap::iterator found = m_blobs.find(uuid); |
| 193 | 184 ASSERT(found != m_blobs.end()); |
| 194 RefPtr<BlobStorageData> src = m_blobs.get(srcURL.string()); | 185 found->value.first += 1; |
| 195 ASSERT(src); | |
| 196 if (!src) | |
| 197 return; | |
| 198 | |
| 199 m_blobs.set(url.string(), src); | |
| 200 } | 186 } |
| 201 | 187 |
| 202 void BlobRegistryImpl::unregisterBlobURL(const KURL& url) | 188 void BlobRegistryImpl::removeBlobDataRef(const String& uuid) |
| 203 { | 189 { |
| 204 ASSERT(isMainThread()); | 190 MutexLocker locker(m_mutex); |
| 205 m_blobs.remove(url.string()); | 191 BlobMap::iterator found = m_blobs.find(uuid); |
| 192 ASSERT(found != m_blobs.end()); |
| 193 found->value.first -= 1; |
| 194 if (found->value.first == 0) |
| 195 m_blobs.remove(found); |
| 196 } |
| 197 |
| 198 // Helper to access the blob url map on the main thread only when registering/re
voking urls. |
| 199 class BlobRegistryImpl::URLRegistrationHelper{ |
| 200 public: |
| 201 URLRegistrationHelper(BlobRegistryImpl* registry, const KURL& url, PassRefPtr<
BlobDataHandle> dataHandle = 0) |
| 202 : registry(registry) |
| 203 , url(url.copy()) |
| 204 , dataHandle(dataHandle) |
| 205 { |
| 206 } |
| 207 |
| 208 static void registerURLTask(void* context) |
| 209 { |
| 210 OwnPtr<URLRegistrationHelper> helper = adoptPtr(static_cast<URLRegistratio
nHelper*>(context)); |
| 211 helper->registerURL(); |
| 212 } |
| 213 |
| 214 static void revokeURLTask(void* context) |
| 215 { |
| 216 OwnPtr<URLRegistrationHelper> helper = adoptPtr(static_cast<URLRegistratio
nHelper*>(context)); |
| 217 helper->revokeURL(); |
| 218 } |
| 219 |
| 220 void registerURL() |
| 221 { |
| 222 registerBlobResourceHandleConstructor(); |
| 223 registry->m_publicURLs.set(url, dataHandle); |
| 224 } |
| 225 |
| 226 void revokeURL() |
| 227 { |
| 228 registry->m_publicURLs.remove(url); |
| 229 } |
| 230 |
| 231 BlobRegistryImpl* registry; |
| 232 KURL url; |
| 233 RefPtr<BlobDataHandle> dataHandle; // NULL for revoke tasks. |
| 234 }; |
| 235 |
| 236 |
| 237 void BlobRegistryImpl::registerPublicBlobURL(SecurityOrigin* origin, const KURL&
url, PassRefPtr<BlobDataHandle> dataHandle) |
| 238 { |
| 239 // The cached origin has thread affinity so we must fiddle with it's registr
ation on the current thread. |
| 240 BlobRegistry::setCachedUniqueOrigin(url, origin); |
| 241 |
| 242 // But the blob map is only accessed on the main thread. |
| 243 OwnPtr<URLRegistrationHelper> helper = adoptPtr(new URLRegistrationHelper(th
is, url, dataHandle)); |
| 244 if (isMainThread()) |
| 245 helper->registerURL(); |
| 246 else |
| 247 callOnMainThread(&URLRegistrationHelper::registerURLTask, helper.leakPtr
()); |
| 248 } |
| 249 |
| 250 void BlobRegistryImpl::revokePublicBlobURL(const KURL& url) |
| 251 { |
| 252 BlobRegistry::clearCachedUniqueOrigin(url); |
| 253 |
| 254 OwnPtr<URLRegistrationHelper> helper = adoptPtr(new URLRegistrationHelper(th
is, url)); |
| 255 if (isMainThread()) |
| 256 helper->revokeURL(); |
| 257 else |
| 258 callOnMainThread(&URLRegistrationHelper::revokeURLTask, helper.leakPtr()
); |
| 259 } |
| 260 |
| 261 PassRefPtr<SecurityOrigin> BlobRegistryImpl::cachedUniqueOrigin(const KURL& url) |
| 262 { |
| 263 return BlobRegistry::getCachedUniqueOrigin(url); |
| 264 } |
| 265 |
| 266 PassRefPtr<BlobStorageData> BlobRegistryImpl::getBlobDataFromUUID(const String&
uuid) const |
| 267 { |
| 268 MutexLocker locker(m_mutex); |
| 269 return m_blobs.get(uuid).second; |
| 206 } | 270 } |
| 207 | 271 |
| 208 PassRefPtr<BlobStorageData> BlobRegistryImpl::getBlobDataFromURL(const KURL& url
) const | 272 PassRefPtr<BlobStorageData> BlobRegistryImpl::getBlobDataFromURL(const KURL& url
) const |
| 209 { | 273 { |
| 210 ASSERT(isMainThread()); | 274 ASSERT(isMainThread()); |
| 211 return m_blobs.get(url.string()); | 275 RefPtr<BlobDataHandle> dataHandle = m_publicURLs.get(url.string()); |
| 276 if (!dataHandle) |
| 277 return 0; |
| 278 return getBlobDataFromUUID(dataHandle->uuid()); |
| 212 } | 279 } |
| 213 | 280 |
| 214 } // namespace WebCore | 281 } // namespace WebCore |
| 215 | 282 |
| 216 #endif // ENABLE(BLOB) | 283 #endif // ENABLE(BLOB) |
| OLD | NEW |