1/*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31#include "config.h"
32
33#include "ThreadableBlobRegistry.h"
34
35#include "BlobData.h"
36#include "BlobRegistry.h"
37#include "BlobURL.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>
44
45using WTF::ThreadSpecific;
46
47namespace WebCore {
48
49struct BlobRegistryContext {
50    WTF_MAKE_FAST_ALLOCATED;
51public:
52    BlobRegistryContext(const KURL& url, PassOwnPtr<BlobData> blobData)
53        : url(url.copy())
54        , blobData(blobData)
55    {
56        this->blobData->detachFromCurrentThread();
57    }
58
59    BlobRegistryContext(const KURL& url, const KURL& srcURL)
60        : url(url.copy())
61        , srcURL(srcURL.copy())
62    {
63    }
64
65    BlobRegistryContext(const KURL& url)
66        : url(url.copy())
67    {
68    }
69
70    KURL url;
71    KURL srcURL;
72    OwnPtr<BlobData> blobData;
73};
74
75#if ENABLE(BLOB)
76
77typedef HashMap<String, RefPtr<SecurityOrigin> > BlobUrlOriginMap;
78static ThreadSpecific<BlobUrlOriginMap>& originMap()
79{
80    AtomicallyInitializedStatic(ThreadSpecific<BlobUrlOriginMap>*, map = new ThreadSpecific<BlobUrlOriginMap>);
81    return *map;
82}
83
84static void registerBlobURLTask(void* context)
85{
86    OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
87    blobRegistry().registerBlobURL(blobRegistryContext->url, blobRegistryContext->blobData.release());
88}
89
90void ThreadableBlobRegistry::registerBlobURL(const KURL& url, PassOwnPtr<BlobData> blobData)
91{
92    if (isMainThread())
93        blobRegistry().registerBlobURL(url, blobData);
94    else {
95        OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, blobData));
96        callOnMainThread(&registerBlobURLTask, context.leakPtr());
97    }
98}
99
100static void registerBlobURLFromTask(void* context)
101{
102    OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
103    blobRegistry().registerBlobURL(blobRegistryContext->url, blobRegistryContext->srcURL);
104}
105
106void ThreadableBlobRegistry::registerBlobURL(SecurityOrigin* origin, const KURL& url, const KURL& srcURL)
107{
108    // 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.
109    if (origin && BlobURL::getOrigin(url) == "null")
110        originMap()->add(url.string(), origin);
111
112    if (isMainThread())
113        blobRegistry().registerBlobURL(url, srcURL);
114    else {
115        OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, srcURL));
116        callOnMainThread(&registerBlobURLFromTask, context.leakPtr());
117    }
118}
119
120static void unregisterBlobURLTask(void* context)
121{
122    OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
123    blobRegistry().unregisterBlobURL(blobRegistryContext->url);
124}
125
126void ThreadableBlobRegistry::unregisterBlobURL(const KURL& url)
127{
128    if (BlobURL::getOrigin(url) == "null")
129        originMap()->remove(url.string());
130
131    if (isMainThread())
132        blobRegistry().unregisterBlobURL(url);
133    else {
134        OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url));
135        callOnMainThread(&unregisterBlobURLTask, context.leakPtr());
136    }
137}
138
139PassRefPtr<SecurityOrigin> ThreadableBlobRegistry::getCachedOrigin(const KURL& url)
140{
141    return originMap()->get(url.string());
142}
143
144#else
145
146void ThreadableBlobRegistry::registerBlobURL(const KURL&, PassOwnPtr<BlobData>)
147{
148}
149
150void ThreadableBlobRegistry::registerBlobURL(SecurityOrigin*, const KURL&, const KURL&)
151{
152}
153
154void ThreadableBlobRegistry::unregisterBlobURL(const KURL&)
155{
156}
157
158PassRefPtr<SecurityOrigin> ThreadableBlobRegistry::getCachedOrigin(const KURL&)
159{
160    return 0;
161}
162
163#endif // ENABL(BLOB)
164
165} // namespace WebCore
166