Extensions: Remove the old permission message interface
[chromium-blink-merge.git] / chrome / browser / file_select_helper_mac.mm
blob4f0f4aad8cde97bc2e61bf08ee7a9c8b55451662
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>
8 #include <sys/stat.h>
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"
18 namespace {
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()];
26   // Couldn't get the temporary directory.
27   if (!dest)
28     return base::FilePath();
30   [dest appendFormat:@"%@/zip_cache/%@",
31                      [[NSBundle mainBundle] bundleIdentifier],
32                      [[NSProcessInfo processInfo] globallyUniqueString]];
34   return base::mac::NSStringToFilePath(dest);
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();
44   // Add the package as the first relative path.
45   std::vector<base::FilePath> relative_paths;
46   relative_paths.push_back(package.BaseName());
48   // Add the components of the package as relative paths.
49   base::FileEnumerator file_enumerator(
50       package,
51       true /* recursive */,
52       base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
53   for (base::FilePath path = file_enumerator.Next(); !path.empty();
54        path = file_enumerator.Next()) {
55     base::FilePath relative_path;
56     bool success = base_dir.AppendRelativePath(path, &relative_path);
57     if (success)
58       relative_paths.push_back(relative_path);
59   }
61   return relative_paths;
64 }  // namespace
66 base::FilePath FileSelectHelper::ZipPackage(const base::FilePath& path) {
67   base::FilePath dest(ZipDestination(path));
68   if (dest.empty())
69     return dest;
71   if (!base::CreateDirectory(dest.DirName()))
72     return base::FilePath();
74   base::File file(dest, base::File::FLAG_CREATE | base::File::FLAG_WRITE);
75   if (!file.IsValid())
76     return base::FilePath();
78   std::vector<base::FilePath> files_to_zip(RelativePathsForPackage(path));
79   base::FilePath base_dir = path.DirName();
80   bool success = zip::ZipFiles(base_dir, files_to_zip, file.GetPlatformFile());
82   int result = -1;
83   if (success)
84     result = fchmod(file.GetPlatformFile(), S_IRUSR);
86   return result >= 0 ? dest : base::FilePath();
89 void FileSelectHelper::ProcessSelectedFilesMac(
90     const std::vector<ui::SelectedFileInfo>& files) {
91   DCHECK_CURRENTLY_ON(content::BrowserThread::FILE_USER_BLOCKING);
93   // Make a mutable copy of the input files.
94   std::vector<ui::SelectedFileInfo> files_out(files);
95   std::vector<base::FilePath> temporary_files;
97   for (auto& file_info : files_out) {
98     NSString* filename = base::mac::FilePathToNSString(file_info.local_path);
99     BOOL isPackage =
100         [[NSWorkspace sharedWorkspace] isFilePackageAtPath:filename];
101     if (isPackage && base::DirectoryExists(file_info.local_path)) {
102       base::FilePath result = ZipPackage(file_info.local_path);
104       if (!result.empty()) {
105         temporary_files.push_back(result);
106         file_info.local_path = result;
107         file_info.file_path = result;
108         file_info.display_name.append(".zip");
109       }
110     }
111   }
113   content::BrowserThread::PostTask(
114       content::BrowserThread::UI,
115       FROM_HERE,
116       base::Bind(&FileSelectHelper::ProcessSelectedFilesMacOnUIThread,
117                  base::Unretained(this),
118                  files_out,
119                  temporary_files));
122 void FileSelectHelper::ProcessSelectedFilesMacOnUIThread(
123     const std::vector<ui::SelectedFileInfo>& files,
124     const std::vector<base::FilePath>& temporary_files) {
125   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
127   if (!temporary_files.empty()) {
128     temporary_files_.insert(
129         temporary_files_.end(), temporary_files.begin(), temporary_files.end());
131     // Typically, |temporary_files| are deleted after |web_contents_| is
132     // destroyed. If |web_contents_| is already NULL, then the temporary files
133     // need to be deleted now.
134     if (!web_contents_) {
135       DeleteTemporaryFiles();
136       RunFileChooserEnd();
137       return;
138     }
139   }
141   NotifyRenderViewHostAndEnd(files);