OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/gdata/gdata_file_writing.h" |
| 6 |
| 7 #include "base/threading/sequenced_worker_pool.h" |
| 8 #include "content/public/browser/browser_thread.h" |
| 9 |
| 10 using content::BrowserThread; |
| 11 |
| 12 namespace gdata { |
| 13 |
| 14 GDataFileWriting::GDataFileWriting(GDataFileSystemInterface* file_system) |
| 15 : file_system_(file_system), |
| 16 ui_weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 17 ui_weak_ptr_(ui_weak_ptr_factory_.GetWeakPtr()) { |
| 18 // Must be created in GDataSystemService. |
| 19 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 20 } |
| 21 |
| 22 GDataFileWriting::~GDataFileWriting() { |
| 23 // Must be destroyed in GDataSystemService. |
| 24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 25 } |
| 26 |
| 27 void GDataFileWriting::PrepareWritableFileAndRun( |
| 28 const FilePath& file_path, |
| 29 const OpenFileCallback& callback) { |
| 30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 31 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 32 |
| 33 // TODO(kinaba): factor out RunTaskOnThread family from gdata_file_system.cc |
| 34 // and use it. |
| 35 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 36 PrepareWritableFileAndRunOnUIThread(file_path, callback); |
| 37 } else { |
| 38 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)->PostTask( |
| 39 FROM_HERE, |
| 40 base::Bind(&GDataFileWriting::PrepareWritableFileAndRunOnUIThread, |
| 41 ui_weak_ptr_, |
| 42 file_path, |
| 43 callback)); |
| 44 } |
| 45 } |
| 46 |
| 47 void GDataFileWriting::PrepareWritableFileAndRunOnUIThread( |
| 48 const FilePath& file_path, |
| 49 const OpenFileCallback& callback) { |
| 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 51 |
| 52 file_system_->CreateFile( |
| 53 file_path, |
| 54 false, // it is not an error, even if the path already exists. |
| 55 base::Bind(&GDataFileWriting::PrepareWritableFileAndRunAfterCreateFile, |
| 56 ui_weak_ptr_, |
| 57 file_path, |
| 58 callback)); |
| 59 } |
| 60 |
| 61 void GDataFileWriting::PrepareWritableFileAndRunAfterCreateFile( |
| 62 const FilePath& file_path, |
| 63 const OpenFileCallback& callback, |
| 64 GDataFileError error) { |
| 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 66 |
| 67 if (error != gdata::GDATA_FILE_OK) { |
| 68 if (!callback.is_null()) { |
| 69 content::BrowserThread::GetBlockingPool()->PostTask( |
| 70 FROM_HERE, |
| 71 base::Bind(callback, error, FilePath())); |
| 72 } |
| 73 return; |
| 74 } |
| 75 file_system_->OpenFile( |
| 76 file_path, |
| 77 base::Bind(&GDataFileWriting::PrepareWritableFileAndRunAfterOpenFile, |
| 78 ui_weak_ptr_, |
| 79 file_path, |
| 80 callback)); |
| 81 } |
| 82 |
| 83 void GDataFileWriting::PrepareWritableFileAndRunAfterOpenFile( |
| 84 const FilePath& file_path, |
| 85 const OpenFileCallback& callback, |
| 86 GDataFileError error, |
| 87 const FilePath& local_cache_path) { |
| 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 89 |
| 90 if (error != gdata::GDATA_FILE_OK) { |
| 91 if (!callback.is_null()) { |
| 92 content::BrowserThread::GetBlockingPool()->PostTask( |
| 93 FROM_HERE, |
| 94 base::Bind(callback, error, FilePath())); |
| 95 } |
| 96 return; |
| 97 } |
| 98 |
| 99 if (!callback.is_null()) { |
| 100 content::BrowserThread::GetBlockingPool()->PostTaskAndReply( |
| 101 FROM_HERE, |
| 102 base::Bind(callback, GDATA_FILE_OK, local_cache_path), |
| 103 base::Bind(&GDataFileWriting::PrepareWritableFileAndRunAfterCallback, |
| 104 ui_weak_ptr_, |
| 105 file_path)); |
| 106 } else { |
| 107 PrepareWritableFileAndRunAfterCallback(file_path); |
| 108 } |
| 109 } |
| 110 |
| 111 void GDataFileWriting::PrepareWritableFileAndRunAfterCallback( |
| 112 const FilePath& file_path) { |
| 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 114 file_system_->CloseFile(file_path, FileOperationCallback()); |
| 115 } |
| 116 |
| 117 } // namespace gdata |
OLD | NEW |