Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / chromeos / file_manager / zip_file_creator.cc
blob6af8346c21b42ab9c99340f39a9e5c063aa609b7
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"
7 #include "base/bind.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;
20 namespace {
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);
27 } // namespace
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),
37 src_dir_(src_dir),
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(),
48 FROM_HERE,
49 base::Bind(&OpenFileHandleOnBlockingThreadPool, dest_file_),
50 base::Bind(&ZipFileCreator::OnOpenFileHandle, this));
53 ZipFileCreator::~ZipFileCreator() {
56 bool ZipFileCreator::OnMessageReceived(const IPC::Message& message) {
57 bool handled = true;
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)
64 IPC_END_MESSAGE_MAP()
65 return handled;
68 void ZipFileCreator::OnProcessCrashed(int exit_code) {
69 ReportDone(false);
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();
77 ReportDone(false);
78 return;
81 BrowserThread::PostTask(
82 BrowserThread::IO,
83 FROM_HERE,
84 base::Bind(
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(
94 this,
95 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get());
96 host->SetName(
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_,
100 dest_fd));
103 void ZipFileCreator::OnCreateZipFileSucceeded() {
104 ReportDone(true);
107 void ZipFileCreator::OnCreateZipFileFailed() {
108 ReportDone(false);
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