1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_EXTENSION_PRINTER_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_EXTENSION_PRINTER_HANDLER_H_
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/strings/string16.h"
15 #include "chrome/browser/ui/webui/print_preview/printer_handler.h"
16 #include "extensions/browser/api/printer_provider/printer_provider_api.h"
19 class DictionaryValue
;
21 class RefCountedMemory
;
29 namespace cloud_devices
{
30 class CloudDeviceDescription
;
41 namespace local_discovery
{
42 class PWGRasterConverter
;
45 // Implementation of PrinterHandler interface backed by printerProvider
47 class ExtensionPrinterHandler
: public PrinterHandler
{
49 using PrintJobCallback
=
50 base::Callback
<void(scoped_ptr
<extensions::PrinterProviderPrintJob
>)>;
52 ExtensionPrinterHandler(
53 content::BrowserContext
* browser_context
,
54 const scoped_refptr
<base::TaskRunner
>& slow_task_runner
);
56 ~ExtensionPrinterHandler() override
;
58 // PrinterHandler implementation:
59 void Reset() override
;
60 void StartGetPrinters(
61 const PrinterHandler::GetPrintersCallback
& callback
) override
;
62 void StartGetCapability(
63 const std::string
& destination_id
,
64 const PrinterHandler::GetCapabilityCallback
& calback
) override
;
65 // TODO(tbarzic): It might make sense to have the strings in a single struct.
66 void StartPrint(const std::string
& destination_id
,
67 const std::string
& capability
,
68 const base::string16
& job_title
,
69 const std::string
& ticket_json
,
70 const gfx::Size
& page_size
,
71 const scoped_refptr
<base::RefCountedMemory
>& print_data
,
72 const PrinterHandler::PrintCallback
& callback
) override
;
75 friend class ExtensionPrinterHandlerTest
;
77 void SetPwgRasterConverterForTesting(
78 scoped_ptr
<local_discovery::PWGRasterConverter
> pwg_raster_converter
);
80 // Converts |data| to PWG raster format (from PDF) for a printer described
81 // by |printer_description|.
82 // |callback| is called with the converted data.
83 void ConvertToPWGRaster(
84 const scoped_refptr
<base::RefCountedMemory
>& data
,
85 const cloud_devices::CloudDeviceDescription
& printer_description
,
86 const cloud_devices::CloudDeviceDescription
& ticket
,
87 const gfx::Size
& page_size
,
88 scoped_ptr
<extensions::PrinterProviderPrintJob
> job
,
89 const PrintJobCallback
& callback
);
91 // Sets print job document data and dispatches it using printerProvider API.
92 void DispatchPrintJob(
93 const PrinterHandler::PrintCallback
& callback
,
94 scoped_ptr
<extensions::PrinterProviderPrintJob
> print_job
);
96 // Methods used as wrappers to callbacks for extensions::PrinterProviderAPI
97 // methods, primarily so the callbacks can be bound to this class' weak ptr.
98 // They just propagate results to callbacks passed to them.
99 void WrapGetPrintersCallback(
100 const PrinterHandler::GetPrintersCallback
& callback
,
101 const base::ListValue
& printers
,
103 void WrapGetCapabilityCallback(
104 const PrinterHandler::GetCapabilityCallback
& callback
,
105 const std::string
& destination_id
,
106 const base::DictionaryValue
& capability
);
107 void WrapPrintCallback(const PrinterHandler::PrintCallback
& callback
,
109 const std::string
& status
);
110 void OnUsbDevicesEnumerated(
111 const PrinterHandler::GetPrintersCallback
& callback
,
112 const std::vector
<scoped_refptr
<device::UsbDevice
>>& devices
);
114 content::BrowserContext
* browser_context_
;
116 scoped_ptr
<local_discovery::PWGRasterConverter
> pwg_raster_converter_
;
117 int pending_enumeration_count_
= 0;
119 scoped_refptr
<base::TaskRunner
> slow_task_runner_
;
121 base::WeakPtrFactory
<ExtensionPrinterHandler
> weak_ptr_factory_
;
123 DISALLOW_COPY_AND_ASSIGN(ExtensionPrinterHandler
);
126 #endif // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_EXTENSION_PRINTER_HANDLER_H_