1 // Copyright 2014 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/file_select_helper.h"
7 #include <Cocoa/Cocoa.h>
10 #include "base/bind.h"
11 #include "base/files/file.h"
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h"
14 #include "base/mac/foundation_util.h"
15 #include "content/public/browser/browser_task_traits.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "third_party/zlib/google/zip.h"
18 #include "ui/shell_dialogs/selected_file_info.h"
22 // Given the |path| of a package, returns the destination that the package
23 // should be zipped to. Returns an empty path on any errors.
24 base::FilePath ZipDestination(const base::FilePath& path) {
27 if (!base::GetTempDir(&dest)) {
28 // Couldn't get the temporary directory.
29 return base::FilePath();
32 // TMPDIR/<bundleID>/zip_cache/<guid>
34 NSString* bundleID = [[NSBundle mainBundle] bundleIdentifier];
35 dest = dest.Append([bundleID fileSystemRepresentation]);
37 dest = dest.Append("zip_cache");
39 NSString* guid = [[NSProcessInfo processInfo] globallyUniqueString];
40 dest = dest.Append([guid fileSystemRepresentation]);
45 // Returns the path of the package and its components relative to the package's
47 std::vector<base::FilePath> RelativePathsForPackage(
48 const base::FilePath& package) {
49 // Get the base directory.
50 base::FilePath base_dir = package.DirName();
52 // Add the package as the first relative path.
53 std::vector<base::FilePath> relative_paths;
54 relative_paths.push_back(package.BaseName());
56 // Add the components of the package as relative paths.
57 base::FileEnumerator file_enumerator(
60 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
61 for (base::FilePath path = file_enumerator.Next(); !path.empty();
62 path = file_enumerator.Next()) {
63 base::FilePath relative_path;
64 bool success = base_dir.AppendRelativePath(path, &relative_path);
66 relative_paths.push_back(relative_path);
69 return relative_paths;
74 base::FilePath FileSelectHelper::ZipPackage(const base::FilePath& path) {
75 base::FilePath dest(ZipDestination(path));
79 if (!base::CreateDirectory(dest.DirName()))
80 return base::FilePath();
82 base::File file(dest, base::File::FLAG_CREATE | base::File::FLAG_WRITE);
84 return base::FilePath();
86 std::vector<base::FilePath> files_to_zip(RelativePathsForPackage(path));
87 base::FilePath base_dir = path.DirName();
88 bool success = zip::ZipFiles(base_dir, files_to_zip, file.GetPlatformFile());
92 result = fchmod(file.GetPlatformFile(), S_IRUSR);
94 return result >= 0 ? dest : base::FilePath();
97 void FileSelectHelper::ProcessSelectedFilesMac(
98 const std::vector<ui::SelectedFileInfo>& files) {
99 // Make a mutable copy of the input files.
100 std::vector<ui::SelectedFileInfo> files_out(files);
101 std::vector<base::FilePath> temporary_files;
103 for (auto& file_info : files_out) {
104 NSString* filename = base::mac::FilePathToNSString(file_info.local_path);
106 [[NSWorkspace sharedWorkspace] isFilePackageAtPath:filename];
107 if (isPackage && base::DirectoryExists(file_info.local_path)) {
108 base::FilePath result = ZipPackage(file_info.local_path);
110 if (!result.empty()) {
111 temporary_files.push_back(result);
112 file_info.local_path = result;
113 file_info.file_path = result;
114 file_info.display_name.append(".zip");
119 content::GetUIThreadTaskRunner({})->PostTask(
121 base::BindOnce(&FileSelectHelper::ProcessSelectedFilesMacOnUIThread,
122 base::Unretained(this), files_out, temporary_files));
125 void FileSelectHelper::ProcessSelectedFilesMacOnUIThread(
126 const std::vector<ui::SelectedFileInfo>& files,
127 const std::vector<base::FilePath>& temporary_files) {
128 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
130 if (!temporary_files.empty()) {
131 temporary_files_.insert(
132 temporary_files_.end(), temporary_files.begin(), temporary_files.end());
134 // Typically, |temporary_files| are deleted after |web_contents_| is
135 // destroyed. If |web_contents_| is already NULL, then the temporary files
136 // need to be deleted now.
137 if (!web_contents_) {
138 DeleteTemporaryFiles();
144 ConvertToFileChooserFileInfoList(files);