121
122// This class is used to post a openFileSystem request to the main thread.
123// This must be destructed on the worker thread.
124class OpenFileSystemMainThreadBridge {
125public:
126 static void start(WebWorkerBase* worker, WebCommonWorkerClient* commonClient, WebFrame* frame, const WTF::String& mode, WebFileSystem::Type type, long long size, WebFileSystemCallbacks* callbacks)
127 {
128 // This class is self-destructed.
129 ASSERT(new OpenFileSystemMainThreadBridge(worker, commonClient, frame, mode, type, size, callbacks));
130 }
131
132 ~OpenFileSystemMainThreadBridge()
133 {
134 ASSERT(!m_workerContextCallbacks);
135 }
136
137private:
138 OpenFileSystemMainThreadBridge(WebWorkerBase* worker, WebCommonWorkerClient* commonClient, WebFrame* frame, const WTF::String& mode, WebFileSystem::Type type, long long size, WebFileSystemCallbacks* callbacks)
139 : m_worker(worker)
140 , m_workerContextCallbacks(callbacks)
141 , m_mode(mode)
142 {
143 worker->dispatchTaskToMainThread(createCallbackTask(&openFileSystemOnMainThread, worker, commonClient, frame, type, size, this));
144 }
145
146 static void openFileSystemOnMainThread(WebCore::ScriptExecutionContext*, WebWorkerBase* worker, WebCommonWorkerClient* commonClient, WebFrame* frame, WebFileSystem::Type type, long long size, OpenFileSystemMainThreadBridge* bridge)
147 {
148 if (!commonClient)
149 bridge->didFailOnMainThread(WebFileErrorAbort);
150 else
151 commonClient->openFileSystem(frame, type, size, new MainThreadFileSystemCallbacks(bridge));
152 }
153
154 // FileSystemCallbacks that are dispatched on the main thread.
155 class MainThreadFileSystemCallbacks : public WebFileSystemCallbacks {
156 public:
157 MainThreadFileSystemCallbacks(PassOwnPtr<OpenFileSystemMainThreadBridge> bridge)
158 : m_bridge(bridge)
159 {
160 }
161
162 virtual ~MainThreadFileSystemCallbacks()
163 {
164 if (m_bridge)
165 didFail(WebFileErrorAbort);
166 }
167
168 virtual void didOpenFileSystem(const WebString& name, const WebString& path)
169 {
170 ASSERT(m_bridge);
171 m_bridge.leakPtr()->didOpenFileSystemOnMainThread(name, path);
172 delete this;
173 }
174
175 virtual void didFail(WebFileError error)
176 {
177 ASSERT(m_bridge);
178 m_bridge.leakPtr()->didFailOnMainThread(error);
179 delete this;
180 }
181
182 virtual void didSucceed() { WEBKIT_ASSERT_NOT_REACHED(); }
183 virtual void didReadMetadata(const WebFileInfo&) { WEBKIT_ASSERT_NOT_REACHED(); }
184 virtual void didReadDirectory(const WebVector<WebFileSystemEntry>&, bool) { WEBKIT_ASSERT_NOT_REACHED(); }
185
186 private:
187 OwnPtr<OpenFileSystemMainThreadBridge> m_bridge;
188 };
189 friend class MainThreadFileSystemCallbacks;
190
191 static void didFailOnWorkerThread(WebCore::ScriptExecutionContext*, OpenFileSystemMainThreadBridge* bridgePtr, WebFileError error)
192 {
193 OwnPtr<OpenFileSystemMainThreadBridge> bridge(bridgePtr);
194 bridge->m_workerContextCallbacks->didFail(error);
195 bridge->m_workerContextCallbacks = 0;
196 }
197
198 static void didOpenFileSystemOnWorkerThread(WebCore::ScriptExecutionContext*, OpenFileSystemMainThreadBridge* bridgePtr, const WTF::String& name, const WTF::String& rootPath)
199 {
200 OwnPtr<OpenFileSystemMainThreadBridge> bridge(bridgePtr);
201 bridge->m_workerContextCallbacks->didOpenFileSystem(name, rootPath);
202 bridge->m_workerContextCallbacks = 0;
203 }
204
205 void didFailOnMainThread(WebFileError error)
206 {
207 m_worker->postTaskForModeToWorkerContext(createCallbackTask(&didFailOnWorkerThread, this, error), m_mode);
208 }
209
210 void didOpenFileSystemOnMainThread(const WTF::String& name, const WTF::String& rootPath)
211 {
212 m_worker->postTaskForModeToWorkerContext(createCallbackTask(&didOpenFileSystemOnWorkerThread, this, name, rootPath), m_mode);
213 }
214
215 WebWorkerBase* m_worker;
216 WebFileSystemCallbacks* m_workerContextCallbacks;
217 WTF::String m_mode;
218};
219