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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 120 } | 120 } |
| 121 } | 121 } |
| 122 | 122 |
| 123 ExecuteFileTaskForUrl(profile, chosen_task->task_descriptor(), url); | 123 ExecuteFileTaskForUrl(profile, chosen_task->task_descriptor(), url); |
| 124 callback.Run(true); | 124 callback.Run(true); |
| 125 } | 125 } |
| 126 | 126 |
| 127 // Opens the file specified by |url| by finding and executing a file task for | 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 | 128 // the file. In case of success, calls |callback| with true. Otherwise the |
| 129 // returned value is false. | 129 // returned value is false. |
| 130 void OpenFile(Profile* profile, | 130 void OpenFileInternal(Profile* profile, |
| 131 const base::FilePath& path, | 131 const base::FilePath& path, |
| 132 const GURL& url, | 132 const GURL& url, |
| 133 const base::Callback<void(bool)>& callback) { | 133 const base::Callback<void(bool)>& callback) { |
| 134 extensions::app_file_handler_util::GetMimeTypeForLocalPath( | 134 extensions::app_file_handler_util::GetMimeTypeForLocalPath( |
| 135 profile, | 135 profile, |
| 136 path, | 136 path, |
| 137 base::Bind(&OpenFileWithMimeType, profile, path, url, callback)); | 137 base::Bind(&OpenFileWithMimeType, profile, path, url, callback)); |
| 138 } | 138 } |
| 139 | 139 |
| 140 enum ExpectedOpenTarget { | |
| 141 OPEN_FILE, | |
| 142 OPEN_FOLDER | |
| 143 }; | |
| 144 | |
| 140 // Called when execution of ContinueOpenItem() is completed. | 145 // Called when execution of ContinueOpenItem() is completed. |
| 141 void OnContinueOpenItemCompleted(Profile* profile, | 146 void OnContinueOpenItemCompleted(Profile* profile, |
| 142 const base::FilePath& file_path, | 147 const base::FilePath& file_path, |
| 143 bool result) { | 148 bool result) { |
| 144 if (!result) { | 149 if (!result) { |
| 145 int message; | 150 int message; |
| 146 if (file_path.Extension() == FILE_PATH_LITERAL(".dmg")) | 151 if (file_path.Extension() == FILE_PATH_LITERAL(".dmg")) |
| 147 message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE_FOR_DMG; | 152 message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE_FOR_DMG; |
| 148 else if (file_path.Extension() == FILE_PATH_LITERAL(".exe") || | 153 else if (file_path.Extension() == FILE_PATH_LITERAL(".exe") || |
| 149 file_path.Extension() == FILE_PATH_LITERAL(".msi")) | 154 file_path.Extension() == FILE_PATH_LITERAL(".msi")) |
| 150 message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE_FOR_EXECUTABLE; | 155 message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE_FOR_EXECUTABLE; |
| 151 else | 156 else |
| 152 message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE; | 157 message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE; |
| 153 ShowWarningMessageBox(profile, file_path, message); | 158 ShowWarningMessageBox(profile, file_path, message); |
| 154 } | 159 } |
| 155 } | 160 } |
| 156 | 161 |
| 157 // Used to implement OpenItem(). | 162 // Used to implement OpenFile()/OpenFolder(). |
| 158 void ContinueOpenItem(Profile* profile, | 163 void ContinueOpenItem(Profile* profile, |
|
hashimoto
2014/09/25 02:34:05
I think it's better to split this function into tw
asanka
2014/09/25 22:24:20
Done. Good call.
| |
| 164 ExpectedOpenTarget expected_target, | |
| 159 const base::FilePath& file_path, | 165 const base::FilePath& file_path, |
| 160 const GURL& url, | 166 const GURL& url, |
| 161 base::File::Error error) { | 167 base::File::Error check_directory_result) { |
| 162 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 168 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 169 const bool target_is_a_directory = | |
| 170 (check_directory_result == base::File::FILE_OK); | |
| 163 | 171 |
| 164 if (error == base::File::FILE_OK) { | 172 switch (expected_target) { |
| 165 // A directory exists at |url|. Open it with the file manager. | 173 case OPEN_FILE: |
| 166 OpenFileManagerWithInternalActionId(profile, url, "open"); | 174 if (target_is_a_directory) |
| 167 } else { | 175 break; |
| 168 // |url| should be a file. Open it. | 176 OpenFileInternal( |
| 169 OpenFile(profile, | 177 profile, |
| 170 file_path, | 178 file_path, |
| 171 url, | 179 url, |
| 172 base::Bind(&OnContinueOpenItemCompleted, profile, file_path)); | 180 base::Bind(&OnContinueOpenItemCompleted, profile, file_path)); |
| 181 return; | |
| 182 | |
| 183 case OPEN_FOLDER: | |
| 184 if (target_is_a_directory) { | |
| 185 OpenFileManagerWithInternalActionId(profile, url, "open"); | |
| 186 return; | |
| 187 } | |
| 188 break; | |
| 173 } | 189 } |
| 190 OnContinueOpenItemCompleted(profile, file_path, false); | |
| 174 } | 191 } |
| 175 | 192 |
| 176 // Converts the |given_path| passed from external callers to the form that the | 193 // 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 | 194 // 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. | 195 // folder path to the new formats, and then converts path to filesystem URL. |
| 179 // | 196 // |
| 180 // When conversion fails, it shows a warning dialog UI and returns false. | 197 // When conversion fails, it shows a warning dialog UI and returns false. |
| 181 bool ConvertPath(Profile* profile, | 198 bool ConvertPath(Profile* profile, |
| 182 const base::FilePath& given_path, | 199 const base::FilePath& given_path, |
| 183 base::FilePath* path, | 200 base::FilePath* path, |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 203 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 220 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 204 | 221 |
| 205 base::FilePath converted_path; | 222 base::FilePath converted_path; |
| 206 GURL url; | 223 GURL url; |
| 207 if (!ConvertPath(profile, file_path, &converted_path, &url)) | 224 if (!ConvertPath(profile, file_path, &converted_path, &url)) |
| 208 return; | 225 return; |
| 209 | 226 |
| 210 OpenFileManagerWithInternalActionId(profile, url, "auto-open"); | 227 OpenFileManagerWithInternalActionId(profile, url, "auto-open"); |
| 211 } | 228 } |
| 212 | 229 |
| 213 void OpenItem(Profile* profile, const base::FilePath& file_path) { | 230 void OpenFile(Profile* profile, const base::FilePath& file_path) { |
| 214 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 231 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 215 | 232 |
| 216 base::FilePath converted_path; | 233 base::FilePath converted_path; |
| 234 GURL url; | |
| 235 if (!ConvertPath(profile, file_path, &converted_path, &url)) | |
| 236 return; | |
| 237 | |
| 238 CheckIfDirectoryExists( | |
| 239 GetFileSystemContextForExtensionId(profile, kFileManagerAppId), | |
| 240 url, | |
| 241 base::Bind(&ContinueOpenItem, profile, OPEN_FILE, converted_path, url)); | |
| 242 } | |
| 243 | |
| 244 void OpenFolder(Profile* profile, const base::FilePath& file_path) { | |
| 245 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 246 | |
| 247 base::FilePath converted_path; | |
| 217 GURL url; | 248 GURL url; |
| 218 if (!ConvertPath(profile, file_path, &converted_path, &url)) | 249 if (!ConvertPath(profile, file_path, &converted_path, &url)) |
| 219 return; | 250 return; |
| 220 | 251 |
| 221 CheckIfDirectoryExists( | 252 CheckIfDirectoryExists( |
| 222 GetFileSystemContextForExtensionId(profile, kFileManagerAppId), | 253 GetFileSystemContextForExtensionId(profile, kFileManagerAppId), |
| 223 url, | 254 url, |
| 224 base::Bind(&ContinueOpenItem, profile, converted_path, url)); | 255 base::Bind(&ContinueOpenItem, profile, OPEN_FOLDER, converted_path, url)); |
| 225 } | 256 } |
| 226 | 257 |
| 227 void ShowItemInFolder(Profile* profile, const base::FilePath& file_path) { | 258 void ShowItemInFolder(Profile* profile, const base::FilePath& file_path) { |
| 228 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 259 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 229 | 260 |
| 230 base::FilePath converted_path; | 261 base::FilePath converted_path; |
| 231 GURL url; | 262 GURL url; |
| 232 if (!ConvertPath(profile, file_path, &converted_path, &url)) | 263 if (!ConvertPath(profile, file_path, &converted_path, &url)) |
| 233 return; | 264 return; |
| 234 | 265 |
| 235 // This action changes the selection so we do not reuse existing tabs. | 266 // This action changes the selection so we do not reuse existing tabs. |
| 236 OpenFileManagerWithInternalActionId(profile, url, "select"); | 267 OpenFileManagerWithInternalActionId(profile, url, "select"); |
| 237 } | 268 } |
| 238 | 269 |
| 239 } // namespace util | 270 } // namespace util |
| 240 } // namespace file_manager | 271 } // namespace file_manager |
| OLD | NEW |