blob: e105e55addb4dce220ebe5eaf5cc992389280531 [file] [log] [blame]
asanka655d1112015-03-07 05:33:411// 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 Marchandf1349f52019-01-25 03:16:417#include "base/bind.h"
Hans Wennborg1790e6b2020-04-24 19:10:338#include "base/check_op.h"
asanka655d1112015-03-07 05:33:419#include "base/files/file.h"
10#include "base/files/file_util.h"
Gabriel Charette055039132020-02-26 23:02:0611#include "base/task/thread_pool.h"
asanka655d1112015-03-07 05:33:4112#include "chrome/browser/platform_util_internal.h"
Eric Seckler8652dcd52018-09-20 10:42:2813#include "content/public/browser/browser_task_traits.h"
asanka655d1112015-03-07 05:33:4114#include "content/public/browser/browser_thread.h"
15
16using content::BrowserThread;
17
18namespace platform_util {
19
20namespace {
21
22bool shell_operations_allowed = true;
23
24void VerifyAndOpenItemOnBlockingThread(const base::FilePath& path,
25 OpenItemType type,
Alexander Cooper71fa2b02020-07-16 17:49:3626 OpenOperationCallback callback) {
asanka655d1112015-03-07 05:33:4127 base::File target_item(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
28 if (!base::PathExists(path)) {
29 if (!callback.is_null())
Gabriel Charettee7cdc5cd2020-05-27 23:35:0530 content::GetUIThreadTaskRunner({})->PostTask(
Alexander Cooper71fa2b02020-07-16 17:49:3631 FROM_HERE,
32 base::BindOnce(std::move(callback), OPEN_FAILED_PATH_NOT_FOUND));
asanka655d1112015-03-07 05:33:4133 return;
34 }
35 if (base::DirectoryExists(path) != (type == OPEN_FOLDER)) {
36 if (!callback.is_null())
Gabriel Charettee7cdc5cd2020-05-27 23:35:0537 content::GetUIThreadTaskRunner({})->PostTask(
Alexander Cooper71fa2b02020-07-16 17:49:3638 FROM_HERE,
39 base::BindOnce(std::move(callback), OPEN_FAILED_INVALID_TYPE));
asanka655d1112015-03-07 05:33:4140 return;
41 }
42
43 if (shell_operations_allowed)
44 internal::PlatformOpenVerifiedItem(path, type);
45 if (!callback.is_null())
Gabriel Charettee7cdc5cd2020-05-27 23:35:0546 content::GetUIThreadTaskRunner({})->PostTask(
Alexander Cooper71fa2b02020-07-16 17:49:3647 FROM_HERE, base::BindOnce(std::move(callback), OPEN_SUCCEEDED));
asanka655d1112015-03-07 05:33:4148}
49
50} // namespace
51
52namespace internal {
53
54void DisableShellOperationsForTesting() {
55 shell_operations_allowed = false;
56}
57
Jesse McKenna14bd4f1a2020-08-10 23:54:2958bool AreShellOperationsAllowed() {
59 return shell_operations_allowed;
60}
61
asanka655d1112015-03-07 05:33:4162} // namespace internal
63
64void OpenItem(Profile* profile,
65 const base::FilePath& full_path,
66 OpenItemType item_type,
Alexander Cooper71fa2b02020-07-16 17:49:3667 OpenOperationCallback callback) {
asanka655d1112015-03-07 05:33:4168 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Gabriel Charette40182e62019-07-25 19:36:2669 // 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 Charette055039132020-02-26 23:02:0674 base::ThreadPool::PostTask(
Sami Kyostila7d640eb2019-07-31 18:50:2675 FROM_HERE,
Gabriel Charette055039132020-02-26 23:02:0676 {base::MayBlock(), base::TaskPriority::USER_BLOCKING,
Sami Kyostila7d640eb2019-07-31 18:50:2677 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
78 base::BindOnce(&VerifyAndOpenItemOnBlockingThread, full_path, item_type,
Alexander Cooper71fa2b02020-07-16 17:49:3679 std::move(callback)));
asanka655d1112015-03-07 05:33:4180}
81
Ivan Sandrkc8e238b62019-03-18 15:00:0282bool IsBrowserLockedFullscreen(const Browser* browser) {
83 return false;
84}
85
asanka655d1112015-03-07 05:33:4186} // namespace platform_util