Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / filesystem / 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 "modules/filesystem/DOMFileSystem.h"
33
34 #include "modules/filesystem/DOMFilePath.h"
35 #include "modules/filesystem/DirectoryEntry.h"
36 #include "modules/filesystem/ErrorCallback.h"
37 #include "modules/filesystem/FileCallback.h"
38 #include "modules/filesystem/FileEntry.h"
39 #include "modules/filesystem/FileSystemCallbacks.h"
40 #include "modules/filesystem/FileWriter.h"
41 #include "modules/filesystem/FileWriterBaseCallback.h"
42 #include "modules/filesystem/FileWriterCallback.h"
43 #include "modules/filesystem/MetadataCallback.h"
44 #include "platform/FileMetadata.h"
45 #include "platform/weborigin/DatabaseIdentifier.h"
46 #include "platform/weborigin/SecurityOrigin.h"
47 #include "public/platform/WebFileSystem.h"
48 #include "public/platform/WebFileSystemCallbacks.h"
49 #include "wtf/OwnPtr.h"
50 #include "wtf/text/StringBuilder.h"
51 #include "wtf/text/WTFString.h"
52
53 namespace blink {
54
55 // static
56 DOMFileSystem* DOMFileSystem::create(ExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL)
57 {
58     DOMFileSystem* fileSystem(new DOMFileSystem(context, name, type, rootURL));
59     fileSystem->suspendIfNeeded();
60     return fileSystem;
61 }
62
63 DOMFileSystem* DOMFileSystem::createIsolatedFileSystem(ExecutionContext* context, const String& filesystemId)
64 {
65     if (filesystemId.isEmpty())
66         return 0;
67
68     StringBuilder filesystemName;
69     filesystemName.append(createDatabaseIdentifierFromSecurityOrigin(context->securityOrigin()));
70     filesystemName.appendLiteral(":Isolated_");
71     filesystemName.append(filesystemId);
72
73     // The rootURL created here is going to be attached to each filesystem request and
74     // is to be validated each time the request is being handled.
75     StringBuilder rootURL;
76     rootURL.appendLiteral("filesystem:");
77     rootURL.append(context->securityOrigin()->toString());
78     rootURL.append('/');
79     rootURL.append(isolatedPathPrefix);
80     rootURL.append('/');
81     rootURL.append(filesystemId);
82     rootURL.append('/');
83
84     return DOMFileSystem::create(context, filesystemName.toString(), FileSystemTypeIsolated, KURL(ParsedURLString, rootURL.toString()));
85 }
86
87 DOMFileSystem::DOMFileSystem(ExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL)
88     : DOMFileSystemBase(context, name, type, rootURL)
89     , ActiveDOMObject(context)
90     , m_numberOfPendingCallbacks(0)
91 {
92 }
93
94 DirectoryEntry* DOMFileSystem::root()
95 {
96     return DirectoryEntry::create(this, DOMFilePath::root);
97 }
98
99 void DOMFileSystem::addPendingCallbacks()
100 {
101     ++m_numberOfPendingCallbacks;
102 }
103
104 void DOMFileSystem::removePendingCallbacks()
105 {
106     ASSERT(m_numberOfPendingCallbacks > 0);
107     --m_numberOfPendingCallbacks;
108 }
109
110 bool DOMFileSystem::hasPendingActivity() const
111 {
112     ASSERT(m_numberOfPendingCallbacks >= 0);
113     return m_numberOfPendingCallbacks;
114 }
115
116 void DOMFileSystem::reportError(ErrorCallback* errorCallback, PassRefPtrWillBeRawPtr<FileError> fileError)
117 {
118     scheduleCallback(errorCallback, fileError);
119 }
120
121 namespace {
122
123 class ConvertToFileWriterCallback : public FileWriterBaseCallback {
124 public:
125     static ConvertToFileWriterCallback* create(FileWriterCallback* callback)
126     {
127         return new ConvertToFileWriterCallback(callback);
128     }
129
130     void trace(Visitor* visitor)
131     {
132         visitor->trace(m_callback);
133         FileWriterBaseCallback::trace(visitor);
134     }
135
136     void handleEvent(FileWriterBase* fileWriterBase)
137     {
138         m_callback->handleEvent(static_cast<FileWriter*>(fileWriterBase));
139     }
140 private:
141     explicit ConvertToFileWriterCallback(FileWriterCallback* callback)
142         : m_callback(callback)
143     {
144     }
145     Member<FileWriterCallback> m_callback;
146 };
147
148 }
149
150 void DOMFileSystem::createWriter(const FileEntry* fileEntry, FileWriterCallback* successCallback, ErrorCallback* errorCallback)
151 {
152     ASSERT(fileEntry);
153
154     if (!fileSystem()) {
155         reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
156         return;
157     }
158
159     FileWriter* fileWriter = FileWriter::create(executionContext());
160     FileWriterBaseCallback* conversionCallback = ConvertToFileWriterCallback::create(successCallback);
161     OwnPtr<AsyncFileSystemCallbacks> callbacks = FileWriterBaseCallbacks::create(fileWriter, conversionCallback, errorCallback, m_context);
162     fileSystem()->createFileWriter(createFileSystemURL(fileEntry), fileWriter, callbacks.release());
163 }
164
165 void DOMFileSystem::createFile(const FileEntry* fileEntry, FileCallback* successCallback, ErrorCallback* errorCallback)
166 {
167     KURL fileSystemURL = createFileSystemURL(fileEntry);
168     if (!fileSystem()) {
169         reportError(errorCallback, FileError::create(FileError::ABORT_ERR));
170         return;
171     }
172
173     fileSystem()->createSnapshotFileAndReadMetadata(fileSystemURL, SnapshotFileCallback::create(this, fileEntry->name(), fileSystemURL, successCallback, errorCallback, m_context));
174 }
175
176 } // namespace blink