tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / fileapi / DOMFileSystem.cpp
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 "DOMFileSystem.h"
33
34 #if ENABLE(FILE_SYSTEM)
35
36 #include "AsyncFileSystem.h"
37 #include "DOMFilePath.h"
38 #include "DirectoryEntry.h"
39 #include "ErrorCallback.h"
40 #include "File.h"
41 #include "FileCallback.h"
42 #include "FileEntry.h"
43 #include "FileMetadata.h"
44 #include "FileSystemCallbacks.h"
45 #include "FileWriter.h"
46 #include "FileWriterBaseCallback.h"
47 #include "FileWriterCallback.h"
48 #include "InspectorFileSystemInstrumentation.h"
49 #include "MetadataCallback.h"
50 #include "ScriptExecutionContext.h"
51 #include <wtf/OwnPtr.h>
52
53 namespace WebCore {
54
55 // static
56 PassRefPtr<DOMFileSystem> DOMFileSystem::create(ScriptExecutionContext* context, const String& name, PassOwnPtr<AsyncFileSystem> asyncFileSystem)
57 {
58     RefPtr<DOMFileSystem> fileSystem(adoptRef(new DOMFileSystem(context, name, asyncFileSystem)));
59     InspectorInstrumentation::didOpenFileSystem(fileSystem.get());
60     return fileSystem;
61 }
62
63 DOMFileSystem::DOMFileSystem(ScriptExecutionContext* context, const String& name, PassOwnPtr<AsyncFileSystem> asyncFileSystem)
64     : DOMFileSystemBase(context, name, asyncFileSystem)
65     , ActiveDOMObject(context, this)
66 {
67 }
68
69 PassRefPtr<DirectoryEntry> DOMFileSystem::root()
70 {
71     return DirectoryEntry::create(this, DOMFilePath::root);
72 }
73
74 void DOMFileSystem::stop()
75 {
76     m_asyncFileSystem->stop();
77 }
78
79 bool DOMFileSystem::hasPendingActivity() const
80 {
81     return m_asyncFileSystem->hasPendingActivity();
82 }
83
84 void DOMFileSystem::contextDestroyed()
85 {
86     m_asyncFileSystem->stop();
87     ActiveDOMObject::contextDestroyed();
88 }
89
90 namespace {
91
92 class ConvertToFileWriterCallback : public FileWriterBaseCallback {
93 public:
94     static PassRefPtr<ConvertToFileWriterCallback> create(PassRefPtr<FileWriterCallback> callback)
95     {
96         return adoptRef(new ConvertToFileWriterCallback(callback));
97     }
98
99     bool handleEvent(FileWriterBase* fileWriterBase)
100     {
101         return m_callback->handleEvent(static_cast<FileWriter*>(fileWriterBase));
102     }
103 private:
104     ConvertToFileWriterCallback(PassRefPtr<FileWriterCallback> callback)
105         : m_callback(callback)
106     {
107     }
108     RefPtr<FileWriterCallback> m_callback;
109 };
110
111 }
112
113 void DOMFileSystem::createWriter(const FileEntry* fileEntry, PassRefPtr<FileWriterCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
114 {
115     ASSERT(fileEntry);
116
117     RefPtr<FileWriter> fileWriter = FileWriter::create(scriptExecutionContext());
118     RefPtr<FileWriterBaseCallback> conversionCallback = ConvertToFileWriterCallback::create(successCallback);
119     OwnPtr<FileWriterBaseCallbacks> callbacks = FileWriterBaseCallbacks::create(fileWriter, conversionCallback, errorCallback);
120     m_asyncFileSystem->createWriter(fileWriter.get(), fileEntry->fullPath(), callbacks.release());
121 }
122
123 namespace {
124
125 class GetPathCallback : public FileSystemCallbacksBase {
126 public:
127     static PassOwnPtr<GetPathCallback> create(PassRefPtr<DOMFileSystem> filesystem, const String& name, PassRefPtr<FileCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
128     {
129         return adoptPtr(new GetPathCallback(filesystem, name, successCallback, errorCallback));
130     }
131
132     virtual void didReadMetadata(const FileMetadata& metadata)
133     {
134         ASSERT(!metadata.platformPath.isEmpty());
135         m_filesystem->scheduleCallback(m_successCallback.release(), File::createWithName(metadata.platformPath, m_name));
136     }
137
138 private:
139     GetPathCallback(PassRefPtr<DOMFileSystem> filesystem, const String& name,  PassRefPtr<FileCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
140         : FileSystemCallbacksBase(errorCallback)
141         , m_filesystem(filesystem)
142         , m_name(name)
143         , m_successCallback(successCallback)
144     {
145     }
146
147     RefPtr<DOMFileSystem> m_filesystem;
148     String m_name;
149     RefPtr<FileCallback> m_successCallback;
150 };
151
152 } // namespace
153
154 void DOMFileSystem::createFile(const FileEntry* fileEntry, PassRefPtr<FileCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
155 {
156     m_asyncFileSystem->readMetadata(fileEntry->fullPath(), GetPathCallback::create(this, fileEntry->name(), successCallback, errorCallback));
157 }
158
159 } // namespace WebCore
160
161 #endif // ENABLE(FILE_SYSTEM)