2 * Copyright (C) 2013 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.
32 #include "modules/filesystem/LocalFileSystem.h"
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/inspector/InspectorController.h"
40 #include "core/workers/WorkerGlobalScope.h"
41 #include "modules/filesystem/FileSystemClient.h"
42 #include "modules/filesystem/InspectorFileSystemAgent.h"
43 #include "platform/AsyncFileSystemCallbacks.h"
44 #include "public/platform/Platform.h"
45 #include "public/platform/WebFileSystem.h"
51 void fileSystemNotAllowed(ExecutionContext*, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
53 callbacks->didFail(FileError::ABORT_ERR);
58 PassOwnPtr<LocalFileSystem> LocalFileSystem::create(PassOwnPtr<FileSystemClient> client)
60 return adoptPtr(new LocalFileSystem(client));
63 LocalFileSystem::~LocalFileSystem()
67 void LocalFileSystem::resolveURL(ExecutionContext* context, const KURL& fileSystemURL, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
69 if (!client() || !client()->allowFileSystem(context)) {
70 context->postTask(createCallbackTask(&fileSystemNotAllowed, callbacks));
73 blink::Platform::current()->fileSystem()->resolveURL(fileSystemURL, callbacks);
76 void LocalFileSystem::requestFileSystem(ExecutionContext* context, FileSystemType type, long long size, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
78 if (!client() || !client()->allowFileSystem(context)) {
79 context->postTask(createCallbackTask(&fileSystemNotAllowed, callbacks));
82 KURL storagePartition = KURL(KURL(), context->securityOrigin()->toString());
83 blink::Platform::current()->fileSystem()->openFileSystem(storagePartition, static_cast<blink::WebFileSystemType>(type), callbacks);
86 void LocalFileSystem::deleteFileSystem(ExecutionContext* context, FileSystemType type, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
89 ASSERT_WITH_SECURITY_IMPLICATION(context->isDocument());
91 if (!client() || !client()->allowFileSystem(context)) {
92 context->postTask(createCallbackTask(&fileSystemNotAllowed, callbacks));
95 KURL storagePartition = KURL(KURL(), context->securityOrigin()->toString());
96 blink::Platform::current()->fileSystem()->deleteFileSystem(storagePartition, static_cast<blink::WebFileSystemType>(type), callbacks);
99 LocalFileSystem::LocalFileSystem(PassOwnPtr<FileSystemClient> client)
104 const char* LocalFileSystem::supplementName()
106 return "LocalFileSystem";
109 LocalFileSystem* LocalFileSystem::from(ExecutionContext* context)
111 if (context->isDocument()) {
112 return static_cast<LocalFileSystem*>(Supplement<Page>::from(toDocument(context)->page(), supplementName()));
114 ASSERT(context->isWorkerGlobalScope());
115 return static_cast<LocalFileSystem*>(Supplement<WorkerClients>::from(toWorkerGlobalScope(context)->clients(), supplementName()));
118 void provideLocalFileSystemTo(Page* page, PassOwnPtr<FileSystemClient> client)
120 page->provideSupplement(LocalFileSystem::supplementName(), LocalFileSystem::create(client));
121 page->inspectorController().registerModuleAgent(InspectorFileSystemAgent::create(page));
124 void provideLocalFileSystemToWorker(WorkerClients* clients, PassOwnPtr<FileSystemClient> client)
126 clients->provideSupplement(LocalFileSystem::supplementName(), LocalFileSystem::create(client));
129 } // namespace WebCore