Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/file_select_helper.h" | |
| 6 | |
| 7 #include <Cocoa/Cocoa.h> | |
| 8 #include <sys/stat.h> | |
| 9 | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/files/file_util.h" | |
| 12 #include "base/mac/foundation_util.h" | |
| 13 #include "content/public/browser/browser_thread.h" | |
| 14 #include "third_party/zlib/google/zip.h" | |
| 15 #include "ui/shell_dialogs/selected_file_info.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // Given the |path| of a package, returns the destination that the package | |
| 20 // should be zipped to. Returns an empty path on any errors. | |
| 21 base::FilePath ZipDestination(const base::FilePath& path) { | |
| 22 NSMutableString* dest = | |
| 23 [NSMutableString stringWithString:NSTemporaryDirectory()]; | |
| 24 | |
| 25 // Couldn't get the temporary directory. | |
| 26 if (!dest) | |
| 27 return base::FilePath(); | |
| 28 | |
| 29 [dest appendFormat:@"%@/%@/%s.zip", | |
| 30 [[NSBundle mainBundle] bundleIdentifier], | |
| 31 [[NSProcessInfo processInfo] globallyUniqueString], | |
| 32 path.BaseName().value().c_str()]; | |
| 33 | |
| 34 return base::mac::NSStringToFilePath(dest); | |
| 35 } | |
| 36 | |
| 37 // Returns the path of the package and its components relative to the package's | |
| 38 // parent directory. | |
| 39 std::vector<base::FilePath> RelativePathsForPackage( | |
| 40 const base::FilePath& package) { | |
| 41 // Get the base directory. | |
| 42 base::FilePath base_dir = package.DirName(); | |
| 43 | |
| 44 // Get the number of components in the base directory. | |
| 45 std::vector<std::string> base_dir_components; | |
| 46 base_dir.GetComponents(&base_dir_components); | |
| 47 int count = base_dir_components.size(); | |
| 48 | |
| 49 // Add the package as the first relative path. | |
| 50 std::vector<base::FilePath> relative_paths; | |
| 51 relative_paths.push_back(package.BaseName()); | |
| 52 | |
| 53 // Add the components of the package as relative paths. | |
| 54 base::FileEnumerator file_enumerator( | |
| 55 package, | |
| 56 true /* recursive */, | |
| 57 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES); | |
| 58 for (base::FilePath path = file_enumerator.Next(); !path.value().empty(); | |
| 59 path = file_enumerator.Next()) { | |
| 60 // Break the path into its components. | |
| 61 std::vector<std::string> path_components; | |
| 62 path.GetComponents(&path_components); | |
| 63 | |
| 64 // Ignore the first |count| components to create the relative path. | |
| 65 base::FilePath relative_path; | |
| 66 for (std::vector<std::string>::iterator it = | |
| 67 path_components.begin() + count; | |
| 68 it != path_components.end(); | |
| 69 ++it) { | |
| 70 relative_path = relative_path.Append(*it); | |
| 71 } | |
| 72 | |
| 73 relative_paths.push_back(relative_path); | |
| 74 } | |
| 75 | |
| 76 return relative_paths; | |
| 77 } | |
| 78 | |
| 79 } // namespace | |
| 80 | |
| 81 base::FilePath FileSelectHelper::ZipPackage(const base::FilePath& path) { | |
| 82 base::FilePath dest(ZipDestination(path)); | |
| 83 if (dest.empty()) | |
| 84 return dest; | |
| 85 | |
| 86 bool success = base::CreateDirectory(dest.DirName()); | |
| 87 if (!success) | |
| 88 return base::FilePath(); | |
| 89 | |
| 90 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'
| |
| 91 if (dest_fd < 0) | |
| 92 return base::FilePath(); | |
| 93 | |
| 94 std::vector<base::FilePath> files_to_zip(RelativePathsForPackage(path)); | |
| 95 base::FilePath base_dir = path.DirName(); | |
| 96 success = zip::ZipFiles(base_dir, files_to_zip, dest_fd); | |
| 97 | |
| 98 int result = -1; | |
| 99 if (success) | |
| 100 result = fchmod(dest_fd, S_IRUSR); | |
| 101 | |
| 102 close(dest_fd); | |
| 103 | |
| 104 return result >= 0 ? dest : base::FilePath(); | |
| 105 } | |
| 106 | |
| 107 void FileSelectHelper::ProcessSelectedFilesMac( | |
| 108 const std::vector<ui::SelectedFileInfo>& files) { | |
| 109 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE_USER_BLOCKING); | |
| 110 | |
| 111 // Make a mutable copy of the input files. | |
| 112 std::vector<ui::SelectedFileInfo> files_out(files); | |
| 113 std::vector<base::FilePath> temporary_files; | |
| 114 | |
| 115 for (std::vector<ui::SelectedFileInfo>::iterator it = files_out.begin(); | |
| 116 it != files_out.end(); | |
| 117 ++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
| |
| 118 ui::SelectedFileInfo& file_info = *it; | |
| 119 NSString* filename = | |
| 120 [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.
| |
| 121 BOOL isPackage = | |
| 122 [[NSWorkspace sharedWorkspace] isFilePackageAtPath:filename]; | |
| 123 if (isPackage && base::DirectoryExists(file_info.local_path)) { | |
| 124 base::FilePath result = ZipPackage(file_info.local_path); | |
| 125 | |
| 126 if (!result.empty()) { | |
| 127 // The parent directory of the zipped file is a temporary directory with | |
| 128 // no other children. | |
| 129 temporary_files.push_back(result.DirName()); | |
| 130 file_info.local_path = result; | |
| 131 file_info.file_path = result; | |
| 132 file_info.display_name = result.BaseName().value(); | |
| 133 } | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 content::BrowserThread::PostTask( | |
| 138 content::BrowserThread::UI, | |
| 139 FROM_HERE, | |
| 140 base::Bind(&FileSelectHelper::ProcessSelectedFilesMacOnUIThread, | |
| 141 base::Unretained(this), | |
| 142 files_out, | |
| 143 temporary_files)); | |
| 144 } | |
| 145 | |
| 146 void FileSelectHelper::ProcessSelectedFilesMacOnUIThread( | |
| 147 const std::vector<ui::SelectedFileInfo>& files, | |
| 148 const std::vector<base::FilePath>& temporary_files) { | |
| 149 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 150 | |
| 151 if (!temporary_files.empty()) { | |
| 152 temporary_files_.insert( | |
| 153 temporary_files_.end(), temporary_files.begin(), temporary_files.end()); | |
| 154 | |
| 155 // Typically, |temporary_files| are deleted after web_contents_ is | |
| 156 // destroyed. If web_contents_ is already NULL, then the temporary files | |
| 157 // need to be deleted now. | |
| 158 if (!web_contents_) { | |
| 159 DeleteTemporaryFiles(); | |
| 160 RunFileChooserEnd(); | |
| 161 return; | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 NotifyRenderViewHostAndEnd(files); | |
| 166 } | |
| OLD | NEW |