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 #if ENABLE(FILE_SYSTEM)
35
36 #include "ActiveDOMObject.h"
37 #include "DOMFileSystemBase.h"
38 #include "ScriptExecutionContext.h"
39
40 namespace WebCore {
41
42 class DirectoryEntry;
43 class File;
44 class FileCallback;
45 class FileEntry;
46 class FileWriterCallback;
47
48 class DOMFileSystem : public DOMFileSystemBase, public ActiveDOMObject {
49 public:
create(ScriptExecutionContext * context,const String & name,PassOwnPtr<AsyncFileSystem> asyncFileSystem)50 static PassRefPtr<DOMFileSystem> create(ScriptExecutionContext* context, const String& name, PassOwnPtr<AsyncFileSystem> asyncFileSystem)
51 {
52 return adoptRef(new DOMFileSystem(context, name, asyncFileSystem));
53 }
54
55 PassRefPtr<DirectoryEntry> root();
56
57 // ActiveDOMObject methods.
58 virtual void stop();
59 virtual bool hasPendingActivity() const;
60 virtual void contextDestroyed();
61
62 void createWriter(const FileEntry*, PassRefPtr<FileWriterCallback>, PassRefPtr<ErrorCallback>);
63 void createFile(const FileEntry*, PassRefPtr<FileCallback>, PassRefPtr<ErrorCallback>);
64
65 // Schedule a callback. This should not cross threads (should be called on the same context thread).
66 // FIXME: move this to a more generic place.
67 template <typename CB, typename CBArg>
68 static void scheduleCallback(ScriptExecutionContext*, PassRefPtr<CB>, PassRefPtr<CBArg>);
69
70 template <typename CB, typename CBArg>
scheduleCallback(PassRefPtr<CB> callback,PassRefPtr<CBArg> callbackArg)71 void scheduleCallback(PassRefPtr<CB> callback, PassRefPtr<CBArg> callbackArg)
72 {
73 scheduleCallback(scriptExecutionContext(), callback, callbackArg);
74 }
75
76 private:
77 DOMFileSystem(ScriptExecutionContext*, const String& name, PassOwnPtr<AsyncFileSystem>);
78
79 // A helper template to schedule a callback task.
80 template <typename CB, typename CBArg>
81 class DispatchCallbackTask : public ScriptExecutionContext::Task {
82 public:
DispatchCallbackTask(PassRefPtr<CB> callback,PassRefPtr<CBArg> arg)83 DispatchCallbackTask(PassRefPtr<CB> callback, PassRefPtr<CBArg> arg)
84 : m_callback(callback)
85 , m_callbackArg(arg)
86 {
87 }
88
performTask(ScriptExecutionContext *)89 virtual void performTask(ScriptExecutionContext*)
90 {
91 m_callback->handleEvent(m_callbackArg.get());
92 }
93
94 private:
95 RefPtr<CB> m_callback;
96 RefPtr<CBArg> m_callbackArg;
97 };
98 };
99
100 template <typename CB, typename CBArg>
scheduleCallback(ScriptExecutionContext * scriptExecutionContext,PassRefPtr<CB> callback,PassRefPtr<CBArg> arg)101 void DOMFileSystem::scheduleCallback(ScriptExecutionContext* scriptExecutionContext, PassRefPtr<CB> callback, PassRefPtr<CBArg> arg)
102 {
103 ASSERT(scriptExecutionContext->isContextThread());
104 ASSERT(callback);
105 scriptExecutionContext->postTask(new DispatchCallbackTask<CB, CBArg>(callback, arg));
106 }
107
108 } // namespace
109
110 #endif // ENABLE(FILE_SYSTEM)
111
112 #endif // DOMFileSystem_h
113