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 #ifndef DOMFileSystem_h
32 #define DOMFileSystem_h
33
34 #include "bindings/v8/ScriptWrappable.h"
35 #include "core/dom/ActiveDOMObject.h"
36 #include "core/dom/ExecutionContext.h"
37 #include "core/dom/ExecutionContextTask.h"
38 #include "modules/filesystem/DOMFileSystemBase.h"
39 #include "modules/filesystem/EntriesCallback.h"
40 #include "platform/heap/Handle.h"
41
42 namespace WebCore {
43
44 class DirectoryEntry;
45 class File;
46 class FileCallback;
47 class FileEntry;
48 class FileWriterCallback;
49
50 class DOMFileSystem FINAL : public DOMFileSystemBase, public ScriptWrappable, public ActiveDOMObject {
51 public:
52 static DOMFileSystem* create(ExecutionContext*, const String& name, FileSystemType, const KURL& rootURL);
53
54 // Creates a new isolated file system for the given filesystemId.
55 static DOMFileSystem* createIsolatedFileSystem(ExecutionContext*, const String& filesystemId);
56
57 DirectoryEntry* root();
58
59 // DOMFileSystemBase overrides.
60 virtual void addPendingCallbacks() OVERRIDE;
61 virtual void removePendingCallbacks() OVERRIDE;
62 virtual void reportError(PassOwnPtr<ErrorCallback>, PassRefPtrWillBeRawPtr<FileError>) OVERRIDE;
63
64 // ActiveDOMObject overrides.
65 virtual bool hasPendingActivity() const OVERRIDE;
66
67 void createWriter(const FileEntry*, PassOwnPtr<FileWriterCallback>, PassOwnPtr<ErrorCallback>);
68 void createFile(const FileEntry*, PassOwnPtr<FileCallback>, PassOwnPtr<ErrorCallback>);
69
70 // Schedule a callback. This should not cross threads (should be called on the same context thread).
71 // FIXME: move this to a more generic place.
72 template <typename CB, typename CBArg>
73 static void scheduleCallback(ExecutionContext*, PassOwnPtr<CB>, PassRefPtrWillBeRawPtr<CBArg>);
74
75 template <typename CB, typename CBArg>
76 static void scheduleCallback(ExecutionContext*, PassOwnPtr<CB>, CBArg*);
77
78 template <typename CB, typename CBArg>
79 static void scheduleCallback(ExecutionContext*, PassOwnPtr<CB>, const HeapVector<CBArg>&);
80
81 template <typename CB, typename CBArg>
82 static void scheduleCallback(ExecutionContext*, PassOwnPtr<CB>, const CBArg&);
83
84 template <typename CB>
85 static void scheduleCallback(ExecutionContext*, PassOwnPtr<CB>);
86
87 template <typename CB, typename CBArg>
scheduleCallback(PassOwnPtr<CB> callback,PassRefPtrWillBeRawPtr<CBArg> callbackArg)88 void scheduleCallback(PassOwnPtr<CB> callback, PassRefPtrWillBeRawPtr<CBArg> callbackArg)
89 {
90 scheduleCallback(executionContext(), callback, callbackArg);
91 }
92
93 template <typename CB, typename CBArg>
scheduleCallback(PassOwnPtr<CB> callback,CBArg * callbackArg)94 void scheduleCallback(PassOwnPtr<CB> callback, CBArg* callbackArg)
95 {
96 scheduleCallback(executionContext(), callback, callbackArg);
97 }
98
99 template <typename CB, typename CBArg>
scheduleCallback(PassOwnPtr<CB> callback,const CBArg & callbackArg)100 void scheduleCallback(PassOwnPtr<CB> callback, const CBArg& callbackArg)
101 {
102 scheduleCallback(executionContext(), callback, callbackArg);
103 }
104
105 private:
106 DOMFileSystem(ExecutionContext*, const String& name, FileSystemType, const KURL& rootURL);
107
108 // A helper template to schedule a callback task.
109 template <typename CB, typename CBArg>
110 class DispatchCallbackRefPtrArgTask FINAL : public ExecutionContextTask {
111 public:
DispatchCallbackRefPtrArgTask(PassOwnPtr<CB> callback,PassRefPtrWillBeRawPtr<CBArg> arg)112 DispatchCallbackRefPtrArgTask(PassOwnPtr<CB> callback, PassRefPtrWillBeRawPtr<CBArg> arg)
113 : m_callback(callback)
114 , m_callbackArg(arg)
115 {
116 }
117
performTask(ExecutionContext *)118 virtual void performTask(ExecutionContext*) OVERRIDE
119 {
120 m_callback->handleEvent(m_callbackArg.get());
121 }
122
123 private:
124 OwnPtr<CB> m_callback;
125 RefPtrWillBePersistent<CBArg> m_callbackArg;
126 };
127
128 template <typename CB, typename CBArg>
129 class DispatchCallbackPtrArgTask FINAL : public ExecutionContextTask {
130 public:
DispatchCallbackPtrArgTask(PassOwnPtr<CB> callback,CBArg * arg)131 DispatchCallbackPtrArgTask(PassOwnPtr<CB> callback, CBArg* arg)
132 : m_callback(callback)
133 , m_callbackArg(arg)
134 {
135 }
136
performTask(ExecutionContext *)137 virtual void performTask(ExecutionContext*) OVERRIDE
138 {
139 m_callback->handleEvent(m_callbackArg.get());
140 }
141
142 private:
143 OwnPtr<CB> m_callback;
144 Persistent<CBArg> m_callbackArg;
145 };
146
147 template <typename CB, typename CBArg>
148 class DispatchCallbackNonPtrArgTask FINAL : public ExecutionContextTask {
149 public:
DispatchCallbackNonPtrArgTask(PassOwnPtr<CB> callback,const CBArg & arg)150 DispatchCallbackNonPtrArgTask(PassOwnPtr<CB> callback, const CBArg& arg)
151 : m_callback(callback)
152 , m_callbackArg(arg)
153 {
154 }
155
performTask(ExecutionContext *)156 virtual void performTask(ExecutionContext*) OVERRIDE
157 {
158 m_callback->handleEvent(m_callbackArg);
159 }
160
161 private:
162 OwnPtr<CB> m_callback;
163 CBArg m_callbackArg;
164 };
165
166 template <typename CB>
167 class DispatchCallbackNoArgTask FINAL : public ExecutionContextTask {
168 public:
DispatchCallbackNoArgTask(PassOwnPtr<CB> callback)169 DispatchCallbackNoArgTask(PassOwnPtr<CB> callback)
170 : m_callback(callback)
171 {
172 }
173
performTask(ExecutionContext *)174 virtual void performTask(ExecutionContext*) OVERRIDE
175 {
176 m_callback->handleEvent();
177 }
178
179 private:
180 OwnPtr<CB> m_callback;
181 };
182
183 int m_numberOfPendingCallbacks;
184 };
185
186 template <typename CB, typename CBArg>
scheduleCallback(ExecutionContext * executionContext,PassOwnPtr<CB> callback,PassRefPtrWillBeRawPtr<CBArg> arg)187 void DOMFileSystem::scheduleCallback(ExecutionContext* executionContext, PassOwnPtr<CB> callback, PassRefPtrWillBeRawPtr<CBArg> arg)
188 {
189 ASSERT(executionContext->isContextThread());
190 if (callback)
191 executionContext->postTask(adoptPtr(new DispatchCallbackRefPtrArgTask<CB, CBArg>(callback, arg)));
192 }
193
194 template <typename CB, typename CBArg>
scheduleCallback(ExecutionContext * executionContext,PassOwnPtr<CB> callback,CBArg * arg)195 void DOMFileSystem::scheduleCallback(ExecutionContext* executionContext, PassOwnPtr<CB> callback, CBArg* arg)
196 {
197 ASSERT(executionContext->isContextThread());
198 if (callback)
199 executionContext->postTask(adoptPtr(new DispatchCallbackPtrArgTask<CB, CBArg>(callback, arg)));
200 }
201
202 template <typename CB, typename CBArg>
scheduleCallback(ExecutionContext * executionContext,PassOwnPtr<CB> callback,const HeapVector<CBArg> & arg)203 void DOMFileSystem::scheduleCallback(ExecutionContext* executionContext, PassOwnPtr<CB> callback, const HeapVector<CBArg>& arg)
204 {
205 ASSERT(executionContext->isContextThread());
206 if (callback)
207 executionContext->postTask(adoptPtr(new DispatchCallbackNonPtrArgTask<CB, PersistentHeapVector<CBArg> >(callback, arg)));
208 }
209
210 template <typename CB, typename CBArg>
scheduleCallback(ExecutionContext * executionContext,PassOwnPtr<CB> callback,const CBArg & arg)211 void DOMFileSystem::scheduleCallback(ExecutionContext* executionContext, PassOwnPtr<CB> callback, const CBArg& arg)
212 {
213 ASSERT(executionContext->isContextThread());
214 if (callback)
215 executionContext->postTask(adoptPtr(new DispatchCallbackNonPtrArgTask<CB, CBArg>(callback, arg)));
216 }
217
218 template <typename CB>
scheduleCallback(ExecutionContext * executionContext,PassOwnPtr<CB> callback)219 void DOMFileSystem::scheduleCallback(ExecutionContext* executionContext, PassOwnPtr<CB> callback)
220 {
221 ASSERT(executionContext->isContextThread());
222 if (callback)
223 executionContext->postTask(adoptPtr(new DispatchCallbackNoArgTask<CB>(callback)));
224 }
225
226 } // namespace
227
228 #endif // DOMFileSystem_h
229