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/ExecutionContext.h" 38 #include "core/fileapi/FileError.h" 39 #include "core/workers/WorkerGlobalScope.h" 40 #include "modules/filesystem/FileSystemClient.h" 41 #include "platform/AsyncFileSystemCallbacks.h" 42 #include "public/platform/Platform.h" 43 #include "public/platform/WebFileSystem.h" 44 45 namespace WebCore { 46 47 namespace { 48 49 void fileSystemNotAllowed(ExecutionContext*, PassOwnPtr<AsyncFileSystemCallbacks> callbacks) 50 { 51 callbacks->didFail(FileError::ABORT_ERR); 52 } 53 54 } // namespace 55 56 PassOwnPtr<LocalFileSystem> LocalFileSystem::create(PassOwnPtr<FileSystemClient> client) 57 { 58 return adoptPtr(new LocalFileSystem(client)); 59 } 60 61 LocalFileSystem::~LocalFileSystem() 62 { 63 } 64 65 void LocalFileSystem::resolveURL(ExecutionContext* context, const KURL& fileSystemURL, PassOwnPtr<AsyncFileSystemCallbacks> callbacks) 66 { 67 if (!client() || !client()->allowFileSystem(context)) { 68 context->postTask(createCallbackTask(&fileSystemNotAllowed, callbacks)); 69 return; 70 } 71 blink::Platform::current()->fileSystem()->resolveURL(fileSystemURL, callbacks); 72 } 73 74 void LocalFileSystem::requestFileSystem(ExecutionContext* context, FileSystemType type, long long size, PassOwnPtr<AsyncFileSystemCallbacks> callbacks) 75 { 76 if (!client() || !client()->allowFileSystem(context)) { 77 context->postTask(createCallbackTask(&fileSystemNotAllowed, callbacks)); 78 return; 79 } 80 KURL storagePartition = KURL(KURL(), context->securityOrigin()->toString()); 81 blink::Platform::current()->fileSystem()->openFileSystem(storagePartition, static_cast<blink::WebFileSystemType>(type), callbacks); 82 } 83 84 void LocalFileSystem::deleteFileSystem(ExecutionContext* context, FileSystemType type, PassOwnPtr<AsyncFileSystemCallbacks> callbacks) 85 { 86 ASSERT(context); 87 ASSERT_WITH_SECURITY_IMPLICATION(context->isDocument()); 88 89 if (!client() || !client()->allowFileSystem(context)) { 90 context->postTask(createCallbackTask(&fileSystemNotAllowed, callbacks)); 91 return; 92 } 93 KURL storagePartition = KURL(KURL(), context->securityOrigin()->toString()); 94 blink::Platform::current()->fileSystem()->deleteFileSystem(storagePartition, static_cast<blink::WebFileSystemType>(type), callbacks); 95 } 96 97 LocalFileSystem::LocalFileSystem(PassOwnPtr<FileSystemClient> client) 98 : m_client(client) 99 { 100 } 101 102 const char* LocalFileSystem::supplementName() 103 { 104 return "LocalFileSystem"; 105 } 106 107 LocalFileSystem* LocalFileSystem::from(ExecutionContext* context) 108 { 109 if (context->isDocument()) { 110 return static_cast<LocalFileSystem*>(Supplement<Page>::from(toDocument(context)->page(), supplementName())); 111 } 112 ASSERT(context->isWorkerGlobalScope()); 113 return static_cast<LocalFileSystem*>(Supplement<WorkerClients>::from(toWorkerGlobalScope(context)->clients(), supplementName())); 114 } 115 116 void provideLocalFileSystemTo(Page* page, PassOwnPtr<FileSystemClient> client) 117 { 118 page->provideSupplement(LocalFileSystem::supplementName(), LocalFileSystem::create(client)); 119 } 120 121 void provideLocalFileSystemToWorker(WorkerClients* clients, PassOwnPtr<FileSystemClient> client) 122 { 123 clients->provideSupplement(LocalFileSystem::supplementName(), LocalFileSystem::create(client)); 124 } 125 126 } // namespace WebCore 127