| 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/platform_file.h" | 8 #include "base/platform_file.h" |
| 9 #include "base/synchronization/waitable_event.h" | |
| 10 #include "content/child/child_thread.h" | 9 #include "content/child/child_thread.h" |
| 11 #include "content/child/fileapi/file_system_dispatcher.h" | 10 #include "content/child/fileapi/file_system_dispatcher.h" |
| 12 #include "webkit/child/worker_task_runner.h" | 11 #include "webkit/child/worker_task_runner.h" |
| 13 | 12 |
| 14 using webkit_glue::WorkerTaskRunner; | 13 using webkit_glue::WorkerTaskRunner; |
| 15 | 14 |
| 16 namespace content { | 15 namespace content { |
| 17 | 16 |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| 20 FileSystemDispatcher* GetFileSystemDispatcher() { | 19 FileSystemDispatcher* GetFileSystemDispatcher() { |
| 21 return ChildThread::current() ? | 20 return ChildThread::current() ? |
| 22 ChildThread::current()->file_system_dispatcher() : NULL; | 21 ChildThread::current()->file_system_dispatcher() : NULL; |
| 23 } | 22 } |
| 24 | 23 |
| 25 } // namespace | 24 } // namespace |
| 26 | 25 |
| 27 typedef FileSystemDispatcher::StatusCallback StatusCallback; | 26 typedef FileSystemDispatcher::StatusCallback StatusCallback; |
| 28 typedef FileSystemDispatcher::WriteCallback WriteCallback; | 27 typedef FileSystemDispatcher::WriteCallback WriteCallback; |
| 29 | 28 |
| 30 // This instance may be created outside main thread but runs mainly | 29 // This instance may be created outside main thread but runs mainly |
| 31 // on main thread. | 30 // on main thread. |
| 32 class WebFileWriterImpl::WriterBridge | 31 class WebFileWriterImpl::WriterBridge |
| 33 : public base::RefCountedThreadSafe<WriterBridge> { | 32 : public base::RefCountedThreadSafe<WriterBridge> { |
| 34 public: | 33 public: |
| 35 WriterBridge(WebFileWriterImpl::Type type) | 34 WriterBridge() |
| 36 : request_id_(0), | 35 : request_id_(0), |
| 37 thread_id_(WorkerTaskRunner::Instance()->CurrentWorkerId()), | 36 thread_id_(WorkerTaskRunner::Instance()->CurrentWorkerId()) {} |
| 38 written_bytes_(0) { | |
| 39 if (type == WebFileWriterImpl::TYPE_SYNC) | |
| 40 waitable_event_.reset(new base::WaitableEvent(false, false)); | |
| 41 } | |
| 42 | 37 |
| 43 void Truncate(const GURL& path, int64 offset, | 38 void Truncate(const GURL& path, int64 offset, |
| 44 const StatusCallback& status_callback) { | 39 const StatusCallback& status_callback) { |
| 45 status_callback_ = status_callback; | 40 status_callback_ = status_callback; |
| 46 if (!GetFileSystemDispatcher()) | 41 if (!GetFileSystemDispatcher()) |
| 47 return; | 42 return; |
| 48 ChildThread::current()->file_system_dispatcher()->Truncate( | 43 ChildThread::current()->file_system_dispatcher()->Truncate( |
| 49 path, offset, &request_id_, | 44 path, offset, &request_id_, |
| 50 base::Bind(&WriterBridge::DidFinish, this)); | 45 base::Bind(&WriterBridge::DidFinish, this)); |
| 51 } | 46 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 65 | 60 |
| 66 void Cancel(const StatusCallback& status_callback) { | 61 void Cancel(const StatusCallback& status_callback) { |
| 67 status_callback_ = status_callback; | 62 status_callback_ = status_callback; |
| 68 if (!GetFileSystemDispatcher()) | 63 if (!GetFileSystemDispatcher()) |
| 69 return; | 64 return; |
| 70 ChildThread::current()->file_system_dispatcher()->Cancel( | 65 ChildThread::current()->file_system_dispatcher()->Cancel( |
| 71 request_id_, | 66 request_id_, |
| 72 base::Bind(&WriterBridge::DidFinish, this)); | 67 base::Bind(&WriterBridge::DidFinish, this)); |
| 73 } | 68 } |
| 74 | 69 |
| 75 base::WaitableEvent* waitable_event() { | |
| 76 return waitable_event_.get(); | |
| 77 } | |
| 78 | |
| 79 void WaitAndRun() { | |
| 80 waitable_event_->Wait(); | |
| 81 DCHECK(!results_closure_.is_null()); | |
| 82 results_closure_.Run(); | |
| 83 written_bytes_ = 0; | |
| 84 } | |
| 85 | |
| 86 private: | 70 private: |
| 87 friend class base::RefCountedThreadSafe<WriterBridge>; | 71 friend class base::RefCountedThreadSafe<WriterBridge>; |
| 88 virtual ~WriterBridge() {} | 72 virtual ~WriterBridge() {} |
| 89 | 73 |
| 90 void DidWrite(int64 bytes, bool complete) { | 74 void DidWrite(int64 bytes, bool complete) { |
| 91 written_bytes_ += bytes; | 75 PostTaskToWorker(base::Bind(write_callback_, bytes, complete)); |
| 92 if (waitable_event_ && !complete) | |
| 93 return; | |
| 94 PostTaskToWorker(base::Bind(write_callback_, written_bytes_, complete)); | |
| 95 } | 76 } |
| 96 | 77 |
| 97 void DidFinish(base::PlatformFileError status) { | 78 void DidFinish(base::PlatformFileError status) { |
| 98 PostTaskToWorker(base::Bind(status_callback_, status)); | 79 PostTaskToWorker(base::Bind(status_callback_, status)); |
| 99 } | 80 } |
| 100 | 81 |
| 101 void PostTaskToWorker(const base::Closure& closure) { | 82 void PostTaskToWorker(const base::Closure& closure) { |
| 102 if (!thread_id_) { | 83 if (!thread_id_) |
| 103 DCHECK(!waitable_event_); | |
| 104 closure.Run(); | 84 closure.Run(); |
| 105 return; | 85 else |
| 106 } | 86 WorkerTaskRunner::Instance()->PostTask(thread_id_, closure); |
| 107 if (waitable_event_) { | |
| 108 results_closure_ = closure; | |
| 109 waitable_event_->Signal(); | |
| 110 return; | |
| 111 } | |
| 112 WorkerTaskRunner::Instance()->PostTask(thread_id_, closure); | |
| 113 written_bytes_ = 0; | |
| 114 } | 87 } |
| 115 | 88 |
| 116 StatusCallback status_callback_; | 89 StatusCallback status_callback_; |
| 117 WriteCallback write_callback_; | 90 WriteCallback write_callback_; |
| 118 int request_id_; | 91 int request_id_; |
| 119 int thread_id_; | 92 int thread_id_; |
| 120 int written_bytes_; | |
| 121 scoped_ptr<base::WaitableEvent> waitable_event_; | |
| 122 base::Closure results_closure_; | |
| 123 }; | 93 }; |
| 124 | 94 |
| 125 WebFileWriterImpl::WebFileWriterImpl( | 95 WebFileWriterImpl::WebFileWriterImpl( |
| 126 const GURL& path, WebKit::WebFileWriterClient* client, | 96 const GURL& path, WebKit::WebFileWriterClient* client, |
| 127 Type type, | |
| 128 base::MessageLoopProxy* main_thread_loop) | 97 base::MessageLoopProxy* main_thread_loop) |
| 129 : WebFileWriterBase(path, client), | 98 : WebFileWriterBase(path, client), |
| 130 main_thread_loop_(main_thread_loop), | 99 main_thread_loop_(main_thread_loop), |
| 131 bridge_(new WriterBridge(type)) { | 100 bridge_(new WriterBridge) { |
| 132 } | 101 } |
| 133 | 102 |
| 134 WebFileWriterImpl::~WebFileWriterImpl() { | 103 WebFileWriterImpl::~WebFileWriterImpl() { |
| 135 } | 104 } |
| 136 | 105 |
| 137 void WebFileWriterImpl::DoTruncate(const GURL& path, int64 offset) { | 106 void WebFileWriterImpl::DoTruncate(const GURL& path, int64 offset) { |
| 138 RunOnMainThread(base::Bind(&WriterBridge::Truncate, bridge_, | 107 RunOnMainThread(base::Bind(&WriterBridge::Truncate, bridge_, |
| 139 path, offset, | 108 path, offset, |
| 140 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr()))); | 109 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr()))); |
| 141 } | 110 } |
| 142 | 111 |
| 143 void WebFileWriterImpl::DoWrite( | 112 void WebFileWriterImpl::DoWrite( |
| 144 const GURL& path, const GURL& blob_url, int64 offset) { | 113 const GURL& path, const GURL& blob_url, int64 offset) { |
| 145 RunOnMainThread(base::Bind(&WriterBridge::Write, bridge_, | 114 RunOnMainThread(base::Bind(&WriterBridge::Write, bridge_, |
| 146 path, blob_url, offset, | 115 path, blob_url, offset, |
| 147 base::Bind(&WebFileWriterImpl::DidWrite, AsWeakPtr()), | 116 base::Bind(&WebFileWriterImpl::DidWrite, AsWeakPtr()), |
| 148 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr()))); | 117 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr()))); |
| 149 } | 118 } |
| 150 | 119 |
| 151 void WebFileWriterImpl::DoCancel() { | 120 void WebFileWriterImpl::DoCancel() { |
| 152 RunOnMainThread(base::Bind(&WriterBridge::Cancel, bridge_, | 121 RunOnMainThread(base::Bind(&WriterBridge::Cancel, bridge_, |
| 153 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr()))); | 122 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr()))); |
| 154 } | 123 } |
| 155 | 124 |
| 156 void WebFileWriterImpl::RunOnMainThread(const base::Closure& closure) { | 125 void WebFileWriterImpl::RunOnMainThread(const base::Closure& closure) { |
| 157 if (main_thread_loop_->RunsTasksOnCurrentThread()) { | 126 if (main_thread_loop_->RunsTasksOnCurrentThread()) |
| 158 DCHECK(!bridge_->waitable_event()); | |
| 159 closure.Run(); | 127 closure.Run(); |
| 160 return; | 128 else |
| 161 } | 129 main_thread_loop_->PostTask(FROM_HERE, closure); |
| 162 main_thread_loop_->PostTask(FROM_HERE, closure); | |
| 163 if (bridge_->waitable_event()) | |
| 164 bridge_->WaitAndRun(); | |
| 165 } | 130 } |
| 166 | 131 |
| 167 } // namespace content | 132 } // namespace content |
| OLD | NEW |