Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: chrome/browser/file_select_helper_mac.mm

Issue 634833003: mac: Zip packages when they are selected by the file opener. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments from avi. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/mac/foundation_util.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "third_party/zlib/google/zip.h"
16 #include "ui/shell_dialogs/selected_file_info.h"
17
18 namespace {
19
20 // Given the |path| of a package, returns the destination that the package
21 // should be zipped to. Returns an empty path on any errors.
22 base::FilePath ZipDestination(const base::FilePath& path) {
23 NSMutableString* dest =
24 [NSMutableString stringWithString:NSTemporaryDirectory()];
25
26 // Couldn't get the temporary directory.
27 if (!dest)
28 return base::FilePath();
29
30 [dest appendFormat:@"%@/%@/%s.zip",
31 [[NSBundle mainBundle] bundleIdentifier],
32 [[NSProcessInfo processInfo] globallyUniqueString],
33 path.BaseName().value().c_str()];
34
35 return base::mac::NSStringToFilePath(dest);
36 }
37
38 // Returns the path of the package and its components relative to the package's
39 // parent directory.
40 std::vector<base::FilePath> RelativePathsForPackage(
41 const base::FilePath& package) {
42 // Get the base directory.
43 base::FilePath base_dir = package.DirName();
44
45 // Get the number of components in the base directory.
46 std::vector<std::string> base_dir_components;
47 base_dir.GetComponents(&base_dir_components);
48 int count = base_dir_components.size();
Lei Zhang 2014/10/09 18:41:54 size_t
erikchen 2014/10/09 23:33:35 Done.
49
50 // Add the package as the first relative path.
51 std::vector<base::FilePath> relative_paths;
52 relative_paths.push_back(package.BaseName());
53
54 // Add the components of the package as relative paths.
55 base::FileEnumerator file_enumerator(
56 package,
57 true /* recursive */,
58 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
59 for (base::FilePath path = file_enumerator.Next(); !path.value().empty();
Lei Zhang 2014/10/09 18:41:54 nit: !path.empty()
erikchen 2014/10/09 23:33:35 Done.
60 path = file_enumerator.Next()) {
61 // Break the path into its components.
62 std::vector<std::string> path_components;
63 path.GetComponents(&path_components);
64
65 // Ignore the first |count| components to create the relative path.
Lei Zhang 2014/10/09 18:41:54 Are you reinventing FilePath::AppendRelativePath()
erikchen 2014/10/09 23:33:35 Alas...no. zip::ZipFiles() requires a list of rel
Lei Zhang 2014/10/10 01:32:28 I still suspect base_dir.AppendRelativePath(path,
erikchen 2014/10/10 17:21:38 You're right! The name of the method primed me to
66 base::FilePath relative_path;
67 for (std::vector<std::string>::iterator it =
68 path_components.begin() + count;
69 it != path_components.end();
70 ++it) {
71 relative_path = relative_path.Append(*it);
72 }
73
74 relative_paths.push_back(relative_path);
75 }
76
77 return relative_paths;
78 }
79
80 } // namespace
81
82 base::FilePath FileSelectHelper::ZipPackage(const base::FilePath& path) {
83 base::FilePath dest(ZipDestination(path));
84 if (dest.empty())
85 return dest;
86
87 bool success = base::CreateDirectory(dest.DirName());
Lei Zhang 2014/10/09 18:41:54 nit: you don't really need |success|.
erikchen 2014/10/09 23:33:36 I removed this instance of success, but I kept the
88 if (!success)
89 return base::FilePath();
90
91 base::File file(dest, base::File::FLAG_CREATE | base::File::FLAG_WRITE);
92 if (!file.IsValid())
93 return base::FilePath();
94
95 std::vector<base::FilePath> files_to_zip(RelativePathsForPackage(path));
96 base::FilePath base_dir = path.DirName();
97 success = zip::ZipFiles(base_dir, files_to_zip, file.GetPlatformFile());
98
99 int result = -1;
100 if (success)
101 result = fchmod(file.GetPlatformFile(), S_IRUSR);
Lei Zhang 2014/10/09 18:41:54 I'm surprised the destination file don't sane perm
erikchen 2014/10/09 23:33:35 Me too.
102
103 return result >= 0 ? dest : base::FilePath();
104 }
105
106 void FileSelectHelper::ProcessSelectedFilesMac(
107 const std::vector<ui::SelectedFileInfo>& files) {
108 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE_USER_BLOCKING);
109
110 // Make a mutable copy of the input files.
111 std::vector<ui::SelectedFileInfo> files_out(files);
112 std::vector<base::FilePath> temporary_files;
113
114 for (auto& file_info : files_out) {
115 NSString* filename = base::mac::FilePathToNSString(file_info.local_path);
116 BOOL isPackage =
117 [[NSWorkspace sharedWorkspace] isFilePackageAtPath:filename];
118 if (isPackage && base::DirectoryExists(file_info.local_path)) {
119 base::FilePath result = ZipPackage(file_info.local_path);
120
121 if (!result.empty()) {
122 // The parent directory of the zipped file is a temporary directory with
123 // no other children.
124 temporary_files.push_back(result.DirName());
125 file_info.local_path = result;
126 file_info.file_path = result;
127 file_info.display_name = result.BaseName().value();
128 }
129 }
130 }
131
132 content::BrowserThread::PostTask(
133 content::BrowserThread::UI,
134 FROM_HERE,
135 base::Bind(&FileSelectHelper::ProcessSelectedFilesMacOnUIThread,
136 base::Unretained(this),
137 files_out,
138 temporary_files));
139 }
140
141 void FileSelectHelper::ProcessSelectedFilesMacOnUIThread(
142 const std::vector<ui::SelectedFileInfo>& files,
143 const std::vector<base::FilePath>& temporary_files) {
144 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
145
146 if (!temporary_files.empty()) {
147 temporary_files_.insert(
148 temporary_files_.end(), temporary_files.begin(), temporary_files.end());
149
150 // Typically, |temporary_files| are deleted after web_contents_ is
Lei Zhang 2014/10/09 18:41:54 nit: |web_contents_|
erikchen 2014/10/09 23:33:36 Done.
151 // destroyed. If web_contents_ is already NULL, then the temporary files
152 // need to be deleted now.
153 if (!web_contents_) {
154 DeleteTemporaryFiles();
155 RunFileChooserEnd();
156 return;
157 }
158 }
159
160 NotifyRenderViewHostAndEnd(files);
161 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698