Chromium Code Reviews| 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 // Run WebFileSystemCallbacks's |method| with |params|. | 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 class WebFileSystemCallbacksBridge { | |
|
tzik
2014/07/15 10:41:26
I think it's not worth adding new class to combine
hiroshige
2014/07/15 12:30:31
Done.
| |
| 133 public: | |
| 134 WebFileSystemCallbacksBridge(WebFileSystemCallbacks *callbacks) | |
| 135 : callbacks_(callbacks) {} | |
| 136 | |
| 137 void didSucceed() { | |
| 138 callbacks_->didSucceed(); | |
| 139 } | |
| 140 | |
| 141 void didReadMetadata(const base::File::Info& file_info) { | |
| 142 WebFileInfo web_file_info; | |
| 143 FileInfoToWebFileInfo(file_info, &web_file_info); | |
| 144 callbacks_->didReadMetadata(web_file_info); | |
| 145 } | |
| 146 | |
| 147 void didReadDirectory( | |
| 148 const std::vector<fileapi::DirectoryEntry>& entries, | |
| 149 bool has_more) { | |
| 150 WebVector<WebFileSystemEntry> file_system_entries(entries.size()); | |
| 151 for (size_t i = 0; i < entries.size(); i++) { | |
| 152 file_system_entries[i].name = | |
| 153 base::FilePath(entries[i].name).AsUTF16Unsafe(); | |
| 154 file_system_entries[i].isDirectory = entries[i].is_directory; | |
| 155 } | |
| 156 callbacks_->didReadDirectory(file_system_entries, has_more); | |
| 157 } | |
| 158 | |
| 159 void didOpenFileSystem(const base::string16& name, const GURL& root) { | |
| 160 callbacks_->didOpenFileSystem( | |
| 161 static_cast<WebString>(name), | |
| 162 static_cast<WebURL>(root)); | |
| 163 } | |
| 164 | |
| 165 void didResolveURL( | |
| 166 const base::string16& name, | |
| 167 const GURL& root_url, | |
| 168 fileapi::FileSystemType mount_type, | |
| 169 const base::string16& file_path, | |
| 170 bool is_directory) { | |
| 171 callbacks_->didResolveURL( | |
| 172 static_cast<WebString>(name), | |
| 173 static_cast<WebURL>(root_url), | |
| 174 static_cast<blink::WebFileSystemType>(mount_type), | |
| 175 static_cast<WebString>(file_path), | |
| 176 is_directory); | |
| 177 } | |
| 178 | |
| 179 void didFail(base::File::Error error) { | |
| 180 callbacks_->didFail(fileapi::FileErrorToWebFileError(error)); | |
| 181 } | |
| 182 | |
| 183 private: | |
| 184 WebFileSystemCallbacks* callbacks_; | |
| 185 }; | |
| 186 | |
| 187 // Run WebFileSystemCallbacks's |method| with |params| via | |
| 127 template <typename Method, typename Params> | 188 template <typename Method, typename Params> |
| 128 void RunCallbacks(int callbacks_id, Method method, const Params& params, | 189 void RunCallbacks(int callbacks_id, Method method, const Params& params, |
|
tzik
2014/07/15 10:41:26
(continued from above)
And replace Method and Para
hiroshige
2014/07/15 12:30:31
Done.
| |
| 129 CallbacksUnregisterMode callbacks_unregister_mode) { | 190 CallbacksUnregisterMode callbacks_unregister_mode) { |
| 130 WebFileSystemImpl* filesystem = | 191 WebFileSystemImpl* filesystem = |
| 131 WebFileSystemImpl::ThreadSpecificInstance(NULL); | 192 WebFileSystemImpl::ThreadSpecificInstance(NULL); |
| 132 if (!filesystem) | 193 if (!filesystem) |
| 133 return; | 194 return; |
| 134 WebFileSystemCallbacks callbacks = filesystem->GetCallbacks(callbacks_id); | 195 WebFileSystemCallbacks callbacks = filesystem->GetCallbacks(callbacks_id); |
| 135 if (callbacks_unregister_mode == UNREGISTER_CALLBACKS) | 196 if (callbacks_unregister_mode == UNREGISTER_CALLBACKS) |
| 136 filesystem->UnregisterCallbacks(callbacks_id); | 197 filesystem->UnregisterCallbacks(callbacks_id); |
| 137 DispatchToMethod(&callbacks, method, params); | 198 WebFileSystemCallbacksBridge bridge(&callbacks); |
| 199 DispatchToMethod(&bridge, method, params); | |
| 138 } | 200 } |
| 139 | 201 |
| 140 void DispatchResultsClosure(int thread_id, int callbacks_id, | 202 void DispatchResultsClosure(int thread_id, int callbacks_id, |
| 141 WaitableCallbackResults* waitable_results, | 203 WaitableCallbackResults* waitable_results, |
| 142 const base::Closure& results_closure) { | 204 const base::Closure& results_closure) { |
| 143 if (thread_id != CurrentWorkerId()) { | 205 if (thread_id != CurrentWorkerId()) { |
| 144 if (waitable_results) { | 206 if (waitable_results) { |
| 145 // If someone is waiting, this should result in running the closure. | 207 // If someone is waiting, this should result in running the closure. |
| 146 waitable_results->AddResultsAndSignal(results_closure); | 208 waitable_results->AddResultsAndSignal(results_closure); |
| 147 // In case no one is waiting, post a task to run the closure. | 209 // In case no one is waiting, post a task to run the closure. |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 171 | 233 |
| 172 //----------------------------------------------------------------------------- | 234 //----------------------------------------------------------------------------- |
| 173 // Callback adapters. Callbacks must be called on the original calling thread, | 235 // Callback adapters. Callbacks must be called on the original calling thread, |
| 174 // so these callback adapters relay back the results to the calling thread | 236 // so these callback adapters relay back the results to the calling thread |
| 175 // if necessary. | 237 // if necessary. |
| 176 | 238 |
| 177 void OpenFileSystemCallbackAdapter( | 239 void OpenFileSystemCallbackAdapter( |
| 178 int thread_id, int callbacks_id, | 240 int thread_id, int callbacks_id, |
| 179 WaitableCallbackResults* waitable_results, | 241 WaitableCallbackResults* waitable_results, |
| 180 const std::string& name, const GURL& root) { | 242 const std::string& name, const GURL& root) { |
| 181 CallbackFileSystemCallbacks( | 243 CallbackFileSystemCallbacks( |
|
tzik
2014/07/15 10:41:26
(continued from above)
And then, could you replace
hiroshige
2014/07/15 12:30:31
I retained CallbackFilesystemCallbacks to make the
| |
| 182 thread_id, callbacks_id, waitable_results, | 244 thread_id, callbacks_id, waitable_results, |
| 183 &WebFileSystemCallbacks::didOpenFileSystem, | 245 &WebFileSystemCallbacksBridge::didOpenFileSystem, |
| 184 MakeTuple(base::UTF8ToUTF16(name), root), | 246 MakeTuple(base::UTF8ToUTF16(name), root), |
| 185 UNREGISTER_CALLBACKS); | 247 UNREGISTER_CALLBACKS); |
| 186 } | 248 } |
| 187 | 249 |
| 188 void ResolveURLCallbackAdapter( | 250 void ResolveURLCallbackAdapter( |
| 189 int thread_id, int callbacks_id, | 251 int thread_id, int callbacks_id, |
| 190 WaitableCallbackResults* waitable_results, | 252 WaitableCallbackResults* waitable_results, |
| 191 const fileapi::FileSystemInfo& info, | 253 const fileapi::FileSystemInfo& info, |
| 192 const base::FilePath& file_path, bool is_directory) { | 254 const base::FilePath& file_path, bool is_directory) { |
| 193 base::FilePath normalized_path( | 255 base::FilePath normalized_path( |
| 194 fileapi::VirtualPath::GetNormalizedFilePath(file_path)); | 256 fileapi::VirtualPath::GetNormalizedFilePath(file_path)); |
| 195 CallbackFileSystemCallbacks( | 257 CallbackFileSystemCallbacks( |
| 196 thread_id, callbacks_id, waitable_results, | 258 thread_id, callbacks_id, waitable_results, |
| 197 &WebFileSystemCallbacks::didResolveURL, | 259 &WebFileSystemCallbacksBridge::didResolveURL, |
| 198 MakeTuple(base::UTF8ToUTF16(info.name), info.root_url, | 260 MakeTuple(base::UTF8ToUTF16(info.name), info.root_url, |
| 199 static_cast<blink::WebFileSystemType>(info.mount_type), | 261 info.mount_type, |
| 200 normalized_path.AsUTF16Unsafe(), is_directory), | 262 normalized_path.AsUTF16Unsafe(), is_directory), |
| 201 UNREGISTER_CALLBACKS); | 263 UNREGISTER_CALLBACKS); |
| 202 } | 264 } |
| 203 | 265 |
| 204 void StatusCallbackAdapter(int thread_id, int callbacks_id, | 266 void StatusCallbackAdapter(int thread_id, int callbacks_id, |
| 205 WaitableCallbackResults* waitable_results, | 267 WaitableCallbackResults* waitable_results, |
| 206 base::File::Error error) { | 268 base::File::Error error) { |
| 207 if (error == base::File::FILE_OK) { | 269 if (error == base::File::FILE_OK) { |
| 208 CallbackFileSystemCallbacks( | 270 CallbackFileSystemCallbacks( |
| 209 thread_id, callbacks_id, waitable_results, | 271 thread_id, callbacks_id, waitable_results, |
| 210 &WebFileSystemCallbacks::didSucceed, MakeTuple(), | 272 &WebFileSystemCallbacksBridge::didSucceed, MakeTuple(), |
| 211 UNREGISTER_CALLBACKS); | 273 UNREGISTER_CALLBACKS); |
| 212 } else { | 274 } else { |
| 213 CallbackFileSystemCallbacks( | 275 CallbackFileSystemCallbacks( |
| 214 thread_id, callbacks_id, waitable_results, | 276 thread_id, callbacks_id, waitable_results, |
| 215 &WebFileSystemCallbacks::didFail, | 277 &WebFileSystemCallbacksBridge::didFail, |
| 216 MakeTuple(fileapi::FileErrorToWebFileError(error)), | 278 MakeTuple(error), |
| 217 UNREGISTER_CALLBACKS); | 279 UNREGISTER_CALLBACKS); |
| 218 } | 280 } |
| 219 } | 281 } |
| 220 | 282 |
| 221 void ReadMetadataCallbackAdapter(int thread_id, int callbacks_id, | 283 void ReadMetadataCallbackAdapter(int thread_id, int callbacks_id, |
| 222 WaitableCallbackResults* waitable_results, | 284 WaitableCallbackResults* waitable_results, |
| 223 const base::File::Info& file_info) { | 285 const base::File::Info& file_info) { |
| 224 WebFileInfo web_file_info; | |
| 225 FileInfoToWebFileInfo(file_info, &web_file_info); | |
| 226 CallbackFileSystemCallbacks( | 286 CallbackFileSystemCallbacks( |
| 227 thread_id, callbacks_id, waitable_results, | 287 thread_id, callbacks_id, waitable_results, |
| 228 &WebFileSystemCallbacks::didReadMetadata, | 288 &WebFileSystemCallbacksBridge::didReadMetadata, |
| 229 MakeTuple(web_file_info), | 289 MakeTuple(file_info), |
| 230 UNREGISTER_CALLBACKS); | 290 UNREGISTER_CALLBACKS); |
| 231 } | 291 } |
| 232 | 292 |
| 233 void ReadDirectoryCallbackAdapter( | 293 void ReadDirectoryCallbackAdapter( |
| 234 int thread_id, int callbacks_id, WaitableCallbackResults* waitable_results, | 294 int thread_id, int callbacks_id, WaitableCallbackResults* waitable_results, |
| 235 const std::vector<fileapi::DirectoryEntry>& entries, | 295 const std::vector<fileapi::DirectoryEntry>& entries, |
| 236 bool has_more) { | 296 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( | 297 CallbackFileSystemCallbacks( |
| 244 thread_id, callbacks_id, waitable_results, | 298 thread_id, callbacks_id, waitable_results, |
| 245 &WebFileSystemCallbacks::didReadDirectory, | 299 &WebFileSystemCallbacksBridge::didReadDirectory, |
| 246 MakeTuple(file_system_entries, has_more), | 300 MakeTuple(entries, has_more), |
| 247 has_more ? DO_NOT_UNREGISTER_CALLBACKS : UNREGISTER_CALLBACKS); | 301 has_more ? DO_NOT_UNREGISTER_CALLBACKS : UNREGISTER_CALLBACKS); |
| 248 } | 302 } |
| 249 | 303 |
| 250 void DidCreateFileWriter( | 304 void DidCreateFileWriter( |
| 251 int callbacks_id, | 305 int callbacks_id, |
| 252 const GURL& path, | 306 const GURL& path, |
| 253 blink::WebFileWriterClient* client, | 307 blink::WebFileWriterClient* client, |
| 254 base::MessageLoopProxy* main_thread_loop, | 308 base::MessageLoopProxy* main_thread_loop, |
| 255 const base::File::Info& file_info) { | 309 const base::File::Info& file_info) { |
| 256 WebFileSystemImpl* filesystem = | 310 WebFileSystemImpl* filesystem = |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 646 WaitableCallbackResults* WebFileSystemImpl::MaybeCreateWaitableResults( | 700 WaitableCallbackResults* WebFileSystemImpl::MaybeCreateWaitableResults( |
| 647 const WebFileSystemCallbacks& callbacks, int callbacks_id) { | 701 const WebFileSystemCallbacks& callbacks, int callbacks_id) { |
| 648 if (!callbacks.shouldBlockUntilCompletion()) | 702 if (!callbacks.shouldBlockUntilCompletion()) |
| 649 return NULL; | 703 return NULL; |
| 650 WaitableCallbackResults* results = new WaitableCallbackResults(); | 704 WaitableCallbackResults* results = new WaitableCallbackResults(); |
| 651 waitable_results_[callbacks_id] = results; | 705 waitable_results_[callbacks_id] = results; |
| 652 return results; | 706 return results; |
| 653 } | 707 } |
| 654 | 708 |
| 655 } // namespace content | 709 } // namespace content |
| OLD | NEW |