zip_file_creator.h revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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 <string>
9
10#include "base/files/file.h"
11#include "base/files/file_path.h"
12#include "content/public/browser/browser_thread.h"
13#include "content/public/browser/utility_process_host_client.h"
14#include "extensions/common/extension.h"
15
16namespace file_manager {
17
18// ZipFileCreator creates a ZIP file from a specified list of files and
19// directories under a common parent directory. This is done in a sandboxed
20// subprocess to protect the browser process from handling arbitrary input data
21// from untrusted sources.
22//
23// Lifetime management:
24//
25// This class is ref-counted by each call it makes to itself on another thread,
26// and by UtilityProcessHost.
27//
28// Additionally, we hold a reference to our own client so that it lives at least
29// long enough to receive the result of zip file creation.
30class ZipFileCreator : public content::UtilityProcessHostClient {
31 public:
32  class Observer {
33   public:
34    virtual void OnZipDone(bool success) = 0;
35
36   protected:
37    virtual ~Observer() {}
38  };
39
40  // Creates a zip file from the specified list of files and directories.
41  ZipFileCreator(Observer* observer,
42                 const base::FilePath& src_dir,
43                 const std::vector<base::FilePath>& src_relative_paths,
44                 const base::FilePath& dest_file);
45
46  // Start creating the zip file. The client is called with the results.
47  void Start();
48
49 private:
50  friend class ProcessHostClient;
51
52  virtual ~ZipFileCreator();
53
54  // Opens a handle for the zip file, and proceeds to StartProcessOnIOThread.
55  void OpenFileHandleOnBlockingThreadPool();
56
57  // Starts the utility process that creates the zip file.
58  void StartProcessOnIOThread(base::File dest_file);
59
60  // UtilityProcessHostClient
61  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
62  virtual void OnProcessCrashed(int exit_code) OVERRIDE;
63
64  // IPC message handlers.
65  void OnCreateZipFileSucceeded();
66  void OnCreateZipFileFailed();
67
68  void ReportDone(bool success);
69
70  // The observer's thread. This is the thread we respond on.
71  content::BrowserThread::ID thread_identifier_;
72
73  // The observer.
74  Observer* observer_;
75
76  // The source directory for input files.
77  base::FilePath src_dir_;
78
79  // The list of source files paths to be included in the zip file.
80  // Entries are relative paths under directory |src_dir_|.
81  std::vector<base::FilePath> src_relative_paths_;
82
83  // The output zip file.
84  base::FilePath dest_file_;
85
86  // Whether we've received a response from the utility process yet.
87  bool got_response_;
88};
89
90}  // namespace file_manager
91
92#endif  // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_ZIP_FILE_CREATOR_H_
93