Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(334)

Side by Side Diff: Source/core/fileapi/BlobRegistry.cpp

Issue 26283005: Move blob-related code to platform/blob (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Fix win build Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/fileapi/BlobRegistry.h ('k') | Source/core/fileapi/BlobURL.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 "core/fileapi/BlobRegistry.h"
33
34 #include "core/fileapi/BlobURL.h"
35 #include "core/platform/network/BlobData.h"
36 #include "public/platform/Platform.h"
37 #include "public/platform/WebBlobData.h"
38 #include "public/platform/WebBlobRegistry.h"
39 #include "public/platform/WebString.h"
40 #include "public/platform/WebThreadSafeData.h"
41 #include "weborigin/SecurityOrigin.h"
42 #include "weborigin/SecurityOriginCache.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 using WebKit::WebBlobData;
53 using WebKit::WebBlobRegistry;
54 using WebKit::WebThreadSafeData;
55 using WTF::ThreadSpecific;
56
57 namespace WebCore {
58
59 class BlobOriginCache : public SecurityOriginCache {
60 public:
61 BlobOriginCache();
62 virtual SecurityOrigin* cachedOrigin(const KURL&) OVERRIDE;
63 };
64
65 struct BlobRegistryContext {
66 WTF_MAKE_FAST_ALLOCATED;
67 public:
68 BlobRegistryContext(const KURL& url, PassOwnPtr<BlobData> blobData)
69 : url(url.copy())
70 , blobData(blobData)
71 {
72 this->blobData->detachFromCurrentThread();
73 }
74
75 BlobRegistryContext(const KURL& url, const String& type)
76 : url(url.copy())
77 , type(type.isolatedCopy())
78 {
79 }
80
81 BlobRegistryContext(const KURL& url, const KURL& srcURL)
82 : url(url.copy())
83 , srcURL(srcURL.copy())
84 {
85 }
86
87 BlobRegistryContext(const KURL& url, PassRefPtr<RawData> streamData)
88 : url(url.copy())
89 , streamData(streamData)
90 {
91 }
92
93 BlobRegistryContext(const KURL& url)
94 : url(url.copy())
95 {
96 }
97
98 KURL url;
99 KURL srcURL;
100 OwnPtr<BlobData> blobData;
101 PassRefPtr<RawData> streamData;
102 String type;
103 };
104
105 static WebBlobRegistry* blobRegistry()
106 {
107 return WebKit::Platform::current()->blobRegistry();
108 }
109
110 typedef HashMap<String, RefPtr<SecurityOrigin> > BlobURLOriginMap;
111 static ThreadSpecific<BlobURLOriginMap>& originMap()
112 {
113 // We want to create the BlobOriginCache exactly once because it is shared b y all the threads.
114 AtomicallyInitializedStatic(BlobOriginCache*, cache = new BlobOriginCache);
115
116 AtomicallyInitializedStatic(ThreadSpecific<BlobURLOriginMap>*, map = new Thr eadSpecific<BlobURLOriginMap>);
117 return *map;
118 }
119
120 static void saveToOriginMap(SecurityOrigin* origin, const KURL& url)
121 {
122 // If the blob URL contains null origin, as in the context with unique
123 // security origin or file URL, save the mapping between url and origin so
124 // that the origin can be retrived when doing security origin check.
125 if (origin && BlobURL::getOrigin(url) == "null")
126 originMap()->add(url.string(), origin);
127 }
128
129 static void removeFromOriginMap(const KURL& url)
130 {
131 if (BlobURL::getOrigin(url) == "null")
132 originMap()->remove(url.string());
133 }
134
135 void BlobRegistry::registerBlobData(const String& uuid, PassOwnPtr<BlobData> dat a)
136 {
137 blobRegistry()->registerBlobData(uuid, WebKit::WebBlobData(data));
138 }
139
140 void BlobRegistry::addBlobDataRef(const String& uuid)
141 {
142 blobRegistry()->addBlobDataRef(uuid);
143 }
144
145 void BlobRegistry::removeBlobDataRef(const String& uuid)
146 {
147 blobRegistry()->removeBlobDataRef(uuid);
148 }
149
150 void BlobRegistry::registerPublicBlobURL(SecurityOrigin* origin, const KURL& url , PassRefPtr<BlobDataHandle> handle)
151 {
152 saveToOriginMap(origin, url);
153 blobRegistry()->registerPublicBlobURL(url, handle->uuid());
154 }
155
156 void BlobRegistry::revokePublicBlobURL(const KURL& url)
157 {
158 removeFromOriginMap(url);
159 blobRegistry()->revokePublicBlobURL(url);
160 }
161
162 static void registerStreamURLTask(void* context)
163 {
164 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobR egistryContext*>(context));
165 if (WebBlobRegistry* registry = blobRegistry())
166 registry->registerStreamURL(blobRegistryContext->url, blobRegistryContex t->type);
167 }
168
169 void BlobRegistry::registerStreamURL(const KURL& url, const String& type)
170 {
171 if (isMainThread()) {
172 if (WebBlobRegistry* registry = blobRegistry())
173 registry->registerStreamURL(url, type);
174 } else {
175 OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(u rl, type));
176 callOnMainThread(&registerStreamURLTask, context.leakPtr());
177 }
178 }
179
180 static void registerStreamURLFromTask(void* context)
181 {
182 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobR egistryContext*>(context));
183 if (WebBlobRegistry* registry = blobRegistry())
184 registry->registerStreamURL(blobRegistryContext->url, blobRegistryContex t->srcURL);
185 }
186
187 void BlobRegistry::registerStreamURL(SecurityOrigin* origin, const KURL& url, co nst KURL& srcURL)
188 {
189 saveToOriginMap(origin, url);
190
191 if (isMainThread()) {
192 if (WebBlobRegistry* registry = blobRegistry())
193 registry->registerStreamURL(url, srcURL);
194 } else {
195 OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(u rl, srcURL));
196 callOnMainThread(&registerStreamURLFromTask, context.leakPtr());
197 }
198 }
199
200 static void addDataToStreamTask(void* context)
201 {
202 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobR egistryContext*>(context));
203 if (WebBlobRegistry* registry = blobRegistry()) {
204 WebThreadSafeData webThreadSafeData(blobRegistryContext->streamData);
205 registry->addDataToStream(blobRegistryContext->url, webThreadSafeData);
206 }
207 }
208
209 void BlobRegistry::addDataToStream(const KURL& url, PassRefPtr<RawData> streamDa ta)
210 {
211 if (isMainThread()) {
212 if (WebBlobRegistry* registry = blobRegistry()) {
213 WebThreadSafeData webThreadSafeData(streamData);
214 registry->addDataToStream(url, webThreadSafeData);
215 }
216 } else {
217 OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(u rl, streamData));
218 callOnMainThread(&addDataToStreamTask, context.leakPtr());
219 }
220 }
221
222 static void finalizeStreamTask(void* context)
223 {
224 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobR egistryContext*>(context));
225 if (WebBlobRegistry* registry = blobRegistry())
226 registry->finalizeStream(blobRegistryContext->url);
227 }
228
229 void BlobRegistry::finalizeStream(const KURL& url)
230 {
231 if (isMainThread()) {
232 if (WebBlobRegistry* registry = blobRegistry())
233 registry->finalizeStream(url);
234 } else {
235 OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(u rl));
236 callOnMainThread(&finalizeStreamTask, context.leakPtr());
237 }
238 }
239
240 static void abortStreamTask(void* context)
241 {
242 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobR egistryContext*>(context));
243 if (WebBlobRegistry* registry = blobRegistry())
244 registry->abortStream(blobRegistryContext->url);
245 }
246
247 void BlobRegistry::abortStream(const KURL& url)
248 {
249 if (isMainThread()) {
250 if (WebBlobRegistry* registry = blobRegistry())
251 registry->abortStream(url);
252 } else {
253 OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(u rl));
254 callOnMainThread(&abortStreamTask, context.leakPtr());
255 }
256 }
257
258 static void unregisterStreamURLTask(void* context)
259 {
260 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobR egistryContext*>(context));
261 if (WebBlobRegistry* registry = blobRegistry())
262 registry->unregisterStreamURL(blobRegistryContext->url);
263 }
264
265 void BlobRegistry::unregisterStreamURL(const KURL& url)
266 {
267 removeFromOriginMap(url);
268
269 if (isMainThread()) {
270 if (WebBlobRegistry* registry = blobRegistry())
271 registry->unregisterStreamURL(url);
272 } else {
273 OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(u rl));
274 callOnMainThread(&unregisterStreamURLTask, context.leakPtr());
275 }
276 }
277
278 BlobOriginCache::BlobOriginCache()
279 {
280 SecurityOrigin::setCache(this);
281 }
282
283 SecurityOrigin* BlobOriginCache::cachedOrigin(const KURL& url)
284 {
285 if (url.protocolIs("blob"))
286 return originMap()->get(url.string());
287 return 0;
288 }
289
290 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/fileapi/BlobRegistry.h ('k') | Source/core/fileapi/BlobURL.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698