Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4782)

Unified Diff: chrome/browser/chromeos/file_manager/open_util.cc

Issue 352393002: Be explicit about target type in platform_util::OpenItem() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Chdir on Linux, Fix memory leak in test and address Mac comment. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/file_manager/open_util.cc
diff --git a/chrome/browser/chromeos/file_manager/open_util.cc b/chrome/browser/chromeos/file_manager/open_util.cc
index de05b8b7c39c07a9983d9acfe069d6a204b9daa6..ae334e360479c6d9d183449eae134b54b1b76dc9 100644
--- a/chrome/browser/chromeos/file_manager/open_util.cc
+++ b/chrome/browser/chromeos/file_manager/open_util.cc
@@ -15,6 +15,7 @@
#include "chrome/browser/chromeos/file_manager/path_util.h"
#include "chrome/browser/chromeos/file_manager/url_util.h"
#include "chrome/browser/extensions/api/file_handlers/mime_util.h"
+#include "chrome/browser/platform_util_internal.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
@@ -35,25 +36,14 @@ namespace file_manager {
namespace util {
namespace {
-// Shows a warning message box saying that the file could not be opened.
-void ShowWarningMessageBox(Profile* profile,
- const base::FilePath& file_path,
- int message_id) {
- Browser* browser = chrome::FindTabbedBrowser(
- profile, false, chrome::HOST_DESKTOP_TYPE_ASH);
- chrome::ShowMessageBox(
- browser ? browser->window()->GetNativeWindow() : NULL,
- l10n_util::GetStringFUTF16(
- IDS_FILE_BROWSER_ERROR_VIEWING_FILE_TITLE,
- base::UTF8ToUTF16(file_path.BaseName().AsUTF8Unsafe())),
- l10n_util::GetStringUTF16(message_id),
- chrome::MESSAGE_BOX_TYPE_WARNING);
-}
+bool shell_operations_allowed = true;
// Executes the |task| for the file specified by |url|.
void ExecuteFileTaskForUrl(Profile* profile,
const file_tasks::TaskDescriptor& task,
const GURL& url) {
+ if (!shell_operations_allowed)
+ return;
storage::FileSystemContext* file_system_context =
GetFileSystemContextForExtensionId(profile, kFileManagerAppId);
@@ -75,6 +65,8 @@ void OpenFileManagerWithInternalActionId(Profile* profile,
const GURL& url,
const std::string& action_id) {
DCHECK(action_id == "open" || action_id == "select");
+ if (!shell_operations_allowed)
+ return;
content::RecordAction(base::UserMetricsAction("ShowFileBrowserFullTab"));
file_tasks::TaskDescriptor task(kFileManagerAppId,
@@ -83,11 +75,11 @@ void OpenFileManagerWithInternalActionId(Profile* profile,
ExecuteFileTaskForUrl(profile, task, url);
}
-// Opens the file with fetched MIME type and calls the callback.
+// Opens the file with fetched MIME type and warns the user on failure.
void OpenFileWithMimeType(Profile* profile,
+ const platform_util::OpenOperationCallback& callback,
const base::FilePath& path,
const GURL& url,
- const base::Callback<void(bool)>& callback,
const std::string& mime_type) {
extensions::app_file_handler_util::PathAndMimeTypeSet path_mime_set;
path_mime_set.insert(std::make_pair(path, mime_type));
@@ -104,109 +96,110 @@ void OpenFileWithMimeType(Profile* profile,
&tasks);
if (tasks.empty()) {
- callback.Run(false);
+ callback.Run(platform_util::OPEN_FAILED_NO_HANLDER_FOR_FILE_TYPE);
return;
}
const file_tasks::FullTaskDescriptor* chosen_task = &tasks[0];
- for (size_t i = 0; i < tasks.size(); ++i) {
- if (tasks[i].is_default()) {
- chosen_task = &tasks[i];
+ for (const auto& task : tasks) {
+ if (task.is_default()) {
+ chosen_task = &task;
break;
}
}
- ExecuteFileTaskForUrl(profile, chosen_task->task_descriptor(), url);
- callback.Run(true);
-}
-
-// Opens the file specified by |url| by finding and executing a file task for
-// the file. In case of success, calls |callback| with true. Otherwise the
-// returned value is false.
-void OpenFile(Profile* profile,
- const base::FilePath& path,
- const GURL& url,
- const base::Callback<void(bool)>& callback) {
- extensions::app_file_handler_util::GetMimeTypeForLocalPath(
- profile,
- path,
- base::Bind(&OpenFileWithMimeType, profile, path, url, callback));
-}
-
-// Called when execution of ContinueOpenItem() is completed.
-void OnContinueOpenItemCompleted(Profile* profile,
- const base::FilePath& file_path,
- bool result) {
- if (!result) {
- int message;
- if (file_path.Extension() == FILE_PATH_LITERAL(".dmg"))
- message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE_FOR_DMG;
- else if (file_path.Extension() == FILE_PATH_LITERAL(".exe") ||
- file_path.Extension() == FILE_PATH_LITERAL(".msi"))
- message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE_FOR_EXECUTABLE;
- else
- message = IDS_FILE_BROWSER_ERROR_VIEWING_FILE;
- ShowWarningMessageBox(profile, file_path, message);
- }
+ if (shell_operations_allowed)
+ ExecuteFileTaskForUrl(profile, chosen_task->task_descriptor(), url);
+ callback.Run(platform_util::OPEN_SUCCEEDED);
}
-// Used to implement OpenItem().
void ContinueOpenItem(Profile* profile,
const base::FilePath& file_path,
const GURL& url,
- base::File::Error error) {
+ platform_util::internal::OpenItemType expected_type,
+ const platform_util::OpenOperationCallback& callback,
+ base::File::Error error,
+ const base::File::Info& file_info) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ if (error != base::File::FILE_OK) {
+ callback.Run(error == base::File::FILE_ERROR_NOT_FOUND
+ ? platform_util::OPEN_FAILED_PATH_NOT_FOUND
+ : platform_util::OPEN_FAILED_FILE_ERROR);
+ return;
+ }
- if (error == base::File::FILE_OK) {
- // A directory exists at |url|. Open it with the file manager.
+ if (expected_type == platform_util::internal::OPEN_FOLDER &&
+ file_info.is_directory) {
OpenFileManagerWithInternalActionId(profile, url, "open");
- } else {
- // |url| should be a file. Open it.
- OpenFile(profile,
- file_path,
- url,
- base::Bind(&OnContinueOpenItemCompleted, profile, file_path));
+ callback.Run(platform_util::OPEN_SUCCEEDED);
+ return;
}
-}
-// Converts the |path| passed from external callers to the filesystem URL
-// that the file manager can correctly handle.
-//
-// When conversion fails, it shows a warning dialog UI and returns false.
-bool ConvertPath(Profile* profile, const base::FilePath& path, GURL* url) {
- if (!ConvertAbsoluteFilePathToFileSystemUrl(
- profile, path, kFileManagerAppId, url)) {
- ShowWarningMessageBox(profile, path,
- IDS_FILE_BROWSER_ERROR_UNRESOLVABLE_FILE);
- return false;
+ if (expected_type == platform_util::internal::OPEN_FILE &&
+ !file_info.is_directory) {
+ extensions::app_file_handler_util::GetMimeTypeForLocalPath(
+ profile, file_path,
+ base::Bind(&OpenFileWithMimeType, profile, callback, file_path, url));
+ return;
}
- return true;
-}
-} // namespace
+ callback.Run(platform_util::OPEN_FAILED_INVALID_TYPE);
+}
-void OpenItem(Profile* profile, const base::FilePath& file_path) {
+void VerifyAndOpenItem(Profile* profile,
+ const base::FilePath& file_path,
+ platform_util::internal::OpenItemType expected_type,
+ const platform_util::OpenOperationCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
GURL url;
- if (!ConvertPath(profile, file_path, &url))
+ if (!ConvertAbsoluteFilePathToFileSystemUrl(profile, file_path,
+ kFileManagerAppId, &url)) {
+ callback.Run(platform_util::OPEN_FAILED_PATH_NOT_FOUND);
return;
+ }
- CheckIfDirectoryExists(
- GetFileSystemContextForExtensionId(profile, kFileManagerAppId),
- url,
- base::Bind(&ContinueOpenItem, profile, file_path, url));
+ GetMetadataForPath(
+ GetFileSystemContextForExtensionId(profile, kFileManagerAppId), url,
+ base::Bind(&ContinueOpenItem, profile, file_path, url, expected_type,
+ callback));
}
-void ShowItemInFolder(Profile* profile, const base::FilePath& file_path) {
+} // namespace
+
+void OpenFile(Profile* profile,
+ const base::FilePath& file_path,
+ const platform_util::OpenOperationCallback& callback) {
+ VerifyAndOpenItem(profile, file_path, platform_util::internal::OPEN_FILE,
+ callback);
+}
+
+void OpenFolder(Profile* profile,
+ const base::FilePath& file_path,
+ const platform_util::OpenOperationCallback& callback) {
+ VerifyAndOpenItem(profile, file_path, platform_util::internal::OPEN_FOLDER,
+ callback);
+}
+
+void ShowItemInFolder(Profile* profile,
+ const base::FilePath& file_path,
+ const platform_util::OpenOperationCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
GURL url;
- if (!ConvertPath(profile, file_path, &url))
+ if (!ConvertAbsoluteFilePathToFileSystemUrl(profile, file_path,
+ kFileManagerAppId, &url)) {
+ callback.Run(platform_util::OPEN_FAILED_PATH_NOT_FOUND);
return;
+ }
// This action changes the selection so we do not reuse existing tabs.
OpenFileManagerWithInternalActionId(profile, url, "select");
+ callback.Run(platform_util::OPEN_SUCCEEDED);
+}
+
+void DisableShellOperationsForTesting() {
+ shell_operations_allowed = false;
}
} // namespace util

Powered by Google App Engine
This is Rietveld 408576698