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