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 <wtf/MainThread.h>
38
39namespace WebCore {
40
41struct BlobRegistryContext {
42    BlobRegistryContext(const KURL& url, PassOwnPtr<BlobData> blobData)
43        : url(url.copy())
44        , blobData(blobData)
45    {
46        this->blobData->detachFromCurrentThread();
47    }
48
49    BlobRegistryContext(const KURL& url, const KURL& srcURL)
50        : url(url.copy())
51        , srcURL(srcURL.copy())
52    {
53    }
54
55    BlobRegistryContext(const KURL& url)
56        : url(url.copy())
57    {
58    }
59
60    KURL url;
61    KURL srcURL;
62    OwnPtr<BlobData> blobData;
63};
64
65#if ENABLE(BLOB)
66
67static void registerBlobURLTask(void* context)
68{
69    OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
70    blobRegistry().registerBlobURL(blobRegistryContext->url, blobRegistryContext->blobData.release());
71}
72
73void ThreadableBlobRegistry::registerBlobURL(const KURL& url, PassOwnPtr<BlobData> blobData)
74{
75    if (isMainThread())
76        blobRegistry().registerBlobURL(url, blobData);
77    else {
78        OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, blobData));
79        callOnMainThread(&registerBlobURLTask, context.leakPtr());
80    }
81}
82
83static void registerBlobURLFromTask(void* context)
84{
85    OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
86    blobRegistry().registerBlobURL(blobRegistryContext->url, blobRegistryContext->srcURL);
87}
88
89void ThreadableBlobRegistry::registerBlobURL(const KURL& url, const KURL& srcURL)
90{
91    if (isMainThread())
92        blobRegistry().registerBlobURL(url, srcURL);
93    else {
94        OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, srcURL));
95        callOnMainThread(&registerBlobURLFromTask, context.leakPtr());
96    }
97}
98
99static void unregisterBlobURLTask(void* context)
100{
101    OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
102    blobRegistry().unregisterBlobURL(blobRegistryContext->url);
103}
104
105void ThreadableBlobRegistry::unregisterBlobURL(const KURL& url)
106{
107    if (isMainThread())
108        blobRegistry().unregisterBlobURL(url);
109    else {
110        OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url));
111        callOnMainThread(&unregisterBlobURLTask, context.leakPtr());
112    }
113}
114
115#else
116
117void ThreadableBlobRegistry::registerBlobURL(const KURL&, PassOwnPtr<BlobData>)
118{
119}
120
121void ThreadableBlobRegistry::registerBlobURL(const KURL&, const KURL&)
122{
123}
124
125void ThreadableBlobRegistry::unregisterBlobURL(const KURL&)
126{
127}
128#endif // ENABL(BLOB)
129
130} // namespace WebCore
131