OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_MANAGER_ZIP_FILE_CREATOR_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_MANAGER_ZIP_FILE_CREATOR_H_ |
6 #define CHROME_BROWSER_CHROMEOS_FILE_MANAGER_ZIP_FILE_CREATOR_H_ | 6 #define CHROME_BROWSER_CHROMEOS_FILE_MANAGER_ZIP_FILE_CREATOR_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "content/public/browser/utility_process_host_client.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "chrome/common/zip_file_creator.mojom.h" |
| 13 #include "content/public/browser/utility_process_mojo_client.h" |
12 | 14 |
13 namespace file_manager { | 15 namespace file_manager { |
14 | 16 |
15 // ZipFileCreator creates a ZIP file from a specified list of files and | 17 // 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 | 18 // directories under a common parent directory. This is done in a sandboxed |
17 // subprocess to protect the browser process from handling arbitrary input data | 19 // utility process to protect the browser process from handling arbitrary |
18 // from untrusted sources. | 20 // input data from untrusted sources. |
19 // | 21 class ZipFileCreator : public base::RefCountedThreadSafe<ZipFileCreator> { |
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. | |
23 class ZipFileCreator : public content::UtilityProcessHostClient { | |
24 public: | 22 public: |
25 typedef base::Callback<void(bool)> ResultCallback; | 23 typedef base::Callback<void(bool)> ResultCallback; |
26 | 24 |
27 // Creates a zip file from the specified list of files and directories. | 25 // Creates a zip file from the specified list of files and directories. |
28 ZipFileCreator(const ResultCallback& callback, | 26 ZipFileCreator(const ResultCallback& callback, |
29 const base::FilePath& src_dir, | 27 const base::FilePath& src_dir, |
30 const std::vector<base::FilePath>& src_relative_paths, | 28 const std::vector<base::FilePath>& src_relative_paths, |
31 const base::FilePath& dest_file); | 29 const base::FilePath& dest_file); |
32 | 30 |
33 // Starts creating the zip file. Must be called from the UI thread. | 31 // 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 | 32 // The result will be passed to |callback|. After the task is finished |
35 // |callback| is run, ZipFileCreator instance is deleted. | 33 // and |callback| is run, ZipFileCreator instance is deleted. |
36 void Start(); | 34 void Start(); |
37 | 35 |
38 private: | 36 private: |
39 friend class ProcessHostClient; | 37 friend class base::RefCountedThreadSafe<ZipFileCreator>; |
40 | 38 |
41 ~ZipFileCreator() override; | 39 ~ZipFileCreator(); |
42 | 40 |
43 // Called after the file handle is opened on blocking pool. | 41 // Called after the dest_file |file| is opened on the blocking pool to |
44 void OnOpenFileHandle(base::File file); | 42 // create the zip file in it using a sandboxed utility process. |
45 | 43 void CreateZipFile(base::File file); |
46 // Starts the utility process that creates the zip file. | |
47 void StartProcessOnIOThread(base::File dest_file); | |
48 | |
49 // UtilityProcessHostClient | |
50 bool OnMessageReceived(const IPC::Message& message) override; | |
51 void OnProcessCrashed(int exit_code) override; | |
52 | |
53 // IPC message handlers. | |
54 void OnCreateZipFileSucceeded(); | |
55 void OnCreateZipFileFailed(); | |
56 | 44 |
57 void ReportDone(bool success); | 45 void ReportDone(bool success); |
58 | 46 |
59 // The callback. | 47 // The callback. |
60 ResultCallback callback_; | 48 ResultCallback callback_; |
61 | 49 |
62 // The source directory for input files. | 50 // The source directory for input files. |
63 base::FilePath src_dir_; | 51 base::FilePath src_dir_; |
64 | 52 |
65 // The list of source files paths to be included in the zip file. | 53 // The list of source files paths to be included in the zip file. |
66 // Entries are relative paths under directory |src_dir_|. | 54 // Entries are relative paths under directory |src_dir_|. |
67 std::vector<base::FilePath> src_relative_paths_; | 55 std::vector<base::FilePath> src_relative_paths_; |
68 | 56 |
69 // The output zip file. | 57 // The output zip file. |
70 base::FilePath dest_file_; | 58 base::FilePath dest_file_; |
| 59 |
| 60 // Utility process used to create the zip file. |
| 61 std::unique_ptr< |
| 62 content::UtilityProcessMojoClient<chrome::mojom::ZipFileCreator>> |
| 63 utility_process_mojo_client_; |
71 }; | 64 }; |
72 | 65 |
73 } // namespace file_manager | 66 } // namespace file_manager |
74 | 67 |
75 #endif // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_ZIP_FILE_CREATOR_H_ | 68 #endif // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_ZIP_FILE_CREATOR_H_ |
OLD | NEW |