asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "chrome/browser/platform_util.h" |
| 6 | |
Sebastien Marchand | f1349f5 | 2019-01-25 03:16:41 | [diff] [blame] | 7 | #include "base/bind.h" |
Hans Wennborg | 1790e6b | 2020-04-24 19:10:33 | [diff] [blame] | 8 | #include "base/check_op.h" |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 9 | #include "base/files/file.h" |
| 10 | #include "base/files/file_util.h" |
Gabriel Charette | 05503913 | 2020-02-26 23:02:06 | [diff] [blame] | 11 | #include "base/task/thread_pool.h" |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 12 | #include "chrome/browser/platform_util_internal.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 13 | #include "content/public/browser/browser_task_traits.h" |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 14 | #include "content/public/browser/browser_thread.h" |
| 15 | |
| 16 | using content::BrowserThread; |
| 17 | |
| 18 | namespace platform_util { |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | bool shell_operations_allowed = true; |
| 23 | |
| 24 | void VerifyAndOpenItemOnBlockingThread(const base::FilePath& path, |
| 25 | OpenItemType type, |
Alexander Cooper | 71fa2b0 | 2020-07-16 17:49:36 | [diff] [blame] | 26 | OpenOperationCallback callback) { |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 27 | base::File target_item(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 28 | if (!base::PathExists(path)) { |
| 29 | if (!callback.is_null()) |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 30 | content::GetUIThreadTaskRunner({})->PostTask( |
Alexander Cooper | 71fa2b0 | 2020-07-16 17:49:36 | [diff] [blame] | 31 | FROM_HERE, |
| 32 | base::BindOnce(std::move(callback), OPEN_FAILED_PATH_NOT_FOUND)); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 33 | return; |
| 34 | } |
| 35 | if (base::DirectoryExists(path) != (type == OPEN_FOLDER)) { |
| 36 | if (!callback.is_null()) |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 37 | content::GetUIThreadTaskRunner({})->PostTask( |
Alexander Cooper | 71fa2b0 | 2020-07-16 17:49:36 | [diff] [blame] | 38 | FROM_HERE, |
| 39 | base::BindOnce(std::move(callback), OPEN_FAILED_INVALID_TYPE)); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 40 | return; |
| 41 | } |
| 42 | |
| 43 | if (shell_operations_allowed) |
| 44 | internal::PlatformOpenVerifiedItem(path, type); |
| 45 | if (!callback.is_null()) |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 46 | content::GetUIThreadTaskRunner({})->PostTask( |
Alexander Cooper | 71fa2b0 | 2020-07-16 17:49:36 | [diff] [blame] | 47 | FROM_HERE, base::BindOnce(std::move(callback), OPEN_SUCCEEDED)); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | } // namespace |
| 51 | |
| 52 | namespace internal { |
| 53 | |
| 54 | void DisableShellOperationsForTesting() { |
| 55 | shell_operations_allowed = false; |
| 56 | } |
| 57 | |
Jesse McKenna | 14bd4f1a | 2020-08-10 23:54:29 | [diff] [blame] | 58 | bool AreShellOperationsAllowed() { |
| 59 | return shell_operations_allowed; |
| 60 | } |
| 61 | |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 62 | } // namespace internal |
| 63 | |
| 64 | void OpenItem(Profile* profile, |
| 65 | const base::FilePath& full_path, |
| 66 | OpenItemType item_type, |
Alexander Cooper | 71fa2b0 | 2020-07-16 17:49:36 | [diff] [blame] | 67 | OpenOperationCallback callback) { |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 68 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Gabriel Charette | 40182e6 | 2019-07-25 19:36:26 | [diff] [blame] | 69 | // TaskPriority::USER_BLOCKING because this is usually opened as a result of a |
| 70 | // user action (e.g. open-downloaded-file or show-item-in-folder). |
| 71 | // TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN because this doesn't need global |
| 72 | // state and can hang shutdown without this trait as it may result in an |
| 73 | // interactive dialog. |
Gabriel Charette | 05503913 | 2020-02-26 23:02:06 | [diff] [blame] | 74 | base::ThreadPool::PostTask( |
Sami Kyostila | 7d640eb | 2019-07-31 18:50:26 | [diff] [blame] | 75 | FROM_HERE, |
Gabriel Charette | 05503913 | 2020-02-26 23:02:06 | [diff] [blame] | 76 | {base::MayBlock(), base::TaskPriority::USER_BLOCKING, |
Sami Kyostila | 7d640eb | 2019-07-31 18:50:26 | [diff] [blame] | 77 | base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, |
| 78 | base::BindOnce(&VerifyAndOpenItemOnBlockingThread, full_path, item_type, |
Alexander Cooper | 71fa2b0 | 2020-07-16 17:49:36 | [diff] [blame] | 79 | std::move(callback))); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 80 | } |
| 81 | |
Ivan Sandrk | c8e238b6 | 2019-03-18 15:00:02 | [diff] [blame] | 82 | bool IsBrowserLockedFullscreen(const Browser* browser) { |
| 83 | return false; |
| 84 | } |
| 85 | |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 86 | } // namespace platform_util |