| 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(); |
| 78 CloseFile(file_system_getter_, file_path_); | 63 DCHECK(!on_close_file_callback_on_ui_thread_.is_null()); |
| 64 BrowserThread::PostTask(BrowserThread::UI, |
| 65 FROM_HERE, |
| 66 on_close_file_callback_on_ui_thread_); |
| 79 } | 67 } |
| 80 } | 68 } |
| 81 | 69 |
| 82 int WebkitFileStreamWriterImpl::Write(net::IOBuffer* buf, | 70 int WebkitFileStreamWriterImpl::Write(net::IOBuffer* buf, |
| 83 int buf_len, | 71 int buf_len, |
| 84 const net::CompletionCallback& callback) { | 72 const net::CompletionCallback& callback) { |
| 85 DCHECK(pending_write_callback_.is_null()); | 73 DCHECK(pending_write_callback_.is_null()); |
| 86 DCHECK(pending_cancel_callback_.is_null()); | 74 DCHECK(pending_cancel_callback_.is_null()); |
| 87 DCHECK(!callback.is_null()); | 75 DCHECK(!callback.is_null()); |
| 88 | 76 |
| (...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. | 127 // Here is the case Flush() is called before any Write() invocation. |
| 140 // Do nothing. | 128 // Do nothing. |
| 141 // Synchronization to the remote server is not done until the file is closed. | 129 // Synchronization to the remote server is not done until the file is closed. |
| 142 return net::OK; | 130 return net::OK; |
| 143 } | 131 } |
| 144 | 132 |
| 145 void WebkitFileStreamWriterImpl::WriteAfterCreateWritableSnapshotFile( | 133 void WebkitFileStreamWriterImpl::WriteAfterCreateWritableSnapshotFile( |
| 146 net::IOBuffer* buf, | 134 net::IOBuffer* buf, |
| 147 int buf_len, | 135 int buf_len, |
| 148 base::PlatformFileError open_result, | 136 base::PlatformFileError open_result, |
| 149 const base::FilePath& local_path) { | 137 const base::FilePath& local_path, |
| 138 const base::Closure& on_close_file_callback_on_ui_thread) { |
| 150 DCHECK(!local_file_writer_); | 139 DCHECK(!local_file_writer_); |
| 151 | 140 |
| 152 if (!pending_cancel_callback_.is_null()) { | 141 if (!pending_cancel_callback_.is_null()) { |
| 153 DCHECK(pending_write_callback_.is_null()); | 142 DCHECK(pending_write_callback_.is_null()); |
| 154 // Cancel() is called during the creation of the snapshot file. | 143 // Cancel() is called during the creation of the snapshot file. |
| 155 // Don't write to the file. | 144 // Don't write to the file. |
| 156 if (open_result == base::PLATFORM_FILE_OK) { | 145 if (open_result == base::PLATFORM_FILE_OK) { |
| 157 // Here the file is internally created. To revert the operation, close | 146 // Here the file is internally created. To revert the operation, close |
| 158 // the file. | 147 // the file. |
| 159 DCHECK(!local_path.empty()); | 148 DCHECK(!on_close_file_callback_on_ui_thread.is_null()); |
| 160 CloseFile(file_system_getter_, file_path_); | 149 BrowserThread::PostTask(BrowserThread::UI, |
| 150 FROM_HERE, |
| 151 on_close_file_callback_on_ui_thread); |
| 161 } | 152 } |
| 162 | 153 |
| 163 base::ResetAndReturn(&pending_cancel_callback_).Run(net::OK); | 154 base::ResetAndReturn(&pending_cancel_callback_).Run(net::OK); |
| 164 return; | 155 return; |
| 165 } | 156 } |
| 166 | 157 |
| 167 DCHECK(!pending_write_callback_.is_null()); | 158 DCHECK(!pending_write_callback_.is_null()); |
| 168 | 159 |
| 169 const net::CompletionCallback callback = | 160 const net::CompletionCallback callback = |
| 170 base::ResetAndReturn(&pending_write_callback_); | 161 base::ResetAndReturn(&pending_write_callback_); |
| 171 if (open_result != base::PLATFORM_FILE_OK) { | 162 if (open_result != base::PLATFORM_FILE_OK) { |
| 163 DCHECK(on_close_file_callback_on_ui_thread.is_null()); |
| 172 callback.Run(net::PlatformFileErrorToNetError(open_result)); | 164 callback.Run(net::PlatformFileErrorToNetError(open_result)); |
| 173 return; | 165 return; |
| 174 } | 166 } |
| 175 | 167 |
| 168 // Keep |on_close_callback| to close the file when the stream is destructed. |
| 169 DCHECK(!on_close_file_callback_on_ui_thread.is_null()); |
| 170 on_close_file_callback_on_ui_thread_ = on_close_file_callback_on_ui_thread; |
| 176 local_file_writer_.reset(new fileapi::LocalFileStreamWriter( | 171 local_file_writer_.reset(new fileapi::LocalFileStreamWriter( |
| 177 file_task_runner_.get(), local_path, offset_)); | 172 file_task_runner_.get(), local_path, offset_)); |
| 178 int result = local_file_writer_->Write(buf, buf_len, callback); | 173 int result = local_file_writer_->Write(buf, buf_len, callback); |
| 179 if (result != net::ERR_IO_PENDING) | 174 if (result != net::ERR_IO_PENDING) |
| 180 callback.Run(result); | 175 callback.Run(result); |
| 181 } | 176 } |
| 182 | 177 |
| 183 } // namespace internal | 178 } // namespace internal |
| 184 } // namespace drive | 179 } // namespace drive |
| OLD | NEW |