file_manager_ui.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2020 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 "ash/webui/file_manager/file_manager_ui.h"
  5. #include "ash/webui/file_manager/file_manager_page_handler.h"
  6. #include "ash/webui/file_manager/resource_loader.h"
  7. #include "ash/webui/file_manager/resources/grit/file_manager_swa_resources.h"
  8. #include "ash/webui/file_manager/resources/grit/file_manager_swa_resources_map.h"
  9. #include "ash/webui/file_manager/url_constants.h"
  10. #include "base/check_op.h"
  11. #include "base/logging.h"
  12. #include "base/strings/string_util.h"
  13. #include "base/values.h"
  14. #include "content/public/browser/browser_thread.h"
  15. #include "content/public/browser/web_contents.h"
  16. #include "content/public/browser/web_ui.h"
  17. #include "content/public/browser/web_ui_data_source.h"
  18. #include "content/public/common/url_constants.h"
  19. #include "services/network/public/mojom/content_security_policy.mojom.h"
  20. #include "ui/file_manager/grit/file_manager_gen_resources_map.h"
  21. #include "ui/file_manager/grit/file_manager_resources.h"
  22. #include "ui/file_manager/grit/file_manager_resources_map.h"
  23. namespace ash {
  24. namespace file_manager {
  25. FileManagerUI::FileManagerUI(content::WebUI* web_ui,
  26. std::unique_ptr<FileManagerUIDelegate> delegate)
  27. : MojoWebDialogUI(web_ui), delegate_(std::move(delegate)) {
  28. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  29. // Count the number of active windows. This is done so that we can tell if
  30. // there are any active Files SWA windows.
  31. ++instance_count_;
  32. DCHECK_GT(instance_count_, 0);
  33. DLOG(WARNING) << "Starting FileManagerUI. Open windows: " << instance_count_;
  34. // Increment the counter each time a window is opened. This is to give a
  35. // unique ID to each window.
  36. ++window_counter_;
  37. auto* browser_context = web_ui->GetWebContents()->GetBrowserContext();
  38. auto* trusted_source = CreateTrustedAppDataSource(window_counter_);
  39. content::WebUIDataSource::Add(browser_context, trusted_source);
  40. // Add ability to request chrome-untrusted: URLs
  41. web_ui->AddRequestableScheme(content::kChromeUIUntrustedScheme);
  42. }
  43. content::WebUIDataSource* FileManagerUI::CreateTrustedAppDataSource(
  44. int window_number) {
  45. content::WebUIDataSource* source =
  46. content::WebUIDataSource::Create(kChromeUIFileManagerHost);
  47. // Setup chrome://file-manager main and default page.
  48. source->AddResourcePath("", IDR_FILE_MANAGER_SWA_MAIN_HTML);
  49. // Add chrome://file-manager content.
  50. source->AddResourcePaths(
  51. base::make_span(kFileManagerSwaResources, kFileManagerSwaResourcesSize));
  52. AddFilesAppResources(source, kFileManagerResources,
  53. kFileManagerResourcesSize);
  54. AddFilesAppResources(source, kFileManagerGenResources,
  55. kFileManagerGenResourcesSize);
  56. // Load time data: add files app strings and feature flags.
  57. source->EnableReplaceI18nInJS();
  58. base::Value::Dict dict = delegate_->GetLoadTimeData();
  59. dict.Set("WINDOW_NUMBER", window_number);
  60. source->AddLocalizedStrings(dict);
  61. source->UseStringsJs();
  62. // Script security policy.
  63. source->OverrideContentSecurityPolicy(
  64. network::mojom::CSPDirectiveName::ScriptSrc,
  65. "script-src chrome-extension://pmfjbimdmchhbnneeidfognadeopoehp "
  66. "chrome://resources "
  67. "'self' ;");
  68. // Metadata Shared Worker security policy.
  69. source->OverrideContentSecurityPolicy(
  70. network::mojom::CSPDirectiveName::WorkerSrc, "worker-src 'self' ;");
  71. // Allow using the chrome-untrusted:// scheme in the host.
  72. source->OverrideContentSecurityPolicy(
  73. network::mojom::CSPDirectiveName::FrameSrc,
  74. "frame-src chrome-untrusted://file-manager "
  75. "'self';");
  76. // TODO(crbug.com/1098685): Trusted Type remaining WebUI.
  77. source->DisableTrustedTypesCSP();
  78. return source;
  79. }
  80. int FileManagerUI::GetNumInstances() {
  81. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  82. return instance_count_;
  83. }
  84. FileManagerUI::~FileManagerUI() {
  85. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  86. DCHECK_GT(instance_count_, 0);
  87. --instance_count_;
  88. DLOG(WARNING) << "Stopping FileManagerUI. Open windows: " << instance_count_;
  89. }
  90. void FileManagerUI::BindInterface(
  91. mojo::PendingReceiver<mojom::PageHandlerFactory> pending_receiver) {
  92. if (page_factory_receiver_.is_bound())
  93. page_factory_receiver_.reset();
  94. page_factory_receiver_.Bind(std::move(pending_receiver));
  95. }
  96. void FileManagerUI::CreatePageHandler(
  97. mojo::PendingRemote<mojom::Page> pending_page,
  98. mojo::PendingReceiver<mojom::PageHandler> pending_page_handler) {
  99. DCHECK(pending_page.is_valid());
  100. page_handler_ = std::make_unique<FileManagerPageHandler>(
  101. this, std::move(pending_page_handler), std::move(pending_page));
  102. }
  103. WEB_UI_CONTROLLER_TYPE_IMPL(FileManagerUI)
  104. } // namespace file_manager
  105. } // namespace ash