1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80

ash / webui / status_area_internals / status_area_internals_ui.cc [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "ash/webui/status_area_internals/status_area_internals_ui.h"

#include <memory>

#include "ash/webui/common/trusted_types_util.h"
#include "ash/webui/grit/ash_status_area_internals_resources.h"
#include "ash/webui/grit/ash_status_area_internals_resources_map.h"
#include "ash/webui/status_area_internals/status_area_internals_handler.h"
#include "ash/webui/status_area_internals/url_constants.h"
#include "base/containers/span.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/common/url_constants.h"
#include "ui/base/webui/resource_path.h"
#include "ui/webui/mojo_web_ui_controller.h"
#include "ui/webui/resources/grit/webui_resources.h"

namespace ash {

namespace {

void SetupWebUIDataSource(content::WebUIDataSource* source,
                          base::span<const webui::ResourcePath> resources,
                          int default_resource) {
  source->OverrideContentSecurityPolicy(
      network::mojom::CSPDirectiveName::ScriptSrc,
      "script-src chrome://resources chrome://webui-test 'self';");
  ash::EnableTrustedTypesCSP(source);

  source->UseStringsJs();
  source->EnableReplaceI18nInJS();
  source->AddResourcePath("test_loader.js", IDR_WEBUI_JS_TEST_LOADER_JS);
  source->AddResourcePath("test_loader_util.js",
                          IDR_WEBUI_JS_TEST_LOADER_UTIL_JS);
  source->AddResourcePath("test_loader.html", IDR_WEBUI_TEST_LOADER_HTML);

  source->AddResourcePaths(resources);
  source->AddResourcePath("", default_resource);
}

}  // namespace

StatusAreaInternalsUI::StatusAreaInternalsUI(content::WebUI* web_ui)
    : ui::MojoWebUIController(web_ui) {
  // Set up the chrome://status-area-internals source.
  content::WebUIDataSource* html_source =
      content::WebUIDataSource::CreateAndAdd(
          web_ui->GetWebContents()->GetBrowserContext(),
          kChromeUIStatusAreaInternalsHost);

  // Add required resources.
  SetupWebUIDataSource(html_source, kAshStatusAreaInternalsResources,
                       IDR_ASH_STATUS_AREA_INTERNALS_MAIN_HTML);
}

StatusAreaInternalsUI::~StatusAreaInternalsUI() = default;

void StatusAreaInternalsUI::BindInterface(
    mojo::PendingReceiver<mojom::status_area_internals::PageHandler> receiver) {
  page_handler_ =
      std::make_unique<StatusAreaInternalsHandler>(std::move(receiver));
}

WEB_UI_CONTROLLER_TYPE_IMPL(StatusAreaInternalsUI)

StatusAreaInternalsUIConfig::StatusAreaInternalsUIConfig()
    : DefaultWebUIConfig(content::kChromeUIScheme,
                         kChromeUIStatusAreaInternalsHost) {}

}  // namespace ash