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.
5 #include "chrome/browser/chromeos/file_manager/zip_file_creator.h"
8 #include "base/callback_helpers.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/threading/sequenced_worker_pool.h"
11 #include "chrome/common/chrome_utility_messages.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/utility_process_host.h"
15 #include "ui/base/l10n/l10n_util.h"
17 using content::BrowserThread
;
18 using content::UtilityProcessHost
;
22 // Creates the destination zip file only if it does not already exist.
23 base::File
OpenFileHandleOnBlockingThreadPool(const base::FilePath
& zip_path
) {
24 return base::File(zip_path
, base::File::FLAG_CREATE
| base::File::FLAG_WRITE
);
29 namespace file_manager
{
31 ZipFileCreator::ZipFileCreator(
32 const ResultCallback
& callback
,
33 const base::FilePath
& src_dir
,
34 const std::vector
<base::FilePath
>& src_relative_paths
,
35 const base::FilePath
& dest_file
)
36 : callback_(callback
),
38 src_relative_paths_(src_relative_paths
),
39 dest_file_(dest_file
) {
40 DCHECK(!callback_
.is_null());
43 void ZipFileCreator::Start() {
44 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
46 base::PostTaskAndReplyWithResult(
47 BrowserThread::GetBlockingPool(),
49 base::Bind(&OpenFileHandleOnBlockingThreadPool
, dest_file_
),
50 base::Bind(&ZipFileCreator::OnOpenFileHandle
, this));
53 ZipFileCreator::~ZipFileCreator() {
56 bool ZipFileCreator::OnMessageReceived(const IPC::Message
& message
) {
58 IPC_BEGIN_MESSAGE_MAP(ZipFileCreator
, message
)
59 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_CreateZipFile_Succeeded
,
60 OnCreateZipFileSucceeded
)
61 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_CreateZipFile_Failed
,
62 OnCreateZipFileFailed
)
63 IPC_MESSAGE_UNHANDLED(handled
= false)
68 void ZipFileCreator::OnProcessCrashed(int exit_code
) {
72 void ZipFileCreator::OnOpenFileHandle(base::File file
) {
73 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
75 if (!file
.IsValid()) {
76 LOG(ERROR
) << "Failed to create dest zip file " << dest_file_
.value();
81 BrowserThread::PostTask(
85 &ZipFileCreator::StartProcessOnIOThread
, this, base::Passed(&file
)));
88 void ZipFileCreator::StartProcessOnIOThread(base::File dest_file
) {
89 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
91 base::FileDescriptor
dest_fd(dest_file
.Pass());
93 UtilityProcessHost
* host
= UtilityProcessHost::Create(
95 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
).get());
97 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_ZIP_FILE_CREATOR_NAME
));
98 host
->SetExposedDir(src_dir_
);
99 host
->Send(new ChromeUtilityMsg_CreateZipFile(src_dir_
, src_relative_paths_
,
103 void ZipFileCreator::OnCreateZipFileSucceeded() {
107 void ZipFileCreator::OnCreateZipFileFailed() {
111 void ZipFileCreator::ReportDone(bool success
) {
112 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
114 // Guard against calling observer multiple times.
115 if (!callback_
.is_null())
116 base::ResetAndReturn(&callback_
).Run(success
);
119 } // namespace file_manager