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
41 namespace WebCore {
42
43 class DirectoryEntry;
44 class File;
45 class FileCallback;
46 class FileEntry;
47 class FileWriterCallback;
48
49 class DOMFileSystem : public DOMFileSystemBase, public ScriptWrappable, public ActiveDOMObject {
50 public:
51 static PassRefPtr<DOMFileSystem> create(ExecutionContext*, const String& name, FileSystemType, const KURL& rootURL);
52
53 // Creates a new isolated file system for the given filesystemId.
54 static PassRefPtr<DOMFileSystem> createIsolatedFileSystem(ExecutionContext*, const String& filesystemId);
55
56 PassRefPtr<DirectoryEntry> root();
57
58 // DOMFileSystemBase overrides.
59 virtual void addPendingCallbacks() OVERRIDE;
60 virtual void removePendingCallbacks() OVERRIDE;
61 virtual void reportError(PassOwnPtr<ErrorCallback>, PassRefPtr<FileError>) OVERRIDE;
62
63 void createWriter(const FileEntry*, PassOwnPtr<FileWriterCallback>, PassOwnPtr<ErrorCallback>);
64 void createFile(const FileEntry*, PassOwnPtr<FileCallback>, PassOwnPtr<ErrorCallback>);
65
66 // Schedule a callback. This should not cross threads (should be called on the same context thread).
67 // FIXME: move this to a more generic place.
68 template <typename CB, typename CBArg>
69 static void scheduleCallback(ExecutionContext*, PassOwnPtr<CB>, PassRefPtr<CBArg>);
70
71 template <typename CB, typename CBArg>
72 static void scheduleCallback(ExecutionContext*, PassOwnPtr<CB>, const CBArg&);
73
74 template <typename CB, typename CBArg>
scheduleCallback(PassOwnPtr<CB> callback,PassRefPtr<CBArg> callbackArg)75 void scheduleCallback(PassOwnPtr<CB> callback, PassRefPtr<CBArg> callbackArg)
76 {
77 scheduleCallback(executionContext(), callback, callbackArg);
78 }
79
80 template <typename CB, typename CBArg>
scheduleCallback(PassOwnPtr<CB> callback,const CBArg & callbackArg)81 void scheduleCallback(PassOwnPtr<CB> callback, const CBArg& callbackArg)
82 {
83 scheduleCallback(executionContext(), callback, callbackArg);
84 }
85
86 private:
87 DOMFileSystem(ExecutionContext*, const String& name, FileSystemType, const KURL& rootURL);
88
89 // A helper template to schedule a callback task.
90 template <typename CB, typename CBArg>
91 class DispatchCallbacRefPtrArgTask : public ExecutionContextTask {
92 public:
DispatchCallbacRefPtrArgTask(PassOwnPtr<CB> callback,PassRefPtr<CBArg> arg)93 DispatchCallbacRefPtrArgTask(PassOwnPtr<CB> callback, PassRefPtr<CBArg> arg)
94 : m_callback(callback)
95 , m_callbackArg(arg)
96 {
97 }
98
performTask(ExecutionContext *)99 virtual void performTask(ExecutionContext*)
100 {
101 m_callback->handleEvent(m_callbackArg.get());
102 }
103
104 private:
105 OwnPtr<CB> m_callback;
106 RefPtr<CBArg> m_callbackArg;
107 };
108
109 template <typename CB, typename CBArg>
110 class DispatchCallbackNonPtrArgTask : public ExecutionContextTask {
111 public:
DispatchCallbackNonPtrArgTask(PassOwnPtr<CB> callback,const CBArg & arg)112 DispatchCallbackNonPtrArgTask(PassOwnPtr<CB> callback, const CBArg& arg)
113 : m_callback(callback)
114 , m_callbackArg(arg)
115 {
116 }
117
performTask(ExecutionContext *)118 virtual void performTask(ExecutionContext*)
119 {
120 m_callback->handleEvent(m_callbackArg);
121 }
122
123 private:
124 OwnPtr<CB> m_callback;
125 CBArg m_callbackArg;
126 };
127 };
128
129 template <typename CB, typename CBArg>
scheduleCallback(ExecutionContext * executionContext,PassOwnPtr<CB> callback,PassRefPtr<CBArg> arg)130 void DOMFileSystem::scheduleCallback(ExecutionContext* executionContext, PassOwnPtr<CB> callback, PassRefPtr<CBArg> arg)
131 {
132 ASSERT(executionContext->isContextThread());
133 if (callback)
134 executionContext->postTask(adoptPtr(new DispatchCallbacRefPtrArgTask<CB, CBArg>(callback, arg)));
135 }
136
137 template <typename CB, typename CBArg>
scheduleCallback(ExecutionContext * executionContext,PassOwnPtr<CB> callback,const CBArg & arg)138 void DOMFileSystem::scheduleCallback(ExecutionContext* executionContext, PassOwnPtr<CB> callback, const CBArg& arg)
139 {
140 ASSERT(executionContext->isContextThread());
141 if (callback)
142 executionContext->postTask(adoptPtr(new DispatchCallbackNonPtrArgTask<CB, CBArg>(callback, arg)));
143 }
144
145 } // namespace
146
147 #endif // DOMFileSystem_h
148