Fix the incorrect behavior touch sound on single tap event.
[framework/web/webkit-efl.git] / Source / WebCore / fileapi / ThreadableBlobRegistry.cpp
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
45 using WTF::ThreadSpecific;
46
47 namespace WebCore {
48
49 struct BlobRegistryContext {
50     BlobRegistryContext(const KURL& url, PassOwnPtr<BlobData> blobData)
51         : url(url.copy())
52         , blobData(blobData)
53     {
54         this->blobData->detachFromCurrentThread();
55     }
56
57     BlobRegistryContext(const KURL& url, const KURL& srcURL)
58         : url(url.copy())
59         , srcURL(srcURL.copy())
60     {
61     }
62
63     BlobRegistryContext(const KURL& url)
64         : url(url.copy())
65     {
66     }
67
68     KURL url;
69     KURL srcURL;
70     OwnPtr<BlobData> blobData;
71 };
72
73 #if ENABLE(BLOB)
74
75 typedef HashMap<String, RefPtr<SecurityOrigin> > BlobUrlOriginMap;
76 static ThreadSpecific<BlobUrlOriginMap>& originMap()
77 {
78     AtomicallyInitializedStatic(ThreadSpecific<BlobUrlOriginMap>*, map = new ThreadSpecific<BlobUrlOriginMap>);
79     return *map;
80 }
81
82 static void registerBlobURLTask(void* context)
83 {
84     OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
85     blobRegistry().registerBlobURL(blobRegistryContext->url, blobRegistryContext->blobData.release());
86 }
87
88 void ThreadableBlobRegistry::registerBlobURL(const KURL& url, PassOwnPtr<BlobData> blobData)
89 {
90     if (isMainThread())
91         blobRegistry().registerBlobURL(url, blobData);
92     else {
93         OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, blobData));
94         callOnMainThread(&registerBlobURLTask, context.leakPtr());
95     }
96 }
97
98 static void registerBlobURLFromTask(void* context)
99 {
100     OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
101     blobRegistry().registerBlobURL(blobRegistryContext->url, blobRegistryContext->srcURL);
102 }
103
104 void ThreadableBlobRegistry::registerBlobURL(SecurityOrigin* origin, const KURL& url, const KURL& srcURL)
105 {
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);
109
110     if (isMainThread())
111         blobRegistry().registerBlobURL(url, srcURL);
112     else {
113         OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, srcURL));
114         callOnMainThread(&registerBlobURLFromTask, context.leakPtr());
115     }
116 }
117
118 static void unregisterBlobURLTask(void* context)
119 {
120     OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
121     blobRegistry().unregisterBlobURL(blobRegistryContext->url);
122 }
123
124 void ThreadableBlobRegistry::unregisterBlobURL(const KURL& url)
125 {
126     if (BlobURL::getOrigin(url) == "null")
127         originMap()->remove(url.string());
128
129     if (isMainThread())
130         blobRegistry().unregisterBlobURL(url);
131     else {
132         OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url));
133         callOnMainThread(&unregisterBlobURLTask, context.leakPtr());
134     }
135 }
136
137 PassRefPtr<SecurityOrigin> ThreadableBlobRegistry::getCachedOrigin(const KURL& url)
138 {
139     return originMap()->get(url.string());
140 }
141
142 #else
143
144 void ThreadableBlobRegistry::registerBlobURL(const KURL&, PassOwnPtr<BlobData>)
145 {
146 }
147
148 void ThreadableBlobRegistry::registerBlobURL(SecurityOrigin*, const KURL&, const KURL&)
149 {
150 }
151
152 void ThreadableBlobRegistry::unregisterBlobURL(const KURL&)
153 {
154 }
155
156 PassRefPtr<SecurityOrigin> ThreadableBlobRegistry::getCachedOrigin(const KURL& url)
157 {
158     return 0;
159 }
160
161 #endif // ENABL(BLOB)
162
163 } // namespace WebCore