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.
33 #include "ThreadableBlobRegistry.h"
36 #include "BlobRegistry.h"
38 #include "SecurityOrigin.h"
39 #include <wtf/HashMap.h>
40 #include <wtf/MainThread.h>
41 #include <wtf/RefPtr.h>
42 #include <wtf/ThreadSpecific.h>
43 #include <wtf/text/StringHash.h>
45 using WTF::ThreadSpecific;
49 struct BlobRegistryContext {
50 BlobRegistryContext(const KURL& url, PassOwnPtr<BlobData> blobData)
54 this->blobData->detachFromCurrentThread();
57 BlobRegistryContext(const KURL& url, const KURL& srcURL)
59 , srcURL(srcURL.copy())
63 BlobRegistryContext(const KURL& url)
70 OwnPtr<BlobData> blobData;
75 typedef HashMap<String, RefPtr<SecurityOrigin> > BlobUrlOriginMap;
76 static ThreadSpecific<BlobUrlOriginMap>& originMap()
78 AtomicallyInitializedStatic(ThreadSpecific<BlobUrlOriginMap>*, map = new ThreadSpecific<BlobUrlOriginMap>);
82 static void registerBlobURLTask(void* context)
84 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
85 blobRegistry().registerBlobURL(blobRegistryContext->url, blobRegistryContext->blobData.release());
88 void ThreadableBlobRegistry::registerBlobURL(const KURL& url, PassOwnPtr<BlobData> blobData)
91 blobRegistry().registerBlobURL(url, blobData);
93 OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, blobData));
94 callOnMainThread(®isterBlobURLTask, context.leakPtr());
98 static void registerBlobURLFromTask(void* context)
100 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
101 blobRegistry().registerBlobURL(blobRegistryContext->url, blobRegistryContext->srcURL);
104 void ThreadableBlobRegistry::registerBlobURL(SecurityOrigin* origin, const KURL& url, const KURL& srcURL)
106 // If the blob URL contains null origin, as in the context with unique security origin or file URL, save the mapping between url and origin so that the origin can be retrived when doing security origin check.
107 if (origin && BlobURL::getOrigin(url) == "null")
108 originMap()->add(url.string(), origin);
111 blobRegistry().registerBlobURL(url, srcURL);
113 OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, srcURL));
114 callOnMainThread(®isterBlobURLFromTask, context.leakPtr());
118 static void unregisterBlobURLTask(void* context)
120 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
121 blobRegistry().unregisterBlobURL(blobRegistryContext->url);
124 void ThreadableBlobRegistry::unregisterBlobURL(const KURL& url)
126 if (BlobURL::getOrigin(url) == "null")
127 originMap()->remove(url.string());
130 blobRegistry().unregisterBlobURL(url);
132 OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url));
133 callOnMainThread(&unregisterBlobURLTask, context.leakPtr());
137 PassRefPtr<SecurityOrigin> ThreadableBlobRegistry::getCachedOrigin(const KURL& url)
139 return originMap()->get(url.string());
144 void ThreadableBlobRegistry::registerBlobURL(const KURL&, PassOwnPtr<BlobData>)
148 void ThreadableBlobRegistry::registerBlobURL(SecurityOrigin*, const KURL&, const KURL&)
152 void ThreadableBlobRegistry::unregisterBlobURL(const KURL&)
156 PassRefPtr<SecurityOrigin> ThreadableBlobRegistry::getCachedOrigin(const KURL& url)
161 #endif // ENABL(BLOB)
163 } // namespace WebCore