Chromium Code Reviews| 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_write_helper.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 GDataFileWriteHelper::GDataFileWriteHelper( | |
| 15 GDataFileSystemInterface* file_system) | |
| 16 : file_system_(file_system), | |
| 17 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 18 // Must be created in GDataSystemService. | |
| 19 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 20 } | |
| 21 | |
| 22 GDataFileWriteHelper::~GDataFileWriteHelper() { | |
| 23 // Must be destroyed in GDataSystemService. | |
| 24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 25 } | |
| 26 | |
| 27 void GDataFileWriteHelper::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(&GDataFileWriteHelper::PrepareWritableFileAndRunOnUIThread, | |
| 41 weak_ptr_factory_.GetWeakPtr(), | |
|
satorux1
2012/08/02 06:56:53
ah, I was wrong. If you are to post a task to UI t
satorux1
2012/08/02 06:59:16
Another option. As of of now, there is only one ca
kinaba
2012/08/02 07:22:39
Took the latter option.
I'll soon add another clie
| |
| 42 file_path, | |
| 43 callback)); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 void GDataFileWriteHelper::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( | |
| 56 &GDataFileWriteHelper::PrepareWritableFileAndRunAfterCreateFile, | |
| 57 weak_ptr_factory_.GetWeakPtr(), | |
| 58 file_path, | |
| 59 callback)); | |
| 60 } | |
| 61 | |
| 62 void GDataFileWriteHelper::PrepareWritableFileAndRunAfterCreateFile( | |
| 63 const FilePath& file_path, | |
| 64 const OpenFileCallback& callback, | |
| 65 GDataFileError error) { | |
| 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 67 | |
| 68 if (error != gdata::GDATA_FILE_OK) { | |
| 69 if (!callback.is_null()) { | |
| 70 content::BrowserThread::GetBlockingPool()->PostTask( | |
| 71 FROM_HERE, | |
| 72 base::Bind(callback, error, FilePath())); | |
| 73 } | |
| 74 return; | |
| 75 } | |
| 76 file_system_->OpenFile( | |
| 77 file_path, | |
| 78 base::Bind(&GDataFileWriteHelper::PrepareWritableFileAndRunAfterOpenFile, | |
| 79 weak_ptr_factory_.GetWeakPtr(), | |
| 80 file_path, | |
| 81 callback)); | |
| 82 } | |
| 83 | |
| 84 void GDataFileWriteHelper::PrepareWritableFileAndRunAfterOpenFile( | |
| 85 const FilePath& file_path, | |
| 86 const OpenFileCallback& callback, | |
| 87 GDataFileError error, | |
| 88 const FilePath& local_cache_path) { | |
| 89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 90 | |
| 91 if (error != gdata::GDATA_FILE_OK) { | |
| 92 if (!callback.is_null()) { | |
| 93 content::BrowserThread::GetBlockingPool()->PostTask( | |
| 94 FROM_HERE, | |
| 95 base::Bind(callback, error, FilePath())); | |
| 96 } | |
| 97 return; | |
| 98 } | |
| 99 | |
| 100 if (!callback.is_null()) { | |
| 101 content::BrowserThread::GetBlockingPool()->PostTaskAndReply( | |
| 102 FROM_HERE, | |
| 103 base::Bind(callback, GDATA_FILE_OK, local_cache_path), | |
| 104 base::Bind( | |
| 105 &GDataFileWriteHelper::PrepareWritableFileAndRunAfterCallback, | |
| 106 weak_ptr_factory_.GetWeakPtr(), | |
| 107 file_path)); | |
| 108 } else { | |
| 109 PrepareWritableFileAndRunAfterCallback(file_path); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 void GDataFileWriteHelper::PrepareWritableFileAndRunAfterCallback( | |
| 114 const FilePath& file_path) { | |
| 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 116 file_system_->CloseFile(file_path, FileOperationCallback()); | |
| 117 } | |
| 118 | |
| 119 } // namespace gdata | |
| OLD | NEW |