9ef365d1f3192cfce0b826d625515690570226ec
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_manager / zip_file_creator.cc
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/file_manager/zip_file_creator.h"
6
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/files/file_util_proxy.h"
10 #include "base/memory/scoped_handle.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/path_service.h"
13 #include "base/threading/sequenced_worker_pool.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/chrome_utility_messages.h"
17 #include "chrome/common/extensions/extension_file_util.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/utility_process_host.h"
20 #include "grit/generated_resources.h"
21
22 using content::BrowserThread;
23 using content::UtilityProcessHost;
24
25 namespace file_manager {
26
27 ZipFileCreator::ZipFileCreator(
28     Observer* observer,
29     const base::FilePath& src_dir,
30     const std::vector<base::FilePath>& src_relative_paths,
31     const base::FilePath& dest_file)
32     : thread_identifier_(BrowserThread::ID_COUNT),
33       observer_(observer),
34       src_dir_(src_dir),
35       src_relative_paths_(src_relative_paths),
36       dest_file_(dest_file),
37       got_response_(false) {
38 }
39
40 void ZipFileCreator::Start() {
41   CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_identifier_));
42   BrowserThread::GetBlockingPool()->PostTask(
43       FROM_HERE,
44       base::Bind(&ZipFileCreator::OpenFileHandleOnBlockingThreadPool, this));
45 }
46
47 ZipFileCreator::~ZipFileCreator() {
48 }
49
50 bool ZipFileCreator::OnMessageReceived(const IPC::Message& message) {
51   bool handled = true;
52   IPC_BEGIN_MESSAGE_MAP(ZipFileCreator, message)
53     IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_CreateZipFile_Succeeded,
54                         OnCreateZipFileSucceeded)
55     IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_CreateZipFile_Failed,
56                         OnCreateZipFileFailed)
57     IPC_MESSAGE_UNHANDLED(handled = false)
58   IPC_END_MESSAGE_MAP()
59   return handled;
60 }
61
62 void ZipFileCreator::OnProcessCrashed(int exit_code) {
63   // Don't report crashes if they happen after we got a response.
64   if (got_response_)
65     return;
66
67   // Utility process crashed while trying to create the zip file.
68   ReportDone(false);
69 }
70
71 void ZipFileCreator::OpenFileHandleOnBlockingThreadPool() {
72   // Create the destination zip file only if it does not already exist.
73   int flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
74   base::PlatformFileError error_code = base::PLATFORM_FILE_OK;
75   base::PlatformFile dest_file =
76       base::CreatePlatformFile(dest_file_, flags, NULL, &error_code);
77
78   if (error_code != base::PLATFORM_FILE_OK) {
79     LOG(ERROR) << "Failed to create dest zip file " << dest_file_.value();
80
81     BrowserThread::GetMessageLoopProxyForThread(thread_identifier_)->PostTask(
82         FROM_HERE,
83         base::Bind(&ZipFileCreator::ReportDone, this, false));
84     return;
85   }
86
87   BrowserThread::PostTask(
88       BrowserThread::IO, FROM_HERE,
89       base::Bind(&ZipFileCreator::StartProcessOnIOThread, this, dest_file));
90 }
91
92 void ZipFileCreator::StartProcessOnIOThread(base::PlatformFile dest_file) {
93   base::FileDescriptor dest_fd;
94   dest_fd.fd = dest_file;
95   dest_fd.auto_close = true;
96
97   UtilityProcessHost* host = UtilityProcessHost::Create(
98       this,
99       BrowserThread::GetMessageLoopProxyForThread(thread_identifier_).get());
100   host->SetExposedDir(src_dir_);
101   host->Send(new ChromeUtilityMsg_CreateZipFile(src_dir_, src_relative_paths_,
102                                                 dest_fd));
103 }
104
105 void ZipFileCreator::OnCreateZipFileSucceeded() {
106   ReportDone(true);
107 }
108
109 void ZipFileCreator::OnCreateZipFileFailed() {
110   ReportDone(false);
111 }
112
113 void ZipFileCreator::ReportDone(bool success) {
114   // Skip check for unittests.
115   if (thread_identifier_ != BrowserThread::ID_COUNT)
116     DCHECK(BrowserThread::CurrentlyOn(thread_identifier_));
117
118   // Guard against calling observer multiple times.
119   if (got_response_)
120     return;
121
122   got_response_ = true;
123   observer_->OnZipDone(success);
124 }
125
126 }  // namespace file_manager