1 /* 2 * Copyright (C) 2013 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/LocalFileSystem.h" 33 34 #include "core/dom/CrossThreadTask.h" 35 #include "core/dom/Document.h" 36 #include "core/dom/ExceptionCode.h" 37 #include "core/dom/ScriptExecutionContext.h" 38 #include "core/fileapi/FileError.h" 39 #include "core/page/Page.h" 40 #include "core/platform/AsyncFileSystemCallbacks.h" 41 #include "modules/filesystem/FileSystemClient.h" 42 #include "modules/filesystem/WorkerLocalFileSystem.h" 43 44 namespace WebCore { 45 46 namespace { 47 48 void fileSystemNotAllowed(ScriptExecutionContext*, PassOwnPtr<AsyncFileSystemCallbacks> callbacks) 49 { 50 callbacks->didFail(FileError::ABORT_ERR); 51 } 52 53 } // namespace 54 55 LocalFileSystemBase::~LocalFileSystemBase() 56 { 57 } 58 59 void LocalFileSystemBase::readFileSystem(ScriptExecutionContext* context, FileSystemType type, PassOwnPtr<AsyncFileSystemCallbacks> callbacks, FileSystemSynchronousType synchronousType) 60 { 61 if (!client() || !client()->allowFileSystem(context)) { 62 context->postTask(createCallbackTask(&fileSystemNotAllowed, callbacks)); 63 return; 64 } 65 client()->openFileSystem(context, type, callbacks, synchronousType, 0, OpenExistingFileSystem); 66 } 67 68 void LocalFileSystemBase::requestFileSystem(ScriptExecutionContext* context, FileSystemType type, long long size, PassOwnPtr<AsyncFileSystemCallbacks> callbacks, FileSystemSynchronousType synchronousType) 69 { 70 if (!client() || !client()->allowFileSystem(context)) { 71 context->postTask(createCallbackTask(&fileSystemNotAllowed, callbacks)); 72 return; 73 } 74 client()->openFileSystem(context, type, callbacks, synchronousType, size, CreateFileSystemIfNotPresent); 75 } 76 77 void LocalFileSystemBase::deleteFileSystem(ScriptExecutionContext* context, FileSystemType type, PassOwnPtr<AsyncFileSystemCallbacks> callbacks) 78 { 79 ASSERT(context); 80 ASSERT_WITH_SECURITY_IMPLICATION(context->isDocument()); 81 82 if (!client() || !client()->allowFileSystem(context)) { 83 context->postTask(createCallbackTask(&fileSystemNotAllowed, callbacks)); 84 return; 85 } 86 client()->deleteFileSystem(context, type, callbacks); 87 } 88 89 LocalFileSystemBase::LocalFileSystemBase(PassOwnPtr<FileSystemClient> client) 90 : m_client(client) 91 { 92 } 93 94 PassOwnPtr<LocalFileSystem> LocalFileSystem::create(PassOwnPtr<FileSystemClient> client) 95 { 96 return adoptPtr(new LocalFileSystem(client)); 97 } 98 99 const char* LocalFileSystem::supplementName() 100 { 101 return "LocalFileSystem"; 102 } 103 104 LocalFileSystem::LocalFileSystem(PassOwnPtr<FileSystemClient> client) 105 : LocalFileSystemBase(client) 106 { 107 } 108 109 LocalFileSystem::~LocalFileSystem() 110 { 111 } 112 113 LocalFileSystem* LocalFileSystem::from(ScriptExecutionContext* context) 114 { 115 return static_cast<LocalFileSystem*>(Supplement<Page>::from(toDocument(context)->page(), supplementName())); 116 } 117 118 void provideLocalFileSystemTo(Page* page, PassOwnPtr<FileSystemClient> client) 119 { 120 LocalFileSystem::provideTo(page, LocalFileSystem::supplementName(), LocalFileSystem::create(client)); 121 } 122 123 } // namespace WebCore 124