Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_manager / zip_file_creator.cc
index 9ef365d..f6e0788 100644 (file)
@@ -5,43 +5,47 @@
 #include "chrome/browser/chromeos/file_manager/zip_file_creator.h"
 
 #include "base/bind.h"
-#include "base/command_line.h"
-#include "base/files/file_util_proxy.h"
-#include "base/memory/scoped_handle.h"
+#include "base/callback_helpers.h"
 #include "base/message_loop/message_loop.h"
-#include "base/path_service.h"
 #include "base/threading/sequenced_worker_pool.h"
-#include "chrome/common/chrome_paths.h"
-#include "chrome/common/chrome_switches.h"
 #include "chrome/common/chrome_utility_messages.h"
-#include "chrome/common/extensions/extension_file_util.h"
 #include "content/public/browser/browser_thread.h"
 #include "content/public/browser/utility_process_host.h"
-#include "grit/generated_resources.h"
 
 using content::BrowserThread;
 using content::UtilityProcessHost;
 
+namespace {
+
+// Creates the destination zip file only if it does not already exist.
+base::File OpenFileHandleOnBlockingThreadPool(const base::FilePath& zip_path) {
+  return base::File(zip_path, base::File::FLAG_CREATE | base::File::FLAG_WRITE);
+}
+
+}  // namespace
+
 namespace file_manager {
 
 ZipFileCreator::ZipFileCreator(
-    Observer* observer,
+    const ResultCallback& callback,
     const base::FilePath& src_dir,
     const std::vector<base::FilePath>& src_relative_paths,
     const base::FilePath& dest_file)
-    : thread_identifier_(BrowserThread::ID_COUNT),
-      observer_(observer),
+    : callback_(callback),
       src_dir_(src_dir),
       src_relative_paths_(src_relative_paths),
-      dest_file_(dest_file),
-      got_response_(false) {
+      dest_file_(dest_file) {
+  DCHECK(!callback_.is_null());
 }
 
 void ZipFileCreator::Start() {
-  CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_identifier_));
-  BrowserThread::GetBlockingPool()->PostTask(
+  DCHECK_CURRENTLY_ON(BrowserThread::UI);
+
+  base::PostTaskAndReplyWithResult(
+      BrowserThread::GetBlockingPool(),
       FROM_HERE,
-      base::Bind(&ZipFileCreator::OpenFileHandleOnBlockingThreadPool, this));
+      base::Bind(&OpenFileHandleOnBlockingThreadPool, dest_file_),
+      base::Bind(&ZipFileCreator::OnOpenFileHandle, this));
 }
 
 ZipFileCreator::~ZipFileCreator() {
@@ -60,43 +64,33 @@ bool ZipFileCreator::OnMessageReceived(const IPC::Message& message) {
 }
 
 void ZipFileCreator::OnProcessCrashed(int exit_code) {
-  // Don't report crashes if they happen after we got a response.
-  if (got_response_)
-    return;
-
-  // Utility process crashed while trying to create the zip file.
   ReportDone(false);
 }
 
-void ZipFileCreator::OpenFileHandleOnBlockingThreadPool() {
-  // Create the destination zip file only if it does not already exist.
-  int flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
-  base::PlatformFileError error_code = base::PLATFORM_FILE_OK;
-  base::PlatformFile dest_file =
-      base::CreatePlatformFile(dest_file_, flags, NULL, &error_code);
+void ZipFileCreator::OnOpenFileHandle(base::File file) {
+  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
 
-  if (error_code != base::PLATFORM_FILE_OK) {
+  if (!file.IsValid()) {
     LOG(ERROR) << "Failed to create dest zip file " << dest_file_.value();
-
-    BrowserThread::GetMessageLoopProxyForThread(thread_identifier_)->PostTask(
-        FROM_HERE,
-        base::Bind(&ZipFileCreator::ReportDone, this, false));
+    ReportDone(false);
     return;
   }
 
   BrowserThread::PostTask(
-      BrowserThread::IO, FROM_HERE,
-      base::Bind(&ZipFileCreator::StartProcessOnIOThread, this, dest_file));
+      BrowserThread::IO,
+      FROM_HERE,
+      base::Bind(
+          &ZipFileCreator::StartProcessOnIOThread, this, base::Passed(&file)));
 }
 
-void ZipFileCreator::StartProcessOnIOThread(base::PlatformFile dest_file) {
-  base::FileDescriptor dest_fd;
-  dest_fd.fd = dest_file;
-  dest_fd.auto_close = true;
+void ZipFileCreator::StartProcessOnIOThread(base::File dest_file) {
+  DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+  base::FileDescriptor dest_fd(dest_file.Pass());
 
   UtilityProcessHost* host = UtilityProcessHost::Create(
       this,
-      BrowserThread::GetMessageLoopProxyForThread(thread_identifier_).get());
+      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get());
   host->SetExposedDir(src_dir_);
   host->Send(new ChromeUtilityMsg_CreateZipFile(src_dir_, src_relative_paths_,
                                                 dest_fd));
@@ -111,16 +105,11 @@ void ZipFileCreator::OnCreateZipFileFailed() {
 }
 
 void ZipFileCreator::ReportDone(bool success) {
-  // Skip check for unittests.
-  if (thread_identifier_ != BrowserThread::ID_COUNT)
-    DCHECK(BrowserThread::CurrentlyOn(thread_identifier_));
+  DCHECK_CURRENTLY_ON(BrowserThread::UI);
 
   // Guard against calling observer multiple times.
-  if (got_response_)
-    return;
-
-  got_response_ = true;
-  observer_->OnZipDone(success);
+  if (!callback_.is_null())
+    base::ResetAndReturn(&callback_).Run(success);
 }
 
 }  // namespace file_manager