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 "ThreadableBlobRegistry.h"
35 #include "BlobRegistry.h"
37 #include "SecurityOrigin.h"
38 #include <wtf/HashMap.h>
39 #include <wtf/MainThread.h>
40 #include <wtf/RefPtr.h>
41 #include <wtf/ThreadSpecific.h>
42 #include <wtf/text/StringHash.h>
44 using WTF::ThreadSpecific;
48 struct BlobRegistryContext {
49 WTF_MAKE_FAST_ALLOCATED;
51 BlobRegistryContext(const URL& url, std::unique_ptr<BlobData> blobData)
53 , blobData(std::move(blobData))
55 this->blobData->detachFromCurrentThread();
58 BlobRegistryContext(const URL& url, const URL& srcURL)
60 , srcURL(srcURL.copy())
64 BlobRegistryContext(const URL& url)
71 std::unique_ptr<BlobData> blobData;
76 typedef HashMap<String, RefPtr<SecurityOrigin>> BlobUrlOriginMap;
77 static ThreadSpecific<BlobUrlOriginMap>& originMap()
79 AtomicallyInitializedStatic(ThreadSpecific<BlobUrlOriginMap>*, map = new ThreadSpecific<BlobUrlOriginMap>);
83 static void registerBlobURLTask(void* context)
85 std::unique_ptr<BlobRegistryContext> blobRegistryContext(static_cast<BlobRegistryContext*>(context));
86 blobRegistry().registerBlobURL(blobRegistryContext->url, std::move(blobRegistryContext->blobData));
89 void ThreadableBlobRegistry::registerBlobURL(const URL& url, std::unique_ptr<BlobData> blobData)
92 blobRegistry().registerBlobURL(url, std::move(blobData));
94 callOnMainThread(®isterBlobURLTask, new BlobRegistryContext(url, std::move(blobData)));
97 static void registerBlobURLFromTask(void* context)
99 std::unique_ptr<BlobRegistryContext> blobRegistryContext(static_cast<BlobRegistryContext*>(context));
100 blobRegistry().registerBlobURL(blobRegistryContext->url, blobRegistryContext->srcURL);
103 void ThreadableBlobRegistry::registerBlobURL(SecurityOrigin* origin, const URL& url, const URL& srcURL)
105 // 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.
106 if (origin && BlobURL::getOrigin(url) == "null")
107 originMap()->add(url.string(), origin);
110 blobRegistry().registerBlobURL(url, srcURL);
112 callOnMainThread(®isterBlobURLFromTask, new BlobRegistryContext(url, srcURL));
115 static void unregisterBlobURLTask(void* context)
117 std::unique_ptr<BlobRegistryContext> blobRegistryContext(static_cast<BlobRegistryContext*>(context));
118 blobRegistry().unregisterBlobURL(blobRegistryContext->url);
121 void ThreadableBlobRegistry::unregisterBlobURL(const URL& url)
123 if (BlobURL::getOrigin(url) == "null")
124 originMap()->remove(url.string());
127 blobRegistry().unregisterBlobURL(url);
129 callOnMainThread(&unregisterBlobURLTask, new BlobRegistryContext(url));
132 PassRefPtr<SecurityOrigin> ThreadableBlobRegistry::getCachedOrigin(const URL& url)
134 return originMap()->get(url.string());
139 void ThreadableBlobRegistry::registerBlobURL(const URL&, std::unique_ptr<BlobData> blobData)
143 void ThreadableBlobRegistry::registerBlobURL(SecurityOrigin*, const URL&, const URL&)
147 void ThreadableBlobRegistry::unregisterBlobURL(const URL&)
151 PassRefPtr<SecurityOrigin> ThreadableBlobRegistry::getCachedOrigin(const URL&)
156 #endif // ENABL(BLOB)
158 } // namespace WebCore