OLD | NEW |
1 /* | 1 // DELETE ME |
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 WTF_MAKE_FAST_ALLOCATED; | |
51 public: | |
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 | |
77 typedef HashMap<String, RefPtr<SecurityOrigin> > BlobUrlOriginMap; | |
78 static ThreadSpecific<BlobUrlOriginMap>& originMap() | |
79 { | |
80 AtomicallyInitializedStatic(ThreadSpecific<BlobUrlOriginMap>*, map = new Thr
eadSpecific<BlobUrlOriginMap>); | |
81 return *map; | |
82 } | |
83 | |
84 static void registerBlobURLTask(void* context) | |
85 { | |
86 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobR
egistryContext*>(context)); | |
87 blobRegistry().registerBlobURL(blobRegistryContext->url, blobRegistryContext
->blobData.release()); | |
88 } | |
89 | |
90 void ThreadableBlobRegistry::registerBlobURL(const KURL& url, PassOwnPtr<BlobDat
a> blobData) | |
91 { | |
92 if (isMainThread()) | |
93 blobRegistry().registerBlobURL(url, blobData); | |
94 else { | |
95 OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(u
rl, blobData)); | |
96 callOnMainThread(®isterBlobURLTask, context.leakPtr()); | |
97 } | |
98 } | |
99 | |
100 static void registerBlobURLFromTask(void* context) | |
101 { | |
102 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobR
egistryContext*>(context)); | |
103 blobRegistry().registerBlobURL(blobRegistryContext->url, blobRegistryContext
->srcURL); | |
104 } | |
105 | |
106 void 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 secur
ity origin or file URL, save the mapping between url and origin so that the orig
in 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(u
rl, srcURL)); | |
116 callOnMainThread(®isterBlobURLFromTask, context.leakPtr()); | |
117 } | |
118 } | |
119 | |
120 static void unregisterBlobURLTask(void* context) | |
121 { | |
122 OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobR
egistryContext*>(context)); | |
123 blobRegistry().unregisterBlobURL(blobRegistryContext->url); | |
124 } | |
125 | |
126 void 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(u
rl)); | |
135 callOnMainThread(&unregisterBlobURLTask, context.leakPtr()); | |
136 } | |
137 } | |
138 | |
139 PassRefPtr<SecurityOrigin> ThreadableBlobRegistry::getCachedOrigin(const KURL& u
rl) | |
140 { | |
141 return originMap()->get(url.string()); | |
142 } | |
143 | |
144 #else | |
145 | |
146 void ThreadableBlobRegistry::registerBlobURL(const KURL&, PassOwnPtr<BlobData>) | |
147 { | |
148 } | |
149 | |
150 void ThreadableBlobRegistry::registerBlobURL(SecurityOrigin*, const KURL&, const
KURL&) | |
151 { | |
152 } | |
153 | |
154 void ThreadableBlobRegistry::unregisterBlobURL(const KURL&) | |
155 { | |
156 } | |
157 | |
158 PassRefPtr<SecurityOrigin> ThreadableBlobRegistry::getCachedOrigin(const KURL& u
rl) | |
159 { | |
160 return 0; | |
161 } | |
162 | |
163 #endif // ENABL(BLOB) | |
164 | |
165 } // namespace WebCore | |
OLD | NEW |