chromium/chrome/browser/printing/printer_query_oop.cc

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

#include "chrome/browser/printing/printer_query_oop.h"

#include <memory>
#include <utility>

#include "base/check_op.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/printing/oop_features.h"
#include "chrome/browser/printing/print_backend_service_manager.h"
#include "chrome/browser/printing/print_job_worker_oop.h"
#include "components/device_event_log/device_event_log.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/web_contents.h"
#include "printing/buildflags/buildflags.h"

namespace printing {

PrinterQueryOop::PrinterQueryOop(content::GlobalRenderFrameHostId rfh_id)
    :{}

PrinterQueryOop::~PrinterQueryOop() = default;

std::unique_ptr<PrintJobWorker> PrinterQueryOop::TransferContextToNewWorker(
    PrintJob* print_job) {}

#if BUILDFLAG(IS_WIN)
void PrinterQueryOop::UpdatePrintableArea(
    PrintSettings* print_settings,
    OnDidUpdatePrintableAreaCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  std::string printer_name = base::UTF16ToUTF8(print_settings->device_name());
  PRINTER_LOG(EVENT) << "Updating paper printable area via service for "
                     << printer_name;

  PrintBackendServiceManager& service_mgr =
      PrintBackendServiceManager::GetInstance();

  // Caller is required to ensure `print_settings` stays alive until `callback`
  // runs.
  service_mgr.GetPaperPrintableArea(
      printer_name, print_settings->requested_media(),
      base::BindOnce(&PrinterQueryOop::OnDidGetPaperPrintableArea,
                     weak_factory_.GetWeakPtr(), print_settings,
                     std::move(callback)));
}
#endif

void PrinterQueryOop::SetClientId(
    PrintBackendServiceManager::ClientId client_id) {}

void PrinterQueryOop::OnDidUseDefaultSettings(
    SettingsCallback callback,
    mojom::PrintSettingsResultPtr print_settings) {}

#if BUILDFLAG(ENABLE_OOP_BASIC_PRINT_DIALOG)
void PrinterQueryOop::OnDidAskUserForSettings(
    SettingsCallback callback,
    mojom::PrintSettingsResultPtr print_settings) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  mojom::ResultCode result;
  if (print_settings->is_settings()) {
    VLOG(1) << "Ask user for settings from service complete";
    result = mojom::ResultCode::kSuccess;
    printing_context()->SetPrintSettings(print_settings->get_settings());

    // Use the same PrintBackendService for querying and printing, so that the
    // same device context can be used with both.
    print_document_client_id_ =
        PrintBackendServiceManager::GetInstance()
            .RegisterPrintDocumentClientReusingClientRemote(
                *query_with_ui_client_id_);
    if (!print_document_client_id_.has_value()) {
      // A failure after getting settings, override result to failure.
      result = mojom::ResultCode::kFailed;
      PRINTER_LOG(ERROR) << "Error after getting settings from user via "
                            "service due to service unavailable";
    }
  } else {
    result = print_settings->get_result_code();
    DCHECK_NE(result, mojom::ResultCode::kSuccess);
    if (result != mojom::ResultCode::kCanceled) {
      PRINTER_LOG(ERROR) << "Error getting settings from user via service: "
                         << result;
    }

    // TODO(crbug.com/40561724)  Fill in support for handling of access-denied
    // result code.  Blocked on crbug.com/1243873 for Windows.
  }

  InvokeSettingsCallback(std::move(callback), result);
}
#else   // BUILDFLAG(ENABLE_OOP_BASIC_PRINT_DIALOG)
void PrinterQueryOop::OnDidAskUserForSettings(
    SettingsCallback callback,
    std::unique_ptr<PrintSettings> new_settings,
    mojom::ResultCode result) {}
#endif  // BUILDFLAG(ENABLE_OOP_BASIC_PRINT_DIALOG)

void PrinterQueryOop::UseDefaultSettings(SettingsCallback callback) {}

void PrinterQueryOop::GetSettingsWithUI(uint32_t document_page_count,
                                        bool has_selection,
                                        bool is_scripted,
                                        SettingsCallback callback) {}

void PrinterQueryOop::UpdatePrintSettings(base::Value::Dict new_settings,
                                          SettingsCallback callback) {}

void PrinterQueryOop::OnDidUpdatePrintSettings(
    const std::string& device_name,
    SettingsCallback callback,
    mojom::PrintSettingsResultPtr print_settings) {}

#if BUILDFLAG(IS_WIN)
void PrinterQueryOop::OnDidGetPaperPrintableArea(
    PrintSettings* print_settings,
    OnDidUpdatePrintableAreaCallback callback,
    const gfx::Rect& printable_area_um) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  if (printable_area_um.IsEmpty()) {
    std::move(callback).Run(/*success=*/false);
    return;
  }

  print_settings->UpdatePrinterPrintableArea(printable_area_um);
  std::move(callback).Run(/*success=*/true);
}
#endif

void PrinterQueryOop::SendEstablishPrintingContext(
    PrintBackendServiceManager::ClientId client_id,
    const std::string& printer_name) {}

void PrinterQueryOop::SendUseDefaultSettings(SettingsCallback callback) {}

#if BUILDFLAG(ENABLE_OOP_BASIC_PRINT_DIALOG)
void PrinterQueryOop::SendAskUserForSettings(uint32_t document_page_count,
                                             bool has_selection,
                                             bool is_scripted,
                                             SettingsCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(ShouldPrintJobOop());

  if (document_page_count > kMaxPageCount) {
    InvokeSettingsCallback(std::move(callback), mojom::ResultCode::kFailed);
    return;
  }

  content::WebContents* web_contents = GetWebContents();

  // Running a dialog causes an exit to webpage-initiated fullscreen.
  // http://crbug.com/728276
  if (web_contents && web_contents->IsFullscreen()) {
    web_contents->ExitFullscreen(true);
  }

  PrintBackendServiceManager& service_mgr =
      PrintBackendServiceManager::GetInstance();
  service_mgr.AskUserForSettings(
      *query_with_ui_client_id_, *context_id_, document_page_count,
      has_selection, is_scripted,
      base::BindOnce(&PrinterQueryOop::OnDidAskUserForSettings,
                     weak_factory_.GetWeakPtr(), std::move(callback)));
}
#endif  // BUILDFLAG(ENABLE_OOP_BASIC_PRINT_DIALOG)

std::unique_ptr<PrintJobWorkerOop> PrinterQueryOop::CreatePrintJobWorkerOop(
    PrintJob* print_job) {}

}  // namespace printing