zip_file_creator.h revision 010d83a9304c5a91596085d917d248abff47903a
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#ifndef CHROME_BROWSER_CHROMEOS_FILE_MANAGER_ZIP_FILE_CREATOR_H_
6#define CHROME_BROWSER_CHROMEOS_FILE_MANAGER_ZIP_FILE_CREATOR_H_
7
8#include "base/callback.h"
9#include "base/files/file.h"
10#include "base/files/file_path.h"
11#include "content/public/browser/utility_process_host_client.h"
12
13namespace file_manager {
14
15// ZipFileCreator creates a ZIP file from a specified list of files and
16// directories under a common parent directory. This is done in a sandboxed
17// subprocess to protect the browser process from handling arbitrary input data
18// from untrusted sources.
19//
20// The class is ref-counted and its ownership is passed around internal callback
21// objects and finally to UtilityProcessHost. After the job finishes, the host
22// releases the ref-pointer and then ZipFileCreator is automatically deleted.
23class ZipFileCreator : public content::UtilityProcessHostClient {
24 public:
25  typedef base::Callback<void(bool)> ResultCallback;
26
27  // Creates a zip file from the specified list of files and directories.
28  ZipFileCreator(const ResultCallback& callback,
29                 const base::FilePath& src_dir,
30                 const std::vector<base::FilePath>& src_relative_paths,
31                 const base::FilePath& dest_file);
32
33  // Starts creating the zip file. Must be called from the UI thread.
34  // The result will be passed to |callback|. After the task is finished and
35  // |callback| is run, ZipFileCreator instance is deleted.
36  void Start();
37
38 private:
39  friend class ProcessHostClient;
40
41  virtual ~ZipFileCreator();
42
43  // Called after the file handle is opened on blocking pool.
44  void OnOpenFileHandle(base::File file);
45
46  // Starts the utility process that creates the zip file.
47  void StartProcessOnIOThread(base::File dest_file);
48
49  // UtilityProcessHostClient
50  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
51  virtual void OnProcessCrashed(int exit_code) OVERRIDE;
52
53  // IPC message handlers.
54  void OnCreateZipFileSucceeded();
55  void OnCreateZipFileFailed();
56
57  void ReportDone(bool success);
58
59  // The callback.
60  ResultCallback callback_;
61
62  // The source directory for input files.
63  base::FilePath src_dir_;
64
65  // The list of source files paths to be included in the zip file.
66  // Entries are relative paths under directory |src_dir_|.
67  std::vector<base::FilePath> src_relative_paths_;
68
69  // The output zip file.
70  base::FilePath dest_file_;
71};
72
73}  // namespace file_manager
74
75#endif  // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_ZIP_FILE_CREATOR_H_
76