1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/platform_util.h"
8 #include "base/check_op.h"
9 #include "base/files/file.h"
10 #include "base/files/file_util.h"
11 #include "base/task/thread_pool.h"
12 #include "chrome/browser/platform_util_internal.h"
13 #include "content/public/browser/browser_task_traits.h"
14 #include "content/public/browser/browser_thread.h"
16 using content::BrowserThread;
18 namespace platform_util {
22 bool shell_operations_allowed = true;
24 void VerifyAndOpenItemOnBlockingThread(const base::FilePath& path,
26 const OpenOperationCallback& callback) {
27 base::File target_item(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
28 if (!base::PathExists(path)) {
29 if (!callback.is_null())
30 content::GetUIThreadTaskRunner({})->PostTask(
31 FROM_HERE, base::BindOnce(callback, OPEN_FAILED_PATH_NOT_FOUND));
34 if (base::DirectoryExists(path) != (type == OPEN_FOLDER)) {
35 if (!callback.is_null())
36 content::GetUIThreadTaskRunner({})->PostTask(
37 FROM_HERE, base::BindOnce(callback, OPEN_FAILED_INVALID_TYPE));
41 if (shell_operations_allowed)
42 internal::PlatformOpenVerifiedItem(path, type);
43 if (!callback.is_null())
44 content::GetUIThreadTaskRunner({})->PostTask(
45 FROM_HERE, base::BindOnce(callback, OPEN_SUCCEEDED));
52 void DisableShellOperationsForTesting() {
53 shell_operations_allowed = false;
56 } // namespace internal
58 void OpenItem(Profile* profile,
59 const base::FilePath& full_path,
60 OpenItemType item_type,
61 const OpenOperationCallback& callback) {
62 DCHECK_CURRENTLY_ON(BrowserThread::UI);
63 // TaskPriority::USER_BLOCKING because this is usually opened as a result of a
64 // user action (e.g. open-downloaded-file or show-item-in-folder).
65 // TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN because this doesn't need global
66 // state and can hang shutdown without this trait as it may result in an
67 // interactive dialog.
68 base::ThreadPool::PostTask(
70 {base::MayBlock(), base::TaskPriority::USER_BLOCKING,
71 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
72 base::BindOnce(&VerifyAndOpenItemOnBlockingThread, full_path, item_type,
76 bool IsBrowserLockedFullscreen(const Browser* browser) {
80 } // namespace platform_util