48b0f116ce9533f9492246225a27d40ffff519b5
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / blob / BlobRegistry.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 #include "platform/blob/BlobRegistry.h"
33
34 #include "platform/blob/BlobData.h"
35 #include "platform/blob/BlobURL.h"
36 #include "platform/weborigin/SecurityOrigin.h"
37 #include "platform/weborigin/SecurityOriginCache.h"
38 #include "public/platform/Platform.h"
39 #include "public/platform/WebBlobData.h"
40 #include "public/platform/WebBlobRegistry.h"
41 #include "public/platform/WebString.h"
42 #include "public/platform/WebThreadSafeData.h"
43 #include "wtf/Assertions.h"
44 #include "wtf/HashMap.h"
45 #include "wtf/MainThread.h"
46 #include "wtf/RefPtr.h"
47 #include "wtf/ThreadSpecific.h"
48 #include "wtf/Threading.h"
49 #include "wtf/text/StringHash.h"
50 #include "wtf/text/WTFString.h"
51
52 namespace blink {
53
54 class BlobOriginCache : public SecurityOriginCache {
55 public:
56     BlobOriginCache();
57     virtual SecurityOrigin* cachedOrigin(const KURL&) OVERRIDE;
58 };
59
60 struct BlobRegistryContext {
61     WTF_MAKE_FAST_ALLOCATED;
62 public:
63     BlobRegistryContext(const KURL& url, PassOwnPtr<BlobData> blobData)
64         : url(url.copy())
65         , blobData(blobData)
66     {
67         this->blobData->detachFromCurrentThread();
68     }
69
70     BlobRegistryContext(const KURL& url, const String& type)
71         : url(url.copy())
72         , type(type.isolatedCopy())
73     {
74     }
75
76     BlobRegistryContext(const KURL& url, const KURL& srcURL)
77         : url(url.copy())
78         , srcURL(srcURL.copy())
79     {
80     }
81
82     BlobRegistryContext(const KURL& url, PassRefPtr<RawData> streamData)
83         : url(url.copy())
84         , streamData(streamData)
85     {
86     }
87
88     BlobRegistryContext(const KURL& url)
89         : url(url.copy())
90     {
91     }
92
93     KURL url;
94     KURL srcURL;
95     OwnPtr<BlobData> blobData;
96     PassRefPtr<RawData> streamData;
97     String type;
98 };
99
100 static WebBlobRegistry* blobRegistry()
101 {
102     return Platform::current()->blobRegistry();
103 }
104
105 typedef HashMap<String, RefPtr<SecurityOrigin> > BlobURLOriginMap;
106 static ThreadSpecific<BlobURLOriginMap>& originMap()
107 {
108     // We want to create the BlobOriginCache exactly once because it is shared by all the threads.
109     AtomicallyInitializedStatic(BlobOriginCache*, cache = new BlobOriginCache);
110     (void)cache; // BlobOriginCache's constructor does the interesting work.
111
112     AtomicallyInitializedStatic(ThreadSpecific<BlobURLOriginMap>*, map = new ThreadSpecific<BlobURLOriginMap>);
113     return *map;
114 }
115
116 static void saveToOriginMap(SecurityOrigin* origin, const KURL& url)
117 {
118     // If the blob URL contains null origin, as in the context with unique
119     // security origin or file URL, save the mapping between url and origin so
120     // that the origin can be retrived when doing security origin check.
121     if (origin && BlobURL::getOrigin(url) == "null")
122         originMap()->add(url.string(), origin);
123 }
124
125 static void removeFromOriginMap(const KURL& url)
126 {
127     if (BlobURL::getOrigin(url) == "null")
128         originMap()->remove(url.string());
129 }
130
131 void BlobRegistry::registerBlobData(const String& uuid, PassOwnPtr<BlobData> data)
132 {
133     blobRegistry()->registerBlobData(uuid, WebBlobData(data));
134 }
135
136 void BlobRegistry::addBlobDataRef(const String& uuid)
137 {
138     blobRegistry()->addBlobDataRef(uuid);
139 }
140
141 void BlobRegistry::removeBlobDataRef(const String& uuid)
142 {
143     blobRegistry()->removeBlobDataRef(uuid);
144 }
145
146 void BlobRegistry::registerPublicBlobURL(SecurityOrigin* origin, const KURL& url, PassRefPtr<BlobDataHandle> handle)
147 {
148     saveToOriginMap(origin, url);
149     blobRegistry()->registerPublicBlobURL(url, handle->uuid());
150 }
151
152 void BlobRegistry::revokePublicBlobURL(const KURL& url)
153 {
154     removeFromOriginMap(url);
155     blobRegistry()->revokePublicBlobURL(url);
156 }
157
158 static void registerStreamURLTask(void* context)
159 {
160     OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
161     if (WebBlobRegistry* registry = blobRegistry())
162         registry->registerStreamURL(blobRegistryContext->url, blobRegistryContext->type);
163 }
164
165 void BlobRegistry::registerStreamURL(const KURL& url, const String& type)
166 {
167     if (isMainThread()) {
168         if (WebBlobRegistry* registry = blobRegistry())
169             registry->registerStreamURL(url, type);
170     } else {
171         OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, type));
172         callOnMainThread(&registerStreamURLTask, context.leakPtr());
173     }
174 }
175
176 static void registerStreamURLFromTask(void* context)
177 {
178     OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
179     if (WebBlobRegistry* registry = blobRegistry())
180         registry->registerStreamURL(blobRegistryContext->url, blobRegistryContext->srcURL);
181 }
182
183 void BlobRegistry::registerStreamURL(SecurityOrigin* origin, const KURL& url, const KURL& srcURL)
184 {
185     saveToOriginMap(origin, url);
186
187     if (isMainThread()) {
188         if (WebBlobRegistry* registry = blobRegistry())
189             registry->registerStreamURL(url, srcURL);
190     } else {
191         OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, srcURL));
192         callOnMainThread(&registerStreamURLFromTask, context.leakPtr());
193     }
194 }
195
196 static void addDataToStreamTask(void* context)
197 {
198     OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
199     if (WebBlobRegistry* registry = blobRegistry()) {
200         WebThreadSafeData webThreadSafeData(blobRegistryContext->streamData);
201         registry->addDataToStream(blobRegistryContext->url, webThreadSafeData);
202     }
203 }
204
205 void BlobRegistry::addDataToStream(const KURL& url, PassRefPtr<RawData> streamData)
206 {
207     if (isMainThread()) {
208         if (WebBlobRegistry* registry = blobRegistry()) {
209             WebThreadSafeData webThreadSafeData(streamData);
210             registry->addDataToStream(url, webThreadSafeData);
211         }
212     } else {
213         OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, streamData));
214         callOnMainThread(&addDataToStreamTask, context.leakPtr());
215     }
216 }
217
218 static void finalizeStreamTask(void* context)
219 {
220     OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
221     if (WebBlobRegistry* registry = blobRegistry())
222         registry->finalizeStream(blobRegistryContext->url);
223 }
224
225 void BlobRegistry::finalizeStream(const KURL& url)
226 {
227     if (isMainThread()) {
228         if (WebBlobRegistry* registry = blobRegistry())
229             registry->finalizeStream(url);
230     } else {
231         OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url));
232         callOnMainThread(&finalizeStreamTask, context.leakPtr());
233     }
234 }
235
236 static void abortStreamTask(void* context)
237 {
238     OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
239     if (WebBlobRegistry* registry = blobRegistry())
240         registry->abortStream(blobRegistryContext->url);
241 }
242
243 void BlobRegistry::abortStream(const KURL& url)
244 {
245     if (isMainThread()) {
246         if (WebBlobRegistry* registry = blobRegistry())
247             registry->abortStream(url);
248     } else {
249         OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url));
250         callOnMainThread(&abortStreamTask, context.leakPtr());
251     }
252 }
253
254 static void unregisterStreamURLTask(void* context)
255 {
256     OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
257     if (WebBlobRegistry* registry = blobRegistry())
258         registry->unregisterStreamURL(blobRegistryContext->url);
259 }
260
261 void BlobRegistry::unregisterStreamURL(const KURL& url)
262 {
263     removeFromOriginMap(url);
264
265     if (isMainThread()) {
266         if (WebBlobRegistry* registry = blobRegistry())
267             registry->unregisterStreamURL(url);
268     } else {
269         OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url));
270         callOnMainThread(&unregisterStreamURLTask, context.leakPtr());
271     }
272 }
273
274 BlobOriginCache::BlobOriginCache()
275 {
276     SecurityOrigin::setCache(this);
277 }
278
279 SecurityOrigin* BlobOriginCache::cachedOrigin(const KURL& url)
280 {
281     if (url.protocolIs("blob"))
282         return originMap()->get(url.string());
283     return 0;
284 }
285
286 } // namespace blink