1 // Copyright (c) 2014 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #include "libcef/browser/print_settings_impl.h"
6
7 #include "base/logging.h"
8
9 #include "printing/mojom/print.mojom.h"
10
CefPrintSettingsImpl(std::unique_ptr<printing::PrintSettings> settings,bool read_only)11 CefPrintSettingsImpl::CefPrintSettingsImpl(
12 std::unique_ptr<printing::PrintSettings> settings,
13 bool read_only)
14 : CefValueBase<CefPrintSettings, printing::PrintSettings>(
15 settings.release(),
16 nullptr,
17 kOwnerWillDelete,
18 read_only,
19 nullptr) {}
20
IsValid()21 bool CefPrintSettingsImpl::IsValid() {
22 return !detached();
23 }
24
IsReadOnly()25 bool CefPrintSettingsImpl::IsReadOnly() {
26 return read_only();
27 }
28
SetOrientation(bool landscape)29 void CefPrintSettingsImpl::SetOrientation(bool landscape) {
30 CEF_VALUE_VERIFY_RETURN_VOID(true);
31 mutable_value()->SetOrientation(landscape);
32 }
33
IsLandscape()34 bool CefPrintSettingsImpl::IsLandscape() {
35 CEF_VALUE_VERIFY_RETURN(false, false);
36 return const_value().landscape();
37 }
38
SetPrinterPrintableArea(const CefSize & physical_size_device_units,const CefRect & printable_area_device_units,bool landscape_needs_flip)39 void CefPrintSettingsImpl::SetPrinterPrintableArea(
40 const CefSize& physical_size_device_units,
41 const CefRect& printable_area_device_units,
42 bool landscape_needs_flip) {
43 CEF_VALUE_VERIFY_RETURN_VOID(true);
44 gfx::Size size(physical_size_device_units.width,
45 physical_size_device_units.height);
46 gfx::Rect rect(printable_area_device_units.x, printable_area_device_units.y,
47 printable_area_device_units.width,
48 printable_area_device_units.height);
49 mutable_value()->SetPrinterPrintableArea(size, rect, landscape_needs_flip);
50 }
51
SetDeviceName(const CefString & name)52 void CefPrintSettingsImpl::SetDeviceName(const CefString& name) {
53 CEF_VALUE_VERIFY_RETURN_VOID(true);
54 mutable_value()->set_device_name(name.ToString16());
55 }
56
GetDeviceName()57 CefString CefPrintSettingsImpl::GetDeviceName() {
58 CEF_VALUE_VERIFY_RETURN(false, CefString());
59 return const_value().device_name();
60 }
61
SetDPI(int dpi)62 void CefPrintSettingsImpl::SetDPI(int dpi) {
63 CEF_VALUE_VERIFY_RETURN_VOID(true);
64 mutable_value()->set_dpi(dpi);
65 }
66
GetDPI()67 int CefPrintSettingsImpl::GetDPI() {
68 CEF_VALUE_VERIFY_RETURN(false, 0);
69 return const_value().dpi();
70 }
71
SetPageRanges(const PageRangeList & ranges)72 void CefPrintSettingsImpl::SetPageRanges(const PageRangeList& ranges) {
73 CEF_VALUE_VERIFY_RETURN_VOID(true);
74 printing::PageRanges page_ranges;
75 PageRangeList::const_iterator it = ranges.begin();
76 for (; it != ranges.end(); ++it) {
77 const CefRange& cef_range = *it;
78 printing::PageRange range;
79 range.from = cef_range.from;
80 range.to = cef_range.to;
81 page_ranges.push_back(range);
82 }
83 mutable_value()->set_ranges(page_ranges);
84 }
85
GetPageRangesCount()86 size_t CefPrintSettingsImpl::GetPageRangesCount() {
87 CEF_VALUE_VERIFY_RETURN(false, 0);
88 return const_value().ranges().size();
89 }
90
GetPageRanges(PageRangeList & ranges)91 void CefPrintSettingsImpl::GetPageRanges(PageRangeList& ranges) {
92 CEF_VALUE_VERIFY_RETURN_VOID(false);
93 if (!ranges.empty())
94 ranges.clear();
95 const printing::PageRanges& page_ranges = const_value().ranges();
96 printing::PageRanges::const_iterator it = page_ranges.begin();
97 for (; it != page_ranges.end(); ++it) {
98 const printing::PageRange& range = *it;
99 ranges.push_back(CefRange(range.from, range.to));
100 }
101 }
102
SetSelectionOnly(bool selection_only)103 void CefPrintSettingsImpl::SetSelectionOnly(bool selection_only) {
104 CEF_VALUE_VERIFY_RETURN_VOID(true);
105 mutable_value()->set_selection_only(selection_only);
106 }
107
IsSelectionOnly()108 bool CefPrintSettingsImpl::IsSelectionOnly() {
109 CEF_VALUE_VERIFY_RETURN(false, false);
110 return const_value().selection_only();
111 }
112
SetCollate(bool collate)113 void CefPrintSettingsImpl::SetCollate(bool collate) {
114 CEF_VALUE_VERIFY_RETURN_VOID(true);
115 mutable_value()->set_collate(collate);
116 }
117
WillCollate()118 bool CefPrintSettingsImpl::WillCollate() {
119 CEF_VALUE_VERIFY_RETURN(false, false);
120 return const_value().collate();
121 }
122
SetColorModel(ColorModel model)123 void CefPrintSettingsImpl::SetColorModel(ColorModel model) {
124 CEF_VALUE_VERIFY_RETURN_VOID(true);
125 mutable_value()->set_color(static_cast<printing::mojom::ColorModel>(model));
126 }
127
GetColorModel()128 CefPrintSettings::ColorModel CefPrintSettingsImpl::GetColorModel() {
129 CEF_VALUE_VERIFY_RETURN(false, COLOR_MODEL_UNKNOWN);
130 return static_cast<ColorModel>(const_value().color());
131 }
132
SetCopies(int copies)133 void CefPrintSettingsImpl::SetCopies(int copies) {
134 CEF_VALUE_VERIFY_RETURN_VOID(true);
135 mutable_value()->set_copies(copies);
136 }
137
GetCopies()138 int CefPrintSettingsImpl::GetCopies() {
139 CEF_VALUE_VERIFY_RETURN(false, false);
140 return const_value().copies();
141 }
142
SetDuplexMode(DuplexMode mode)143 void CefPrintSettingsImpl::SetDuplexMode(DuplexMode mode) {
144 CEF_VALUE_VERIFY_RETURN_VOID(true);
145 mutable_value()->set_duplex_mode(
146 static_cast<printing::mojom::DuplexMode>(mode));
147 }
148
GetDuplexMode()149 CefPrintSettings::DuplexMode CefPrintSettingsImpl::GetDuplexMode() {
150 CEF_VALUE_VERIFY_RETURN(false, DUPLEX_MODE_UNKNOWN);
151 return static_cast<DuplexMode>(const_value().duplex_mode());
152 }
153
TakeOwnership()154 std::unique_ptr<printing::PrintSettings> CefPrintSettingsImpl::TakeOwnership() {
155 return base::WrapUnique(Detach(nullptr));
156 }
157
158 // CefPrintSettings implementation.
159
160 // static
Create()161 CefRefPtr<CefPrintSettings> CefPrintSettings::Create() {
162 return new CefPrintSettingsImpl(std::make_unique<printing::PrintSettings>(),
163 false);
164 }
165