2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 #if ENABLE(BLOB) || ENABLE(FILE_SYSTEM)
35 #include "FileStreamProxy.h"
38 #include "CrossThreadTask.h"
39 #include "FileStream.h"
40 #include "FileThread.h"
41 #include "FileThreadTask.h"
42 #include "PlatformString.h"
43 #include "ScriptExecutionContext.h"
47 inline FileStreamProxy::FileStreamProxy(ScriptExecutionContext* context, FileStreamClient* client)
48 : AsyncFileStream(client)
50 , m_stream(FileStream::create())
54 PassRefPtr<FileStreamProxy> FileStreamProxy::create(ScriptExecutionContext* context, FileStreamClient* client)
56 RefPtr<FileStreamProxy> proxy = adoptRef(new FileStreamProxy(context, client));
58 // Hold an ref so that the instance will not get deleted while there are tasks on the file thread.
59 // This is balanced by the deref in derefProxyOnContext below.
62 proxy->fileThread()->postTask(createFileThreadTask(proxy.get(), &FileStreamProxy::startOnFileThread));
64 return proxy.release();
67 FileStreamProxy::~FileStreamProxy()
71 FileThread* FileStreamProxy::fileThread()
73 ASSERT(m_context->isContextThread());
74 ASSERT(m_context->fileThread());
75 return m_context->fileThread();
78 static void didStart(ScriptExecutionContext*, FileStreamProxy* proxy)
81 proxy->client()->didStart();
84 void FileStreamProxy::startOnFileThread()
89 m_context->postTask(createCallbackTask(&didStart, AllowCrossThreadAccess(this)));
92 void FileStreamProxy::stop()
94 // Clear the client so that we won't be calling callbacks on the client.
97 fileThread()->unscheduleTasks(m_stream.get());
98 fileThread()->postTask(createFileThreadTask(this, &FileStreamProxy::stopOnFileThread));
101 static void derefProxyOnContext(ScriptExecutionContext*, FileStreamProxy* proxy)
103 ASSERT(proxy->hasOneRef());
107 void FileStreamProxy::stopOnFileThread()
110 m_context->postTask(createCallbackTask(&derefProxyOnContext, AllowCrossThreadAccess(this)));
113 static void didGetSize(ScriptExecutionContext*, FileStreamProxy* proxy, long long size)
116 proxy->client()->didGetSize(size);
119 void FileStreamProxy::getSize(const String& path, double expectedModificationTime)
121 fileThread()->postTask(createFileThreadTask(this, &FileStreamProxy::getSizeOnFileThread, path, expectedModificationTime));
124 void FileStreamProxy::getSizeOnFileThread(const String& path, double expectedModificationTime)
126 long long size = m_stream->getSize(path, expectedModificationTime);
127 m_context->postTask(createCallbackTask(&didGetSize, AllowCrossThreadAccess(this), size));
130 static void didOpen(ScriptExecutionContext*, FileStreamProxy* proxy, bool success)
133 proxy->client()->didOpen(success);
136 void FileStreamProxy::openForRead(const String& path, long long offset, long long length)
138 fileThread()->postTask(createFileThreadTask(this, &FileStreamProxy::openForReadOnFileThread, path, offset, length));
141 void FileStreamProxy::openForReadOnFileThread(const String& path, long long offset, long long length)
143 bool success = m_stream->openForRead(path, offset, length);
144 m_context->postTask(createCallbackTask(&didOpen, AllowCrossThreadAccess(this), success));
147 void FileStreamProxy::openForWrite(const String& path)
149 fileThread()->postTask(
150 createFileThreadTask(this,
151 &FileStreamProxy::openForWriteOnFileThread, path));
154 void FileStreamProxy::openForWriteOnFileThread(const String& path)
156 bool success = m_stream->openForWrite(path);
157 m_context->postTask(createCallbackTask(&didOpen, AllowCrossThreadAccess(this), success));
160 void FileStreamProxy::close()
162 fileThread()->postTask(createFileThreadTask(this, &FileStreamProxy::closeOnFileThread));
165 void FileStreamProxy::closeOnFileThread()
170 static void didRead(ScriptExecutionContext*, FileStreamProxy* proxy, int bytesRead)
173 proxy->client()->didRead(bytesRead);
176 void FileStreamProxy::read(char* buffer, int length)
178 fileThread()->postTask(
179 createFileThreadTask(this, &FileStreamProxy::readOnFileThread,
180 AllowCrossThreadAccess(buffer), length));
183 void FileStreamProxy::readOnFileThread(char* buffer, int length)
185 int bytesRead = m_stream->read(buffer, length);
186 m_context->postTask(createCallbackTask(&didRead, AllowCrossThreadAccess(this), bytesRead));
189 static void didWrite(ScriptExecutionContext*, FileStreamProxy* proxy, int bytesWritten)
192 proxy->client()->didWrite(bytesWritten);
195 void FileStreamProxy::write(const KURL& blobURL, long long position, int length)
197 fileThread()->postTask(createFileThreadTask(this, &FileStreamProxy::writeOnFileThread, blobURL, position, length));
200 void FileStreamProxy::writeOnFileThread(const KURL& blobURL, long long position, int length)
202 int bytesWritten = m_stream->write(blobURL, position, length);
203 m_context->postTask(createCallbackTask(&didWrite, AllowCrossThreadAccess(this), bytesWritten));
206 static void didTruncate(ScriptExecutionContext*, FileStreamProxy* proxy, bool success)
209 proxy->client()->didTruncate(success);
212 void FileStreamProxy::truncate(long long position)
214 fileThread()->postTask(createFileThreadTask(this, &FileStreamProxy::truncateOnFileThread, position));
217 void FileStreamProxy::truncateOnFileThread(long long position)
219 bool success = m_stream->truncate(position);
220 m_context->postTask(createCallbackTask(&didTruncate, AllowCrossThreadAccess(this), success));
223 } // namespace WebCore
225 #endif // ENABLE(BLOB) || ENABLE(FILE_SYSTEM)