[M85 Dev][EFL] Fix errors to generate ninja files
[platform/framework/web/chromium-efl.git] / chrome / browser / file_select_helper_mac.mm
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/bind.h"
11 #include "base/files/file.h"
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h"
14 #include "base/mac/foundation_util.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"
19
20 namespace {
21
22 // Given the |path| of a package, returns the destination that the package
23 // should be zipped to. Returns an empty path on any errors.
24 base::FilePath ZipDestination(const base::FilePath& path) {
25   base::FilePath dest;
26
27   if (!base::GetTempDir(&dest)) {
28     // Couldn't get the temporary directory.
29     return base::FilePath();
30   }
31
32   // TMPDIR/<bundleID>/zip_cache/<guid>
33
34   NSString* bundleID = [[NSBundle mainBundle] bundleIdentifier];
35   dest = dest.Append([bundleID fileSystemRepresentation]);
36
37   dest = dest.Append("zip_cache");
38
39   NSString* guid = [[NSProcessInfo processInfo] globallyUniqueString];
40   dest = dest.Append([guid fileSystemRepresentation]);
41
42   return dest;
43 }
44
45 // Returns the path of the package and its components relative to the package's
46 // parent directory.
47 std::vector<base::FilePath> RelativePathsForPackage(
48     const base::FilePath& package) {
49   // Get the base directory.
50   base::FilePath base_dir = package.DirName();
51
52   // Add the package as the first relative path.
53   std::vector<base::FilePath> relative_paths;
54   relative_paths.push_back(package.BaseName());
55
56   // Add the components of the package as relative paths.
57   base::FileEnumerator file_enumerator(
58       package,
59       true /* recursive */,
60       base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
61   for (base::FilePath path = file_enumerator.Next(); !path.empty();
62        path = file_enumerator.Next()) {
63     base::FilePath relative_path;
64     bool success = base_dir.AppendRelativePath(path, &relative_path);
65     if (success)
66       relative_paths.push_back(relative_path);
67   }
68
69   return relative_paths;
70 }
71
72 }  // namespace
73
74 base::FilePath FileSelectHelper::ZipPackage(const base::FilePath& path) {
75   base::FilePath dest(ZipDestination(path));
76   if (dest.empty())
77     return dest;
78
79   if (!base::CreateDirectory(dest.DirName()))
80     return base::FilePath();
81
82   base::File file(dest, base::File::FLAG_CREATE | base::File::FLAG_WRITE);
83   if (!file.IsValid())
84     return base::FilePath();
85
86   std::vector<base::FilePath> files_to_zip(RelativePathsForPackage(path));
87   base::FilePath base_dir = path.DirName();
88   bool success = zip::ZipFiles(base_dir, files_to_zip, file.GetPlatformFile());
89
90   int result = -1;
91   if (success)
92     result = fchmod(file.GetPlatformFile(), S_IRUSR);
93
94   return result >= 0 ? dest : base::FilePath();
95 }
96
97 void FileSelectHelper::ProcessSelectedFilesMac(
98     const std::vector<ui::SelectedFileInfo>& files) {
99   // Make a mutable copy of the input files.
100   std::vector<ui::SelectedFileInfo> files_out(files);
101   std::vector<base::FilePath> temporary_files;
102
103   for (auto& file_info : files_out) {
104     NSString* filename = base::mac::FilePathToNSString(file_info.local_path);
105     BOOL isPackage =
106         [[NSWorkspace sharedWorkspace] isFilePackageAtPath:filename];
107     if (isPackage && base::DirectoryExists(file_info.local_path)) {
108       base::FilePath result = ZipPackage(file_info.local_path);
109
110       if (!result.empty()) {
111         temporary_files.push_back(result);
112         file_info.local_path = result;
113         file_info.file_path = result;
114         file_info.display_name.append(".zip");
115       }
116     }
117   }
118
119   content::GetUIThreadTaskRunner({})->PostTask(
120       FROM_HERE,
121       base::BindOnce(&FileSelectHelper::ProcessSelectedFilesMacOnUIThread,
122                      base::Unretained(this), files_out, temporary_files));
123 }
124
125 void FileSelectHelper::ProcessSelectedFilesMacOnUIThread(
126     const std::vector<ui::SelectedFileInfo>& files,
127     const std::vector<base::FilePath>& temporary_files) {
128   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
129
130   if (!temporary_files.empty()) {
131     temporary_files_.insert(
132         temporary_files_.end(), temporary_files.begin(), temporary_files.end());
133
134     // Typically, |temporary_files| are deleted after |web_contents_| is
135     // destroyed. If |web_contents_| is already NULL, then the temporary files
136     // need to be deleted now.
137     if (!web_contents_) {
138       DeleteTemporaryFiles();
139       RunFileChooserEnd();
140       return;
141     }
142   }
143
144   ConvertToFileChooserFileInfoList(files);
145 }