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 "chrome/browser/chromeos/drive/webkit_file_stream_writer_impl.h" | 5 #include "chrome/browser/chromeos/drive/webkit_file_stream_writer_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
| 9 #include "chrome/browser/chromeos/drive/file_system_util.h" | 9 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 10 #include "chrome/browser/chromeos/drive/fileapi_worker.h" | 10 #include "chrome/browser/chromeos/drive/fileapi_worker.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 | 30 |
| 31 BrowserThread::PostTask( | 31 BrowserThread::PostTask( |
| 32 BrowserThread::UI, | 32 BrowserThread::UI, |
| 33 FROM_HERE, | 33 FROM_HERE, |
| 34 base::Bind( | 34 base::Bind( |
| 35 &fileapi_internal::RunFileSystemCallback, | 35 &fileapi_internal::RunFileSystemCallback, |
| 36 file_system_getter, | 36 file_system_getter, |
| 37 base::Bind(&fileapi_internal::CreateWritableSnapshotFile, | 37 base::Bind(&fileapi_internal::CreateWritableSnapshotFile, |
| 38 drive_path, google_apis::CreateRelayCallback(callback)), | 38 drive_path, google_apis::CreateRelayCallback(callback)), |
| 39 google_apis::CreateRelayCallback(base::Bind( | 39 google_apis::CreateRelayCallback(base::Bind( |
| 40 callback, base::PLATFORM_FILE_ERROR_FAILED, base::FilePath())))); | 40 callback, base::PLATFORM_FILE_ERROR_FAILED, base::FilePath(), |
| 41 } | 41 base::Closure())))); |
| 42 | |
| 43 // Closes the writable snapshot file opened by CreateWritableSnapshotFile. | |
| 44 // TODO(hidehiko): Get rid of this function. crbug.com/259184. | |
| 45 void CloseFile( | |
| 46 const WebkitFileStreamWriterImpl::FileSystemGetter& file_system_getter, | |
| 47 const base::FilePath& drive_path) { | |
| 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 49 | |
| 50 BrowserThread::PostTask( | |
| 51 BrowserThread::UI, | |
| 52 FROM_HERE, | |
| 53 base::Bind(&fileapi_internal::RunFileSystemCallback, | |
| 54 file_system_getter, | |
| 55 base::Bind(&fileapi_internal::CloseFile, drive_path), | |
| 56 base::Closure())); | |
| 57 } | 42 } |
| 58 | 43 |
| 59 } // namespace | 44 } // namespace |
| 60 | 45 |
| 61 WebkitFileStreamWriterImpl::WebkitFileStreamWriterImpl( | 46 WebkitFileStreamWriterImpl::WebkitFileStreamWriterImpl( |
| 62 const FileSystemGetter& file_system_getter, | 47 const FileSystemGetter& file_system_getter, |
| 63 base::TaskRunner* file_task_runner, | 48 base::TaskRunner* file_task_runner, |
| 64 const base::FilePath& file_path, | 49 const base::FilePath& file_path, |
| 65 int64 offset) | 50 int64 offset) |
| 66 : file_system_getter_(file_system_getter), | 51 : file_system_getter_(file_system_getter), |
| 67 file_task_runner_(file_task_runner), | 52 file_task_runner_(file_task_runner), |
| 68 file_path_(file_path), | 53 file_path_(file_path), |
| 69 offset_(offset), | 54 offset_(offset), |
| 70 weak_ptr_factory_(this) { | 55 weak_ptr_factory_(this) { |
| 71 } | 56 } |
| 72 | 57 |
| 73 WebkitFileStreamWriterImpl::~WebkitFileStreamWriterImpl() { | 58 WebkitFileStreamWriterImpl::~WebkitFileStreamWriterImpl() { |
| 74 if (local_file_writer_) { | 59 if (local_file_writer_) { |
| 75 // If the file is opened, close it at destructor. | 60 // If the file is opened, close it at destructor. |
| 76 // It is necessary to close the local file in advance. | 61 // It is necessary to close the local file in advance. |
| 77 local_file_writer_.reset(); | 62 local_file_writer_.reset(); |
|
hashimoto
2013/07/23 11:24:58
nit: How about DCHECK(on_close_file_callback_on_ui
hidehiko
2013/07/23 14:32:24
Done.
| |
| 78 CloseFile(file_system_getter_, file_path_); | 63 BrowserThread::PostTask(BrowserThread::UI, |
| 64 FROM_HERE, | |
| 65 on_close_file_callback_on_ui_thread_); | |
| 79 } | 66 } |
| 80 } | 67 } |
| 81 | 68 |
| 82 int WebkitFileStreamWriterImpl::Write(net::IOBuffer* buf, | 69 int WebkitFileStreamWriterImpl::Write(net::IOBuffer* buf, |
| 83 int buf_len, | 70 int buf_len, |
| 84 const net::CompletionCallback& callback) { | 71 const net::CompletionCallback& callback) { |
| 85 DCHECK(pending_write_callback_.is_null()); | 72 DCHECK(pending_write_callback_.is_null()); |
| 86 DCHECK(pending_cancel_callback_.is_null()); | 73 DCHECK(pending_cancel_callback_.is_null()); |
| 87 DCHECK(!callback.is_null()); | 74 DCHECK(!callback.is_null()); |
| 88 | 75 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 // Here is the case Flush() is called before any Write() invocation. | 126 // Here is the case Flush() is called before any Write() invocation. |
| 140 // Do nothing. | 127 // Do nothing. |
| 141 // Synchronization to the remote server is not done until the file is closed. | 128 // Synchronization to the remote server is not done until the file is closed. |
| 142 return net::OK; | 129 return net::OK; |
| 143 } | 130 } |
| 144 | 131 |
| 145 void WebkitFileStreamWriterImpl::WriteAfterCreateWritableSnapshotFile( | 132 void WebkitFileStreamWriterImpl::WriteAfterCreateWritableSnapshotFile( |
| 146 net::IOBuffer* buf, | 133 net::IOBuffer* buf, |
| 147 int buf_len, | 134 int buf_len, |
| 148 base::PlatformFileError open_result, | 135 base::PlatformFileError open_result, |
| 149 const base::FilePath& local_path) { | 136 const base::FilePath& local_path, |
| 137 const base::Closure& on_close_file_callback_on_ui_thread) { | |
| 150 DCHECK(!local_file_writer_); | 138 DCHECK(!local_file_writer_); |
| 151 | 139 |
| 152 if (!pending_cancel_callback_.is_null()) { | 140 if (!pending_cancel_callback_.is_null()) { |
| 153 DCHECK(pending_write_callback_.is_null()); | 141 DCHECK(pending_write_callback_.is_null()); |
| 154 // Cancel() is called during the creation of the snapshot file. | 142 // Cancel() is called during the creation of the snapshot file. |
| 155 // Don't write to the file. | 143 // Don't write to the file. |
| 156 if (open_result == base::PLATFORM_FILE_OK) { | 144 if (open_result == base::PLATFORM_FILE_OK) { |
| 157 // Here the file is internally created. To revert the operation, close | 145 // Here the file is internally created. To revert the operation, close |
| 158 // the file. | 146 // the file. |
| 159 DCHECK(!local_path.empty()); | 147 DCHECK(!on_close_file_callback_on_ui_thread.is_null()); |
| 160 CloseFile(file_system_getter_, file_path_); | 148 BrowserThread::PostTask(BrowserThread::UI, |
| 149 FROM_HERE, | |
| 150 on_close_file_callback_on_ui_thread); | |
| 161 } | 151 } |
| 162 | 152 |
| 163 base::ResetAndReturn(&pending_cancel_callback_).Run(net::OK); | 153 base::ResetAndReturn(&pending_cancel_callback_).Run(net::OK); |
| 164 return; | 154 return; |
| 165 } | 155 } |
| 166 | 156 |
| 167 DCHECK(!pending_write_callback_.is_null()); | 157 DCHECK(!pending_write_callback_.is_null()); |
| 168 | 158 |
| 169 const net::CompletionCallback callback = | 159 const net::CompletionCallback callback = |
| 170 base::ResetAndReturn(&pending_write_callback_); | 160 base::ResetAndReturn(&pending_write_callback_); |
| 171 if (open_result != base::PLATFORM_FILE_OK) { | 161 if (open_result != base::PLATFORM_FILE_OK) { |
| 162 DCHECK(on_close_file_callback_on_ui_thread.is_null()); | |
| 172 callback.Run(net::PlatformFileErrorToNetError(open_result)); | 163 callback.Run(net::PlatformFileErrorToNetError(open_result)); |
| 173 return; | 164 return; |
| 174 } | 165 } |
| 175 | 166 |
| 167 // Keep |on_close_callback| to close the file when the stream is destructed. | |
| 168 DCHECK(!on_close_file_callback_on_ui_thread.is_null()); | |
| 169 on_close_file_callback_on_ui_thread_ = on_close_file_callback_on_ui_thread; | |
| 176 local_file_writer_.reset(new fileapi::LocalFileStreamWriter( | 170 local_file_writer_.reset(new fileapi::LocalFileStreamWriter( |
| 177 file_task_runner_.get(), local_path, offset_)); | 171 file_task_runner_.get(), local_path, offset_)); |
| 178 int result = local_file_writer_->Write(buf, buf_len, callback); | 172 int result = local_file_writer_->Write(buf, buf_len, callback); |
| 179 if (result != net::ERR_IO_PENDING) | 173 if (result != net::ERR_IO_PENDING) |
| 180 callback.Run(result); | 174 callback.Run(result); |
| 181 } | 175 } |
| 182 | 176 |
| 183 } // namespace internal | 177 } // namespace internal |
| 184 } // namespace drive | 178 } // namespace drive |
| OLD | NEW |