Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / filesystem / DOMFileSystemSync.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/DOMFileSystemSync.h"
33
34 #include "bindings/core/v8/ExceptionState.h"
35 #include "core/dom/ExceptionCode.h"
36 #include "core/fileapi/File.h"
37 #include "core/fileapi/FileError.h"
38 #include "modules/filesystem/DOMFilePath.h"
39 #include "modules/filesystem/DirectoryEntrySync.h"
40 #include "modules/filesystem/ErrorCallback.h"
41 #include "modules/filesystem/FileEntrySync.h"
42 #include "modules/filesystem/FileSystemCallbacks.h"
43 #include "modules/filesystem/FileWriterBaseCallback.h"
44 #include "modules/filesystem/FileWriterSync.h"
45 #include "platform/FileMetadata.h"
46 #include "public/platform/WebFileSystem.h"
47 #include "public/platform/WebFileSystemCallbacks.h"
48
49 namespace blink {
50
51 class FileWriterBase;
52
53 DOMFileSystemSync* DOMFileSystemSync::create(DOMFileSystemBase* fileSystem)
54 {
55     return new DOMFileSystemSync(fileSystem->m_context, fileSystem->name(), fileSystem->type(), fileSystem->rootURL());
56 }
57
58 DOMFileSystemSync::DOMFileSystemSync(ExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL)
59     : DOMFileSystemBase(context, name, type, rootURL)
60 {
61 }
62
63 DOMFileSystemSync::~DOMFileSystemSync()
64 {
65 }
66
67 void DOMFileSystemSync::reportError(ErrorCallback* errorCallback, PassRefPtrWillBeRawPtr<FileError> fileError)
68 {
69     errorCallback->handleEvent(fileError.get());
70 }
71
72 DirectoryEntrySync* DOMFileSystemSync::root()
73 {
74     return DirectoryEntrySync::create(this, DOMFilePath::root);
75 }
76
77 namespace {
78
79 class CreateFileHelper FINAL : public AsyncFileSystemCallbacks {
80 public:
81     class CreateFileResult : public GarbageCollectedFinalized<CreateFileResult> {
82       public:
83         static CreateFileResult* create()
84         {
85             return new CreateFileResult();
86         }
87
88         bool m_failed;
89         int m_code;
90         RefPtrWillBeMember<File> m_file;
91
92         void trace(Visitor* visitor)
93         {
94             visitor->trace(m_file);
95         }
96
97       private:
98         CreateFileResult()
99             : m_failed(false)
100             , m_code(0)
101         {
102         }
103     };
104
105     static PassOwnPtr<AsyncFileSystemCallbacks> create(CreateFileResult* result, const String& name, const KURL& url, FileSystemType type)
106     {
107         return adoptPtr(static_cast<AsyncFileSystemCallbacks*>(new CreateFileHelper(result, name, url, type)));
108     }
109
110     virtual void didFail(int code) OVERRIDE
111     {
112         m_result->m_failed = true;
113         m_result->m_code = code;
114     }
115
116     virtual ~CreateFileHelper()
117     {
118     }
119
120     virtual void didCreateSnapshotFile(const FileMetadata& metadata, PassRefPtr<BlobDataHandle> snapshot) OVERRIDE
121     {
122         // We can't directly use the snapshot blob data handle because the content type on it hasn't been set.
123         // The |snapshot| param is here to provide a a chain of custody thru thread bridging that is held onto until
124         // *after* we've coined a File with a new handle that has the correct type set on it. This allows the
125         // blob storage system to track when a temp file can and can't be safely deleted.
126
127         m_result->m_file = DOMFileSystemBase::createFile(metadata, m_url, m_type, m_name);
128     }
129
130     virtual bool shouldBlockUntilCompletion() const OVERRIDE
131     {
132         return true;
133     }
134
135 private:
136     CreateFileHelper(CreateFileResult* result, const String& name, const KURL& url, FileSystemType type)
137         : m_result(result)
138         , m_name(name)
139         , m_url(url)
140         , m_type(type)
141     {
142     }
143
144     Persistent<CreateFileResult> m_result;
145     String m_name;
146     KURL m_url;
147     FileSystemType m_type;
148 };
149
150 } // namespace
151
152 PassRefPtrWillBeRawPtr<File> DOMFileSystemSync::createFile(const FileEntrySync* fileEntry, ExceptionState& exceptionState)
153 {
154     KURL fileSystemURL = createFileSystemURL(fileEntry);
155     CreateFileHelper::CreateFileResult* result(CreateFileHelper::CreateFileResult::create());
156     fileSystem()->createSnapshotFileAndReadMetadata(fileSystemURL, CreateFileHelper::create(result, fileEntry->name(), fileSystemURL, type()));
157     if (result->m_failed) {
158         exceptionState.throwDOMException(result->m_code, "Could not create '" + fileEntry->name() + "'.");
159         return nullptr;
160     }
161     return result->m_file.get();
162 }
163
164 namespace {
165
166 class ReceiveFileWriterCallback FINAL : public FileWriterBaseCallback {
167 public:
168     static ReceiveFileWriterCallback* create()
169     {
170         return new ReceiveFileWriterCallback();
171     }
172
173     virtual void handleEvent(FileWriterBase*) OVERRIDE
174     {
175     }
176
177 private:
178     ReceiveFileWriterCallback()
179     {
180     }
181 };
182
183 class LocalErrorCallback FINAL : public ErrorCallback {
184 public:
185     static LocalErrorCallback* create(FileError::ErrorCode& errorCode)
186     {
187         return new LocalErrorCallback(errorCode);
188     }
189
190     virtual void handleEvent(FileError* error) OVERRIDE
191     {
192         ASSERT(error->code() != FileError::OK);
193         m_errorCode = error->code();
194     }
195
196 private:
197     explicit LocalErrorCallback(FileError::ErrorCode& errorCode)
198         : m_errorCode(errorCode)
199     {
200     }
201
202     FileError::ErrorCode& m_errorCode;
203 };
204
205 }
206
207 FileWriterSync* DOMFileSystemSync::createWriter(const FileEntrySync* fileEntry, ExceptionState& exceptionState)
208 {
209     ASSERT(fileEntry);
210
211     FileWriterSync* fileWriter = FileWriterSync::create();
212     ReceiveFileWriterCallback* successCallback = ReceiveFileWriterCallback::create();
213     FileError::ErrorCode errorCode = FileError::OK;
214     LocalErrorCallback* errorCallback = LocalErrorCallback::create(errorCode);
215
216     OwnPtr<AsyncFileSystemCallbacks> callbacks = FileWriterBaseCallbacks::create(fileWriter, successCallback, errorCallback, m_context);
217     callbacks->setShouldBlockUntilCompletion(true);
218
219     fileSystem()->createFileWriter(createFileSystemURL(fileEntry), fileWriter, callbacks.release());
220     if (errorCode != FileError::OK) {
221         FileError::throwDOMException(exceptionState, errorCode);
222         return 0;
223     }
224     return fileWriter;
225 }
226
227 }