Chromium Code Reviews| 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, |
| 91 const base::FilePath& file_path) { | |
|
hashimoto
2014/09/29 05:34:13
nit: indent.
asanka
2014/09/29 16:17:53
Done.
| |
| 92 int message; | |
|
hashimoto
2014/09/29 05:34:13
nit: While you are here, could you fix this line n
asanka
2014/09/29 16:17:52
Done.
| |
| 93 if (file_path.Extension() == FILE_PATH_LITERAL(".dmg")) | |
| 94 message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE_FOR_DMG; | |
| 95 else if (file_path.Extension() == FILE_PATH_LITERAL(".exe") || | |
| 96 file_path.Extension() == FILE_PATH_LITERAL(".msi")) | |
| 97 message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE_FOR_EXECUTABLE; | |
| 98 else | |
| 99 message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE; | |
| 100 ShowWarningMessageBox(profile, file_path, message); | |
| 101 } | |
| 102 | |
| 103 // Opens the file with fetched MIME type and warns the user on failure. | |
| 91 void OpenFileWithMimeType(Profile* profile, | 104 void OpenFileWithMimeType(Profile* profile, |
| 92 const base::FilePath& path, | 105 const base::FilePath& path, |
| 93 const GURL& url, | 106 const GURL& url, |
| 94 const base::Callback<void(bool)>& callback, | |
| 95 const std::string& mime_type) { | 107 const std::string& mime_type) { |
| 96 extensions::app_file_handler_util::PathAndMimeTypeSet path_mime_set; | 108 extensions::app_file_handler_util::PathAndMimeTypeSet path_mime_set; |
| 97 path_mime_set.insert(std::make_pair(path, mime_type)); | 109 path_mime_set.insert(std::make_pair(path, mime_type)); |
| 98 | 110 |
| 99 std::vector<GURL> file_urls; | 111 std::vector<GURL> file_urls; |
| 100 file_urls.push_back(url); | 112 file_urls.push_back(url); |
| 101 | 113 |
| 102 std::vector<file_tasks::FullTaskDescriptor> tasks; | 114 std::vector<file_tasks::FullTaskDescriptor> tasks; |
| 103 file_tasks::FindAllTypesOfTasks( | 115 file_tasks::FindAllTypesOfTasks( |
| 104 profile, | 116 profile, |
| 105 drive::util::GetDriveAppRegistryByProfile(profile), | 117 drive::util::GetDriveAppRegistryByProfile(profile), |
| 106 path_mime_set, | 118 path_mime_set, |
| 107 file_urls, | 119 file_urls, |
| 108 &tasks); | 120 &tasks); |
| 109 | 121 |
| 110 if (tasks.empty()) { | 122 if (tasks.empty()) { |
| 111 callback.Run(false); | 123 WarnOnOpenFileFailure(profile, path); |
| 112 return; | 124 return; |
| 113 } | 125 } |
| 114 | 126 |
| 115 const file_tasks::FullTaskDescriptor* chosen_task = &tasks[0]; | 127 const file_tasks::FullTaskDescriptor* chosen_task = &tasks[0]; |
| 116 for (size_t i = 0; i < tasks.size(); ++i) { | 128 for (size_t i = 0; i < tasks.size(); ++i) { |
| 117 if (tasks[i].is_default()) { | 129 if (tasks[i].is_default()) { |
| 118 chosen_task = &tasks[i]; | 130 chosen_task = &tasks[i]; |
| 119 break; | 131 break; |
| 120 } | 132 } |
| 121 } | 133 } |
| 122 | 134 |
| 123 ExecuteFileTaskForUrl(profile, chosen_task->task_descriptor(), url); | 135 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 } | 136 } |
| 175 | 137 |
| 176 // Converts the |given_path| passed from external callers to the form that the | 138 // 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 | 139 // 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. | 140 // folder path to the new formats, and then converts path to filesystem URL. |
| 179 // | 141 // |
| 180 // When conversion fails, it shows a warning dialog UI and returns false. | 142 // When conversion fails, it shows a warning dialog UI and returns false. |
| 181 bool ConvertPath(Profile* profile, | 143 bool ConvertPath(Profile* profile, |
| 182 const base::FilePath& given_path, | 144 const base::FilePath& given_path, |
| 183 base::FilePath* path, | 145 base::FilePath* path, |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 203 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 165 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 204 | 166 |
| 205 base::FilePath converted_path; | 167 base::FilePath converted_path; |
| 206 GURL url; | 168 GURL url; |
| 207 if (!ConvertPath(profile, file_path, &converted_path, &url)) | 169 if (!ConvertPath(profile, file_path, &converted_path, &url)) |
| 208 return; | 170 return; |
| 209 | 171 |
| 210 OpenFileManagerWithInternalActionId(profile, url, "auto-open"); | 172 OpenFileManagerWithInternalActionId(profile, url, "auto-open"); |
| 211 } | 173 } |
| 212 | 174 |
| 213 void OpenItem(Profile* profile, const base::FilePath& file_path) { | 175 void OpenFile(Profile* profile, const base::FilePath& file_path) { |
| 214 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 176 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 215 | 177 |
| 216 base::FilePath converted_path; | 178 base::FilePath converted_path; |
| 179 GURL url; | |
| 180 if (!ConvertPath(profile, file_path, &converted_path, &url)) | |
| 181 return; | |
| 182 | |
| 183 extensions::app_file_handler_util::GetMimeTypeForLocalPath( | |
| 184 profile, | |
| 185 converted_path, | |
| 186 base::Bind(&OpenFileWithMimeType, profile, converted_path, url)); | |
| 187 } | |
| 188 | |
| 189 void OpenFolder(Profile* profile, const base::FilePath& file_path) { | |
| 190 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 191 | |
| 192 base::FilePath converted_path; | |
| 217 GURL url; | 193 GURL url; |
| 218 if (!ConvertPath(profile, file_path, &converted_path, &url)) | 194 if (!ConvertPath(profile, file_path, &converted_path, &url)) |
| 219 return; | 195 return; |
| 220 | 196 |
| 221 CheckIfDirectoryExists( | 197 // This call will only open Files.app. |
| 222 GetFileSystemContextForExtensionId(profile, kFileManagerAppId), | 198 OpenFileManagerWithInternalActionId(profile, url, "open"); |
| 223 url, | |
| 224 base::Bind(&ContinueOpenItem, profile, converted_path, url)); | |
| 225 } | 199 } |
| 226 | 200 |
| 227 void ShowItemInFolder(Profile* profile, const base::FilePath& file_path) { | 201 void ShowItemInFolder(Profile* profile, const base::FilePath& file_path) { |
| 228 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 202 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 229 | 203 |
| 230 base::FilePath converted_path; | 204 base::FilePath converted_path; |
| 231 GURL url; | 205 GURL url; |
| 232 if (!ConvertPath(profile, file_path, &converted_path, &url)) | 206 if (!ConvertPath(profile, file_path, &converted_path, &url)) |
| 233 return; | 207 return; |
| 234 | 208 |
| 235 // This action changes the selection so we do not reuse existing tabs. | 209 // This action changes the selection so we do not reuse existing tabs. |
| 236 OpenFileManagerWithInternalActionId(profile, url, "select"); | 210 OpenFileManagerWithInternalActionId(profile, url, "select"); |
| 237 } | 211 } |
| 238 | 212 |
| 239 } // namespace util | 213 } // namespace util |
| 240 } // namespace file_manager | 214 } // namespace file_manager |
| OLD | NEW |