[M120 Migration]Fix for crash during chrome exit
[platform/framework/web/chromium-efl.git] / chrome / browser / platform_util.cc
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.
4
5 #include "chrome/browser/platform_util.h"
6
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"
16
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"
23 #endif
24
25 using content::BrowserThread;
26
27 namespace platform_util {
28
29 namespace {
30
31 bool shell_operations_allowed = true;
32
33 void VerifyAndOpenItemOnBlockingThread(const base::FilePath& path,
34                                        OpenItemType type,
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(
40           FROM_HERE,
41           base::BindOnce(std::move(callback), OPEN_FAILED_PATH_NOT_FOUND));
42     return;
43   }
44   if (base::DirectoryExists(path) != (type == OPEN_FOLDER)) {
45     if (!callback.is_null())
46       content::GetUIThreadTaskRunner({})->PostTask(
47           FROM_HERE,
48           base::BindOnce(std::move(callback), OPEN_FAILED_INVALID_TYPE));
49     return;
50   }
51
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));
57 }
58
59 }  // namespace
60
61 namespace internal {
62
63 void DisableShellOperationsForTesting() {
64   shell_operations_allowed = false;
65 }
66
67 bool AreShellOperationsAllowed() {
68   return shell_operations_allowed;
69 }
70
71 }  // namespace internal
72
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(
84       FROM_HERE,
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)));
89 }
90
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.
95   if (!window)
96     return false;
97
98   return window->GetProperty(lacros::kWindowPinTypeKey) ==
99          chromeos::WindowPinType::kTrustedPinned;
100 #else
101   return false;
102 #endif
103 }
104
105 }  // namespace platform_util