Index: chrome/browser/chromeos/gdata/gdata_file_write_helper.cc |
diff --git a/chrome/browser/chromeos/gdata/gdata_file_write_helper.cc b/chrome/browser/chromeos/gdata/gdata_file_write_helper.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a1b257070694d7eb4dad8139959047195de62216 |
--- /dev/null |
+++ b/chrome/browser/chromeos/gdata/gdata_file_write_helper.cc |
@@ -0,0 +1,119 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/chromeos/gdata/gdata_file_write_helper.h" |
+ |
+#include "base/threading/sequenced_worker_pool.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+using content::BrowserThread; |
+ |
+namespace gdata { |
+ |
+GDataFileWriteHelper::GDataFileWriteHelper( |
+ GDataFileSystemInterface* file_system) |
+ : file_system_(file_system), |
+ weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
+ // Must be created in GDataSystemService. |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+} |
+ |
+GDataFileWriteHelper::~GDataFileWriteHelper() { |
+ // Must be destroyed in GDataSystemService. |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+} |
+ |
+void GDataFileWriteHelper::PrepareWritableFileAndRun( |
+ const FilePath& file_path, |
+ const OpenFileCallback& callback) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
+ BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ |
+ // TODO(kinaba): factor out RunTaskOnThread family from gdata_file_system.cc |
+ // and use it. |
+ if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
+ PrepareWritableFileAndRunOnUIThread(file_path, callback); |
+ } else { |
+ BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)->PostTask( |
+ FROM_HERE, |
+ base::Bind(&GDataFileWriteHelper::PrepareWritableFileAndRunOnUIThread, |
+ 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
|
+ file_path, |
+ callback)); |
+ } |
+} |
+ |
+void GDataFileWriteHelper::PrepareWritableFileAndRunOnUIThread( |
+ const FilePath& file_path, |
+ const OpenFileCallback& callback) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ |
+ file_system_->CreateFile( |
+ file_path, |
+ false, // it is not an error, even if the path already exists. |
+ base::Bind( |
+ &GDataFileWriteHelper::PrepareWritableFileAndRunAfterCreateFile, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ file_path, |
+ callback)); |
+} |
+ |
+void GDataFileWriteHelper::PrepareWritableFileAndRunAfterCreateFile( |
+ const FilePath& file_path, |
+ const OpenFileCallback& callback, |
+ GDataFileError error) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ |
+ if (error != gdata::GDATA_FILE_OK) { |
+ if (!callback.is_null()) { |
+ content::BrowserThread::GetBlockingPool()->PostTask( |
+ FROM_HERE, |
+ base::Bind(callback, error, FilePath())); |
+ } |
+ return; |
+ } |
+ file_system_->OpenFile( |
+ file_path, |
+ base::Bind(&GDataFileWriteHelper::PrepareWritableFileAndRunAfterOpenFile, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ file_path, |
+ callback)); |
+} |
+ |
+void GDataFileWriteHelper::PrepareWritableFileAndRunAfterOpenFile( |
+ const FilePath& file_path, |
+ const OpenFileCallback& callback, |
+ GDataFileError error, |
+ const FilePath& local_cache_path) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ |
+ if (error != gdata::GDATA_FILE_OK) { |
+ if (!callback.is_null()) { |
+ content::BrowserThread::GetBlockingPool()->PostTask( |
+ FROM_HERE, |
+ base::Bind(callback, error, FilePath())); |
+ } |
+ return; |
+ } |
+ |
+ if (!callback.is_null()) { |
+ content::BrowserThread::GetBlockingPool()->PostTaskAndReply( |
+ FROM_HERE, |
+ base::Bind(callback, GDATA_FILE_OK, local_cache_path), |
+ base::Bind( |
+ &GDataFileWriteHelper::PrepareWritableFileAndRunAfterCallback, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ file_path)); |
+ } else { |
+ PrepareWritableFileAndRunAfterCallback(file_path); |
+ } |
+} |
+ |
+void GDataFileWriteHelper::PrepareWritableFileAndRunAfterCallback( |
+ const FilePath& file_path) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ file_system_->CloseFile(file_path, FileOperationCallback()); |
+} |
+ |
+} // namespace gdata |