1 // Copyright 2015 The Chromium Authors
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"
7 #include "base/check_op.h"
8 #include "base/files/file.h"
9 #include "base/files/file_util.h"
10 #include "base/functional/bind.h"
11 #include "base/task/thread_pool.h"
12 #include "build/chromeos_buildflags.h"
13 #include "chrome/browser/platform_util_internal.h"
14 #include "content/public/browser/browser_task_traits.h"
15 #include "content/public/browser/browser_thread.h"
17 #if BUILDFLAG(IS_CHROMEOS_LACROS)
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/browser_window.h"
20 #include "chrome/browser/ui/lacros/window_properties.h"
21 #include "chromeos/ui/base/window_pin_type.h"
22 #include "ui/aura/window.h"
25 using content::BrowserThread;
27 namespace platform_util {
31 bool shell_operations_allowed = true;
33 void VerifyAndOpenItemOnBlockingThread(const base::FilePath& path,
35 OpenOperationCallback callback) {
36 base::File target_item(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
37 if (!base::PathExists(path)) {
38 if (!callback.is_null())
39 content::GetUIThreadTaskRunner({})->PostTask(
41 base::BindOnce(std::move(callback), OPEN_FAILED_PATH_NOT_FOUND));
44 if (base::DirectoryExists(path) != (type == OPEN_FOLDER)) {
45 if (!callback.is_null())
46 content::GetUIThreadTaskRunner({})->PostTask(
48 base::BindOnce(std::move(callback), OPEN_FAILED_INVALID_TYPE));
52 if (shell_operations_allowed)
53 internal::PlatformOpenVerifiedItem(path, type);
54 if (!callback.is_null())
55 content::GetUIThreadTaskRunner({})->PostTask(
56 FROM_HERE, base::BindOnce(std::move(callback), OPEN_SUCCEEDED));
63 void DisableShellOperationsForTesting() {
64 shell_operations_allowed = false;
67 bool AreShellOperationsAllowed() {
68 return shell_operations_allowed;
71 } // namespace internal
73 void OpenItem(Profile* profile,
74 const base::FilePath& full_path,
75 OpenItemType item_type,
76 OpenOperationCallback callback) {
77 DCHECK_CURRENTLY_ON(BrowserThread::UI);
78 // TaskPriority::USER_BLOCKING because this is usually opened as a result of a
79 // user action (e.g. open-downloaded-file or show-item-in-folder).
80 // TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN because this doesn't need global
81 // state and can hang shutdown without this trait as it may result in an
82 // interactive dialog.
83 base::ThreadPool::PostTask(
85 {base::MayBlock(), base::TaskPriority::USER_BLOCKING,
86 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
87 base::BindOnce(&VerifyAndOpenItemOnBlockingThread, full_path, item_type,
88 std::move(callback)));
91 bool IsBrowserLockedFullscreen(const Browser* browser) {
92 #if BUILDFLAG(IS_CHROMEOS_LACROS)
93 aura::Window* window = browser->window()->GetNativeWindow();
94 // |window| can be nullptr inside of unit tests.
98 return window->GetProperty(lacros::kWindowPinTypeKey) ==
99 chromeos::WindowPinType::kTrustedPinned;
105 } // namespace platform_util