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 | |
| 7 | #include "base/files/file.h" |
| 8 | #include "base/files/file_util.h" |
| 9 | #include "base/logging.h" |
fdoray | 46e31b5 | 2017-02-07 15:59:37 | [diff] [blame] | 10 | #include "base/task_scheduler/post_task.h" |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 11 | #include "chrome/browser/platform_util_internal.h" |
| 12 | #include "content/public/browser/browser_thread.h" |
| 13 | |
| 14 | using content::BrowserThread; |
| 15 | |
| 16 | namespace platform_util { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | bool shell_operations_allowed = true; |
| 21 | |
| 22 | void VerifyAndOpenItemOnBlockingThread(const base::FilePath& path, |
| 23 | OpenItemType type, |
| 24 | const OpenOperationCallback& callback) { |
| 25 | base::File target_item(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 26 | if (!base::PathExists(path)) { |
| 27 | if (!callback.is_null()) |
| 28 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 29 | base::Bind(callback, OPEN_FAILED_PATH_NOT_FOUND)); |
| 30 | return; |
| 31 | } |
| 32 | if (base::DirectoryExists(path) != (type == OPEN_FOLDER)) { |
| 33 | if (!callback.is_null()) |
| 34 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 35 | base::Bind(callback, OPEN_FAILED_INVALID_TYPE)); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | if (shell_operations_allowed) |
| 40 | internal::PlatformOpenVerifiedItem(path, type); |
| 41 | if (!callback.is_null()) |
| 42 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 43 | base::Bind(callback, OPEN_SUCCEEDED)); |
| 44 | } |
| 45 | |
| 46 | } // namespace |
| 47 | |
| 48 | namespace internal { |
| 49 | |
| 50 | void DisableShellOperationsForTesting() { |
| 51 | shell_operations_allowed = false; |
| 52 | } |
| 53 | |
| 54 | } // namespace internal |
| 55 | |
| 56 | void OpenItem(Profile* profile, |
| 57 | const base::FilePath& full_path, |
| 58 | OpenItemType item_type, |
| 59 | const OpenOperationCallback& callback) { |
| 60 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
fdoray | 46e31b5 | 2017-02-07 15:59:37 | [diff] [blame] | 61 | base::PostTaskWithTraits(FROM_HERE, |
| 62 | base::TaskTraits().MayBlock().WithPriority( |
| 63 | base::TaskPriority::BACKGROUND), |
| 64 | base::Bind(&VerifyAndOpenItemOnBlockingThread, |
| 65 | full_path, item_type, callback)); |
asanka | 655d111 | 2015-03-07 05:33:41 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | } // namespace platform_util |