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 "base/thread_task_runner_handle.h" | 9 #include "base/thread_task_runner_handle.h" |
10 #include "content/child/child_thread_impl.h" | 10 #include "content/child/child_thread_impl.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 WriterBridge(WebFileWriterImpl::Type type) | 33 WriterBridge(WebFileWriterImpl::Type type) |
34 : request_id_(0), | 34 : request_id_(0), |
35 running_on_worker_(WorkerThread::GetCurrentId() > 0), | 35 running_on_worker_(WorkerThread::GetCurrentId() > 0), |
36 task_runner_(running_on_worker_ ? base::ThreadTaskRunnerHandle::Get() | 36 task_runner_(running_on_worker_ ? base::ThreadTaskRunnerHandle::Get() |
37 : nullptr), | 37 : nullptr), |
38 written_bytes_(0) { | 38 written_bytes_(0) { |
39 if (type == WebFileWriterImpl::TYPE_SYNC) | 39 if (type == WebFileWriterImpl::TYPE_SYNC) |
40 waitable_event_.reset(new base::WaitableEvent(false, false)); | 40 waitable_event_.reset(new base::WaitableEvent(false, false)); |
41 } | 41 } |
42 | 42 |
43 void Truncate(const GURL& path, int64 offset, | 43 void Truncate(const GURL& path, |
| 44 int64_t offset, |
44 const StatusCallback& status_callback) { | 45 const StatusCallback& status_callback) { |
45 status_callback_ = status_callback; | 46 status_callback_ = status_callback; |
46 if (!GetFileSystemDispatcher()) | 47 if (!GetFileSystemDispatcher()) |
47 return; | 48 return; |
48 ChildThreadImpl::current()->file_system_dispatcher()->Truncate( | 49 ChildThreadImpl::current()->file_system_dispatcher()->Truncate( |
49 path, offset, &request_id_, | 50 path, offset, &request_id_, |
50 base::Bind(&WriterBridge::DidFinish, this)); | 51 base::Bind(&WriterBridge::DidFinish, this)); |
51 } | 52 } |
52 | 53 |
53 void Write(const GURL& path, const std::string& id, int64 offset, | 54 void Write(const GURL& path, |
| 55 const std::string& id, |
| 56 int64_t offset, |
54 const WriteCallback& write_callback, | 57 const WriteCallback& write_callback, |
55 const StatusCallback& error_callback) { | 58 const StatusCallback& error_callback) { |
56 write_callback_ = write_callback; | 59 write_callback_ = write_callback; |
57 status_callback_ = error_callback; | 60 status_callback_ = error_callback; |
58 if (!GetFileSystemDispatcher()) | 61 if (!GetFileSystemDispatcher()) |
59 return; | 62 return; |
60 ChildThreadImpl::current()->file_system_dispatcher()->Write( | 63 ChildThreadImpl::current()->file_system_dispatcher()->Write( |
61 path, id, offset, &request_id_, | 64 path, id, offset, &request_id_, |
62 base::Bind(&WriterBridge::DidWrite, this), | 65 base::Bind(&WriterBridge::DidWrite, this), |
63 base::Bind(&WriterBridge::DidFinish, this)); | 66 base::Bind(&WriterBridge::DidFinish, this)); |
(...skipping 15 matching lines...) Expand all Loading... |
79 void WaitAndRun() { | 82 void WaitAndRun() { |
80 waitable_event_->Wait(); | 83 waitable_event_->Wait(); |
81 DCHECK(!results_closure_.is_null()); | 84 DCHECK(!results_closure_.is_null()); |
82 results_closure_.Run(); | 85 results_closure_.Run(); |
83 } | 86 } |
84 | 87 |
85 private: | 88 private: |
86 friend class base::RefCountedThreadSafe<WriterBridge>; | 89 friend class base::RefCountedThreadSafe<WriterBridge>; |
87 virtual ~WriterBridge() {} | 90 virtual ~WriterBridge() {} |
88 | 91 |
89 void DidWrite(int64 bytes, bool complete) { | 92 void DidWrite(int64_t bytes, bool complete) { |
90 written_bytes_ += bytes; | 93 written_bytes_ += bytes; |
91 if (waitable_event_ && !complete) | 94 if (waitable_event_ && !complete) |
92 return; | 95 return; |
93 PostTaskToWorker(base::Bind(write_callback_, written_bytes_, complete)); | 96 PostTaskToWorker(base::Bind(write_callback_, written_bytes_, complete)); |
94 } | 97 } |
95 | 98 |
96 void DidFinish(base::File::Error status) { | 99 void DidFinish(base::File::Error status) { |
97 PostTaskToWorker(base::Bind(status_callback_, status)); | 100 PostTaskToWorker(base::Bind(status_callback_, status)); |
98 } | 101 } |
99 | 102 |
(...skipping 28 matching lines...) Expand all Loading... |
128 Type type, | 131 Type type, |
129 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner) | 132 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner) |
130 : WebFileWriterBase(path, client), | 133 : WebFileWriterBase(path, client), |
131 main_thread_task_runner_(main_thread_task_runner), | 134 main_thread_task_runner_(main_thread_task_runner), |
132 bridge_(new WriterBridge(type)) { | 135 bridge_(new WriterBridge(type)) { |
133 } | 136 } |
134 | 137 |
135 WebFileWriterImpl::~WebFileWriterImpl() { | 138 WebFileWriterImpl::~WebFileWriterImpl() { |
136 } | 139 } |
137 | 140 |
138 void WebFileWriterImpl::DoTruncate(const GURL& path, int64 offset) { | 141 void WebFileWriterImpl::DoTruncate(const GURL& path, int64_t offset) { |
139 RunOnMainThread(base::Bind(&WriterBridge::Truncate, bridge_, | 142 RunOnMainThread(base::Bind(&WriterBridge::Truncate, bridge_, |
140 path, offset, | 143 path, offset, |
141 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr()))); | 144 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr()))); |
142 } | 145 } |
143 | 146 |
144 void WebFileWriterImpl::DoWrite( | 147 void WebFileWriterImpl::DoWrite(const GURL& path, |
145 const GURL& path, const std::string& blob_id, int64 offset) { | 148 const std::string& blob_id, |
| 149 int64_t offset) { |
146 RunOnMainThread(base::Bind(&WriterBridge::Write, bridge_, | 150 RunOnMainThread(base::Bind(&WriterBridge::Write, bridge_, |
147 path, blob_id, offset, | 151 path, blob_id, offset, |
148 base::Bind(&WebFileWriterImpl::DidWrite, AsWeakPtr()), | 152 base::Bind(&WebFileWriterImpl::DidWrite, AsWeakPtr()), |
149 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr()))); | 153 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr()))); |
150 } | 154 } |
151 | 155 |
152 void WebFileWriterImpl::DoCancel() { | 156 void WebFileWriterImpl::DoCancel() { |
153 RunOnMainThread(base::Bind(&WriterBridge::Cancel, bridge_, | 157 RunOnMainThread(base::Bind(&WriterBridge::Cancel, bridge_, |
154 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr()))); | 158 base::Bind(&WebFileWriterImpl::DidFinish, AsWeakPtr()))); |
155 } | 159 } |
156 | 160 |
157 void WebFileWriterImpl::RunOnMainThread(const base::Closure& closure) { | 161 void WebFileWriterImpl::RunOnMainThread(const base::Closure& closure) { |
158 if (main_thread_task_runner_->RunsTasksOnCurrentThread()) { | 162 if (main_thread_task_runner_->RunsTasksOnCurrentThread()) { |
159 DCHECK(!bridge_->waitable_event()); | 163 DCHECK(!bridge_->waitable_event()); |
160 closure.Run(); | 164 closure.Run(); |
161 return; | 165 return; |
162 } | 166 } |
163 main_thread_task_runner_->PostTask(FROM_HERE, closure); | 167 main_thread_task_runner_->PostTask(FROM_HERE, closure); |
164 if (bridge_->waitable_event()) | 168 if (bridge_->waitable_event()) |
165 bridge_->WaitAndRun(); | 169 bridge_->WaitAndRun(); |
166 } | 170 } |
167 | 171 |
168 } // namespace content | 172 } // namespace content |
OLD | NEW |