| 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/webfilewriter_impl.h" | 5 #include "content/child/fileapi/webfilewriter_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
| 9 #include "content/child/child_thread.h" | 9 #include "content/child/child_thread_impl.h" |
| 10 #include "content/child/fileapi/file_system_dispatcher.h" | 10 #include "content/child/fileapi/file_system_dispatcher.h" |
| 11 #include "content/child/worker_task_runner.h" | 11 #include "content/child/worker_task_runner.h" |
| 12 | 12 |
| 13 namespace content { | 13 namespace content { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 FileSystemDispatcher* GetFileSystemDispatcher() { | 17 FileSystemDispatcher* GetFileSystemDispatcher() { |
| 18 return ChildThread::current() ? | 18 return ChildThreadImpl::current() ? |
| 19 ChildThread::current()->file_system_dispatcher() : NULL; | 19 ChildThreadImpl::current()->file_system_dispatcher() : NULL; |
| 20 } | 20 } |
| 21 | 21 |
| 22 } // namespace | 22 } // namespace |
| 23 | 23 |
| 24 typedef FileSystemDispatcher::StatusCallback StatusCallback; | 24 typedef FileSystemDispatcher::StatusCallback StatusCallback; |
| 25 typedef FileSystemDispatcher::WriteCallback WriteCallback; | 25 typedef FileSystemDispatcher::WriteCallback WriteCallback; |
| 26 | 26 |
| 27 // This instance may be created outside main thread but runs mainly | 27 // This instance may be created outside main thread but runs mainly |
| 28 // on main thread. | 28 // on main thread. |
| 29 class WebFileWriterImpl::WriterBridge | 29 class WebFileWriterImpl::WriterBridge |
| 30 : public base::RefCountedThreadSafe<WriterBridge> { | 30 : public base::RefCountedThreadSafe<WriterBridge> { |
| 31 public: | 31 public: |
| 32 WriterBridge(WebFileWriterImpl::Type type) | 32 WriterBridge(WebFileWriterImpl::Type type) |
| 33 : request_id_(0), | 33 : request_id_(0), |
| 34 thread_id_(WorkerTaskRunner::Instance()->CurrentWorkerId()), | 34 thread_id_(WorkerTaskRunner::Instance()->CurrentWorkerId()), |
| 35 written_bytes_(0) { | 35 written_bytes_(0) { |
| 36 if (type == WebFileWriterImpl::TYPE_SYNC) | 36 if (type == WebFileWriterImpl::TYPE_SYNC) |
| 37 waitable_event_.reset(new base::WaitableEvent(false, false)); | 37 waitable_event_.reset(new base::WaitableEvent(false, false)); |
| 38 } | 38 } |
| 39 | 39 |
| 40 void Truncate(const GURL& path, int64 offset, | 40 void Truncate(const GURL& path, int64 offset, |
| 41 const StatusCallback& status_callback) { | 41 const StatusCallback& status_callback) { |
| 42 status_callback_ = status_callback; | 42 status_callback_ = status_callback; |
| 43 if (!GetFileSystemDispatcher()) | 43 if (!GetFileSystemDispatcher()) |
| 44 return; | 44 return; |
| 45 ChildThread::current()->file_system_dispatcher()->Truncate( | 45 ChildThreadImpl::current()->file_system_dispatcher()->Truncate( |
| 46 path, offset, &request_id_, | 46 path, offset, &request_id_, |
| 47 base::Bind(&WriterBridge::DidFinish, this)); | 47 base::Bind(&WriterBridge::DidFinish, this)); |
| 48 } | 48 } |
| 49 | 49 |
| 50 void Write(const GURL& path, const std::string& id, int64 offset, | 50 void Write(const GURL& path, const std::string& id, int64 offset, |
| 51 const WriteCallback& write_callback, | 51 const WriteCallback& write_callback, |
| 52 const StatusCallback& error_callback) { | 52 const StatusCallback& error_callback) { |
| 53 write_callback_ = write_callback; | 53 write_callback_ = write_callback; |
| 54 status_callback_ = error_callback; | 54 status_callback_ = error_callback; |
| 55 if (!GetFileSystemDispatcher()) | 55 if (!GetFileSystemDispatcher()) |
| 56 return; | 56 return; |
| 57 ChildThread::current()->file_system_dispatcher()->Write( | 57 ChildThreadImpl::current()->file_system_dispatcher()->Write( |
| 58 path, id, offset, &request_id_, | 58 path, id, offset, &request_id_, |
| 59 base::Bind(&WriterBridge::DidWrite, this), | 59 base::Bind(&WriterBridge::DidWrite, this), |
| 60 base::Bind(&WriterBridge::DidFinish, this)); | 60 base::Bind(&WriterBridge::DidFinish, this)); |
| 61 } | 61 } |
| 62 | 62 |
| 63 void Cancel(const StatusCallback& status_callback) { | 63 void Cancel(const StatusCallback& status_callback) { |
| 64 status_callback_ = status_callback; | 64 status_callback_ = status_callback; |
| 65 if (!GetFileSystemDispatcher()) | 65 if (!GetFileSystemDispatcher()) |
| 66 return; | 66 return; |
| 67 ChildThread::current()->file_system_dispatcher()->Cancel( | 67 ChildThreadImpl::current()->file_system_dispatcher()->Cancel( |
| 68 request_id_, | 68 request_id_, |
| 69 base::Bind(&WriterBridge::DidFinish, this)); | 69 base::Bind(&WriterBridge::DidFinish, this)); |
| 70 } | 70 } |
| 71 | 71 |
| 72 base::WaitableEvent* waitable_event() { | 72 base::WaitableEvent* waitable_event() { |
| 73 return waitable_event_.get(); | 73 return waitable_event_.get(); |
| 74 } | 74 } |
| 75 | 75 |
| 76 void WaitAndRun() { | 76 void WaitAndRun() { |
| 77 waitable_event_->Wait(); | 77 waitable_event_->Wait(); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 DCHECK(!bridge_->waitable_event()); | 154 DCHECK(!bridge_->waitable_event()); |
| 155 closure.Run(); | 155 closure.Run(); |
| 156 return; | 156 return; |
| 157 } | 157 } |
| 158 main_thread_task_runner_->PostTask(FROM_HERE, closure); | 158 main_thread_task_runner_->PostTask(FROM_HERE, closure); |
| 159 if (bridge_->waitable_event()) | 159 if (bridge_->waitable_event()) |
| 160 bridge_->WaitAndRun(); | 160 bridge_->WaitAndRun(); |
| 161 } | 161 } |
| 162 | 162 |
| 163 } // namespace content | 163 } // namespace content |
| OLD | NEW |