OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/file_manager/open_util.h" | 5 #include "chrome/browser/chromeos/file_manager/open_util.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 action_id == "open" || | 80 action_id == "open" || |
81 action_id == "select"); | 81 action_id == "select"); |
82 content::RecordAction(base::UserMetricsAction("ShowFileBrowserFullTab")); | 82 content::RecordAction(base::UserMetricsAction("ShowFileBrowserFullTab")); |
83 | 83 |
84 file_tasks::TaskDescriptor task(kFileManagerAppId, | 84 file_tasks::TaskDescriptor task(kFileManagerAppId, |
85 file_tasks::TASK_TYPE_FILE_BROWSER_HANDLER, | 85 file_tasks::TASK_TYPE_FILE_BROWSER_HANDLER, |
86 action_id); | 86 action_id); |
87 ExecuteFileTaskForUrl(profile, task, url); | 87 ExecuteFileTaskForUrl(profile, task, url); |
88 } | 88 } |
89 | 89 |
90 // Opens the file with fetched MIME type and calls the callback. | 90 void WarnOnOpenFileFailure(Profile* profile, const base::FilePath& file_path) { |
| 91 int message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE; |
| 92 if (file_path.Extension() == FILE_PATH_LITERAL(".dmg")) |
| 93 message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE_FOR_DMG; |
| 94 else if (file_path.Extension() == FILE_PATH_LITERAL(".exe") || |
| 95 file_path.Extension() == FILE_PATH_LITERAL(".msi")) |
| 96 message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE_FOR_EXECUTABLE; |
| 97 ShowWarningMessageBox(profile, file_path, message); |
| 98 } |
| 99 |
| 100 // Opens the file with fetched MIME type and warns the user on failure. |
91 void OpenFileWithMimeType(Profile* profile, | 101 void OpenFileWithMimeType(Profile* profile, |
92 const base::FilePath& path, | 102 const base::FilePath& path, |
93 const GURL& url, | 103 const GURL& url, |
94 const base::Callback<void(bool)>& callback, | |
95 const std::string& mime_type) { | 104 const std::string& mime_type) { |
96 extensions::app_file_handler_util::PathAndMimeTypeSet path_mime_set; | 105 extensions::app_file_handler_util::PathAndMimeTypeSet path_mime_set; |
97 path_mime_set.insert(std::make_pair(path, mime_type)); | 106 path_mime_set.insert(std::make_pair(path, mime_type)); |
98 | 107 |
99 std::vector<GURL> file_urls; | 108 std::vector<GURL> file_urls; |
100 file_urls.push_back(url); | 109 file_urls.push_back(url); |
101 | 110 |
102 std::vector<file_tasks::FullTaskDescriptor> tasks; | 111 std::vector<file_tasks::FullTaskDescriptor> tasks; |
103 file_tasks::FindAllTypesOfTasks( | 112 file_tasks::FindAllTypesOfTasks( |
104 profile, | 113 profile, |
105 drive::util::GetDriveAppRegistryByProfile(profile), | 114 drive::util::GetDriveAppRegistryByProfile(profile), |
106 path_mime_set, | 115 path_mime_set, |
107 file_urls, | 116 file_urls, |
108 &tasks); | 117 &tasks); |
109 | 118 |
110 if (tasks.empty()) { | 119 if (tasks.empty()) { |
111 callback.Run(false); | 120 WarnOnOpenFileFailure(profile, path); |
112 return; | 121 return; |
113 } | 122 } |
114 | 123 |
115 const file_tasks::FullTaskDescriptor* chosen_task = &tasks[0]; | 124 const file_tasks::FullTaskDescriptor* chosen_task = &tasks[0]; |
116 for (size_t i = 0; i < tasks.size(); ++i) { | 125 for (size_t i = 0; i < tasks.size(); ++i) { |
117 if (tasks[i].is_default()) { | 126 if (tasks[i].is_default()) { |
118 chosen_task = &tasks[i]; | 127 chosen_task = &tasks[i]; |
119 break; | 128 break; |
120 } | 129 } |
121 } | 130 } |
122 | 131 |
123 ExecuteFileTaskForUrl(profile, chosen_task->task_descriptor(), url); | 132 ExecuteFileTaskForUrl(profile, chosen_task->task_descriptor(), url); |
124 callback.Run(true); | |
125 } | |
126 | |
127 // Opens the file specified by |url| by finding and executing a file task for | |
128 // the file. In case of success, calls |callback| with true. Otherwise the | |
129 // returned value is false. | |
130 void OpenFile(Profile* profile, | |
131 const base::FilePath& path, | |
132 const GURL& url, | |
133 const base::Callback<void(bool)>& callback) { | |
134 extensions::app_file_handler_util::GetMimeTypeForLocalPath( | |
135 profile, | |
136 path, | |
137 base::Bind(&OpenFileWithMimeType, profile, path, url, callback)); | |
138 } | |
139 | |
140 // Called when execution of ContinueOpenItem() is completed. | |
141 void OnContinueOpenItemCompleted(Profile* profile, | |
142 const base::FilePath& file_path, | |
143 bool result) { | |
144 if (!result) { | |
145 int message; | |
146 if (file_path.Extension() == FILE_PATH_LITERAL(".dmg")) | |
147 message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE_FOR_DMG; | |
148 else if (file_path.Extension() == FILE_PATH_LITERAL(".exe") || | |
149 file_path.Extension() == FILE_PATH_LITERAL(".msi")) | |
150 message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE_FOR_EXECUTABLE; | |
151 else | |
152 message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE; | |
153 ShowWarningMessageBox(profile, file_path, message); | |
154 } | |
155 } | |
156 | |
157 // Used to implement OpenItem(). | |
158 void ContinueOpenItem(Profile* profile, | |
159 const base::FilePath& file_path, | |
160 const GURL& url, | |
161 base::File::Error error) { | |
162 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
163 | |
164 if (error == base::File::FILE_OK) { | |
165 // A directory exists at |url|. Open it with the file manager. | |
166 OpenFileManagerWithInternalActionId(profile, url, "open"); | |
167 } else { | |
168 // |url| should be a file. Open it. | |
169 OpenFile(profile, | |
170 file_path, | |
171 url, | |
172 base::Bind(&OnContinueOpenItemCompleted, profile, file_path)); | |
173 } | |
174 } | 133 } |
175 | 134 |
176 // Converts the |given_path| passed from external callers to the form that the | 135 // Converts the |given_path| passed from external callers to the form that the |
177 // file manager can correctly handle. It first migrates old Drive/Download | 136 // file manager can correctly handle. It first migrates old Drive/Download |
178 // folder path to the new formats, and then converts path to filesystem URL. | 137 // folder path to the new formats, and then converts path to filesystem URL. |
179 // | 138 // |
180 // When conversion fails, it shows a warning dialog UI and returns false. | 139 // When conversion fails, it shows a warning dialog UI and returns false. |
181 bool ConvertPath(Profile* profile, | 140 bool ConvertPath(Profile* profile, |
182 const base::FilePath& given_path, | 141 const base::FilePath& given_path, |
183 base::FilePath* path, | 142 base::FilePath* path, |
(...skipping 19 matching lines...) Expand all Loading... |
203 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 162 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
204 | 163 |
205 base::FilePath converted_path; | 164 base::FilePath converted_path; |
206 GURL url; | 165 GURL url; |
207 if (!ConvertPath(profile, file_path, &converted_path, &url)) | 166 if (!ConvertPath(profile, file_path, &converted_path, &url)) |
208 return; | 167 return; |
209 | 168 |
210 OpenFileManagerWithInternalActionId(profile, url, "auto-open"); | 169 OpenFileManagerWithInternalActionId(profile, url, "auto-open"); |
211 } | 170 } |
212 | 171 |
213 void OpenItem(Profile* profile, const base::FilePath& file_path) { | 172 void OpenFile(Profile* profile, const base::FilePath& file_path) { |
214 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 173 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
215 | 174 |
216 base::FilePath converted_path; | 175 base::FilePath converted_path; |
| 176 GURL url; |
| 177 if (!ConvertPath(profile, file_path, &converted_path, &url)) |
| 178 return; |
| 179 |
| 180 extensions::app_file_handler_util::GetMimeTypeForLocalPath( |
| 181 profile, |
| 182 converted_path, |
| 183 base::Bind(&OpenFileWithMimeType, profile, converted_path, url)); |
| 184 } |
| 185 |
| 186 void OpenFolder(Profile* profile, const base::FilePath& file_path) { |
| 187 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 188 |
| 189 base::FilePath converted_path; |
217 GURL url; | 190 GURL url; |
218 if (!ConvertPath(profile, file_path, &converted_path, &url)) | 191 if (!ConvertPath(profile, file_path, &converted_path, &url)) |
219 return; | 192 return; |
220 | 193 |
221 CheckIfDirectoryExists( | 194 // This call will only open Files.app. |
222 GetFileSystemContextForExtensionId(profile, kFileManagerAppId), | 195 OpenFileManagerWithInternalActionId(profile, url, "open"); |
223 url, | |
224 base::Bind(&ContinueOpenItem, profile, converted_path, url)); | |
225 } | 196 } |
226 | 197 |
227 void ShowItemInFolder(Profile* profile, const base::FilePath& file_path) { | 198 void ShowItemInFolder(Profile* profile, const base::FilePath& file_path) { |
228 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 199 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
229 | 200 |
230 base::FilePath converted_path; | 201 base::FilePath converted_path; |
231 GURL url; | 202 GURL url; |
232 if (!ConvertPath(profile, file_path, &converted_path, &url)) | 203 if (!ConvertPath(profile, file_path, &converted_path, &url)) |
233 return; | 204 return; |
234 | 205 |
235 // This action changes the selection so we do not reuse existing tabs. | 206 // This action changes the selection so we do not reuse existing tabs. |
236 OpenFileManagerWithInternalActionId(profile, url, "select"); | 207 OpenFileManagerWithInternalActionId(profile, url, "select"); |
237 } | 208 } |
238 | 209 |
239 } // namespace util | 210 } // namespace util |
240 } // namespace file_manager | 211 } // namespace file_manager |
OLD | NEW |