| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/file_write_helper.h" | 5 #include "chrome/browser/chromeos/drive/file_write_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
| 7 #include "base/threading/sequenced_worker_pool.h" | 9 #include "base/threading/sequenced_worker_pool.h" |
| 10 #include "chrome/browser/chromeos/drive/file_system_interface.h" |
| 8 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 9 | 12 |
| 10 using content::BrowserThread; | 13 using content::BrowserThread; |
| 11 | 14 |
| 12 namespace drive { | 15 namespace drive { |
| 13 | 16 |
| 14 namespace { | |
| 15 | |
| 16 // Emits debug log when FileSystem::CloseFile() is complete. | |
| 17 void EmitDebugLogForCloseFile(const base::FilePath& file_path, | |
| 18 FileError file_error) { | |
| 19 if (file_error != FILE_ERROR_OK) { | |
| 20 LOG(WARNING) << "CloseFile failed: " << file_path.AsUTF8Unsafe() << ": " | |
| 21 << file_error; | |
| 22 } | |
| 23 } | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 FileWriteHelper::FileWriteHelper(FileSystemInterface* file_system) | 17 FileWriteHelper::FileWriteHelper(FileSystemInterface* file_system) |
| 28 : file_system_(file_system), | 18 : file_system_(file_system), |
| 29 weak_ptr_factory_(this) { | 19 weak_ptr_factory_(this) { |
| 30 // Must be created in DriveIntegrationService. | 20 // Must be created in DriveIntegrationService. |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 21 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 32 } | 22 } |
| 33 | 23 |
| 34 FileWriteHelper::~FileWriteHelper() { | 24 FileWriteHelper::~FileWriteHelper() { |
| 35 // Must be destroyed in DriveIntegrationService. | 25 // Must be destroyed in DriveIntegrationService. |
| 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 37 } | 27 } |
| 38 | 28 |
| 39 void FileWriteHelper::PrepareWritableFileAndRun( | 29 void FileWriteHelper::PrepareWritableFileAndRun( |
| 40 const base::FilePath& file_path, | 30 const base::FilePath& file_path, |
| 41 const OpenFileCallback& callback) { | 31 const OpenFileCallback& callback) { |
| 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 43 DCHECK(!callback.is_null()); | 33 DCHECK(!callback.is_null()); |
| 44 | 34 |
| 45 file_system_->OpenFile( | 35 file_system_->OpenFile( |
| 46 file_path, OPEN_OR_CREATE_FILE, | 36 file_path, OPEN_OR_CREATE_FILE, |
| 47 base::Bind(&FileWriteHelper::PrepareWritableFileAndRunAfterOpenFile, | 37 base::Bind(&FileWriteHelper::PrepareWritableFileAndRunAfterOpenFile, |
| 48 weak_ptr_factory_.GetWeakPtr(), file_path, callback)); | 38 weak_ptr_factory_.GetWeakPtr(), file_path, callback)); |
| 49 } | 39 } |
| 50 | 40 |
| 51 void FileWriteHelper::PrepareWritableFileAndRunAfterOpenFile( | 41 void FileWriteHelper::PrepareWritableFileAndRunAfterOpenFile( |
| 52 const base::FilePath& file_path, | 42 const base::FilePath& file_path, |
| 53 const OpenFileCallback& callback, | 43 const OpenFileCallback& callback, |
| 54 FileError error, | 44 FileError error, |
| 55 const base::FilePath& local_cache_path) { | 45 const base::FilePath& local_cache_path, |
| 46 const base::Closure& on_close_callback) { |
| 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 57 DCHECK(!callback.is_null()); | 48 DCHECK(!callback.is_null()); |
| 58 | 49 |
| 59 if (error != FILE_ERROR_OK) { | 50 if (error != FILE_ERROR_OK) { |
| 60 content::BrowserThread::GetBlockingPool()->PostTask( | 51 content::BrowserThread::GetBlockingPool()->PostTask( |
| 61 FROM_HERE, | 52 FROM_HERE, |
| 62 base::Bind(callback, error, base::FilePath())); | 53 base::Bind(callback, error, base::FilePath())); |
| 63 return; | 54 return; |
| 64 } | 55 } |
| 65 | 56 |
| 66 content::BrowserThread::GetBlockingPool()->PostTaskAndReply( | 57 content::BrowserThread::GetBlockingPool()->PostTaskAndReply( |
| 67 FROM_HERE, | 58 FROM_HERE, |
| 68 base::Bind(callback, FILE_ERROR_OK, local_cache_path), | 59 base::Bind(callback, FILE_ERROR_OK, local_cache_path), |
| 69 base::Bind(&FileWriteHelper::PrepareWritableFileAndRunAfterCallback, | 60 on_close_callback); |
| 70 weak_ptr_factory_.GetWeakPtr(), | |
| 71 file_path)); | |
| 72 } | |
| 73 | |
| 74 void FileWriteHelper::PrepareWritableFileAndRunAfterCallback( | |
| 75 const base::FilePath& file_path) { | |
| 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 77 file_system_->CloseFile(file_path, | |
| 78 base::Bind(&EmitDebugLogForCloseFile, file_path)); | |
| 79 } | 61 } |
| 80 | 62 |
| 81 } // namespace drive | 63 } // namespace drive |
| OLD | NEW |