Chromium Code Reviews| Index: chrome/browser/file_select_helper_mac.mm |
| diff --git a/chrome/browser/file_select_helper_mac.mm b/chrome/browser/file_select_helper_mac.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bba344d39f5746eb5ad1045cd6029a33ac7a445e |
| --- /dev/null |
| +++ b/chrome/browser/file_select_helper_mac.mm |
| @@ -0,0 +1,166 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/file_select_helper.h" |
| + |
| +#include <Cocoa/Cocoa.h> |
| +#include <sys/stat.h> |
| + |
| +#include "base/files/file_path.h" |
| +#include "base/files/file_util.h" |
| +#include "base/mac/foundation_util.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "third_party/zlib/google/zip.h" |
| +#include "ui/shell_dialogs/selected_file_info.h" |
| + |
| +namespace { |
| + |
| +// Given the |path| of a package, returns the destination that the package |
| +// should be zipped to. Returns an empty path on any errors. |
| +base::FilePath ZipDestination(const base::FilePath& path) { |
| + NSMutableString* dest = |
| + [NSMutableString stringWithString:NSTemporaryDirectory()]; |
| + |
| + // Couldn't get the temporary directory. |
| + if (!dest) |
| + return base::FilePath(); |
| + |
| + [dest appendFormat:@"%@/%@/%s.zip", |
| + [[NSBundle mainBundle] bundleIdentifier], |
| + [[NSProcessInfo processInfo] globallyUniqueString], |
| + path.BaseName().value().c_str()]; |
| + |
| + return base::mac::NSStringToFilePath(dest); |
| +} |
| + |
| +// Returns the path of the package and its components relative to the package's |
| +// parent directory. |
| +std::vector<base::FilePath> RelativePathsForPackage( |
| + const base::FilePath& package) { |
| + // Get the base directory. |
| + base::FilePath base_dir = package.DirName(); |
| + |
| + // Get the number of components in the base directory. |
| + std::vector<std::string> base_dir_components; |
| + base_dir.GetComponents(&base_dir_components); |
| + int count = base_dir_components.size(); |
| + |
| + // Add the package as the first relative path. |
| + std::vector<base::FilePath> relative_paths; |
| + relative_paths.push_back(package.BaseName()); |
| + |
| + // Add the components of the package as relative paths. |
| + base::FileEnumerator file_enumerator( |
| + package, |
| + true /* recursive */, |
| + base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES); |
| + for (base::FilePath path = file_enumerator.Next(); !path.value().empty(); |
| + path = file_enumerator.Next()) { |
| + // Break the path into its components. |
| + std::vector<std::string> path_components; |
| + path.GetComponents(&path_components); |
| + |
| + // Ignore the first |count| components to create the relative path. |
| + base::FilePath relative_path; |
| + for (std::vector<std::string>::iterator it = |
| + path_components.begin() + count; |
| + it != path_components.end(); |
| + ++it) { |
| + relative_path = relative_path.Append(*it); |
| + } |
| + |
| + relative_paths.push_back(relative_path); |
| + } |
| + |
| + return relative_paths; |
| +} |
| + |
| +} // namespace |
| + |
| +base::FilePath FileSelectHelper::ZipPackage(const base::FilePath& path) { |
| + base::FilePath dest(ZipDestination(path)); |
| + if (dest.empty()) |
| + return dest; |
| + |
| + bool success = base::CreateDirectory(dest.DirName()); |
| + if (!success) |
| + return base::FilePath(); |
| + |
| + int dest_fd = open(dest.value().c_str(), O_WRONLY | O_CREAT); |
|
Avi (use Gerrit)
2014/10/08 00:17:32
Please use base::File from base/files/file.h. Crea
erikchen
2014/10/08 20:14:43
Done. I didn't see an alternative to fchmod, so I'
|
| + if (dest_fd < 0) |
| + return base::FilePath(); |
| + |
| + std::vector<base::FilePath> files_to_zip(RelativePathsForPackage(path)); |
| + base::FilePath base_dir = path.DirName(); |
| + success = zip::ZipFiles(base_dir, files_to_zip, dest_fd); |
| + |
| + int result = -1; |
| + if (success) |
| + result = fchmod(dest_fd, S_IRUSR); |
| + |
| + close(dest_fd); |
| + |
| + return result >= 0 ? dest : base::FilePath(); |
| +} |
| + |
| +void FileSelectHelper::ProcessSelectedFilesMac( |
| + const std::vector<ui::SelectedFileInfo>& files) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::FILE_USER_BLOCKING); |
| + |
| + // Make a mutable copy of the input files. |
| + std::vector<ui::SelectedFileInfo> files_out(files); |
| + std::vector<base::FilePath> temporary_files; |
| + |
| + for (std::vector<ui::SelectedFileInfo>::iterator it = files_out.begin(); |
| + it != files_out.end(); |
| + ++it) { |
|
Avi (use Gerrit)
2014/10/08 00:17:32
Why not for( : ) syntax here? You should be able t
erikchen
2014/10/08 20:14:43
I didn't realize that the C++11 style discussions
|
| + ui::SelectedFileInfo& file_info = *it; |
| + NSString* filename = |
| + [NSString stringWithUTF8String:file_info.local_path.value().c_str()]; |
|
Avi (use Gerrit)
2014/10/08 00:17:32
FilePathToNSString from foundation_util.
erikchen
2014/10/08 20:14:43
Done.
|
| + BOOL isPackage = |
| + [[NSWorkspace sharedWorkspace] isFilePackageAtPath:filename]; |
| + if (isPackage && base::DirectoryExists(file_info.local_path)) { |
| + base::FilePath result = ZipPackage(file_info.local_path); |
| + |
| + if (!result.empty()) { |
| + // The parent directory of the zipped file is a temporary directory with |
| + // no other children. |
| + temporary_files.push_back(result.DirName()); |
| + file_info.local_path = result; |
| + file_info.file_path = result; |
| + file_info.display_name = result.BaseName().value(); |
| + } |
| + } |
| + } |
| + |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&FileSelectHelper::ProcessSelectedFilesMacOnUIThread, |
| + base::Unretained(this), |
| + files_out, |
| + temporary_files)); |
| +} |
| + |
| +void FileSelectHelper::ProcessSelectedFilesMacOnUIThread( |
| + const std::vector<ui::SelectedFileInfo>& files, |
| + const std::vector<base::FilePath>& temporary_files) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + if (!temporary_files.empty()) { |
| + temporary_files_.insert( |
| + temporary_files_.end(), temporary_files.begin(), temporary_files.end()); |
| + |
| + // Typically, |temporary_files| are deleted after web_contents_ is |
| + // destroyed. If web_contents_ is already NULL, then the temporary files |
| + // need to be deleted now. |
| + if (!web_contents_) { |
| + DeleteTemporaryFiles(); |
| + RunFileChooserEnd(); |
| + return; |
| + } |
| + } |
| + |
| + NotifyRenderViewHostAndEnd(files); |
| +} |