Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: WebCore/platform/network/BlobRegistryImpl.cpp

Issue 11192017: ********** WebCore blob hacking (Closed) Base URL: http://svn.webkit.org/repository/webkit/trunk/Source/
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
46 namespace WebCore { 47 namespace WebCore {
47 48
48 #if !PLATFORM(CHROMIUM)
49 BlobRegistry& blobRegistry() 49 BlobRegistry& blobRegistry()
50 { 50 {
51 ASSERT(isMainThread()); 51 // TODO: would be nice to ensure that this is first accessed on the main thr ead.
52 DEFINE_STATIC_LOCAL(BlobRegistryImpl, instance, ()); 52 AtomicallyInitializedStatic(BlobRegistryImpl&, intance = *new BlobRegistryIm pl);
53 return instance; 53 return intance;
54 } 54 }
55 55
56 static PassRefPtr<ResourceHandle> createResourceHandle(const ResourceRequest& re quest, ResourceHandleClient* client) 56 static PassRefPtr<ResourceHandle> createResourceHandle(const ResourceRequest& re quest, ResourceHandleClient* client)
57 { 57 {
58 ASSERT(isMainThread());
58 return static_cast<BlobRegistryImpl&>(blobRegistry()).createResourceHandle(r equest, client); 59 return static_cast<BlobRegistryImpl&>(blobRegistry()).createResourceHandle(r equest, client);
59 } 60 }
60 61
61 static void registerBlobResourceHandleConstructor() 62 static void registerBlobResourceHandleConstructor()
62 { 63 {
64 ASSERT(isMainThread());
63 static bool didRegister = false; 65 static bool didRegister = false;
64 if (!didRegister) { 66 if (!didRegister) {
65 ResourceHandle::registerBuiltinConstructor("blob", createResourceHandle) ; 67 ResourceHandle::registerBuiltinConstructor("blob", createResourceHandle) ;
66 didRegister = true; 68 didRegister = true;
67 } 69 }
68 } 70 }
69 71
70 #else
71
72 static void registerBlobResourceHandleConstructor()
73 {
74 }
75 #endif
76
77 bool BlobRegistryImpl::shouldLoadResource(const ResourceRequest& request) const 72 bool BlobRegistryImpl::shouldLoadResource(const ResourceRequest& request) const
78 { 73 {
74 ASSERT(isMainThread());
79 // If the resource is not fetched using the GET method, bail out. 75 // If the resource is not fetched using the GET method, bail out.
80 if (!equalIgnoringCase(request.httpMethod(), "GET")) 76 if (!equalIgnoringCase(request.httpMethod(), "GET"))
81 return false; 77 return false;
82 78
83 return true; 79 return true;
84 } 80 }
85 81
86 PassRefPtr<ResourceHandle> BlobRegistryImpl::createResourceHandle(const Resource Request& request, ResourceHandleClient* client) 82 PassRefPtr<ResourceHandle> BlobRegistryImpl::createResourceHandle(const Resource Request& request, ResourceHandleClient* client)
87 { 83 {
88 if (!shouldLoadResource(request)) 84 if (!shouldLoadResource(request))
89 return 0; 85 return 0;
90 86
91 RefPtr<BlobResourceHandle> handle = BlobResourceHandle::create(m_blobs.get(r equest.url().string()), request, client); 87 RefPtr<BlobResourceHandle> handle = BlobResourceHandle::create(getBlobDataFr omURL(request.url()), request, client);
92 handle->start(); 88 handle->start();
93 return handle.release(); 89 return handle.release();
94 } 90 }
95 91
96 bool BlobRegistryImpl::loadResourceSynchronously(const ResourceRequest& request, ResourceError& error, ResourceResponse& response, Vector<char>& data) 92 bool BlobRegistryImpl::loadResourceSynchronously(const ResourceRequest& request, ResourceError& error, ResourceResponse& response, Vector<char>& data)
97 { 93 {
98 if (!shouldLoadResource(request)) 94 if (!shouldLoadResource(request))
99 return false; 95 return false;
100 96
101 BlobResourceHandle::loadResourceSynchronously(m_blobs.get(request.url().stri ng()), request, error, response, data); 97 BlobResourceHandle::loadResourceSynchronously(getBlobDataFromURL(request.url ()), request, error, response, data);
102 return true; 98 return true;
103 } 99 }
104 100
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) 101 void BlobRegistryImpl::appendStorageItems(BlobStorageData* blobStorageData, cons t BlobDataItemList& items, long long offset, long long length)
122 { 102 {
123 ASSERT(length != BlobDataItem::toEndOfFile); 103 ASSERT(length != BlobDataItem::toEndOfFile);
124 104
125 BlobDataItemList::const_iterator iter = items.begin(); 105 BlobDataItemList::const_iterator iter = items.begin();
126 if (offset) { 106 if (offset) {
127 for (; iter != items.end(); ++iter) { 107 for (; iter != items.end(); ++iter) {
128 if (offset >= iter->length) 108 if (offset >= iter->length)
129 offset -= iter->length; 109 offset -= iter->length;
130 else 110 else
131 break; 111 break;
132 } 112 }
133 } 113 }
134 114
135 for (; iter != items.end() && length > 0; ++iter) { 115 for (; iter != items.end() && length > 0; ++iter) {
136 long long currentLength = iter->length - offset; 116 long long currentLength = iter->length - offset;
137 long long newLength = currentLength > length ? length : currentLength; 117 long long newLength = currentLength > length ? length : currentLength;
138 if (iter->type == BlobDataItem::Data) 118 if (iter->type == BlobDataItem::Data)
139 blobStorageData->m_data.appendData(iter->data, iter->offset + offset , newLength); 119 blobStorageData->m_data.appendData(iter->data, iter->offset + offset , newLength);
140 #if ENABLE(FILE_SYSTEM) 120 #if ENABLE(FILE_SYSTEM)
141 else if (iter->type == BlobDataItem::URL) 121 else if (iter->type == BlobDataItem::URL)
142 blobStorageData->m_data.appendURL(iter->url, iter->offset + offset, newLength, iter->expectedModificationTime); 122 blobStorageData->m_data.appendURL(iter->url.copy(), iter->offset + o ffset, newLength, iter->expectedModificationTime);
143 #endif 123 #endif
144 else { 124 else {
145 ASSERT(iter->type == BlobDataItem::File); 125 ASSERT(iter->type == BlobDataItem::File);
146 blobStorageData->m_data.appendFile(iter->path, iter->offset + offset , newLength, iter->expectedModificationTime); 126 blobStorageData->m_data.appendFile(iter->path.isolatedCopy(), iter-> offset + offset, newLength, iter->expectedModificationTime);
147 } 127 }
148 length -= newLength; 128 length -= newLength;
149 offset = 0; 129 offset = 0;
150 } 130 }
151 } 131 }
152 132
153 void BlobRegistryImpl::registerBlobURL(const KURL& url, PassOwnPtr<BlobData> blo bData) 133 void BlobRegistryImpl::registerBlobData(const String& uuid, PassOwnPtr<BlobData> blobData)
154 { 134 {
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. 135 // 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. 136 // 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. 137 // 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. 138 // 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. 139 // All the Blob items in the passing blob data are resolved and expanded int o a set of Data and File items.
165 140
141 RefPtr<BlobStorageData> blobStorageData = BlobStorageData::create(blobData-> contentType(), blobData->contentDisposition());
166 for (BlobDataItemList::const_iterator iter = blobData->items().begin(); iter != blobData->items().end(); ++iter) { 142 for (BlobDataItemList::const_iterator iter = blobData->items().begin(); iter != blobData->items().end(); ++iter) {
167 switch (iter->type) { 143 switch (iter->type) {
168 case BlobDataItem::Data: 144 case BlobDataItem::Data:
145 // FIXME: Can we avoid making this copy of the data?
169 blobStorageData->m_data.appendData(iter->data, 0, iter->data->length ()); 146 blobStorageData->m_data.appendData(iter->data, 0, iter->data->length ());
170 break; 147 break;
171 case BlobDataItem::File: 148 case BlobDataItem::File:
172 blobStorageData->m_data.appendFile(iter->path, iter->offset, iter->l ength, iter->expectedModificationTime); 149 blobStorageData->m_data.appendFile(iter->path.isolatedCopy(), iter-> offset, iter->length, iter->expectedModificationTime);
173 break; 150 break;
174 #if ENABLE(FILE_SYSTEM) 151 #if ENABLE(FILE_SYSTEM)
175 case BlobDataItem::URL: 152 case BlobDataItem::URL:
176 blobStorageData->m_data.appendURL(iter->url, iter->offset, iter->len gth, iter->expectedModificationTime); 153 blobStorageData->m_data.appendURL(iter->url.copy(), iter->offset, it er->length, iter->expectedModificationTime);
177 break; 154 break;
178 #endif 155 #endif
179 case BlobDataItem::Blob: 156 case BlobDataItem::Blob: {
180 if (m_blobs.contains(iter->url.string())) 157 RefPtr<BlobStorageData> other = getBlobDataFromUUID(iter->blobDa taHandle->uuid());
181 appendStorageItems(blobStorageData.get(), m_blobs.get(iter->url. string())->items(), iter->offset, iter->length); 158 if (other)
159 appendStorageItems(blobStorageData.get(), other->items(), it er->offset, iter->length);
160 }
182 break; 161 break;
183 } 162 }
184 } 163 }
185 164
186 m_blobs.set(url.string(), blobStorageData); 165
166 MutexLocker locker(m_mutex);
167 ASSERT(m_blobs.find(uuid) == m_blobs.end());
168 m_blobs.set(uuid.isolatedCopy(), std::make_pair(1, blobStorageData));
187 } 169 }
188 170
189 void BlobRegistryImpl::registerBlobURL(const KURL& url, const KURL& srcURL) 171 void BlobRegistryImpl::addBlobDataRef(const String& uuid)
190 { 172 {
191 ASSERT(isMainThread()); 173 MutexLocker locker(m_mutex);
192 registerBlobResourceHandleConstructor(); 174 BlobMap::iterator found = m_blobs.find(uuid);
193 175 ASSERT(found != m_blobs.end());
194 RefPtr<BlobStorageData> src = m_blobs.get(srcURL.string()); 176 found->value.first += 1;
195 ASSERT(src);
196 if (!src)
197 return;
198
199 m_blobs.set(url.string(), src);
200 } 177 }
201 178
202 void BlobRegistryImpl::unregisterBlobURL(const KURL& url) 179 void BlobRegistryImpl::removeBlobDataRef(const String& uuid)
203 { 180 {
204 ASSERT(isMainThread()); 181 MutexLocker locker(m_mutex);
205 m_blobs.remove(url.string()); 182 BlobMap::iterator found = m_blobs.find(uuid);
183 ASSERT(found != m_blobs.end());
184 found->value.first -= 1;
185 if (found->value.first == 0)
186 m_blobs.remove(found);
187 }
188
189 // Helper to access the blob url map on the main thread only when registering/re voking urls.
190 class BlobRegistryImpl::URLRegistrationHelper {
191 public:
192 URLRegistrationHelper(BlobRegistryImpl* registry, const KURL& url, PassRefPtr< BlobDataHandle> dataHandle = 0)
193 : registry(registry)
194 , url(url.copy())
195 , dataHandle(dataHandle)
196 {
197 }
198
199 static void registerURLTask(void* context)
200 {
201 OwnPtr<URLRegistrationHelper> helper = adoptPtr(static_cast<URLRegistratio nHelper*>(context));
202 helper->registerURL();
203 }
204
205 static void revokeURLTask(void* context)
206 {
207 OwnPtr<URLRegistrationHelper> helper = adoptPtr(static_cast<URLRegistratio nHelper*>(context));
208 helper->revokeURL();
209 }
210
211 void registerURL()
212 {
213 ASSERT(isMainThread());
214 registerBlobResourceHandleConstructor();
215 registry->m_publicURLs.set(url, dataHandle);
216 }
217
218 void revokeURL()
219 {
220 ASSERT(isMainThread());
221 registry->m_publicURLs.remove(url);
222 }
223
224 BlobRegistryImpl* registry;
225 KURL url;
226 RefPtr<BlobDataHandle> dataHandle; // NULL for revoke tasks.
227 };
228
229
230 void BlobRegistryImpl::registerPublicBlobURL(SecurityOrigin* origin, const KURL& url, PassRefPtr<BlobDataHandle> dataHandle)
231 {
232 // The cached origin has thread affinity so we must fiddle with it's registr ation on the current thread.
233 BlobRegistry::setCachedUniqueOrigin(url, origin);
234
235 // But the blob map is only accessed on the main thread.
236 OwnPtr<URLRegistrationHelper> helper = adoptPtr(new URLRegistrationHelper(th is, url, dataHandle));
237 if (isMainThread())
238 helper->registerURL();
239 else
240 callOnMainThread(&URLRegistrationHelper::registerURLTask, helper.leakPtr ());
241 }
242
243 void BlobRegistryImpl::revokePublicBlobURL(const KURL& url)
244 {
245 BlobRegistry::clearCachedUniqueOrigin(url);
246
247 OwnPtr<URLRegistrationHelper> helper = adoptPtr(new URLRegistrationHelper(th is, url));
248 if (isMainThread())
249 helper->revokeURL();
250 else
251 callOnMainThread(&URLRegistrationHelper::revokeURLTask, helper.leakPtr() );
252 }
253
254 PassRefPtr<SecurityOrigin> BlobRegistryImpl::cachedUniqueOrigin(const KURL& url)
255 {
256 return BlobRegistry::getCachedUniqueOrigin(url);
257 }
258
259 PassRefPtr<BlobStorageData> BlobRegistryImpl::getBlobDataFromUUID(const String& uuid) const
260 {
261 MutexLocker locker(m_mutex);
262 return m_blobs.get(uuid).second;
206 } 263 }
207 264
208 PassRefPtr<BlobStorageData> BlobRegistryImpl::getBlobDataFromURL(const KURL& url ) const 265 PassRefPtr<BlobStorageData> BlobRegistryImpl::getBlobDataFromURL(const KURL& url ) const
209 { 266 {
210 ASSERT(isMainThread()); 267 ASSERT(isMainThread());
211 return m_blobs.get(url.string()); 268 RefPtr<BlobDataHandle> dataHandle = m_publicURLs.get(url.string());
269 if (!dataHandle)
270 return 0;
271 return getBlobDataFromUUID(dataHandle->uuid());
212 } 272 }
213 273
214 } // namespace WebCore 274 } // namespace WebCore
215 275
216 #endif // ENABLE(BLOB) 276 #endif // ENABLE(BLOB)
OLDNEW
« no previous file with comments | « WebCore/platform/network/BlobRegistryImpl.h ('k') | WebCore/platform/network/BlobResourceHandle.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698