#include "extensions/browser/api/printer_provider/printer_provider_api.h"
#include <stddef.h>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/i18n/rtl.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/device_event_log/device_event_log.h"
#include "extensions/browser/api/printer_provider/printer_provider_internal_api.h"
#include "extensions/browser/api/printer_provider/printer_provider_internal_api_observer.h"
#include "extensions/browser/api/printer_provider/printer_provider_print_job.h"
#include "extensions/browser/api/usb/usb_device_manager.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/browser/unloaded_extension_reason.h"
#include "extensions/common/api/printer_provider.h"
#include "extensions/common/api/printer_provider_internal.h"
#include "extensions/common/api/usb.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/mojom/context_type.mojom.h"
#include "extensions/common/mojom/event_dispatcher.mojom-forward.h"
namespace extensions {
namespace {
const char kPrinterIdSeparator = …;
std::string GeneratePrinterId(const ExtensionId& extension_id,
const std::string& internal_printer_id) { … }
bool ParsePrinterId(const std::string& printer_id,
std::string* extension_id,
std::string* internal_printer_id) { … }
void UpdatePrinterWithExtensionInfo(base::Value::Dict* printer,
const Extension* extension) { … }
class GetPrintersRequest { … };
class PendingGetPrintersRequests { … };
class PendingGetCapabilityRequests { … };
constexpr base::TimeDelta PendingGetCapabilityRequests::kGetCapabilityTimeout;
class PendingPrintRequests { … };
class PendingUsbPrinterInfoRequests { … };
class PrinterProviderAPIImpl : public PrinterProviderAPI,
public PrinterProviderInternalAPIObserver,
public ExtensionRegistryObserver { … };
GetPrintersRequest::GetPrintersRequest(
const PrinterProviderAPI::GetPrintersCallback& callback)
: … { … }
GetPrintersRequest::~GetPrintersRequest() { … }
void GetPrintersRequest::AddSource(const ExtensionId& extension_id) { … }
bool GetPrintersRequest::IsDone() const { … }
void GetPrintersRequest::ReportForExtension(const ExtensionId& extension_id,
base::Value::List printers) { … }
PendingGetPrintersRequests::PendingGetPrintersRequests() : … { … }
PendingGetPrintersRequests::~PendingGetPrintersRequests() { … }
int PendingGetPrintersRequests::Add(
const PrinterProviderAPI::GetPrintersCallback& callback) { … }
bool PendingGetPrintersRequests::CompleteForExtension(
const ExtensionId& extension_id,
int request_id,
base::Value::List result) { … }
void PendingGetPrintersRequests::FailAllForExtension(
const ExtensionId& extension_id) { … }
bool PendingGetPrintersRequests::AddSource(int request_id,
const ExtensionId& extension_id) { … }
PendingGetCapabilityRequests::PendingGetCapabilityRequests()
: … { … }
PendingGetCapabilityRequests::~PendingGetCapabilityRequests() { … }
int PendingGetCapabilityRequests::Add(
PrinterProviderAPI::GetCapabilityCallback callback) { … }
void PendingGetCapabilityRequests::Complete(int request_id,
base::Value::Dict response) { … }
void PendingGetCapabilityRequests::FailAll() { … }
PendingPrintRequests::PendingPrintRequests() : … { … }
PendingPrintRequests::~PendingPrintRequests() { … }
int PendingPrintRequests::Add(PrinterProviderPrintJob job,
PrinterProviderAPI::PrintCallback callback) { … }
bool PendingPrintRequests::Complete(
int request_id,
api::printer_provider_internal::PrintError error) { … }
const PrinterProviderPrintJob* PendingPrintRequests::GetPrintJob(
int request_id) const { … }
void PendingPrintRequests::FailAll() { … }
PendingUsbPrinterInfoRequests::PendingUsbPrinterInfoRequests() { … }
PendingUsbPrinterInfoRequests::~PendingUsbPrinterInfoRequests() { … }
int PendingUsbPrinterInfoRequests::Add(
PrinterProviderAPI::GetPrinterInfoCallback callback) { … }
void PendingUsbPrinterInfoRequests::Complete(int request_id,
base::Value::Dict printer_info) { … }
void PendingUsbPrinterInfoRequests::FailAll() { … }
PrinterProviderAPIImpl::PrinterProviderAPIImpl(
content::BrowserContext* browser_context)
: … { … }
PrinterProviderAPIImpl::~PrinterProviderAPIImpl() { … }
void PrinterProviderAPIImpl::DispatchGetPrintersRequested(
const GetPrintersCallback& callback) { … }
void PrinterProviderAPIImpl::DispatchGetCapabilityRequested(
const std::string& printer_id,
GetCapabilityCallback callback) { … }
void PrinterProviderAPIImpl::DispatchPrintRequested(PrinterProviderPrintJob job,
PrintCallback callback) { … }
const PrinterProviderPrintJob* PrinterProviderAPIImpl::GetPrintJob(
const Extension* extension,
int request_id) const { … }
void PrinterProviderAPIImpl::DispatchGetUsbPrinterInfoRequested(
const ExtensionId& extension_id,
const device::mojom::UsbDeviceInfo& device,
GetPrinterInfoCallback callback) { … }
void PrinterProviderAPIImpl::OnGetPrintersResult(
const Extension* extension,
int request_id,
const PrinterProviderInternalAPIObserver::PrinterInfoVector& result) { … }
void PrinterProviderAPIImpl::OnGetCapabilityResult(const Extension* extension,
int request_id,
base::Value::Dict result) { … }
void PrinterProviderAPIImpl::OnPrintResult(
const Extension* extension,
int request_id,
api::printer_provider_internal::PrintError error) { … }
void PrinterProviderAPIImpl::OnGetUsbPrinterInfoResult(
const Extension* extension,
int request_id,
const api::printer_provider::PrinterInfo* result) { … }
void PrinterProviderAPIImpl::OnExtensionUnloaded(
content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionReason reason) { … }
bool PrinterProviderAPIImpl::WillRequestPrinters(
int request_id,
content::BrowserContext* browser_context,
mojom::ContextType target_context,
const Extension* extension,
const base::Value::Dict* listener_filter,
std::optional<base::Value::List>& event_args_out,
mojom::EventFilteringInfoPtr& event_filtering_info_out) { … }
}
PrinterProviderAPI* PrinterProviderAPI::Create(
content::BrowserContext* context) { … }
std::string PrinterProviderAPI::GetDefaultPrintError() { … }
}