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

ash / webui / growth_internals / growth_internals_ui.cc [blame]

// Copyright 2024 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/growth_internals/growth_internals_ui.h"

#include <memory>
#include <string>

#include "ash/constants/ash_features.h"
#include "ash/webui/grit/growth_internals_resources.h"
#include "ash/webui/grit/growth_internals_resources_map.h"
#include "ash/webui/growth_internals/constants.h"
#include "ash/webui/growth_internals/growth_internals_page_handler.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_controller.h"
#include "content/public/browser/web_ui_data_source.h"

namespace ash {

bool GrowthInternalsUIConfig::IsWebUIEnabled(
    content::BrowserContext* browser_context) {
  return ash::features::IsGrowthInternalsEnabled();
}

GrowthInternalsUI::GrowthInternalsUI(content::WebUI* web_ui)
    : ui::MojoWebUIController(web_ui) {
  content::WebUIDataSource* data_source =
      content::WebUIDataSource::CreateAndAdd(
          web_ui->GetWebContents()->GetBrowserContext(),
          std::string(kGrowthInternalsHost));

  data_source->AddResourcePath("", IDR_GROWTH_INTERNALS_INDEX_HTML);
  data_source->AddResourcePaths(kGrowthInternalsResources);

  data_source->OverrideContentSecurityPolicy(
      network::mojom::CSPDirectiveName::StyleSrc,
      "style-src 'self' chrome://resources chrome://resources "
      "'unsafe-inline';");

  data_source->OverrideContentSecurityPolicy(
      network::mojom::CSPDirectiveName::TrustedTypes,
      "trusted-types polymer-html-literal "
      "polymer-template-event-attribute-policy;");
}

GrowthInternalsUI::~GrowthInternalsUI() = default;

void GrowthInternalsUI::BindInterface(
    mojo::PendingReceiver<growth::mojom::PageHandler> receiver) {
  page_handler_ =
      std::make_unique<GrowthInternalsPageHandler>(std::move(receiver));
}

WEB_UI_CONTROLLER_TYPE_IMPL(GrowthInternalsUI)

}  // namespace ash