1 // Copyright 2014 The Chromium Authors
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/apple/foundation_util.h"
11 #include "base/files/file.h"
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h"
14 #include "base/functional/bind.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 base::StringPiece AsStringPiece(NSString* str) {
23 const char* data = [str fileSystemRepresentation];
24 return data ? base::StringPiece(data) : base::StringPiece();
27 // Given the |path| of a package, returns the destination that the package
28 // should be zipped to. Returns an empty path on any errors.
29 base::FilePath ZipDestination(const base::FilePath& path) {
32 if (!base::GetTempDir(&dest)) {
33 // Couldn't get the temporary directory.
34 return base::FilePath();
37 // TMPDIR/<bundleID>/zip_cache/<guid>
39 NSString* bundleID = [[NSBundle mainBundle] bundleIdentifier];
40 dest = dest.Append(AsStringPiece(bundleID));
42 dest = dest.Append("zip_cache");
44 NSString* guid = [[NSProcessInfo processInfo] globallyUniqueString];
45 dest = dest.Append(AsStringPiece(guid));
50 // Returns the path of the package and its components relative to the package's
52 std::vector<base::FilePath> RelativePathsForPackage(
53 const base::FilePath& package) {
54 // Get the base directory.
55 base::FilePath base_dir = package.DirName();
57 // Add the package as the first relative path.
58 std::vector<base::FilePath> relative_paths;
59 relative_paths.push_back(package.BaseName());
61 // Add the components of the package as relative paths.
62 base::FileEnumerator file_enumerator(
65 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
66 for (base::FilePath path = file_enumerator.Next(); !path.empty();
67 path = file_enumerator.Next()) {
68 base::FilePath relative_path;
69 bool success = base_dir.AppendRelativePath(path, &relative_path);
71 relative_paths.push_back(relative_path);
74 return relative_paths;
79 base::FilePath FileSelectHelper::ZipPackage(const base::FilePath& path) {
80 base::FilePath dest(ZipDestination(path));
84 if (!base::CreateDirectory(dest.DirName()))
85 return base::FilePath();
87 base::File file(dest, base::File::FLAG_CREATE | base::File::FLAG_WRITE);
89 return base::FilePath();
91 std::vector<base::FilePath> files_to_zip(RelativePathsForPackage(path));
92 base::FilePath base_dir = path.DirName();
93 bool success = zip::ZipFiles(base_dir, files_to_zip, file.GetPlatformFile());
97 result = fchmod(file.GetPlatformFile(), S_IRUSR);
99 return result >= 0 ? dest : base::FilePath();
102 void FileSelectHelper::ProcessSelectedFilesMac(
103 const std::vector<ui::SelectedFileInfo>& files) {
104 // Make a mutable copy of the input files.
105 std::vector<ui::SelectedFileInfo> files_out(files);
106 std::vector<base::FilePath> temporary_files;
108 for (auto& file_info : files_out) {
109 NSString* filename = base::apple::FilePathToNSString(file_info.local_path);
111 [[NSWorkspace sharedWorkspace] isFilePackageAtPath:filename];
112 if (isPackage && base::DirectoryExists(file_info.local_path)) {
113 base::FilePath result = ZipPackage(file_info.local_path);
115 if (!result.empty()) {
116 temporary_files.push_back(result);
117 file_info.local_path = result;
118 file_info.file_path = result;
119 file_info.display_name.append(".zip");
124 content::GetUIThreadTaskRunner({})->PostTask(
126 base::BindOnce(&FileSelectHelper::ProcessSelectedFilesMacOnUIThread,
127 base::Unretained(this), files_out, temporary_files));
130 void FileSelectHelper::ProcessSelectedFilesMacOnUIThread(
131 const std::vector<ui::SelectedFileInfo>& files,
132 const std::vector<base::FilePath>& temporary_files) {
133 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
135 if (!temporary_files.empty()) {
136 temporary_files_.insert(
137 temporary_files_.end(), temporary_files.begin(), temporary_files.end());
139 // Typically, |temporary_files| are deleted after |web_contents_| is
140 // destroyed. If |web_contents_| is already NULL, then the temporary files
141 // need to be deleted now.
142 if (!web_contents_) {
143 DeleteTemporaryFiles();
149 ConvertToFileChooserFileInfoList(files);