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
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93

ash / webui / print_preview_cros / print_preview_cros_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/print_preview_cros/print_preview_cros_ui.h"

#include <memory>
#include <utility>

#include "ash/constants/ash_features.h"
#include "ash/webui/common/trusted_types_util.h"
#include "ash/webui/grit/ash_print_preview_cros_app_resources.h"
#include "ash/webui/grit/ash_print_preview_cros_app_resources_map.h"
#include "ash/webui/print_preview_cros/backend/destination_provider.h"
#include "ash/webui/print_preview_cros/mojom/destination_provider.mojom.h"
#include "ash/webui/print_preview_cros/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 "ui/webui/color_change_listener/color_change_handler.h"
#include "ui/webui/resources/grit/webui_resources.h"

namespace ash::printing::print_preview {

namespace {

void ConfigurePolicies(content::WebUIDataSource* source) {
  // Enable common and test resources.
  source->OverrideContentSecurityPolicy(
      network::mojom::CSPDirectiveName::ScriptSrc,
      "script-src chrome://resources chrome://webui-test 'self';");
  // Configure ash specific trusted type polices.
  ash::EnableTrustedTypesCSP(source);
}

// Setup app resources and ensure default resource is the app index page.
void ConfigureResources(content::WebUIDataSource* source,
                        int default_resource) {
  source->AddResourcePaths(kAshPrintPreviewCrosAppResources);
  source->SetDefaultResource(default_resource);
  source->AddResourcePath("", default_resource);
}

// Setup common test resources used in browser tests.
void ConfigureTestResources(content::WebUIDataSource* source) {
  source->AddResourcePath("test_loader.html", IDR_WEBUI_TEST_LOADER_HTML);
  source->AddResourcePath("test_loader.js", IDR_WEBUI_JS_TEST_LOADER_JS);
  source->AddResourcePath("test_loader_util.js",
                          IDR_WEBUI_JS_TEST_LOADER_UTIL_JS);
}

}  // namespace

bool PrintPreviewCrosUIConfig::IsWebUIEnabled(
    content::BrowserContext* browser_context) {
  return ChromeOSWebUIConfig::IsWebUIEnabled(browser_context) &&
         ash::features::IsPrinterPreviewCrosAppEnabled();
}

PrintPreviewCrosUI::PrintPreviewCrosUI(content::WebUI* web_ui)
    : MojoWebDialogUI(web_ui) {
  // Configure resources.
  auto* source = content::WebUIDataSource::CreateAndAdd(
      web_ui->GetWebContents()->GetBrowserContext(),
      ash::kChromeUIPrintPreviewCrosHost);
  ConfigurePolicies(source);
  ConfigureResources(source, IDR_ASH_PRINT_PREVIEW_CROS_APP_INDEX_HTML);
  ConfigureTestResources(source);
}

PrintPreviewCrosUI::~PrintPreviewCrosUI() = default;

void PrintPreviewCrosUI::BindInterface(
    mojo::PendingReceiver<color_change_listener::mojom::PageHandler> receiver) {
  color_provider_handler_ = std::make_unique<ui::ColorChangeHandler>(
      web_ui()->GetWebContents(), std::move(receiver));
}

void PrintPreviewCrosUI::BindInterface(
    mojo::PendingReceiver<mojom::DestinationProvider> receiver) {
  destination_provider_ = std::make_unique<DestinationProvider>();
  destination_provider_->BindInterface(std::move(receiver));
}

WEB_UI_CONTROLLER_TYPE_IMPL(PrintPreviewCrosUI)

}  // namespace ash::printing::print_preview