OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/child/fileapi/webfilesystem_impl.h" | 5 #include "content/child/fileapi/webfilesystem_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 DCHECK(!waitable_results); | 116 DCHECK(!waitable_results); |
117 DispatchToMethod(ChildThread::current()->file_system_dispatcher(), | 117 DispatchToMethod(ChildThread::current()->file_system_dispatcher(), |
118 method, params); | 118 method, params); |
119 } | 119 } |
120 | 120 |
121 enum CallbacksUnregisterMode { | 121 enum CallbacksUnregisterMode { |
122 UNREGISTER_CALLBACKS, | 122 UNREGISTER_CALLBACKS, |
123 DO_NOT_UNREGISTER_CALLBACKS, | 123 DO_NOT_UNREGISTER_CALLBACKS, |
124 }; | 124 }; |
125 | 125 |
126 // Bridging functions that convert the arguments into Blink objects | |
127 // (e.g. WebFileInfo, WebString, WebVector<WebFileSystemEntry>) | |
128 // and call WebFileSystemCallbacks's methods. | |
129 // These are called by RunCallbacks after crossing threads to ensure | |
130 // thread safety, because the Blink objects cannot be passed across | |
131 // threads by base::Bind(). | |
132 void didSucceed(WebFileSystemCallbacks *callbacks) { | |
tzik
2014/07/15 13:41:06
s/didSucceed/DidSucceed/, s/ *callbacks/* callback
hiroshige
2014/07/16 04:09:15
Done.
| |
133 callbacks->didSucceed(); | |
134 } | |
135 | |
136 void didReadMetadata(const base::File::Info& file_info, | |
137 WebFileSystemCallbacks *callbacks) { | |
tzik
2014/07/15 13:41:06
ditto
hiroshige
2014/07/16 04:09:15
Done.
| |
138 WebFileInfo web_file_info; | |
139 FileInfoToWebFileInfo(file_info, &web_file_info); | |
140 callbacks->didReadMetadata(web_file_info); | |
141 } | |
142 | |
143 void didReadDirectory(const std::vector<fileapi::DirectoryEntry>& entries, | |
144 bool has_more, WebFileSystemCallbacks *callbacks) { | |
tzik
2014/07/15 13:41:06
ditto
hiroshige
2014/07/16 04:09:15
Done.
| |
145 WebVector<WebFileSystemEntry> file_system_entries(entries.size()); | |
146 for (size_t i = 0; i < entries.size(); i++) { | |
tzik
2014/07/15 13:41:06
s/i++/++i/?
hiroshige
2014/07/16 04:09:15
Done.
| |
147 file_system_entries[i].name = | |
148 base::FilePath(entries[i].name).AsUTF16Unsafe(); | |
149 file_system_entries[i].isDirectory = entries[i].is_directory; | |
150 } | |
151 callbacks->didReadDirectory(file_system_entries, has_more); | |
152 } | |
153 | |
154 void didOpenFileSystem(const base::string16& name, const GURL& root, | |
155 WebFileSystemCallbacks *callbacks) { | |
tzik
2014/07/15 13:41:06
ditto
hiroshige
2014/07/16 04:09:15
Done.
| |
156 callbacks->didOpenFileSystem( | |
157 static_cast<WebString>(name), | |
158 static_cast<WebURL>(root)); | |
tzik
2014/07/15 13:41:06
Can we remove static_cast and let implicit convers
hiroshige
2014/07/16 04:09:15
Done, except for mount_type (explicit casting is n
| |
159 } | |
160 | |
161 void didResolveURL( | |
162 const base::string16& name, | |
163 const GURL& root_url, | |
164 fileapi::FileSystemType mount_type, | |
165 const base::string16& file_path, | |
166 bool is_directory, | |
167 WebFileSystemCallbacks *callbacks) { | |
168 callbacks->didResolveURL( | |
169 static_cast<WebString>(name), | |
170 static_cast<WebURL>(root_url), | |
171 static_cast<blink::WebFileSystemType>(mount_type), | |
172 static_cast<WebString>(file_path), | |
173 is_directory); | |
174 } | |
175 | |
176 void didFail(base::File::Error error, WebFileSystemCallbacks *callbacks) { | |
177 callbacks->didFail(fileapi::FileErrorToWebFileError(error)); | |
178 } | |
179 | |
126 // Run WebFileSystemCallbacks's |method| with |params|. | 180 // Run WebFileSystemCallbacks's |method| with |params|. |
127 template <typename Method, typename Params> | 181 void RunCallbacks( |
128 void RunCallbacks(int callbacks_id, Method method, const Params& params, | 182 int callbacks_id, |
129 CallbacksUnregisterMode callbacks_unregister_mode) { | 183 const base::Callback<void(WebFileSystemCallbacks*)>& callback, |
184 CallbacksUnregisterMode callbacks_unregister_mode) { | |
130 WebFileSystemImpl* filesystem = | 185 WebFileSystemImpl* filesystem = |
131 WebFileSystemImpl::ThreadSpecificInstance(NULL); | 186 WebFileSystemImpl::ThreadSpecificInstance(NULL); |
132 if (!filesystem) | 187 if (!filesystem) |
133 return; | 188 return; |
134 WebFileSystemCallbacks callbacks = filesystem->GetCallbacks(callbacks_id); | 189 WebFileSystemCallbacks callbacks = filesystem->GetCallbacks(callbacks_id); |
135 if (callbacks_unregister_mode == UNREGISTER_CALLBACKS) | 190 if (callbacks_unregister_mode == UNREGISTER_CALLBACKS) |
136 filesystem->UnregisterCallbacks(callbacks_id); | 191 filesystem->UnregisterCallbacks(callbacks_id); |
137 DispatchToMethod(&callbacks, method, params); | 192 callback.Run(&callbacks); |
138 } | 193 } |
139 | 194 |
140 void DispatchResultsClosure(int thread_id, int callbacks_id, | 195 void DispatchResultsClosure(int thread_id, int callbacks_id, |
141 WaitableCallbackResults* waitable_results, | 196 WaitableCallbackResults* waitable_results, |
142 const base::Closure& results_closure) { | 197 const base::Closure& results_closure) { |
143 if (thread_id != CurrentWorkerId()) { | 198 if (thread_id != CurrentWorkerId()) { |
144 if (waitable_results) { | 199 if (waitable_results) { |
145 // If someone is waiting, this should result in running the closure. | 200 // If someone is waiting, this should result in running the closure. |
146 waitable_results->AddResultsAndSignal(results_closure); | 201 waitable_results->AddResultsAndSignal(results_closure); |
147 // In case no one is waiting, post a task to run the closure. | 202 // In case no one is waiting, post a task to run the closure. |
148 WorkerTaskRunner::Instance()->PostTask( | 203 WorkerTaskRunner::Instance()->PostTask( |
149 thread_id, | 204 thread_id, |
150 base::Bind(&WaitableCallbackResults::Run, | 205 base::Bind(&WaitableCallbackResults::Run, |
151 make_scoped_refptr(waitable_results))); | 206 make_scoped_refptr(waitable_results))); |
152 return; | 207 return; |
153 } | 208 } |
154 WorkerTaskRunner::Instance()->PostTask(thread_id, results_closure); | 209 WorkerTaskRunner::Instance()->PostTask(thread_id, results_closure); |
155 return; | 210 return; |
156 } | 211 } |
157 results_closure.Run(); | 212 results_closure.Run(); |
158 } | 213 } |
159 | 214 |
160 template <typename Method, typename Params> | |
161 void CallbackFileSystemCallbacks( | 215 void CallbackFileSystemCallbacks( |
162 int thread_id, int callbacks_id, | 216 int thread_id, int callbacks_id, |
163 WaitableCallbackResults* waitable_results, | 217 WaitableCallbackResults* waitable_results, |
164 Method method, const Params& params, | 218 const base::Callback<void(WebFileSystemCallbacks*)>& callback, |
165 CallbacksUnregisterMode callbacks_unregister_mode) { | 219 CallbacksUnregisterMode callbacksunregister_mode) { |
166 DispatchResultsClosure( | 220 DispatchResultsClosure( |
167 thread_id, callbacks_id, waitable_results, | 221 thread_id, callbacks_id, waitable_results, |
168 base::Bind(&RunCallbacks<Method, Params>, callbacks_id, method, params, | 222 base::Bind(&RunCallbacks, callbacks_id, callback, |
169 callbacks_unregister_mode)); | 223 callbacksunregister_mode)); |
170 } | 224 } |
171 | 225 |
226 | |
172 //----------------------------------------------------------------------------- | 227 //----------------------------------------------------------------------------- |
173 // Callback adapters. Callbacks must be called on the original calling thread, | 228 // Callback adapters. Callbacks must be called on the original calling thread, |
174 // so these callback adapters relay back the results to the calling thread | 229 // so these callback adapters relay back the results to the calling thread |
175 // if necessary. | 230 // if necessary. |
176 | 231 |
177 void OpenFileSystemCallbackAdapter( | 232 void OpenFileSystemCallbackAdapter( |
178 int thread_id, int callbacks_id, | 233 int thread_id, int callbacks_id, |
179 WaitableCallbackResults* waitable_results, | 234 WaitableCallbackResults* waitable_results, |
180 const std::string& name, const GURL& root) { | 235 const std::string& name, const GURL& root) { |
181 CallbackFileSystemCallbacks( | 236 CallbackFileSystemCallbacks( |
182 thread_id, callbacks_id, waitable_results, | 237 thread_id, callbacks_id, waitable_results, |
183 &WebFileSystemCallbacks::didOpenFileSystem, | 238 base::Bind(&didOpenFileSystem, base::UTF8ToUTF16(name), root), |
184 MakeTuple(base::UTF8ToUTF16(name), root), | |
185 UNREGISTER_CALLBACKS); | 239 UNREGISTER_CALLBACKS); |
186 } | 240 } |
187 | 241 |
188 void ResolveURLCallbackAdapter( | 242 void ResolveURLCallbackAdapter( |
189 int thread_id, int callbacks_id, | 243 int thread_id, int callbacks_id, |
190 WaitableCallbackResults* waitable_results, | 244 WaitableCallbackResults* waitable_results, |
191 const fileapi::FileSystemInfo& info, | 245 const fileapi::FileSystemInfo& info, |
192 const base::FilePath& file_path, bool is_directory) { | 246 const base::FilePath& file_path, bool is_directory) { |
193 base::FilePath normalized_path( | 247 base::FilePath normalized_path( |
194 fileapi::VirtualPath::GetNormalizedFilePath(file_path)); | 248 fileapi::VirtualPath::GetNormalizedFilePath(file_path)); |
195 CallbackFileSystemCallbacks( | 249 CallbackFileSystemCallbacks( |
196 thread_id, callbacks_id, waitable_results, | 250 thread_id, callbacks_id, waitable_results, |
197 &WebFileSystemCallbacks::didResolveURL, | 251 base::Bind(&didResolveURL, base::UTF8ToUTF16(info.name), info.root_url, |
198 MakeTuple(base::UTF8ToUTF16(info.name), info.root_url, | 252 info.mount_type, |
199 static_cast<blink::WebFileSystemType>(info.mount_type), | 253 normalized_path.AsUTF16Unsafe(), is_directory), |
200 normalized_path.AsUTF16Unsafe(), is_directory), | |
201 UNREGISTER_CALLBACKS); | 254 UNREGISTER_CALLBACKS); |
202 } | 255 } |
203 | 256 |
204 void StatusCallbackAdapter(int thread_id, int callbacks_id, | 257 void StatusCallbackAdapter(int thread_id, int callbacks_id, |
205 WaitableCallbackResults* waitable_results, | 258 WaitableCallbackResults* waitable_results, |
206 base::File::Error error) { | 259 base::File::Error error) { |
207 if (error == base::File::FILE_OK) { | 260 if (error == base::File::FILE_OK) { |
208 CallbackFileSystemCallbacks( | 261 CallbackFileSystemCallbacks( |
209 thread_id, callbacks_id, waitable_results, | 262 thread_id, callbacks_id, waitable_results, |
210 &WebFileSystemCallbacks::didSucceed, MakeTuple(), | 263 base::Bind(&didSucceed), |
211 UNREGISTER_CALLBACKS); | 264 UNREGISTER_CALLBACKS); |
212 } else { | 265 } else { |
213 CallbackFileSystemCallbacks( | 266 CallbackFileSystemCallbacks( |
214 thread_id, callbacks_id, waitable_results, | 267 thread_id, callbacks_id, waitable_results, |
215 &WebFileSystemCallbacks::didFail, | 268 base::Bind(&didFail, error), |
216 MakeTuple(fileapi::FileErrorToWebFileError(error)), | |
217 UNREGISTER_CALLBACKS); | 269 UNREGISTER_CALLBACKS); |
218 } | 270 } |
219 } | 271 } |
220 | 272 |
221 void ReadMetadataCallbackAdapter(int thread_id, int callbacks_id, | 273 void ReadMetadataCallbackAdapter(int thread_id, int callbacks_id, |
222 WaitableCallbackResults* waitable_results, | 274 WaitableCallbackResults* waitable_results, |
223 const base::File::Info& file_info) { | 275 const base::File::Info& file_info) { |
224 WebFileInfo web_file_info; | |
225 FileInfoToWebFileInfo(file_info, &web_file_info); | |
226 CallbackFileSystemCallbacks( | 276 CallbackFileSystemCallbacks( |
227 thread_id, callbacks_id, waitable_results, | 277 thread_id, callbacks_id, waitable_results, |
228 &WebFileSystemCallbacks::didReadMetadata, | 278 base::Bind(&didReadMetadata, file_info), |
229 MakeTuple(web_file_info), | |
230 UNREGISTER_CALLBACKS); | 279 UNREGISTER_CALLBACKS); |
231 } | 280 } |
232 | 281 |
233 void ReadDirectoryCallbackAdapter( | 282 void ReadDirectoryCallbackAdapter( |
234 int thread_id, int callbacks_id, WaitableCallbackResults* waitable_results, | 283 int thread_id, int callbacks_id, WaitableCallbackResults* waitable_results, |
235 const std::vector<fileapi::DirectoryEntry>& entries, | 284 const std::vector<fileapi::DirectoryEntry>& entries, |
236 bool has_more) { | 285 bool has_more) { |
237 WebVector<WebFileSystemEntry> file_system_entries(entries.size()); | |
238 for (size_t i = 0; i < entries.size(); i++) { | |
239 file_system_entries[i].name = | |
240 base::FilePath(entries[i].name).AsUTF16Unsafe(); | |
241 file_system_entries[i].isDirectory = entries[i].is_directory; | |
242 } | |
243 CallbackFileSystemCallbacks( | 286 CallbackFileSystemCallbacks( |
244 thread_id, callbacks_id, waitable_results, | 287 thread_id, callbacks_id, waitable_results, |
245 &WebFileSystemCallbacks::didReadDirectory, | 288 base::Bind(&didReadDirectory, entries, has_more), |
246 MakeTuple(file_system_entries, has_more), | |
247 has_more ? DO_NOT_UNREGISTER_CALLBACKS : UNREGISTER_CALLBACKS); | 289 has_more ? DO_NOT_UNREGISTER_CALLBACKS : UNREGISTER_CALLBACKS); |
248 } | 290 } |
249 | 291 |
250 void DidCreateFileWriter( | 292 void DidCreateFileWriter( |
251 int callbacks_id, | 293 int callbacks_id, |
252 const GURL& path, | 294 const GURL& path, |
253 blink::WebFileWriterClient* client, | 295 blink::WebFileWriterClient* client, |
254 base::MessageLoopProxy* main_thread_loop, | 296 base::MessageLoopProxy* main_thread_loop, |
255 const base::File::Info& file_info) { | 297 const base::File::Info& file_info) { |
256 WebFileSystemImpl* filesystem = | 298 WebFileSystemImpl* filesystem = |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
646 WaitableCallbackResults* WebFileSystemImpl::MaybeCreateWaitableResults( | 688 WaitableCallbackResults* WebFileSystemImpl::MaybeCreateWaitableResults( |
647 const WebFileSystemCallbacks& callbacks, int callbacks_id) { | 689 const WebFileSystemCallbacks& callbacks, int callbacks_id) { |
648 if (!callbacks.shouldBlockUntilCompletion()) | 690 if (!callbacks.shouldBlockUntilCompletion()) |
649 return NULL; | 691 return NULL; |
650 WaitableCallbackResults* results = new WaitableCallbackResults(); | 692 WaitableCallbackResults* results = new WaitableCallbackResults(); |
651 waitable_results_[callbacks_id] = results; | 693 waitable_results_[callbacks_id] = results; |
652 return results; | 694 return results; |
653 } | 695 } |
654 | 696 |
655 } // namespace content | 697 } // namespace content |
OLD | NEW |