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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_EXTENSION_PRINTER_HANDLER_H_
#define CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_EXTENSION_PRINTER_HANDLER_H_
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string16.h"
#include "chrome/browser/ui/webui/print_preview/printer_handler.h"
#include "extensions/browser/api/printer_provider/printer_provider_api.h"
namespace base {
class DictionaryValue;
class ListValue;
class RefCountedMemory;
class TaskRunner;
}
namespace content {
class BrowserContext;
}
namespace cloud_devices {
class CloudDeviceDescription;
}
namespace device {
class UsbDevice;
}
namespace gfx {
class Size;
}
namespace printing {
class PWGRasterConverter;
}
// Implementation of PrinterHandler interface backed by printerProvider
// extension API.
class ExtensionPrinterHandler : public PrinterHandler {
public:
using PrintJobCallback =
base::Callback<void(scoped_ptr<extensions::PrinterProviderPrintJob>)>;
ExtensionPrinterHandler(
content::BrowserContext* browser_context,
const scoped_refptr<base::TaskRunner>& slow_task_runner);
~ExtensionPrinterHandler() override;
// PrinterHandler implementation:
void Reset() override;
void StartGetPrinters(
const PrinterHandler::GetPrintersCallback& callback) override;
void StartGetCapability(
const std::string& destination_id,
const PrinterHandler::GetCapabilityCallback& calback) override;
// TODO(tbarzic): It might make sense to have the strings in a single struct.
void StartPrint(const std::string& destination_id,
const std::string& capability,
const base::string16& job_title,
const std::string& ticket_json,
const gfx::Size& page_size,
const scoped_refptr<base::RefCountedMemory>& print_data,
const PrinterHandler::PrintCallback& callback) override;
void StartGrantPrinterAccess(
const std::string& printer_id,
const PrinterHandler::GetPrinterInfoCallback& callback) override;
private:
friend class ExtensionPrinterHandlerTest;
void SetPWGRasterConverterForTesting(
scoped_ptr<printing::PWGRasterConverter> pwg_raster_converter);
// Converts |data| to PWG raster format (from PDF) for a printer described
// by |printer_description|.
// |callback| is called with the converted data.
void ConvertToPWGRaster(
const scoped_refptr<base::RefCountedMemory>& data,
const cloud_devices::CloudDeviceDescription& printer_description,
const cloud_devices::CloudDeviceDescription& ticket,
const gfx::Size& page_size,
scoped_ptr<extensions::PrinterProviderPrintJob> job,
const PrintJobCallback& callback);
// Sets print job document data and dispatches it using printerProvider API.
void DispatchPrintJob(
const PrinterHandler::PrintCallback& callback,
scoped_ptr<extensions::PrinterProviderPrintJob> print_job);
// Methods used as wrappers to callbacks for extensions::PrinterProviderAPI
// methods, primarily so the callbacks can be bound to this class' weak ptr.
// They just propagate results to callbacks passed to them.
void WrapGetPrintersCallback(
const PrinterHandler::GetPrintersCallback& callback,
const base::ListValue& printers,
bool done);
void WrapGetCapabilityCallback(
const PrinterHandler::GetCapabilityCallback& callback,
const std::string& destination_id,
const base::DictionaryValue& capability);
void WrapPrintCallback(const PrinterHandler::PrintCallback& callback,
bool success,
const std::string& status);
void WrapGetPrinterInfoCallback(const GetPrinterInfoCallback& callback,
const base::DictionaryValue& printer_info);
void OnUsbDevicesEnumerated(
const PrinterHandler::GetPrintersCallback& callback,
const std::vector<scoped_refptr<device::UsbDevice>>& devices);
content::BrowserContext* browser_context_;
scoped_ptr<printing::PWGRasterConverter> pwg_raster_converter_;
int pending_enumeration_count_ = 0;
scoped_refptr<base::TaskRunner> slow_task_runner_;
base::WeakPtrFactory<ExtensionPrinterHandler> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(ExtensionPrinterHandler);
};
#endif // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_EXTENSION_PRINTER_HANDLER_H_
|