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