2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "platform/blob/BlobRegistry.h"
34 #include "platform/blob/BlobData.h"
35 #include "platform/blob/BlobURL.h"
36 #include "platform/weborigin/SecurityOrigin.h"
37 #include "platform/weborigin/SecurityOriginCache.h"
38 #include "public/platform/Platform.h"
39 #include "public/platform/WebBlobData.h"
40 #include "public/platform/WebBlobRegistry.h"
41 #include "public/platform/WebString.h"
42 #include "public/platform/WebThreadSafeData.h"
43 #include "wtf/Assertions.h"
44 #include "wtf/HashMap.h"
45 #include "wtf/MainThread.h"
46 #include "wtf/RefPtr.h"
47 #include "wtf/ThreadSpecific.h"
48 #include "wtf/Threading.h"
49 #include "wtf/text/StringHash.h"
50 #include "wtf/text/WTFString.h"
54 class BlobOriginCache : public SecurityOriginCache {
57 virtual SecurityOrigin* cachedOrigin(const KURL&) OVERRIDE;
60 struct BlobRegistryContext {
61 WTF_MAKE_FAST_ALLOCATED;
63 BlobRegistryContext(const KURL& url, PassOwnPtr<BlobData> blobData)
67 this->blobData->detachFromCurrentThread();
70 BlobRegistryContext(const KURL& url, const String& type)
72 , type(type.isolatedCopy())
76 BlobRegistryContext(const KURL& url, const KURL& srcURL)
78 , srcURL(srcURL.copy())
82 BlobRegistryContext(const KURL& url, PassRefPtr<RawData> streamData)
84 , streamData(streamData)
88 BlobRegistryContext(const KURL& url)
95 OwnPtr<BlobData> blobData;
96 PassRefPtr<RawData> streamData;
100 static WebBlobRegistry* blobRegistry()
102 return Platform::current()->blobRegistry();
105 typedef HashMap<String, RefPtr<SecurityOrigin> > BlobURLOriginMap;
106 static ThreadSpecific<BlobURLOriginMap>& originMap()
108 // We want to create the BlobOriginCache exactly once because it is shared by all the threads.
109 AtomicallyInitializedStatic(BlobOriginCache*, cache = new BlobOriginCache);
110 (void)cache; // BlobOriginCache's constructor does the interesting work.
112 AtomicallyInitializedStatic(ThreadSpecific<BlobURLOriginMap>*, map = new ThreadSpecific<BlobURLOriginMap>);
116 static void saveToOriginMap(SecurityOrigin* origin, const KURL& url)
118 // If the blob URL contains null origin, as in the context with unique
119 // security origin or file URL, save the mapping between url and origin so
120 // that the origin can be retrived when doing security origin check.
121 if (origin && BlobURL::getOrigin(url) == "null")
122 originMap()->add(url.string(), origin);
125 static void removeFromOriginMap(const KURL& url)
127 if (BlobURL::getOrigin(url) == "null")
128 originMap()->remove(url.string());
131 void BlobRegistry::registerBlobData(const String& uuid, PassOwnPtr<BlobData> data)
133 blobRegistry()->registerBlobData(uuid, WebBlobData(data));
136 void BlobRegistry::addBlobDataRef(const String& uuid)
138 blobRegistry()->addBlobDataRef(uuid);
141 void BlobRegistry::removeBlobDataRef(const String& uuid)
143 blobRegistry()->removeBlobDataRef(uuid);
146 void BlobRegistry::registerPublicBlobURL(SecurityOrigin* origin, const KURL& url, PassRefPtr<BlobDataHandle> handle)
148 saveToOriginMap(origin, url);
149 blobRegistry()->registerPublicBlobURL(url, handle->uuid());
152 void BlobRegistry::revokePublicBlobURL(const KURL& url)
154 removeFromOriginMap(url);
155 blobRegistry()->revokePublicBlobURL(url);
158 static void registerStreamURLTask(void* context)
160 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
161 if (WebBlobRegistry* registry = blobRegistry())
162 registry->registerStreamURL(blobRegistryContext->url, blobRegistryContext->type);
165 void BlobRegistry::registerStreamURL(const KURL& url, const String& type)
167 if (isMainThread()) {
168 if (WebBlobRegistry* registry = blobRegistry())
169 registry->registerStreamURL(url, type);
171 OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, type));
172 callOnMainThread(®isterStreamURLTask, context.leakPtr());
176 static void registerStreamURLFromTask(void* context)
178 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
179 if (WebBlobRegistry* registry = blobRegistry())
180 registry->registerStreamURL(blobRegistryContext->url, blobRegistryContext->srcURL);
183 void BlobRegistry::registerStreamURL(SecurityOrigin* origin, const KURL& url, const KURL& srcURL)
185 saveToOriginMap(origin, url);
187 if (isMainThread()) {
188 if (WebBlobRegistry* registry = blobRegistry())
189 registry->registerStreamURL(url, srcURL);
191 OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, srcURL));
192 callOnMainThread(®isterStreamURLFromTask, context.leakPtr());
196 static void addDataToStreamTask(void* context)
198 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
199 if (WebBlobRegistry* registry = blobRegistry()) {
200 WebThreadSafeData webThreadSafeData(blobRegistryContext->streamData);
201 registry->addDataToStream(blobRegistryContext->url, webThreadSafeData);
205 void BlobRegistry::addDataToStream(const KURL& url, PassRefPtr<RawData> streamData)
207 if (isMainThread()) {
208 if (WebBlobRegistry* registry = blobRegistry()) {
209 WebThreadSafeData webThreadSafeData(streamData);
210 registry->addDataToStream(url, webThreadSafeData);
213 OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, streamData));
214 callOnMainThread(&addDataToStreamTask, context.leakPtr());
218 static void finalizeStreamTask(void* context)
220 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
221 if (WebBlobRegistry* registry = blobRegistry())
222 registry->finalizeStream(blobRegistryContext->url);
225 void BlobRegistry::finalizeStream(const KURL& url)
227 if (isMainThread()) {
228 if (WebBlobRegistry* registry = blobRegistry())
229 registry->finalizeStream(url);
231 OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url));
232 callOnMainThread(&finalizeStreamTask, context.leakPtr());
236 static void abortStreamTask(void* context)
238 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
239 if (WebBlobRegistry* registry = blobRegistry())
240 registry->abortStream(blobRegistryContext->url);
243 void BlobRegistry::abortStream(const KURL& url)
245 if (isMainThread()) {
246 if (WebBlobRegistry* registry = blobRegistry())
247 registry->abortStream(url);
249 OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url));
250 callOnMainThread(&abortStreamTask, context.leakPtr());
254 static void unregisterStreamURLTask(void* context)
256 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
257 if (WebBlobRegistry* registry = blobRegistry())
258 registry->unregisterStreamURL(blobRegistryContext->url);
261 void BlobRegistry::unregisterStreamURL(const KURL& url)
263 removeFromOriginMap(url);
265 if (isMainThread()) {
266 if (WebBlobRegistry* registry = blobRegistry())
267 registry->unregisterStreamURL(url);
269 OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url));
270 callOnMainThread(&unregisterStreamURLTask, context.leakPtr());
274 BlobOriginCache::BlobOriginCache()
276 SecurityOrigin::setCache(this);
279 SecurityOrigin* BlobOriginCache::cachedOrigin(const KURL& url)
281 if (url.protocolIs("blob"))
282 return originMap()->get(url.string());