Index: chrome/browser/chromeos/gdata/gdata_file_writing.cc |
diff --git a/chrome/browser/chromeos/gdata/gdata_file_writing.cc b/chrome/browser/chromeos/gdata/gdata_file_writing.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..aadb00f9072c08aa81a4be634ae3d6afbe40956c |
--- /dev/null |
+++ b/chrome/browser/chromeos/gdata/gdata_file_writing.cc |
@@ -0,0 +1,117 @@ |
+// 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_writing.h" |
+ |
+#include "base/threading/sequenced_worker_pool.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+using content::BrowserThread; |
+ |
+namespace gdata { |
+ |
+GDataFileWriting::GDataFileWriting(GDataFileSystemInterface* file_system) |
+ : file_system_(file_system), |
+ ui_weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
+ ui_weak_ptr_(ui_weak_ptr_factory_.GetWeakPtr()) { |
+ // Must be created in GDataSystemService. |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+} |
+ |
+GDataFileWriting::~GDataFileWriting() { |
+ // Must be destroyed in GDataSystemService. |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+} |
+ |
+void GDataFileWriting::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(&GDataFileWriting::PrepareWritableFileAndRunOnUIThread, |
+ ui_weak_ptr_, |
+ file_path, |
+ callback)); |
+ } |
+} |
+ |
+void GDataFileWriting::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(&GDataFileWriting::PrepareWritableFileAndRunAfterCreateFile, |
+ ui_weak_ptr_, |
+ file_path, |
+ callback)); |
+} |
+ |
+void GDataFileWriting::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(&GDataFileWriting::PrepareWritableFileAndRunAfterOpenFile, |
+ ui_weak_ptr_, |
+ file_path, |
+ callback)); |
+} |
+ |
+void GDataFileWriting::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(&GDataFileWriting::PrepareWritableFileAndRunAfterCallback, |
+ ui_weak_ptr_, |
+ file_path)); |
+ } else { |
+ PrepareWritableFileAndRunAfterCallback(file_path); |
+ } |
+} |
+ |
+void GDataFileWriting::PrepareWritableFileAndRunAfterCallback( |
+ const FilePath& file_path) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ file_system_->CloseFile(file_path, FileOperationCallback()); |
+} |
+ |
+} // namespace gdata |