OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 18 matching lines...) Expand all Loading... |
29 */ | 29 */ |
30 | 30 |
31 #include "modules/filesystem/LocalFileSystem.h" | 31 #include "modules/filesystem/LocalFileSystem.h" |
32 | 32 |
33 #include "core/dom/Document.h" | 33 #include "core/dom/Document.h" |
34 #include "core/dom/ExecutionContext.h" | 34 #include "core/dom/ExecutionContext.h" |
35 #include "core/dom/ExecutionContextTask.h" | 35 #include "core/dom/ExecutionContextTask.h" |
36 #include "core/dom/TaskRunnerHelper.h" | 36 #include "core/dom/TaskRunnerHelper.h" |
37 #include "core/fileapi/FileError.h" | 37 #include "core/fileapi/FileError.h" |
38 #include "core/frame/LocalFrame.h" | 38 #include "core/frame/LocalFrame.h" |
| 39 #include "core/workers/WorkerClients.h" |
39 #include "core/workers/WorkerGlobalScope.h" | 40 #include "core/workers/WorkerGlobalScope.h" |
40 #include "modules/filesystem/FileSystemClient.h" | 41 #include "modules/filesystem/FileSystemClient.h" |
41 #include "platform/AsyncFileSystemCallbacks.h" | 42 #include "platform/AsyncFileSystemCallbacks.h" |
42 #include "platform/ContentSettingCallbacks.h" | 43 #include "platform/ContentSettingCallbacks.h" |
| 44 #include "platform/Supplementable.h" |
| 45 #include "platform/heap/Handle.h" |
43 #include "public/platform/Platform.h" | 46 #include "public/platform/Platform.h" |
44 #include "public/platform/WebFileSystem.h" | 47 #include "public/platform/WebFileSystem.h" |
45 #include "wtf/Functional.h" | |
46 #include <memory> | 48 #include <memory> |
47 | 49 |
48 namespace blink { | 50 namespace blink { |
49 | 51 |
50 namespace { | 52 namespace { |
51 | 53 |
52 void reportFailure(std::unique_ptr<AsyncFileSystemCallbacks> callbacks, | 54 void reportFailure(std::unique_ptr<AsyncFileSystemCallbacks> callbacks, |
53 FileError::ErrorCode error) { | 55 FileError::ErrorCode error) { |
54 callbacks->didFail(error); | 56 callbacks->didFail(error); |
55 } | 57 } |
56 | 58 |
57 } // namespace | 59 } // namespace |
58 | 60 |
59 class CallbackWrapper final | 61 class CallbackWrapper final |
60 : public GarbageCollectedFinalized<CallbackWrapper> { | 62 : public GarbageCollectedFinalized<CallbackWrapper> { |
61 public: | 63 public: |
62 CallbackWrapper(std::unique_ptr<AsyncFileSystemCallbacks> c) | 64 explicit CallbackWrapper(std::unique_ptr<AsyncFileSystemCallbacks> c) |
63 : m_callbacks(std::move(c)) {} | 65 : m_callbacks(std::move(c)) {} |
64 virtual ~CallbackWrapper() {} | 66 ~CallbackWrapper() {} |
| 67 |
65 std::unique_ptr<AsyncFileSystemCallbacks> release() { | 68 std::unique_ptr<AsyncFileSystemCallbacks> release() { |
66 return std::move(m_callbacks); | 69 return std::move(m_callbacks); |
67 } | 70 } |
68 | 71 |
69 DEFINE_INLINE_TRACE() {} | 72 DEFINE_INLINE_TRACE() {} |
70 | 73 |
71 private: | 74 private: |
72 std::unique_ptr<AsyncFileSystemCallbacks> m_callbacks; | 75 std::unique_ptr<AsyncFileSystemCallbacks> m_callbacks; |
73 }; | 76 }; |
74 | 77 |
| 78 template <typename T> |
| 79 class LocalFileSystemImpl final |
| 80 : public GarbageCollectedFinalized<LocalFileSystemImpl<T>>, |
| 81 public LocalFileSystem, |
| 82 public Supplement<T> { |
| 83 WTF_MAKE_NONCOPYABLE(LocalFileSystemImpl); |
| 84 USING_GARBAGE_COLLECTED_MIXIN(LocalFileSystemImpl); |
| 85 |
| 86 public: |
| 87 LocalFileSystemImpl(T& supplementable, |
| 88 std::unique_ptr<FileSystemClient> client) |
| 89 : LocalFileSystem(std::move(client)), Supplement<T>(supplementable) {} |
| 90 ~LocalFileSystemImpl() = default; |
| 91 |
| 92 void resolveURL( |
| 93 ExecutionContext* context, |
| 94 const KURL& fileSystemURL, |
| 95 std::unique_ptr<AsyncFileSystemCallbacks> callbacks) override { |
| 96 CallbackWrapper* wrapper = new CallbackWrapper(std::move(callbacks)); |
| 97 requestFileSystemAccessInternal( |
| 98 context, |
| 99 WTF::bind(&LocalFileSystemImpl::resolveURLInternal, |
| 100 wrapCrossThreadPersistent(this), wrapPersistent(context), |
| 101 fileSystemURL, wrapPersistent(wrapper)), |
| 102 WTF::bind(&LocalFileSystemImpl::fileSystemNotAllowedInternal, |
| 103 wrapCrossThreadPersistent(this), wrapPersistent(context), |
| 104 wrapPersistent(wrapper))); |
| 105 } |
| 106 |
| 107 void requestFileSystem( |
| 108 ExecutionContext* context, |
| 109 FileSystemType type, |
| 110 long long size, |
| 111 std::unique_ptr<AsyncFileSystemCallbacks> callbacks) override { |
| 112 CallbackWrapper* wrapper = new CallbackWrapper(std::move(callbacks)); |
| 113 requestFileSystemAccessInternal( |
| 114 context, |
| 115 WTF::bind(&LocalFileSystemImpl::fileSystemAllowedInternal, |
| 116 wrapCrossThreadPersistent(this), wrapPersistent(context), |
| 117 type, wrapPersistent(wrapper)), |
| 118 WTF::bind(&LocalFileSystemImpl::fileSystemNotAllowedInternal, |
| 119 wrapCrossThreadPersistent(this), wrapPersistent(context), |
| 120 wrapPersistent(wrapper))); |
| 121 } |
| 122 }; |
| 123 |
| 124 LocalFileSystem::LocalFileSystem(std::unique_ptr<FileSystemClient> client) |
| 125 : m_client(std::move(client)) {} |
| 126 |
75 LocalFileSystem::~LocalFileSystem() {} | 127 LocalFileSystem::~LocalFileSystem() {} |
76 | 128 |
77 void LocalFileSystem::resolveURL( | 129 const char* LocalFileSystem::supplementName() { |
78 ExecutionContext* context, | 130 return "LocalFileSystem"; |
79 const KURL& fileSystemURL, | |
80 std::unique_ptr<AsyncFileSystemCallbacks> callbacks) { | |
81 CallbackWrapper* wrapper = new CallbackWrapper(std::move(callbacks)); | |
82 requestFileSystemAccessInternal( | |
83 context, | |
84 WTF::bind(&LocalFileSystem::resolveURLInternal, | |
85 wrapCrossThreadPersistent(this), wrapPersistent(context), | |
86 fileSystemURL, wrapPersistent(wrapper)), | |
87 WTF::bind(&LocalFileSystem::fileSystemNotAllowedInternal, | |
88 wrapCrossThreadPersistent(this), wrapPersistent(context), | |
89 wrapPersistent(wrapper))); | |
90 } | |
91 | |
92 void LocalFileSystem::requestFileSystem( | |
93 ExecutionContext* context, | |
94 FileSystemType type, | |
95 long long size, | |
96 std::unique_ptr<AsyncFileSystemCallbacks> callbacks) { | |
97 CallbackWrapper* wrapper = new CallbackWrapper(std::move(callbacks)); | |
98 requestFileSystemAccessInternal( | |
99 context, | |
100 WTF::bind(&LocalFileSystem::fileSystemAllowedInternal, | |
101 wrapCrossThreadPersistent(this), wrapPersistent(context), type, | |
102 wrapPersistent(wrapper)), | |
103 WTF::bind(&LocalFileSystem::fileSystemNotAllowedInternal, | |
104 wrapCrossThreadPersistent(this), wrapPersistent(context), | |
105 wrapPersistent(wrapper))); | |
106 } | 131 } |
107 | 132 |
108 WebFileSystem* LocalFileSystem::getFileSystem() const { | 133 WebFileSystem* LocalFileSystem::getFileSystem() const { |
109 Platform* platform = Platform::current(); | 134 Platform* platform = Platform::current(); |
110 if (!platform) | 135 if (!platform) |
111 return nullptr; | 136 return nullptr; |
112 | 137 |
113 return platform->fileSystem(); | 138 return platform->fileSystem(); |
114 } | 139 } |
115 | 140 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 const KURL& fileSystemURL, | 194 const KURL& fileSystemURL, |
170 CallbackWrapper* callbacks) { | 195 CallbackWrapper* callbacks) { |
171 WebFileSystem* fileSystem = getFileSystem(); | 196 WebFileSystem* fileSystem = getFileSystem(); |
172 if (!fileSystem) { | 197 if (!fileSystem) { |
173 fileSystemNotAvailable(context, callbacks); | 198 fileSystemNotAvailable(context, callbacks); |
174 return; | 199 return; |
175 } | 200 } |
176 fileSystem->resolveURL(fileSystemURL, callbacks->release()); | 201 fileSystem->resolveURL(fileSystemURL, callbacks->release()); |
177 } | 202 } |
178 | 203 |
179 LocalFileSystem::LocalFileSystem(LocalFrame& frame, | |
180 std::unique_ptr<FileSystemClient> client) | |
181 : Supplement<LocalFrame>(frame), m_client(std::move(client)) {} | |
182 | |
183 LocalFileSystem::LocalFileSystem(WorkerClients& workerClients, | |
184 std::unique_ptr<FileSystemClient> client) | |
185 : Supplement<WorkerClients>(workerClients), m_client(std::move(client)) {} | |
186 | |
187 DEFINE_TRACE(LocalFileSystem) { | |
188 Supplement<LocalFrame>::trace(visitor); | |
189 Supplement<WorkerClients>::trace(visitor); | |
190 } | |
191 | |
192 const char* LocalFileSystem::supplementName() { | |
193 return "LocalFileSystem"; | |
194 } | |
195 | |
196 LocalFileSystem* LocalFileSystem::from(ExecutionContext& context) { | 204 LocalFileSystem* LocalFileSystem::from(ExecutionContext& context) { |
197 if (context.isDocument()) { | 205 if (context.isDocument()) { |
198 LocalFileSystem* fileSystem = | 206 auto fileSystem = static_cast<LocalFileSystemImpl<LocalFrame>*>( |
199 static_cast<LocalFileSystem*>(Supplement<LocalFrame>::from( | 207 Supplement<LocalFrame>::from(toDocument(context).frame(), |
200 toDocument(context).frame(), supplementName())); | 208 supplementName())); |
201 DCHECK(fileSystem); | 209 DCHECK(fileSystem); |
202 return fileSystem; | 210 return fileSystem; |
203 } | 211 } |
204 | 212 |
205 WorkerClients* clients = toWorkerGlobalScope(context).clients(); | 213 WorkerClients* clients = toWorkerGlobalScope(context).clients(); |
206 ASSERT(clients); | 214 ASSERT(clients); |
207 LocalFileSystem* fileSystem = static_cast<LocalFileSystem*>( | 215 auto fileSystem = static_cast<LocalFileSystemImpl<WorkerClients>*>( |
208 Supplement<WorkerClients>::from(clients, supplementName())); | 216 Supplement<WorkerClients>::from(clients, supplementName())); |
209 DCHECK(fileSystem); | 217 DCHECK(fileSystem); |
210 return fileSystem; | 218 return fileSystem; |
211 } | 219 } |
212 | 220 |
213 void provideLocalFileSystemTo(LocalFrame& frame, | 221 void provideLocalFileSystemTo(LocalFrame& frame, |
214 std::unique_ptr<FileSystemClient> client) { | 222 std::unique_ptr<FileSystemClient> client) { |
215 frame.provideSupplement(LocalFileSystem::supplementName(), | 223 Supplement<LocalFrame>::provideTo( |
216 new LocalFileSystem(frame, std::move(client))); | 224 frame, LocalFileSystem::supplementName(), |
| 225 new LocalFileSystemImpl<LocalFrame>(frame, std::move(client))); |
217 } | 226 } |
218 | 227 |
219 void provideLocalFileSystemToWorker(WorkerClients* workerClients, | 228 void provideLocalFileSystemToWorker(WorkerClients* clients, |
220 std::unique_ptr<FileSystemClient> client) { | 229 std::unique_ptr<FileSystemClient> client) { |
221 Supplement<WorkerClients>::provideTo( | 230 Supplement<WorkerClients>::provideTo( |
222 *workerClients, LocalFileSystem::supplementName(), | 231 *clients, LocalFileSystem::supplementName(), |
223 new LocalFileSystem(*workerClients, std::move(client))); | 232 new LocalFileSystemImpl<WorkerClients>(*clients, std::move(client))); |
224 } | 233 } |
225 | 234 |
226 } // namespace blink | 235 } // namespace blink |
OLD | NEW |