platform_util_ash.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (c) 2012 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. #include "chrome/browser/platform_util.h"
  5. #include "base/bind.h"
  6. #include "base/files/file_path.h"
  7. #include "chrome/browser/ash/file_manager/open_util.h"
  8. #include "chrome/browser/ash/guest_os/guest_os_external_protocol_handler.h"
  9. #include "chrome/browser/platform_util_internal.h"
  10. #include "chrome/browser/ui/ash/window_pin_util.h"
  11. #include "chrome/browser/ui/browser.h"
  12. #include "chrome/browser/ui/browser_finder.h"
  13. #include "chrome/browser/ui/browser_window.h"
  14. #include "chrome/browser/ui/simple_message_box.h"
  15. #include "chromeos/strings/grit/chromeos_strings.h"
  16. #include "content/public/browser/browser_thread.h"
  17. #include "ui/aura/window.h"
  18. #include "ui/base/l10n/l10n_util.h"
  19. #include "ui/chromeos/strings/grit/ui_chromeos_strings.h"
  20. #include "url/gurl.h"
  21. using content::BrowserThread;
  22. namespace platform_util {
  23. namespace {
  24. void ShowWarningOnOpenOperationResult(Profile* profile,
  25. const base::FilePath& path,
  26. OpenOperationResult result) {
  27. int message_id = IDS_FILE_BROWSER_ERROR_VIEWING_FILE;
  28. switch (result) {
  29. case OPEN_SUCCEEDED:
  30. return;
  31. case OPEN_FAILED_PATH_NOT_FOUND:
  32. message_id = IDS_FILE_BROWSER_ERROR_UNRESOLVABLE_FILE;
  33. break;
  34. case OPEN_FAILED_INVALID_TYPE:
  35. return;
  36. case OPEN_FAILED_NO_HANLDER_FOR_FILE_TYPE:
  37. if (path.MatchesExtension(FILE_PATH_LITERAL(".dmg")))
  38. message_id = IDS_FILE_BROWSER_ERROR_VIEWING_FILE_FOR_DMG;
  39. else if (path.MatchesExtension(FILE_PATH_LITERAL(".exe")) ||
  40. path.MatchesExtension(FILE_PATH_LITERAL(".msi")))
  41. message_id = IDS_FILE_BROWSER_ERROR_VIEWING_FILE_FOR_EXECUTABLE;
  42. else
  43. message_id = IDS_FILE_BROWSER_ERROR_VIEWING_FILE;
  44. break;
  45. case OPEN_FAILED_FILE_ERROR:
  46. message_id = IDS_FILE_BROWSER_ERROR_VIEWING_FILE;
  47. break;
  48. }
  49. Browser* browser = chrome::FindTabbedBrowser(profile, false);
  50. chrome::ShowWarningMessageBox(
  51. browser ? browser->window()->GetNativeWindow() : nullptr,
  52. l10n_util::GetStringFUTF16(IDS_FILE_BROWSER_ERROR_VIEWING_FILE_TITLE,
  53. path.BaseName().AsUTF16Unsafe()),
  54. l10n_util::GetStringUTF16(message_id));
  55. }
  56. } // namespace
  57. namespace internal {
  58. void DisableShellOperationsForTesting() {
  59. file_manager::util::DisableShellOperationsForTesting();
  60. }
  61. } // namespace internal
  62. void ShowItemInFolder(Profile* profile, const base::FilePath& full_path) {
  63. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  64. file_manager::util::ShowItemInFolder(
  65. profile, full_path,
  66. base::BindOnce(&ShowWarningOnOpenOperationResult, profile, full_path));
  67. }
  68. void OpenItem(Profile* profile,
  69. const base::FilePath& full_path,
  70. OpenItemType item_type,
  71. OpenOperationCallback callback) {
  72. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  73. file_manager::util::OpenItem(
  74. profile, full_path, item_type,
  75. callback.is_null() ? base::BindOnce(&ShowWarningOnOpenOperationResult,
  76. profile, full_path)
  77. : std::move(callback));
  78. }
  79. void OpenExternal(Profile* profile, const GURL& url) {
  80. // This code is called either when:
  81. // 1. ChromeAppDelegate::NewWindowContentsDelegate::OpenURLFromTab determines
  82. // that the currently running chrome is not the system default browser. This
  83. // should not happen for Chrome OS (crrev.com/c/2454769).
  84. // 2. |url| uses a external protocol and either
  85. // ExternalProtocolDialog::OnDialogAccepted invokes this, or the dialog has
  86. // previously been accepted with "Always allow ..." and this is called from
  87. // ChromeContentBrowserClient::HandleExternalProtocol.
  88. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  89. guest_os::Launch(profile, url);
  90. }
  91. bool IsBrowserLockedFullscreen(const Browser* browser) {
  92. aura::Window* window = browser->window()->GetNativeWindow();
  93. // |window| can be nullptr inside of unit tests.
  94. if (!window)
  95. return false;
  96. return GetWindowPinType(window) == chromeos::WindowPinType::kTrustedPinned;
  97. }
  98. } // namespace platform_util